I’ve decided to ‘translate’ in BlitzMax the Mojo commands, just to type a little less š
Basically every single methodsĀ in mojo.graphics has been wrapped in a ‘basic’ function like DrawLine() or DrawRect() instead of using canvas.DrawLine() or canvas.DrawRect()
The only difference is the need of use a SetCanvas(canvas) call to setup the ‘default’ canvas where all the graphics functions are directed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
'Main.monkey2 #import "lib/lib_mojo.monkey2" 'helper #Import "<std>" #Import "<mojo>" Using std.. Using mojo.. Class MojoTest Extends Window Global timer:Timer Method New() ClearColor=New Color( 0,0,.5 ) timer=New Timer(60,OnUpdate )'calls the refresh at 60 Hz End Method OnUpdate() App.RequestRender() End Method OnRender( canvas:Canvas ) Override SetCanvas(canvas) SetColor(Color.White) DrawTriangle(100,100,200,100,150,150) SetColor(Color.Red) SetAlpha(.5) DrawRect(80,80,140,120) SetAlpha(1) SetColor(Color.White) Local r:Recti=GetViewPort() DrawText("ViewPort: "+r.X+" "+r.Y+" "+r.Width+" "+r.Height,320,20,.5,1) DrawText("HALLO",320,200,.5,0) End End Class Function Main() New AppInstance New MojoTest App.Run() End |
I think it’s more readable …
Here the ‘library’ (I can’t how call it honestly!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
#rem lib_mojo #end #Import "<std>" #Import "<mojo>" Using std.. Using mojo.. Global current_canvas:Canvas 'getters (not so handy maybe!) Function GetAlpha:Float() Return current_canvas.Alpha End Function GetBlendMode:BlendMode() Return current_canvas.BlendMode End Function GetColor:Color() Return current_canvas.Color End Function GetFont:Font() Return current_canvas.Font End Function GetMatrix:AffineMat3f() Return current_canvas.Matrix End Function GetScissor:Recti() Return current_canvas.Scissor End Function GetTextureFilteringEnabled:Bool() Return current_canvas.TextureFilteringEnabled End Function GetViewPort:Recti() Return current_canvas.Viewport End '--------------------------------------------------------------------------------------- Function SetCanvas:Void(can:Canvas) current_canvas=can End Function DrawTriangle:Void(x0 : Float , y0 : Float , x1 : Float , y1 : Float , x2 : Float , y2 : Float ) current_canvas.DrawTriangle(x0,y0,x1,y1,x2,y2) End Function DrawTriangle:Void( v0 : Vec2f , v1 : Vec2f , v2 : Vec2f ) current_canvas.DrawTriangle( v0 ,v1,v2 ) End Function SetColor:Void(r:Float=1.0,g:Float=1.0,b:Float=1.0,alpha:Float=1.0) current_canvas.Color=New Color(r,g,b,alpha) End Function SetColor:Void(col:Color) current_canvas.Color=col End Function SetScissor:Void(sx:Int,sy:Int,sw:Int,sh:Int) current_canvas.Scissor=New Recti( sx,sy,sx+sw,sy+sh ) End Function SetScissor:Void(rec:Rect<Int>) current_canvas.Scissor=rec End Function SetClsColor:Void(r:Float=1.0,g:Float=1.0,b:Float=1.0,alpha:Float=1.0) current_canvas.Clear(New Color(r,g,b,alpha)) End Function PushMatrix:Void() current_canvas.PushMatrix() End Function PopMatrix:Void() current_canvas.PopMatrix() End Function Translate:Void(tx:Float,ty:Float) current_canvas.Translate(tx,ty) End Function Scale:Void(sx:Float,sy:Float) current_canvas.Scale(sx,sy) End Function Rotate:Void(r:Double) current_canvas.Rotate(r) End Function SetAlpha:Void(alpha:Double=1.0) current_canvas.Alpha=alpha End Function SetColor:Void(r:Float=1.0,g:Float=1.0,b:Float=1.0) current_canvas.Color=New Color(r,g,b) End Function DrawRect:Void(x:Float,y:Float,w:Float,h:Float) current_canvas.DrawRect(x,y,w,h) End Function DrawRect:Void(rect:Rectf) current_canvas.DrawRect(rect) End Function DrawRect:Void(rect:Rectf,srcImage:Image) current_canvas.DrawRect(rect,srcImage) End Function DrawRect:Void(x:Float,y:Float,w:Float,h:Float,srcImage:Image) current_canvas.DrawRect(x,y,w,h,srcImage) End Function DrawRect:Void(rect:Rectf,srcImage:Image,srcRect:Recti) current_canvas.DrawRect(rect,srcImage,srcRect) End Function DrawRect:Void(x:Float,y:Float,w:Float,h:Float,srcImage:Image,sx:Int,sy:Int,sw:Int,sh:Int) current_canvas.DrawRect(x,y,w,h,srcImage,sx,sy,sw,sh) End Function DrawImage:Void(image:Image,x:Float,y:Float) current_canvas.DrawImage(image,x,y) End Function DrawImage:Void(image:Image,tr:Vec2f) current_canvas.DrawImage(image,tr) End Function DrawImage:Void(im:Image,x:Float,y:Float,r:Float) current_canvas.DrawImage(im,x,y,r) End Function DrawImage:Void(im:Image,t:Vec2f,r:Float) current_canvas.DrawImage(im,t,r) End Function DrawImage:Void(im:Image,x:Float,y:Float,r:Float,sx:Float,sy:Float) current_canvas.DrawImage(im,x,y,r,sx,sy) End Function DrawImage:Void(im:Image,t:Vec2f,r:Float,scale:Vec2f) current_canvas.DrawImage(im,t,r,scale) End Function DrawCircle:Void(x : Float , y : Float , radius : Float ) current_canvas.DrawCircle(x,y,radius) End Function DrawEllipse:Void( x : Float , y : Float , xRadius : Float , yRadius : Float ) current_canvas.DrawEllipse(x,y,xRadius,yRadius) End Function Cls:Void(col:Color) current_canvas.Clear(col) End Function ClearMatrix:Void() current_canvas.ClearMatrix() End Function CopyPixmap:Pixmap(rect:Recti) Return current_canvas.CopyPixmap(rect) End Function Flush:Void() current_canvas.Flush() End Function DrawPoly:Void(pol:Float[]) current_canvas.DrawPoly(pol) End Function DrawPoint:Void(x:Float,y:Float) current_canvas.DrawPoint(x,y) End Function DrawPoint:Void( v : Vec2f ) current_canvas.DrawPoint(v) End Function DrawQuad:Void( x0 : Float , y0 : Float , x1 : Float , y1 : Float , x2 : Float , y2 : Float , x3 : Float , y3 : Float ) current_canvas.DrawQuad( x0 , y0 , x1 , y1 , x2 , y2 , x3 , y3 ) End Function DrawPrimitives:Void( order : Int , count : Int , vertices : Float Ptr , verticesPitch : Int , texCoords : Float Ptr , texCoordsPitch : Int , indices : Int Ptr) current_canvas.DrawPrimitives( order , count , vertices , verticesPitch , texCoords , texCoordsPitch , indices ) End Function DrawOval:Void(x:Float,y:Float,w:Float,h:Float) current_canvas.DrawOval(x,y,w,h) End Function DrawLine:Void(x:Float,y:Float,w:Float,h:Float) current_canvas.DrawLine(x,y,w,h) End Function DrawLine:Void(v0 : Vec2f , v1 : Vec2f ) current_canvas.DrawLine(v0,v1) End Function DrawText:Void(txt:String,x:Float,y:Float,h1:Float=1.0,w1:Float=1.0) current_canvas.DrawText(txt,x,y,h1,w1) end #rem Other Implemented #end Function TileImage:Void(im:Image,w:Int,h:Int) For Local x:Float=0 To w Step im.Width For Local y:Float=0 to h+im.Height Step im.Height DrawImage(im,x,y) Next Next End |
All the functions implements – when available – the different ‘version’, using overriding.
At the moment I’ve implement just ONE ‘special’ function (missinig in mojo standard) : TileImage
Probably I will add more function – if needed.
Building up this ‘library’ was an interesting way to understand MX2 syntax & features, like the new type Recti and Vec2f.