January 23, 2021, 04:42:29 AM

Author Topic: [bmx] TSprite by Mahan [ 1+ years ago ]  (Read 519 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] TSprite by Mahan [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : TSprite
Author : Mahan
Posted : 1+ years ago

Description : Ever felt drawing of TImage:s in bmx felt dull? That keeping track of which order they are drawn manually was tedious? That controlling alpha, color, scaling and rotation by making separate function-calls before DrawImage(), and maybe after too to reset the drawing state wasn't an easy way to handle things?

Fear not because here is the TSprite basic sprite system, that will make life easier for you. Just as in the early days of C64 and Amiga you can now create your sprites and control their respective properties and get them drawn automatically as you want them, and in which order you want them drawn.

(This code is mostly aimed at beginners to make life easier, but can easily be extended for intermediate and professional needs)

edit: bugfix, spelling

sprite.bmx:


Code :
Code: BlitzMax
  1. '
  2. ' TSprite - a very basic sprite system.
  3. '
  4. ' Although rudimentary it keeps track of each sprites unique properties
  5. ' and draws them all to the screen.
  6. '
  7. ' zOrder, Accessed through GetzOrder()/SetzOrder(), makes it simple
  8. ' for the user to control the order in which they are drawn.
  9. ' (the higher the zOrder the later it is drawn and thus appearing "on top"
  10. ' of previously draw sprites)
  11. '
  12. ' Usage:
  13. '       Create Sprites:
  14. '               local mySprite:TSprite=TSprite.createSprite(image:TImage)
  15. '
  16. '       The createSprite() call also has lots of default params (see source below)
  17. '   to set the sprites initial values.
  18. '
  19. '       Delete Sprites:
  20. '               TSprite.deleteSprite(sprite:TSprite)
  21. '
  22. '       Draw all sprites:
  23. '
  24. '               TSprite.drawAll()
  25. '
  26. '
  27. ' A sprite uses a TImage for its appearance. (The same TImage can be used for several sprites.)
  28. '
  29. ' More on zOrder: If several sprites share the same zOrder, they are drawn in the order they got
  30. ' their zOrder (or where created, if not set manually later, as zOrder is set during creation)
  31. '
  32. '
  33. ' Author: Mattias Hansson aka MaHan (2009)
  34. '
  35.  
  36. Type TSprite
  37.  
  38.         Const EPSILON:Float = 0.0001
  39.         Const WHITE:Int = $ff
  40.        
  41.         'The sprite list keeps the sprites in drawing order
  42.         Global _spriteList:TList = New TList
  43.        
  44.         Field image:TImage  ' The image-ref containing the sprite appearance
  45.         Field angle:Float   ' angle (in degrees) of sprite
  46.         Field alpha:Float   ' sprite alpha
  47.         Field blendMode:Int ' sprite blendmode
  48.         Field colorR:Int    ' Red color component
  49.         Field colorG:Int    ' Green color component
  50.         Field colorB:Int    ' blue color component
  51.         Field scaleX:Float  ' horizontal scale (where 1.0 is original size)
  52.         Field scaleY:Float  ' vertical scale (where 1.0 is original size)
  53.         Field x:Int         ' x-position
  54.         Field y:Int         ' y-position
  55.         Field height:Int    ' height of sprite
  56.         Field width:Int     ' width of sprite
  57.         Field _zOrder:Int   ' Z-order of the sprite
  58.         Rem
  59.         bbdoc: Get the zOrder value in this TSprite object.
  60.         End Rem
  61.         Method GetzOrder:Int()
  62.                 Return _zOrder
  63.         End Method
  64.         Rem
  65.         bbdoc: Set the zOrder value for this TSprite object.
  66.         End Rem
  67.         Method SetzOrder(Value:Int)
  68.                 _zOrder = Value
  69.                 _spriteList.Remove(Self)
  70.                 Local inserted:Int = False
  71.                 For Local sprite:TSprite = EachIn Self._spriteList
  72.                         If sprite._zOrder > Self._zOrder Then
  73.                                 Self._spriteList.InsertBeforeLink(Self, Self._spriteList.FindLink(sprite))
  74.                                 inserted = True
  75.                                 Exit
  76.                         End If
  77.                 Next
  78.                 If Not inserted Then
  79.                         Self._spriteList.AddLast(Self)
  80.                 End If
  81.         End Method
  82.        
  83.        
  84.         Function drawAll()
  85.                 For Local sprite:TSprite = EachIn Self._spriteList
  86.                         sprite._draw()
  87.                 Next
  88.         End Function
  89.        
  90.         Method _draw()
  91.                 Local rotated:Int = False
  92.                 Local scaled:Int = False
  93.                 Local colored:Int = False
  94.                 Local alphad:Int = False
  95.  
  96.                                
  97.                 'Set rotation and scaling (of not default)
  98.                 If (Abs(Self.angle - 0.0) > EPSILON) Then
  99.                         rotated = True
  100.                 End If
  101.                
  102.                 ' scale this sprite (if needed)
  103.                 If (Abs(Self.scaleX - 1.0) > EPSILON) Or (Abs(Self.scaleY - 1.0) > EPSILON) Then
  104.                         scaled = True
  105.                 End If
  106.  
  107.                 If scaled Or rotated Then
  108.                         SetTransform(Self.angle, Self.scaleX, Self.scaleY)
  109.                 End If
  110.                
  111.                 If Not ((Self.colorR = WHITE) And (Self.colorG = WHITE) And (Self.colorB = WHITE)) Then
  112.                         SetColor(Self.colorR, Self.colorG, Self.colorB)
  113.                         colored = True
  114.                 End If
  115.  
  116.                 If Not (Self.blendMode = MASKBLEND) Then
  117.                         SetBlend(Self.blendMode)
  118.                 End If
  119.                
  120.                 If (Abs(Self.alpha - 1.0) > EPSILON) Then
  121.                         SetAlpha(Self.alpha)
  122.                         alphad = True
  123.                 End If
  124.                
  125.                                                
  126.                 DrawImage(Self.image, Self.x, Self.y)
  127.  
  128.                
  129.                 If alphad Then
  130.                         SetAlpha(1)
  131.                 End If
  132.                
  133.                 If Not (Self.blendMode = MASKBLEND) Then
  134.                         SetBlend(MASKBLEND)
  135.                 End If
  136.                
  137.                 If colored Then
  138.                         SetColor(WHITE, WHITE, WHITE)
  139.                 End If
  140.  
  141.                 If scaled Or rotated Then
  142.                         SetTransform()
  143.                 End If
  144.                
  145.                
  146.         End Method
  147.        
  148.         ' factory method
  149.         Function createSprite:TSprite(image:TImage, x:Int = 0, y:Int = 0, zOrder:Int = 0, scaleX:Float = 1.0, scaleY:Float = 1.0, angle:Float = 0, alpha:Float = 1.0, blendmode:Int = MASKBLEND)
  150.                 Local ns:TSprite = New TSprite
  151.                
  152.                 ns.image = image
  153.                 ns.x = x
  154.                 ns.y = y
  155.                 ns.scaleX = scaleX
  156.                 ns.scaleY = scaleY
  157.                 ns.angle = angle
  158.                 ns.alpha = alpha
  159.                 ns.blendMode = blendMode
  160.                 ns.colorR = WHITE
  161.                 ns.colorG = WHITE
  162.                 ns.colorB = WHITE
  163.  
  164.                 ns.SetzOrder(zOrder)
  165.                
  166.                 Return ns
  167.         End Function
  168.        
  169.         Function deleteSprite(sprite:TSprite)
  170.                 _spriteList.Remove(sprite)
  171.         End Function
  172.        
  173. End Type


Comments :


Mahan(Posted 1+ years ago)

 Here is a demo:Remember to update this line with your own image (64x64 or 128x128):Local img:TImage = LoadImage("attack e0000.bmp") sprite_demo.bmx:
Code: [Select]

' TSprite demo code
'
' keys:
' [KEY_LEFT] [KEY_RIGHT] - move sprite horizontally
' [A] [D] - rotate sprite
'   [W] [S] - higher or lower zOrder
'   [Q] [E] - adjust alpha
'   [RETURN] - add a sprite at runtime.
'   [BACKSPACE] - delete a sprite at runtime
'

Include "sprite.bmx"

Graphics 800, 600

AutoMidHandle(True)

Local img:TImage = LoadImage("attack e0000.bmp") ' Change to load your own image here. Any 64x64 or 128x128 should do fine.

Local spr1:TSprite = TSprite.createSprite(img, 100, 100)
Local spr2:TSprite = TSprite.createSprite(img, 300, 100)

spr1.blendMode = ALPHABLEND

Local spr3:TSprite = Null

While Not KeyDown(Key_Escape)
Cls

' move sprite x
If KeyDown(KEY_RIGHT) Then spr1.x:+1
If KeyDown(KEY_LEFT) Then spr1.x:-1

' rotate mage
If KeyDown(KEY_A) Then spr1.angle:-1
If KeyDown(KEY_D) Then spr1.angle:+1

' set zOrder
If KeyDown(KEY_W) Then spr1.SetzOrder(1)
If KeyDown(KEY_S) Then spr1.SetzOrder(-1)

' adjust alpha
If KeyDown(KEY_Q) Then spr1.alpha:-0.005
If KeyDown(KEY_E) Then spr1.alpha:+0.005

If KeyDown(KEY_RETURN) And (spr3 = Null) Then spr3 = TSprite.createSprite(img, 500, 100)
If KeyDown(KEY_BACKSPACE) And Not (spr3 = Null) Then TSprite.deleteSprite(spr3) ; spr3 = Null

TSprite.drawAll()

Flip 1
Wend




Chroma(Posted 1+ years ago)

 Nice piece of work to study.  Thanks for this. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal