spacechimps.monkey2 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. Namespace spacechimps
  2. #Import "<std>"
  3. #Import "<mojo>"
  4. #Import "assets/bang.wav"
  5. #Import "assets/spaceship_32.png"
  6. Using std..
  7. Using mojo..
  8. Const VirtualWidth:=320'640
  9. Const VirtualHeight:=240'480
  10. Class MyWindow Extends Window
  11. Field timer:Timer
  12. Field laser:Sound
  13. Field image:Image
  14. Field pos:=New Vec2f
  15. Field vel:=New Vec2f
  16. Field rot:Float
  17. Field filter:Bool=True
  18. Method New( title:String,width:Int,height:Int,flags:WindowFlags )
  19. 'Call super class constructor - this just passes the arguments 'up' to the Window class constructor.
  20. '
  21. Super.New( title,width,height,flags )
  22. ' Layout="fill"
  23. 'if you use letterbox layout, don't forget to implement the OnMeasure() method (see below).
  24. '
  25. Layout="letterbox"
  26. 'Window clear color - this is effectively Letterbox color.
  27. '
  28. ClearColor=New Color( .03,.03,.03 )
  29. 'load audio/images
  30. '
  31. LoadResources()
  32. 'Set initial image pos
  33. '
  34. pos=New Vec2f( VirtualWidth/2,VirtualHeight/2 )
  35. 'Start update timer
  36. '
  37. ' timer=New Timer( 60,OnUpdate )
  38. 'Enable vsync.
  39. '
  40. SwapInterval=1
  41. End
  42. Method LoadResources()
  43. 'Load laser sound effect
  44. '
  45. laser=Sound.Load( "asset::bang.wav" )
  46. 'Load spaceship image.
  47. '
  48. 'Note: scaling image here via Image.Scale is faster than scaling via Canvas matrix.
  49. '
  50. image=Image.Load( "asset::spaceship_32.png" )
  51. image.Scale=New Vec2f( 2 )
  52. image.Handle=New Vec2f( .5 )
  53. End
  54. Method OnKeyEvent( event:KeyEvent ) Override
  55. Select event.Type
  56. Case EventType.KeyDown
  57. Select event.Key
  58. Case Key.Enter
  59. If event.Modifiers & Modifier.Alt
  60. If Fullscreen EndFullscreen() Else BeginFullscreen()
  61. Endif
  62. Case Key.S
  63. 'toggle vsync
  64. '
  65. SwapInterval=1-SwapInterval
  66. Case Key.T
  67. 'toggle timer
  68. '
  69. If timer
  70. timer.Cancel()
  71. timer=Null
  72. RequestRender()
  73. Else
  74. timer=New Timer( 60,OnUpdate )
  75. Endif
  76. Case Key.F
  77. 'toggle texture filtering
  78. '
  79. filter=Not filter
  80. End
  81. End
  82. End
  83. Method OnWindowEvent( event:WindowEvent ) Override
  84. Select event.Type
  85. Case EventType.WindowGainedFocus
  86. If timer timer.Suspended=False
  87. Case EventType.WindowLostFocus
  88. If timer timer.Suspended=True
  89. Default
  90. Super.OnWindowEvent( event )
  91. End
  92. End
  93. Method OnUpdate()
  94. RequestRender()
  95. 'give GC a bit of a thrash....
  96. '
  97. 'LoadResources()
  98. 'rotate
  99. '
  100. If Keyboard.KeyDown( Key.Left )
  101. rot+=.1
  102. Else If Keyboard.KeyDown( Key.Right )
  103. rot-=.1
  104. Endif
  105. 'wrap rot to [-Pi,Pi)
  106. '
  107. rot=(rot+Pi*3) Mod TwoPi-Pi
  108. 'calc forward vector..
  109. '
  110. Local dir:=New Vec2f( Cos( rot ),-Sin( rot ) )
  111. 'thrust
  112. '
  113. If Keyboard.KeyDown( Key.Up )
  114. vel+=(dir * 5 - vel) *.025 'arcadey thruster
  115. ' vel+=dir * .03 'realistic...
  116. Else
  117. vel*=.999
  118. End
  119. 'add velocity to position
  120. '
  121. pos+=vel
  122. 'wrap pos to [0,size)
  123. '
  124. pos.x=(pos.x+Width) Mod Width
  125. pos.y=(pos.y+Height) Mod Height
  126. If Keyboard.KeyPressed( Key.Space )
  127. laser.Play()
  128. ' Print "Fire!"
  129. End
  130. If Keyboard.KeyReleased( Key.Space )
  131. ' Print "UnFire!"
  132. End
  133. End
  134. Field ms:=0
  135. Method OnRender( canvas:Canvas ) Override
  136. Local e:=App.Millisecs-ms 'ideally, e should be 16,17,17,16,17,17 ie: 16.6666...
  137. ' If e<>16 And e<>17 Print "elapsed="+e 'show glitches
  138. ms+=e
  139. If Not timer OnUpdate()
  140. 'Turn off texture filtering for a 'pixel art' look
  141. '
  142. canvas.TextureFilteringEnabled=filter
  143. canvas.Clear( Color.Black )
  144. canvas.DrawText( "FPS="+App.FPS,Width/2,0,.5,0 )
  145. canvas.DrawText( "Arrow keys to fly",Width/2,16,.5,0 )
  146. canvas.DrawText( "Swap interval="+SwapInterval +" ('S' to toggle)",Width/2,32,.5,0 )
  147. canvas.DrawText( "Timer sync="+(timer ? "true" Else "false")+" ('T' to toggle)",Width/2,48,.5,0 )
  148. canvas.DrawText( "Filtering="+(filter ? "true" Else "false")+" ('F' to toggle)",Width/2,64,.5,0 )
  149. 'Draw image
  150. '
  151. Local x:=pos.x,y:=pos.y,r:=rot-Pi/2
  152. canvas.DrawImage( image,x,y,r )
  153. 'Draw wrap around(s)
  154. '
  155. If x-image.Radius<0 canvas.DrawImage( image,x+Width,y,r )
  156. If x+image.Radius>Width canvas.DrawImage( image,x-Width,y,r )
  157. If y-image.Radius<0 canvas.DrawImage( image,x,y+Height,r )
  158. If y+image.Radius>Height canvas.DrawImage( image,x,y-Height,r )
  159. End
  160. Method OnMeasure:Vec2i() Override
  161. Return New Vec2i( VirtualWidth,VirtualHeight )
  162. End
  163. End
  164. Function Main()
  165. New AppInstance
  166. New MyWindow( "Chimps in Space!",640,480,WindowFlags.Resizable )
  167. App.Run()
  168. End