• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Mojo2 DrawPrimitives2 Example

Paul59

Active member
CX Code Contributor
Joined
Dec 13, 2018
Messages
384
@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:
' 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
 
Ah geez, must be a Linux specific thing. SetMousePos works great like expected on Windows10.
 
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.
 
Translating to screen centre does the job though:

Cerberus:
        ' position mouse at screen centre
        SetMousePos(DeviceWidth() / 2, DeviceHeight() / 2)
        myCanvas.Translate(DeviceWidth() / 2, DeviceHeight() / 2)
 
But that causes offset drawing without push/pop.

Just checked and this prints '0.0':
Cerberus:
        ' 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:
Noticed the original version of this example is still included in the latest version of CX - didn't you like my 'animated' version? :D
 
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.
 
No, you need to send me $500 as well :p
 
Back
Top Bottom