rendertoimage.monkey2 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. Namespace test
  2. #Import "<std>"
  3. #Import "<mojo>"
  4. Using std..
  5. Using mojo..
  6. #Import "assets/spaceship.png"
  7. 'A dynamic image has its contents updated frequently so doesn't need to be managed in case of graphics device resets.
  8. '
  9. Const Dynamic:=False'True
  10. Class MyWindow Extends mojo.app.Window
  11. Field image:Image
  12. Field icanvas:Canvas
  13. Method New()
  14. image=New Image( 256,256,PixelFormat.RGBA8,Dynamic ? TextureFlags.Dynamic Else Null )
  15. image.Texture.Flags|=TextureFlags.Filter
  16. image.Handle=New Vec2f( .5,.5 )
  17. icanvas=New Canvas( image )
  18. If Not Dynamic RenderImage()
  19. End
  20. Method RenderImage()
  21. 'render to image...
  22. For Local x:=0 Until 16
  23. For Local y:=0 Until 16
  24. If (x~y)&1
  25. icanvas.Color=New Color( Sin( Millisecs()*.01 )*.5+.5,Cos( Millisecs()*.02 )*.5+.5,.5 )
  26. Else
  27. icanvas.Color=Color.Yellow
  28. Endif
  29. icanvas.DrawRect( x*16,y*16,16,16 )
  30. Next
  31. Next
  32. icanvas.Color=Color.White
  33. icanvas.DrawText( "This way up!",icanvas.Viewport.Width/2,0,.5,0 )
  34. icanvas.Flush()
  35. End
  36. Method OnRender( canvas:Canvas ) Override
  37. App.RequestRender()
  38. If Dynamic RenderImage()
  39. Global rot:=0.0
  40. rot+=.001
  41. canvas.DrawImage( image,App.MouseLocation.x,App.MouseLocation.y,rot )
  42. canvas.DrawText( "Here!",0,0 )
  43. End
  44. Method OnKeyEvent( event:KeyEvent ) Override
  45. Select event.Type
  46. Case EventType.KeyDown
  47. Select event.Key
  48. Case Key.Space
  49. Print "Invalidating graphics!"
  50. mojo.graphics.glutil.glGraphicsSeq+=1
  51. End
  52. End
  53. End
  54. End
  55. Function Main()
  56. New AppInstance
  57. New MyWindow
  58. App.Run()
  59. End