gamegraphics.monkey2 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #Import "<mojo>"
  2. #Import "src/sprite"
  3. #Import "src/background"
  4. Using mojo..
  5. Using std..
  6. Class GameGraphics Abstract
  7. Field images :Image[] 'The array containing all frames
  8. Field debug := False
  9. Protected
  10. Field _handle:= New Vec2f
  11. '************************************* Public Properties *************************************
  12. Public
  13. 'Sets the handles in all sub-images
  14. Property Handle:Vec2f()
  15. Return _handle
  16. Setter( handle:Vec2f )
  17. _handle = handle
  18. For Local i := Eachin images
  19. i.Handle = _handle
  20. Next
  21. End
  22. Property Width:Double()
  23. Return images[0].Width
  24. End
  25. Property Height:Double()
  26. Return images[0].Height
  27. End
  28. '************************************* Public Methods *************************************
  29. 'Loads an array of Images from a sprite sheet
  30. Method LoadFrames:Image[] ( path:String, numFrames:Int, cellWidth:Int, cellHeight:Int, filter:Bool = True, padded:Bool = False )
  31. Local atlasTextture := Texture.Load( path, Null )
  32. Assert( atlasTextture, " ~n ~nGameGraphics: Image " + path + " not found.~n ~n" )
  33. Local atlasImg := New Image( atlasTextture )
  34. Local imgs := New Image[ numFrames ]
  35. If cellHeight = atlasImg.Height
  36. Local x := 0
  37. Local width := cellWidth
  38. If padded
  39. x += 1
  40. width -= 2
  41. End
  42. For Local i := 0 Until numFrames
  43. Local x0 := i * cellWidth + x
  44. Local y0 := 0
  45. imgs[i] = New Image( atlasImg, New Recti( x0, y0, x0 + cellWidth, cellHeight ) )
  46. Next
  47. Else
  48. Local x:= 0
  49. Local width:= cellWidth
  50. Local y:= 0
  51. Local height:= cellHeight
  52. Local columns:= atlasImg.Width / width
  53. If padded
  54. x += 1
  55. y += 1
  56. width -= 2
  57. height -= 2
  58. End If
  59. For Local i:= 0 Until numFrames
  60. Local fx := i Mod columns * cellWidth
  61. Local fy := i / columns * cellHeight
  62. imgs[i] = New Image( atlasImg, New Recti( fx + x, fy + y, fx + cellWidth, fy + cellHeight ) )
  63. Next
  64. Endif
  65. atlasImg = Null
  66. Return imgs
  67. End
  68. Method DrawOutline( canvas:Canvas, x:Float, y:Float, width:Float, height:Float, rz:Float = 0, sx:Float = 1.0, sy:Float = 1.0 )
  69. Local matrix:= canvas.Matrix
  70. canvas.Translate( x, y )
  71. canvas.Rotate( rz )
  72. canvas.Scale( sx,sy )
  73. DrawRectOutline( canvas, -( width * Handle.X ), -( height * Handle.Y ), width, height )
  74. canvas.Matrix=matrix
  75. End
  76. '************************************* Class Functions *************************************
  77. Function DrawRectOutline:Void(canvas:Canvas, left:Int, top:Int, width:Int, height:Int )
  78. canvas.DrawLine( left, top, left+width, top )
  79. canvas.DrawLine( left, top, left ,top+height )
  80. canvas.DrawLine( left, top+height, left+width, top+height )
  81. canvas.DrawLine( left+width, top, left+width, top+height )
  82. End
  83. End