How to avoid canvas.Clear - MOJO2

Monkey Forums/Monkey Programming/How to avoid canvas.Clear - MOJO2

APC(Posted 2016) [#1]
The code below suppose to draw a line on the screen as I move my mouse, because I commented out the canvas.Clear, but for some reason MOJO 2 still clearing the canvas.
Is there a command to tell MOJO 2 to not clear the canvas?
On the code below you can set the #MOJO to 1 or 2 and see the problem happening.


Strict
#MOJO = 1
#IF MOJO=2
	Import mojo2
#ELSE
	Import mojo
#END
Class MyApp Extends App
	Field canvas:Canvas
    Field isMojo2:Bool = False
	Method OnCreate:Int()
		SetUpdateRate(0)
		#IF MOJO=2
			canvas = New Canvas
		#END

		Return 0
	End
 	Method OnRender:Int()
		#If MOJO=2
		canvas.SetColor 1, 1, 0
			canvas.DrawText("MOJO 2 Test", 0, 0)
 			canvas.SetColor 0, 1, 0
			canvas.DrawCircle(MouseX(), MouseY(), 3)
			canvas.Flush
		#Else
		SetColor(255, 0, 255)
			DrawText("MOJO 1 Test", 0, 0)
			SetColor(255, 0, 0)
			DrawCircle(MouseX(), MouseY(), 3)
			
		#EndIf
		Return 0
	End
End
Function Main:Int()
	New MyApp
	Return 0
End



APC(Posted 2016) [#2]
Ok I got it working! I had to create a Render and use layers.

Here is my new code

Mark Sibly , why when I omit the canvas.Clear() the CANVAS still gets cleared? Is that a bug? Is there a way to SetClearMode(0) to the CANVAS , like it is for the Render?

Strict
#MOJO = 2
#IF MOJO=2
	Import mojo2
#ELSE
	Import mojo
#END

Const NUM_LIGHTS:= 5

'create an orthographics projection matrix
Function Mat4Ortho:Float[]( left:Float,right:Float,bottom:Float,top:Float,znear:Float,zfar:Float )

	Local w:=right-left,h:=top-bottom,d:=zfar-znear
	
	Return [ 2.0/w,0,0,0, 0,2.0/h,0,0, 0,0,2.0/d,0, -(right+left)/w,-(top+bottom)/h,-(zfar+znear)/d,1 ]
End

Class MyLight Implements ILight

	'note: x,y,z,w go in last 4 components of matrix...
	Field matrix:=[1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 0.0,0.0,-100.0,1.0]
	Field color:=[0.2,0.2,0.2,1.0]
	Field range:=400.0

	'implement ILight interface...
	'
	Method LightMatrix:Float[]()
		Return matrix
	End
	
	Method LightType:Int()
		Return 1
	End
	
	Method LightColor:Float[]()
		Return color
	End
	
	Method LightRange:Float()
		Return range
	End
	
	Method LightImage:Image()
		Return Null
	End

End

Class MyLayer Extends DrawList Implements ILayer

	Field lights:=New Stack<MyLight>
	Field layerMatrix:=[1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 0.0,0.0,0.0,1.0]
	Field layerFogColor:=[0.0,0.0,0.0,0.0]

	'implement ILayer interface...
	'
	Method LayerMatrix:Float[]()
		Return layerMatrix
	End
	
	Method LayerFogColor:Float[]()
		Return layerFogColor
	End
	
	Method LayerLightMaskImage:Image()
		Return Null
	End
	
	Method EnumLayerLights:Void( lights:Stack<ILight> )
		For Local light:=Eachin Self.lights
			lights.Push light
		Next
	End

	Method OnRenderLayer:Void( drawLists:Stack<DrawList> )
		drawLists.Push Self
	End

End
Class MyApp Extends App
	Field canvas:Canvas
    Field isMojo2:Bool = False
	Field renderer:Renderer
	Field layer0:MyLayer
	
	Method OnCreate:Int()
		SetUpdateRate(0)
		#IF MOJO=2
			canvas = New Canvas
			
			renderer = New Renderer
			
			renderer.SetViewport(0, 0, DeviceWidth(), DeviceHeight())
			renderer.SetProjectionMatrix(Mat4Ortho(0, 640, 0, 480, -1, 1))
			renderer.SetAmbientLight( [0.1,0.1,0.1,1.0] )
			renderer.SetClearMode(0)
			
			'create layer 0
			layer0=New MyLayer
		
		#END
		renderer.Layers.Push layer0
		Return 0
	End
    
	Method OnRender:Int()
		If MouseDown()
			#If MOJO=2
				layer0.SetColor 1, 1, 0
				layer0.DrawText("MOJO 2 Test", 0, 0)
				layer0.SetColor 0, 1, 0
				layer0.DrawCircle(MouseX(), MouseY(), 3)
'				canvas.SetColor 1, 1, 0
'				canvas.DrawText("MOJO 2 Test", 0, 0)
'	 			canvas.SetColor 0, 1, 0
'				canvas.DrawCircle(MouseX(), MouseY(), 3)
'				canvas.Flush
				renderer.Render
			#Else
			SetColor(255, 0, 255)
				DrawText("MOJO 1 Test", 0, 0)
				SetColor(255, 0, 0)
				
				DrawCircle(MouseX(), MouseY(), 3)
				
			#EndIf
		EndIf
		Return 0
	End
End

Function Main:Int()
	New MyApp
	Return 0
End