January 26, 2021, 05:24:04 AM

Author Topic: [bmx] Max2D Stack by N [ 1+ years ago ]  (Read 461 times)

Offline BlitzBot

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

Description : Wrote one of these for my engine's 2D system, so I figured I'd let those of you who don't use it (everyone but me) have the same option for managing 2D state(s).

One of the handier things these can be used for are sprite systems that have parent/child relationships that inherit transformations and such.  E.g.,
Code: [Select]
Type Sprite
   
    Field children:TList = New TList
   
    Method Draw( )
        Push2D( )
       
        ' Change some stuff with the state in here, like scale or rotation or blend mode or something
       
        ' Call this before drawing the image
        State.Update( ) ' <- Applies any changes in the current state
       
        For Local i:Sprite = EachIn children
            i.Draw( )
        Next
       
        Pop2D( )
        ' Back to normal
    End Method
   
End Type


Code :
Code: BlitzMax
  1. Strict
  2.  
  3. Global State:State2D = New State2D
  4.  
  5. Function Push2D( )
  6.     State = State.Push( )
  7. End Function
  8.  
  9. Function Pop2D( )
  10.     State = State.Pop( )
  11. End Function
  12.  
  13. Type State2D
  14.     Const CF_TRANSFORM = 0
  15.     Const CF_BLEND = 1
  16.     Const CF_VIEWPORT = 2
  17.     Const CF_HANDLE = 3
  18.     Const CF_ORIGIN = 4
  19.     Const CF_ALPHA = 5
  20.     Const CF_COLOR = 6
  21.     Const CF_CLSCOLOR = 7
  22.    
  23.     Field _pred:State2D
  24.     Field sx#=1, sy#=1
  25.     Field rot#
  26.     Field r%,g%,b%,a#
  27.     Field ox#, oy#
  28.     Field hx#, hy#
  29.     Field vx%, vy%, vw%, vh%
  30.     Field blend_% = SOLIDBLEND
  31.     Field cr%, cg%, cb%
  32.     Field cf[]=[1,1,1,1,1,1,1,1]
  33.    
  34.     ' Does not copy its predecessor
  35.     Method Clone:State2D( )
  36.         Local s:State2D = New State2D
  37.         MemCopy( Varptr s.sx, Varptr sx, 76 )
  38.         s.cf = cf[..]
  39.         Return s
  40.     End Method
  41.    
  42.     ' Creates a copy of the top state (Self prior to Push()ing) and sets self as its predecessor
  43.     Method Push:State2D( )
  44.         Local s:State2D = Clone( )
  45.         s._pred = Self
  46.         Return s
  47.     End Method
  48.    
  49.     Method Pop:State2D( )
  50.         Assert _pred,"State2D error: stack underflow"
  51.         Local p:State2D = _pred
  52.         _pred = Null
  53.         p.Bind( )
  54.         Return p
  55.     End Method
  56.    
  57.     Method Scale( x#, y# )
  58.         sx :* x
  59.         sy :* y
  60.         cf[CF_TRANSFORM]=True
  61.     End Method
  62.    
  63.     Method Rotate( r# )
  64.         rot :+ r
  65.         cf[CF_TRANSFORM]=True
  66.     End Method
  67.    
  68.     Method ScaleAbsolute( x#, y# )
  69.         sx = x
  70.         sy = y
  71.         cf[CF_TRANSFORM]=True
  72.     End Method
  73.    
  74.     Method Rotation( r# )
  75.         rot = r
  76.         cf[CF_TRANSFORM]=True
  77.     End Method
  78.    
  79.     Method Color( r%, g%, b% )
  80.         Self.r = r
  81.         Self.g = g
  82.         Self.b = b
  83.         cf[CF_COLOR]=True
  84.     End Method
  85.    
  86.     Method Alpha( a# )
  87.         Self.a = a
  88.         cf[CF_ALPHA]=True
  89.     End Method
  90.    
  91.     Method ClearColor( r%, g%, b% )
  92.         cr = r
  93.         cg = g
  94.         cb = b
  95.         cf[CF_CLSCOLOR]=True
  96.     End Method
  97.    
  98.     Method Handle( hx#, hy# )
  99.         Self.hx = hx
  100.         Self.hy = hy
  101.         cf[CF_HANDLE]=True
  102.     End Method
  103.    
  104.     Method Viewport( x%, y%, w%, h% )
  105.         vx = x
  106.         vy = y
  107.         vw = w
  108.         vh = h
  109.         cf[CF_VIEWPORT]=True
  110.     End Method
  111.    
  112.     Method Blend( b% )
  113.         blend_ = b
  114.         cf[CF_BLEND]=True
  115.     End Method
  116.    
  117.     Method Origin( ox#, oy# )
  118.         Self.ox = ox
  119.         Self.oy = oy
  120.         cf[CF_ORIGIN]=True
  121.     End Method
  122.    
  123.     Method MoveOrigin( ox_#, oy_# )
  124.         Self.ox :+ ox_
  125.         Self.oy :+ oy_
  126.         cf[CF_ORIGIN]=True
  127.     End Method
  128.    
  129.     Method Identity( )
  130.         sx = 1
  131.         sy = 1
  132.         rot = 0
  133.        
  134.         r = 255
  135.         g = 255
  136.         b = 255
  137.        
  138.         a = 1
  139.        
  140.         ox = 0
  141.         oy = 0
  142.        
  143.         hx = 0
  144.         hy = 0
  145.        
  146.         vx = 0
  147.         vy = 0
  148.         vw = GraphicsWidth( )
  149.         vh = GraphicsHeight( )
  150.        
  151.         blend_ = SOLIDBLEND
  152.        
  153.         cr = 0
  154.         cg = 0
  155.         cb = 0
  156.        
  157.         cf = [1,1,1,1,1,1,1,1]
  158.     End Method
  159.    
  160.     Method Bind( )
  161.         Update( True )
  162.     End Method
  163.    
  164.     Method Update( Force=0 )
  165.         If cf[CF_ORIGIN] Or Force Then SetOrigin( ox, oy )
  166.         If cf[CF_HANDLE] Or Force Then SetHandle( hx, hy )
  167.         If cf[CF_TRANSFORM] Or Force Then SetTransform( rot, sx, sy )
  168.         If cf[CF_VIEWPORT] Or Force Then SetViewport( vx, vy, vw, vh )
  169.         If cf[CF_COLOR] Or Force Then SetColor( r, g, b )
  170.         If cf[CF_ALPHA] Or Force Then SetAlpha( a )
  171.         If cf[CF_BLEND] Or Force Then SetBlend( blend_ )
  172.         If cf[CF_CLSCOLOR] Or Force Then SetClsColor( cr, cg, cb )
  173.         cf = [0,0,0,0,0,0,0,0]
  174.     End Method
  175. End Type


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal