bunnymark.monkey2 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Namespace bunnies
  2. #Import "assets/wabbit_alpha.png"
  3. #Import "<std>"
  4. #Import "<mojo>"
  5. Using std..
  6. Using mojo..
  7. Global VirtualSize:=New Vec2i( 640,480 )
  8. Const initialCount := 10000
  9. Function Main()
  10. New AppInstance
  11. New Bunnymark
  12. App.Run()
  13. End Function
  14. '******************************************************************************************************
  15. Class Bunnymark Extends Window
  16. Field atlas:Image
  17. Field frames:=New Image[4]
  18. Field bunnies := New Stack<Bunny>
  19. Method New()
  20. Super.New("Bunnymark", 1024, 768, WindowFlags.Resizable )
  21. Layout="letterbox"
  22. atlas=Image.Load( "asset::wabbit_alpha.png" )
  23. For Local j:=0 Until 2
  24. For Local i:=0 Until 2
  25. frames[ j*2+i ]=New Image( atlas,i*32,j*64,32,64 )
  26. frames[ j*2+i ].Handle=New Vec2f( .5,.5 )
  27. Next
  28. Next
  29. For Local n:= 0 Until initialCount
  30. bunnies.Push( New Bunny( 512, 384,frames[ Rnd(4) ] ) )
  31. Next
  32. End
  33. Method OnMeasure:Vec2i() Override
  34. Return VirtualSize
  35. End
  36. Method OnRender( canvas:Canvas ) Override
  37. RequestRender()
  38. If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()
  39. canvas.Color = Color.White
  40. canvas.DrawRect( 0, 0, App.ActiveWindow.Width , 25 )
  41. For Local bunny:=Eachin bunnies
  42. bunny.Update( canvas )
  43. Next
  44. canvas.Color = Color.Black
  45. canvas.DrawText( "Bunnymark="+bunnies.Length+" FPS="+App.FPS+" (LMB=+10 MMB=+100 RMB=+1000 +Alt=Remove)",10,5 )
  46. End
  47. Method OnMouseEvent( event:MouseEvent ) Override
  48. If event.Type = EventType.MouseDown
  49. Local _len := 0
  50. If event.Button = MouseButton.Left
  51. _len = 10
  52. Elseif event.Button = MouseButton.Middle
  53. _len = 100
  54. Elseif event.Button = MouseButton.Right
  55. _len = 1000
  56. End
  57. If Keyboard.KeyDown( Key.LeftAlt ) Or Keyboard.KeyDown( Key.RightAlt )
  58. For Local n := 1 To _len
  59. If bunnies.Length Then bunnies.Pop()
  60. Next
  61. Else
  62. For Local n := 1 To _len
  63. bunnies.Push( New Bunny( Mouse.X, Mouse.Y,frames[ Rnd(4) ] ) )
  64. Next
  65. End
  66. End
  67. End
  68. End
  69. '******************************************************************************************************
  70. Class Bunny
  71. Global gravity := 0.1
  72. Global border := 32.0
  73. Field x: Float
  74. Field y: Float
  75. Field xspeed: Float
  76. Field yspeed: Float
  77. Field maxBounce:= 5.0
  78. Field image:Image
  79. Method New( x:Float,y:Float,image:Image )
  80. Self.x = x
  81. Self.y = y
  82. xspeed = Rnd( -10, 10 )
  83. Self.image=image
  84. End
  85. Method Update:Void( canvas:Canvas )
  86. yspeed += gravity
  87. y += yspeed
  88. x += xspeed
  89. If y < border*2
  90. y = border*2
  91. yspeed *= -1
  92. yspeed = Clamp( yspeed, 0.0, Float( maxBounce ) )
  93. End
  94. If y > App.ActiveWindow.Height - border
  95. y = App.ActiveWindow.Height - border
  96. yspeed = -random.Rnd( maxBounce * 3 )
  97. End
  98. If( x < border ) Or ( x > App.ActiveWindow.Width - border )
  99. xspeed *= -1
  100. x = Clamp( x, border, Float(App.ActiveWindow.Width - border ) )
  101. End
  102. canvas.DrawImage( image,x,y )
  103. End
  104. End