Mojo2 DrawPrimitives2 Example

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
@MikeHart I've, er, 'enhanced' your new DrawPrimitives2 example to make it a bit more interesting - hope you approve :D

I can't update on GitHub atm so hope you can add it from here (it uses the same asset as your original bg.png). I'm not sure why drawing begins at (0,0) rather than the mouse position... maybe something to do with the way the new SetMousePos() works??

Cerberus X:
' Cerberus X - Draw Primitives2 example
'
' Shows how to construct and draw a list of images via DrawPrimitives

Strict

Import mojo2

Class MyApp Extends App

    Field myCanvas:Canvas
    Field grabbed:Bool
    Field myMaterial:Material
    Field verts:Float[]
    Field texcs:Float[]
    Field angle:Float
    
    Method OnCreate:Int()

        SetUpdateRate(60)
        myCanvas = New Canvas()
        myMaterial = Material.Load("bg.png", Image.Filter,  Shader.DefaultShader())
        
        ' where to draw in Offset to the mouse cursor
        Local x1:Float = -128
        Local y1:Float = -128
        Local x2:Float = 0
        Local y2:Float = 0
        ' provide 16 vertices from those coordinates as array
        verts = [x1,y1, x2,y1, x2,y2, x1,y2,
                 x1+129,y1, x2+129,y1, x2+129,y2, x1+129,y2,
                 x1,y1+129, x2,y1+129, x2,y2+129, x1,y2+129,
                 x1+129,y1+129, x2+129,y1+129, x2+129,y2+129, x1+129,y2+129]
        ' piece of texture to draw. Coordinates are float, ranging from 0 to 1.
        Local tx1:Float = 0
        Local ty1:Float = 0
        Local tx2:Float = 1
        Local ty2:Float = 1
        ' provide texture coordinates for vertices
        texcs = [tx1,ty1, tx2,ty1, tx2,ty2, tx1,ty2,
                 tx1,ty1, tx2,ty1, tx2,ty2, tx1,ty2,
                 tx1,ty1, tx2,ty1, tx2,ty2, tx1,ty2,
                 tx1,ty1, tx2,ty1, tx2,ty2, tx1,ty2]
        angle = 0
        ' position mouse at screen centre
        SetMousePos(DeviceWidth() / 2, DeviceHeight() / 2)
        
        Return 0       
    End
    
    
    Method OnRender:Int()   
        myCanvas.Clear(0, 0, 0)
        myCanvas.SetColor(1, 1, 1)
        myCanvas.DrawText("Move the mouse", DeviceWidth()/2, DeviceHeight() - 24, 0.5)               
        myCanvas.PushMatrix()
        
        ' myCanvas.Translate(MouseX(), MouseY())
        ' to draw without rotation and scaling use the above line *instead* of the following
        myCanvas.TranslateRotateScale(MouseX(), MouseY(), angle, 45 / angle, 45 / angle)
        myCanvas.SetColor(1, 1, 1)
        myCanvas.DrawPrimitives(4, 4, verts, texcs, myMaterial)
        myCanvas.PopMatrix()
        myCanvas.Flush()
        Return 0
    End
    
    
    Method OnUpdate:Int()
        
        ' change the texture coordinates using some basic trig to create animation effect
        Local tx1:Float = Cos(angle)
        Local ty1:Float = Sin(angle)
        Local tx2:Float = Abs(0.5 - Cos(angle))
        Local ty2:Float = Abs(0.75 - Sin(angle))
        angle += 0.25
        ' this value is also used for scaling so keep in 0-90
        If angle > 89.75 Then angle = 0
        ' save new value back into the array ready for drawing
        texcs = [tx1,ty1, tx2,ty1, tx2,ty2, tx1,ty2,
                 tx1,ty1, tx2,ty1, tx2,ty2, tx1,ty2,
                 tx1,ty1, tx2,ty1, tx2,ty2, tx1,ty2,
                 tx1,ty1, tx2,ty1, tx2,ty2, tx1,ty2]
        Return 0
    End

End

Function Main:Int()
    New MyApp()
    Return 0
End
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
Ah geez, must be a Linux specific thing. SetMousePos works great like expected on Windows10.
I don't think it's SetMousePos() - if I use set the mouse to position (0, 300) for example, the cursor is in the correct place, it's just that the drawing isn't 'attached' to the mouse until it has moved. Tried translating to mouse position first but has no effect.
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
Translating to screen centre does the job though:

Cerberus X:
        ' position mouse at screen centre
        SetMousePos(DeviceWidth() / 2, DeviceHeight() / 2)
        myCanvas.Translate(DeviceWidth() / 2, DeviceHeight() / 2)
 

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
But that causes offset drawing without push/pop.

Just checked and this prints '0.0':
Cerberus X:
        ' position mouse at screen centre
        SetMousePos(DeviceWidth() / 2, DeviceHeight() / 2)
        Print(MouseX())
I guess that wherever MouseX()/Y() get their values from needs an update after calling SetMouse().
 
Last edited:

Paul59

Active Member
CX Code Contributor
Joined
Dec 13, 2018
Location
UK
Noticed the original version of this example is still included in the latest version of CX - didn't you like my 'animated' version? :D
 

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
Oh noooooo, I simply forgot. And I am sooooo sorry Paul, i should have never forgotten about this. :eek:
As punishment i won't allow myself to be on the computer and work on anything CX related for a month, at least. That is the minimum i can do. I hope you are satisfied now.
 
Top Bottom