January 15, 2021, 05:59:04 PM

Author Topic: [bb] B2D-->B3D by John Pickford [ 1+ years ago ]  (Read 539 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] B2D-->B3D by John Pickford [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : B2D-->B3D
Author : John Pickford
Posted : 1+ years ago

Description : A system for drawing 2D sprites in B3D.  The plan is to make it 99% compatible tith B2D commands so porting B2D games will be trivial.

So far I have emulated LoadAnimImage and DrawImage which are the most common commands.

There are a couple of known issues:

If B3D resisizes and image   exture on loading then this code will not notice and get the UV's wrong.  I'm looking for an elegant fix for this.

Transparent color must be 0,0,0 which appears to be a limitation of B3D at the moment.

How does it work?
Replace every drawImage and LoadAnimImage command in your code with a call to my functions.  You also need an extra call to ClearDisplayList at the start of each frame (and of course Renderworld before FLIP).

Each time you draw a sprite a quad will be added to a mesh which will be drawn at renderworld.  There's no need to worry about cameras or managing entities - existing code should work.

SPEED:  Seems a fair bit slower than B2D.  Hopefully I'll find a way to improve this.

FUTURE:  I intend to add extra parameters to DrawImage which support scaling, rotation and variable alpha(tranlucency).  This is ultimately the main reason for moving a 2D game to B3D - access to better rendering features.

A side effect of all this is future-proofing code as future editions of DirectX are dropping support for 2D.


Code :
Code: BlitzBasic
  1. ;*******************************************************************
  2. ;
  3. ; 2Din3D         By J.Pickford - Freeware!
  4. ;..........................................
  5. ;
  6. ; A set of 2D sprite functions implemented in B3D
  7. ;
  8. ; Functions are intended to be VERY close to B2D commands to make
  9. ; conversion of B2D games easy
  10. ;
  11. ;
  12. ;
  13. ; This code is a work-in-progress. I hope to emulate more of the B2D
  14. ; functionality in future versions.
  15. ;
  16. ; Future Enhancements: Sprite Scaling, Rotation, Variable Alpha
  17. ;
  18. ;********************************************************************
  19.  
  20. Const maxframes=1000            ;max number of animation frames allowed in a single texture
  21.  
  22. Type texturebank
  23.  
  24.         Field t_texture
  25.         Field t_brush
  26.         Field t_sx#[maxframes]
  27.         Field t_sy#[maxframes]
  28.         Field t_width#
  29.     Field t_height#
  30.     Field t_maxframe
  31.         Field t_totalwidth#
  32.         Field t_totalheight#
  33.  
  34. End Type
  35.  
  36. Const width#=640                        ;Screensize
  37. Const height#=480
  38.  
  39.  
  40.         Graphics3D width,height
  41.         SetBuffer BackBuffer()
  42.        
  43. Global camera=CreateCamera()
  44. Global pivot=CreateCamera()
  45. Global displaylist=CreateMesh(camera)
  46. Global lasttexture.texturebank
  47.  
  48.  
  49.  
  50. Global alien.texturebank=loadanimimageb3d ("alien.bmp",64,64)  ;Just an example
  51.  
  52.  
  53.  
  54.         clock#=0
  55.        
  56.         While Not MouseHit(2)
  57.        
  58.        
  59.        
  60.         ClearDisplayList() ;This resets the display list - ESSENTIAL!
  61.        
  62.  
  63.         ;"Draw" some stuff-------------------------------Actual drawing takes place at Renderworld()
  64.        
  65.                 For f#=1 To 360 Step 5
  66.                        
  67.                         drawimageb3d (alien,width/2+width/2*Sin(clock/4)*Cos(clock+f),height/2+Cos(clock/5)*height/2*Sin(clock+f),(f/5) Mod 64)
  68.        
  69.                 Next
  70.        
  71.         ;should be at opposite corners of the screen
  72.                
  73.                 drawimageb3d (alien,0,0,(clock/5)Mod 64)
  74.                 drawimageb3d (alien,width-64,height-64,(clock/7)Mod 64)
  75.        
  76.         ;-------------------------------------------------
  77.                
  78.                 RenderWorld() ;Actual drawing takes place
  79.                 Color 255,255,255
  80.                 Text 100,10,"Click Right Mouse Button to Quit"
  81.                 Flip
  82.        
  83.                 clock=clock+1
  84.        
  85.         Wend
  86.  
  87.  
  88.         End
  89.  
  90. ;--------------------------------------
  91. ; Call this at the start of each frame
  92.  
  93.  
  94. Function ClearDisplayList()
  95.  
  96.         FreeEntity displaylist
  97.         displaylist=CreateMesh(camera)
  98.         PositionEntity displaylist,-width/2,-height/2,width/2
  99.         lasttexture=Null
  100.        
  101. End Function
  102.  
  103. ;------------------------------------------------------------------------
  104. ; LoadAnimImageB3D
  105. ;...................
  106. ;
  107. ;  Intended to be compatible with B2D command "LoadAnimImage"
  108. ;
  109. ;  Known problems: If B3D resizes texture because it's too big then
  110. ;                  the UV's will be wrong.  Looking for a neat fix...
  111. ;
  112. ;                                  The transparent colour must be 0,0,0
  113. ;-------------------------------------------------------------------------
  114.  
  115.  
  116. Function LoadAnimImageB3D.texturebank (file$,swidth#,sheight#)
  117.  
  118.         this.texturebank=New texturebank
  119.         this    _texture=LoadTexture (file$,1+4)                ;color + masked
  120.         this    _brush=CreateBrush()
  121.         BrushTexture this       _brush,this     _texture
  122.     BrushFX this        _brush,1                                                ;Full bright - no lighting needed
  123.  
  124.         this    _totalwidth=TextureWidth (this  _texture)
  125.         this    _totalheight=TextureHeight (this        _texture)
  126.         this    _width=swidth
  127.         this    _height=sheight
  128.        
  129.         frame=0
  130.         sy#=0
  131.        
  132. ;Create the UV coords for each animation frame (just for speed later on)
  133.        
  134.         While sy<this   _totalheight
  135.        
  136.                 sx#=0
  137.          
  138.                 While sx<this   _totalwidth
  139.        
  140.                         this    _sx[frame]=sx/this      _totalwidth
  141.                         this    _sy[frame]=sy/this      _totalheight
  142.                         frame=frame+1
  143.                         sx=sx+swidth
  144.                
  145.                 Wend
  146.                
  147.                 sy=sy+sheight
  148.        
  149.         Wend
  150.        
  151.        
  152.         this    _maxframe=frame-1
  153.  
  154.         Return this
  155.  
  156. End Function
  157.  
  158.  
  159. ;--------------------------------------------------------------------------------
  160. ; DrawImageB3D (Simple sprite with no fancy functions)
  161. ;......................................................
  162. ;
  163. ;
  164. ; Intended to be compatible with the B2D equivelant (DrawImage)
  165. ; The only enhancement being x,y are floats allowing sub-pixel positioning
  166. ;
  167. ;--------------------------------------------------------------------------------
  168.  
  169. Function DrawImageB3D (texture.texturebank,x#,y#,frame)
  170.  
  171.         y=height-y      ;in B3D y is up - in B2D y is down
  172.                
  173. ;use existing surface except when switching source textures
  174.        
  175.         If texture=lasttexture Then surface=FindSurface (displaylist,texture    _brush)
  176.         If Not surface Then surface=CreateSurface (displaylist,texture  _brush)
  177.         lasttexture=texture
  178.        
  179.         w#=texture      _width-1                        ;size of image (pixels)
  180.         h#=texture      _height-1
  181.         tw#=w/texture   _totalwidth             ;size of image (texture coords)
  182.         th#=h/texture   _totalheight
  183.         sx#=texture     _sx[frame]                      ;position of image within texture (texture coords)
  184.         sy#=texture     _sy[frame]
  185.  
  186.         ;                                                               four vertices represent four corners of sprite
  187.  
  188.         v1=AddVertex (surface,x,y,0,sx,sy)
  189.         v2=AddVertex (surface,x+w,y,0,sx+tw,sy)
  190.         v3=AddVertex (surface,x,y-h,0,sx,sy+th)
  191.         v4=AddVertex (surface,x+w,y-h,0,sx+tw,sy+th)
  192.                  
  193.         AddTriangle (surface,v1,v2,v3)  ;two triangle make rectangular up image
  194.         AddTriangle (surface,v2,v4,v3)
  195.  
  196. End Function


Comments :


thelizardking(Posted 1+ years ago)

 pretty nice :D


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal