- 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
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??
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