Very Basic Noob Sprite Demo

BlitzMax Forums/BlitzMax Beginners Area/Very Basic Noob Sprite Demo

MGE(Posted 2008) [#1]
I know it's always good for noobs playing around with BMax to have something like this, so I threw it together. Nothing fancy, probably would have been better to use a TList, but I thought noobs might be more comfortable at the start with arrays. :)

Features:
Array of objects for sprite handling.
Basic delta time movement.
Frames per second displayed.

SuperStrict
'
' Basic Blitzmax Noob Sprite Demo By "MGE"
'
Const howmany:Int = 1000 ' <<<<<< Change this value for how many
'
Global sprites:TSprite[howmany]  ' Create an array of the type "TSprite"
Global MyImage:Timage = LoadImage("tile.png") ' any image file you want here
Global fps:Int        ' used to display the frames per second
Global fpstotal:Int   ' counts how many frames rendered in 1000ms (1 second)
Global ms:Float       ' var to keep track if 1000ms (1 sec) has passed
Global ms_start:Float ' var holds the beginning ms value at top of loop
Global delta:Float    ' simple delta time var. Difference between start and end in loop.
'
Type TSprite
 Field x:Float,y:Float
 Field xdir:Float,ydir:Float
 Field alfa:Float
 Field blendtype:Int
 '
 Method init()
  x = Rand(0,800)
  y = Rand(0,600)
  xdir = Rnd(-.2,.2)
  ydir = Rnd(-.2,.2)
  If Rand(0,100)>50
   blendtype = ALPHABLEND
  Else
   blendtype = LIGHTBLEND
  EndIf
  alfa = Rnd(.1,1)
 End Method
 '
 Method update()
  x=x+(xdir*delta)
  y=y+(ydir*delta)
  If x>800 Or x<0
   xdir=-xdir
   x=x+xdir
  EndIf
  If y>600 Or y<0
   ydir=-ydir
   y=y+ydir
  EndIf
  '
 End Method
 '
 Method render()
  SetAlpha alfa
  SetBlend blendtype
  DrawImage MyImage:Timage,x,y
 End Method
 '
End Type
'
Graphics(800,600,0,60) ' width, height, depth (16,32, 0=windowed), hz
'
For Local c:Int = 0 To howmany-1 ' arrays start at 0 so we go to total - 1
 sprites[c] = New TSprite            ' fill our array with our object type
 sprites[c].init                   ' call the object's init routine to set up the sprite
Next
'
resetstates()                    ' reset render states
ms = MilliSecs()                 ' get the current 1000ms clock
'
Repeat
 ' 
 ms_start=MilliSecs()
 '
 Cls
 '
 For Local obj:TSprite = EachIn sprites ' cycle through all objects in array
  obj.update          ' Call the "update" method to update sprite's position.
  obj.render          ' Call the "render" method to display the sprite.
 Next
 '
 resetstates() 
 DrawText "FPS: "+fps,0,0           ' Display how fast the program is running.
 Flip(0)                            ' Flip(1) for vsync, Flip(0) for full speed
 delta = MilliSecs() - ms_start     ' Computer delta, start time - end time of loop
 '
 DoFPS()                            ' Keep track of how many frames per second
 ' 
Until KeyHit(KEY_ESCAPE)            ' Press ESC to exit
'
EndGraphics()                       ' Close down graphics device
End                                 ' Still need docs on this command.
'
Function resetstates()              ' Reset render states
 SetColor(255,255,255)
 SetAlpha(1)
 SetBlend(ALPHABLEND)
 SetTransform 0,1,1
End Function
'
Function DoFPS()                    ' Inc frames, and track if 1 sec has passed
 fpstotal = fpstotal + 1
 If MilliSecs()>ms
  fps = fpstotal
  fpstotal = 0
  ms = MilliSecs() + 1000
 EndIf
End Function



deps(Posted 2008) [#2]
Why have you put a comment on the empty lines?


MGE(Posted 2008) [#3]
Just to add a little white space in between lines. A very old habit of mine that's impossible to break. lol...


dmaz(Posted 2008) [#4]
IMO
your sprite array (should be list) should be a global in your class. I think the image global should be in there too as a global or a field.

http://www.blitzbasic.com/Community/posts.php?topic=56922#633530

also, just to clean things up, for my sprites I put 2 "Function"s in the type that are UpdateAll() and RenderAll(). that keeps things nice and clean.

one other thing... all my objects always (extended from the base type, often abstract) have a Remove method so I don't have to worry about bad references and things since all that code is easy to keep straight when all together.

one of the best things to remember with OO is to always try "encapsulate" everything into the class. this helps immensely for modularity.


MGE(Posted 2008) [#5]
Good stuff dmaz, thanks. I came from VB6 to Blitzmax (and now working with C++ too) so I'm doing things a little different to say the least. In my actual game engine I'm using TLists for everything. But I still don't have everything inside the class as you suggest. I'll be re-writing my engine after my currrent game is done with all the goodies I've learned. :)


Grey Alien(Posted 2008) [#6]
Of course using an array for sprite pooling may be better than lists for speed. I'm considering make a TFastParticle class in the future that does this.


MGE(Posted 2008) [#7]
In my tests a while back, an array system wasn't that much faster if at all, especially when you're rendering hundreds of particles, which at that point your biggest hurdle is GPU related.


dmaz(Posted 2008) [#8]
yeah we had these "fights" before :) arrays are a little faster but it's just not worth it. memory allocation is the bigger problem. I posted a pooling list type that does object reallocation.
http://www.blitzbasic.com/Community/posts.php?topic=62667
nobody commented on it :(
TList: 143ms
TBeList: 8ms

if you want the fastest (faster than arrays) then just use a basic single or double list built into your type... that's the fastest loop you'll get. but again, it's just not worth it (IMO) you save say 2ms over iterating 50,000 object TList.


Grey Alien(Posted 2008) [#9]
In my tests a while back, an array system wasn't that much faster if at all
Yeah as Dmaz sys I'm not talking about it from a point of arrays being quicker to iterate, but the fact that a max number of particles are ALREADY created (i.e. "pooled") so that there's no memory allocation/deallocation.

Dmaz: JEEZ that's an improvement. I can say for sure that the overhead on my PC seems to be CPU not GPU. I could add some single surface code for maybe 10% improvement but more is to be had with pooling and also making a slimmed down super fast particle type for basic purposes and keeping my more complex one for, well, more complex purposes.


Rico(Posted 2008) [#10]
Thats a really good idea MGE. I know I would have found that sprite demo useful when I first started! Nice idea!