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.
'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!)
#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.