About Monkey 2 › Forums › Monkey 2 Programming Help › Monkey2 conversion
Tagged: Conversion Monkey2
This topic contains 19 replies, has 3 voices, and was last updated by
PhatPeter 1 year, 4 months ago.
-
AuthorPosts
-
November 20, 2017 at 10:44 pm #11866
So I’ve had a sleepless week while trying to convert this source into Monkey2 but I never quiet succeeded, still bumping into all sorts of problems. Anyone here good at Monkey1 and want to help out?
It’s the base for an efficient shader and tile engine, It would also make a great example for Monkey2 to build things upon.
[/crayon]Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697[crayon-5cba845261596455582474 inline="true" ]Import mojo2Class MyApp Extends AppField canvas:Canvas,icanvas:CanvasField image:Image,sourceImage:Image,targetImage:ImageField shaderonplease:Int,size:Int=32,w:Int = 800,h:Int = 600Field effect:ShaderEffect,level:Float=1,cx:IntMethod OnCreate()sourceImage=Image.Load("32x32sheet.png",0,0,0)targetImage=New Image(sourceImage.Width,sourceImage.Height,,,0)image=New Image(512,512,0,0,0) ; Image.SetFlagsMask(Image.Managed) ; effect=New ShaderEffectcanvas=New Canvas ; icanvas=New Canvas(image)EndMethod OnUpdate()If KeyHit(KEY_SPACE) Then effect.SetColor(Rnd(1.0),Rnd(1.0),Rnd(1.0),1.0) ; shaderonplease = TrueIf shaderonplease Then level = Sin(Millisecs*.5)*.5+.5 ' SHADER PARAMETEREndMethod OnRender()' Shadereffect.SetLevel level ; effect.Render(sourceImage,targetImage)canvas.DrawImage(targetImage,100,100)' Init canvas & imagecanvas.SetViewport 0,0,w,h ; canvas.SetScissor 0,0,w,hcanvas.SetProjection2d 0,w,0,h ; canvas.Clear 0,0,0icanvas.SetViewport 0,0,w,h ; icanvas.SetScissor 0,0,w,hicanvas.SetProjection2d 0,w,0,h ; icanvas.Clear 0,0,0' Draw to imageicanvas.DrawImage targetImage,256,256cx=(cx+2) Mod 512 ; For Local temp:=0 To 127 ; icanvas.DrawRect 064+temp,0,1,64,targetImage,0+temp+cx,0,1,64 ; Nexticanvas.Flush' Draw to canvascanvas.DrawRect 0,0,256,256,image,0,0,256,256canvas.FlushEndEndFunction Main()New MyAppEndClass BWShader Extends ShaderPrivateGlobal _instance:BWShaderMethod New()Build(LoadString("monkey://data/shader.glsl"))EndMethod OnInitMaterial:Void(material:Material)material.SetTexture "ColorTexture",Texture.White() ; material.SetScalar "EffectLevel",1material.SetScalar "color_r",1 ; material.SetScalar "color_g",1 ; material.SetScalar "color_b",1 ; material.SetScalar "color_a",1EndFunction Instance:BWShader()If Not _instance _instance=New BWShaderReturn _instanceEndEndClass ShaderEffectPrivateGlobal _canvas:CanvasField _material:MaterialMethod New()If Not _canvas _canvas=New Canvas_material=New Material(BWShader.Instance())EndMethod SetLevel:Void(level:Float)_material.SetScalar "EffectLevel",levelEndMethod SetColor:Void(r:Float,g:Float,b:Float,a:Float=1.0)_material.SetScalar "color_r",r ; _material.SetScalar "color_g",g ; _material.SetScalar "color_b",b ;_material.SetScalar "color_a",aEndMethod Render:Void(source:Image,target:Image)_material.SetTexture "ColorTexture",source.Material.ColorTexture_canvas.Clear 0,0,0,0_canvas.SetRenderTarget target_canvas.SetViewport 0,0,target.Width,target.Height_canvas.SetProjection2d 0,target.Width,0,target.Height_canvas.DrawRect 0,0,target.Width,target.Height,_material_canvas.FlushEndEndAttachments:
November 21, 2017 at 2:32 pm #11885The reason I as is that I really need to draw a texture onto itself efficiently in Monkey2.
Separate experiments went well but throwing everything together made it a snail
Mx did it without resorting to opengl so if M2 differ in its implementation I would need to know how to be able to optimise things back. Are there examples on how to effeciently draw parts of an image onto itself and then draw that in turn onto the canvas? This chain of events are what seem to slow down everything in MX2. Do you need to do Ping pong rendering to do that? I don’t think the source above does it.
The MX source above produce exactly this and it does it well despite being so simple and straightforward. It gives you at least one layer of 64×64 of (shaded) tiles on all Android devices that I´ve tried so far. It’s a good minimum spec. On a PC or Mac you get 16-180 layers(!)
Doing this as efficitly on Monkey2 is the hardest bit when switching to Monkey2 for me.
November 21, 2017 at 6:54 pm #11888I’m not sure to understand what you’re looking for but may be pixmaps are what you need!? It helped me to boost some processes sometimes.
with the PastePixmap for example. And you can modify pixmaps pixels using direct pointer access too (for plot by plot faster drawing. There’s is also discussions about shaders in the help forums too.
Monkey12image.Texture.PastePixmap( pixmap,0,0 )canvas.DrawImage( image,0,0 )November 21, 2017 at 7:52 pm #11889It’s such a simple problem but I probably said it wrong.
Say that you want to do this
– You want to draw thousands of images to the canvas, no performance issue.
– You might draw equally many images onto another image, no problem (it even outperforms the canvas, probably due to that images are often smaller, it’s really nice).The problem is the actual combo, and it’s an important combo to have in the toolbox.
Of course this is not about a once-initiated-pre-drawn-image, it’s about the need to do it continuously.
MonkeyX does it and the code above proves it even if it lacks the few lines that does the actual cookie-cutting which is just a bunch of DrawRect really.
Monkey2 on the other hand seem very picky about this?
This is the only gripe I have with Monkey2 now, I really need it to convert totally.
November 21, 2017 at 9:24 pm #11892Still not quite sure what you mean here…could you try and explain the problem a bit more clearly.
I assume it’s speed related as there’s mention of snails in there, and that it’s something to do with drawing images to images but beyond that…?
Ideally, some nice simple runnable monkey2 code with a clear description of the ‘symptoms’ would be ideal!
November 21, 2017 at 11:00 pm #11893This is the best demo I’ve come to manage to produce in Monkey2 as I’m in the process of learning it.
I could provide full MX code if this M2 can’t put some light on the issue. I’m still new to Monkey2 so bear with me.I made a test which uses an untouched image (Barabarian) to blit to the Checkerboard (just another image) and to the main canvas. While doing that it blits some more using the checkerboard as the source and draws onto both itself and the canvas. I would like to throw away the “selfblitting” part and compare, but I think I tried that with similar slow results so that’s not it.
The test can show that the upper combinations will run fine, while the lower combination one will not.
If this fails to explain the problem I can show the full source when Mx is doing it, with shader ontop aswell.
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147[crayon-5cba845276227938475759 inline="true" ]Namespace Myapp#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"#Import "assets/"Using std..Using mojo..Const VWIDTH:=1920, VHEIGHT:=1080Class Myapp Extends WindowField Myapp:Stack<sprite>[] = New Stack<sprite>[4]Field images:Image[] = New Image[](Image.Load("asset::32x32sheet.png"),Image.Load("asset::32x32sheet.png"),Image.Load("asset::32x32sheet.png"),Image.Load("asset::32x32sheet.png") )Field image:Image ' Bunny residue, are using only [0] for now.Field icanvas:CanvasMethod New()Super.New("Myapp", VWIDTH, VHEIGHT, WindowFlags.Resizable )image=New Image(512,512,PixelFormat.RGBA8,TextureFlags.Dynamic)image.Texture.Flags &=~ TextureFlags.FilterMipmap'images[0].TextureFlags.Dynamic'images[0].Texture.Flags &=~ TextureFlags.FilterMipmapimage.Handle=New Vec2f(0.5,0.5)icanvas=New Canvas(image)Local tmpimage:=Floor( random.Rnd( 3 ))For Local i:=0 Until Myapp.LengthMyapp[i] = New Stack<sprite>EndEnd' ---------------------------------------------------------------------------------------Method OnRender( canvas:Canvas ) OverrideApp.RequestRender()icanvas.Clear(Color.Black)canvas.Alpha=1.0icanvas.Alpha=0.2For Local x:=0 Until 512/16For Local y:=0 Until 512/16If (x~y)&1icanvas.Color=New Color( Sin( Millisecs()*.01 )*.5+.5,Cos( Millisecs()*.02 )*.5+.5,.5 )Elseicanvas.Color=Color.YellowEndificanvas.DrawRect( Int(x)*16,Int(y)*16,16,16 )NextNextGlobal currentx : Intcurrentx = (currentx+2) Mod 512For Local temp:=0 To 511icanvas.DrawRect(64+temp,0, 1, 64, image, 0+temp+currentx,0,1,64)Nexticanvas.Color=Color.Whiteicanvas.DrawText( "This way up!",icanvas.Viewport.Width/2,0,.5,0)If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()Local count:Inticanvas.Alpha = 1canvas.Alpha = 1For Local s:=Eachin MyappFor Local sprite:=Eachin scanvas.Alpha = 1.0sprite.Update(); sprite.Draw(icanvas);count+=1 ' Draw to checkerboard. Using this instead of tilesheet as source slows things down in Monkey2 but not MonkeyX.canvas.Alpha = 0.2sprite.Update(); sprite.Draw(canvas);count+=1 ' Draw to canvas.NextNexticanvas.Flush()Global rot:=0.0canvas.Alpha = 1.0canvas.DrawImage(image,App.MouseLocation.x,App.MouseLocation.y,rot)icanvas.Alpha = 1canvas.Color = Color.Whitecanvas.DrawRect( 0, 0, VWIDTH, 25 )canvas.Color = Color.Blackcanvas.DrawText("Counting ("+ count +")",0,0);canvas.DrawText(" FPS: " + App.FPS, 300, 0)End MethodMethod OnMouseEvent( event:MouseEvent ) OverrideIf event.Type = EventType.MouseDownLocal _len := 0If event.Button = MouseButton.Left Then _len = 512Local tmpimage:IntFor Local i:=1 Until _len + 1' Test to try to show the differences in sources and destinations of draws, uncomment one' ---------------------------------------------------------------------------------------' No problem wioth performance using barbarian as source to whatever destination' Myapp[ tmpimage ].Add( New sprite(Mouse.X, Mouse.Y, images[0])) ' Draws from barbarian to drawingboard & canvas' Myapp[ tmpimage ].Add( New sprite(Mouse.X, Mouse.Y, images[0])) ' Draws from barabarian to drawingboard & canvas' Slowdown happens when using checkerboard as sourceMyapp[ tmpimage ].Add( New sprite(Mouse.X, Mouse.Y, images[0])) ' Draws from barbarian to checkerboard & canvasMyapp[ tmpimage ].Add( New sprite(Mouse.X, Mouse.Y, image)) ' Draws from checkerboard to checkerboard & canvas (this drops the framrate sigininficatly in Monkey2)EndEndEnd MethodEndClass spriteField x: FloatField y: FloatField xspeed: FloatField yspeed: FloatField texture: ImageGlobal gravity := 0.2Method New( x: Float, y: Float, texture:Image )Self.x = x ; Self.y = y ; Self.texture = texture ; xspeed = random.Rnd(7)EndMethod Update:Void( )yspeed += gravity ; y += yspeed ; x += xspeedIf y >= VHEIGHT Then y = VHEIGHT ; yspeed = -random.Rnd( 25 )If x < 0 Or x > VWIDTH Then xspeed *= -1 ; x = Clamp(x, 0.0, Float(VWIDTH) )EndMethod Draw(canvas:Canvas)canvas.BlendMode = BlendMode.AlphaLocal w:Int = 64Local h:Int = 64Local rx:Int = -(w * 0.5)Local ry:Int = -(h * 0.5)canvas.PushMatrix()canvas.Translate(x,y)canvas.Rotate(Pi/4.0)canvas.DrawRect(rx+w,ry+h,-w,-h,texture,0*32,1*32,w,h)canvas.PopMatrix()EndEndFunction Main()New AppInstanceNew MyappApp.Run()End FunctionAttachments:
November 21, 2017 at 11:51 pm #11895Ok, that’s pretty hard to follow, but as far as I can tell you’re trying to draw a texture onto itself, ie: you’re drawing ‘image’ to an ‘icanvas’, where icanvas has the same image as a render target. Please correct me if I’m wrong here…
Anyway, you can’t do this – it’s technically ‘undefined behaviour’ in opengl/gles/webgl and I’m surprised it works at all!
If you want to do stuff like this, you need to double buffer, ie: have 2 images/icanvses, and draw image0->icanvas1 and image1->icanvas0 etc and flip between them.
November 22, 2017 at 12:13 am #11896Yes I know that it’s undefined to draw to itself but In MonkeyX you can do thath without slowdowns and that’s what makes me confused.
I use a shader to read sourceimage and process it to targetimage.
After that I plot some stuff onto targetimage which will therefore be unaffected by the shader, and then I use targetimage as the tilesheet to draw parts onto the canvas.But there are self-refference to create a scrolling/rolling effect, and it just works.
I did some tests without any self-reffering and it’s still as slow in Monkey2.Drawing an image that you’ve just draw an image to is expansive in Monkey2.
The slowdown seem to be x4. 4K draws instead of 16K draws. I would accept that If MonkeyX had the same but it doesn’t.November 22, 2017 at 12:35 am #11897It could be anything – it could be the fact that monkey2 really uses direct3d, whereas monkey-x uses ‘pure’ opengl, it could be the way I uploaded textures to the driver, the order of API operations I use, whatever…the point is it’s invalid so don’t do it.
Drawing an image that you’ve just draw an image to is expansive in Monkey2.
If you have some runnable sample code, please post it, but nothing that uses undefined or invalid behaviour please!
[edit]You may be right, there may be some sort of issue here, but testing with invalid code is not the right way to find it.
November 22, 2017 at 1:25 am #11898I managed to simplify things and maybe I’m able to convert everything now I just need help with the corresponding Monkey2 commands for: (the extra commas and zeros and also Image.SetFlagMask Managed).
[/crayon]Monkey123456[crayon-5cba8452881f1697942994 inline="true" ] sourceImage=Image.Load("´sheet.png",0,0,0)targetImage=New Image(sourceImage.Width,sourceImage.Height,,,0)image=New Image(512,512,0,0,0)Image.SetFlagsMask(Image.Managed)November 22, 2017 at 2:28 am #11901Any kind soul here? I converted the monkeyX not Monkey2 but it’s not running.
CONVERSION (MEMORY CRASH)
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108[crayon-5cba84528b505485753202 inline="true" ]Namespace Myapp#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"#Import "assets/"Using std..Using mojo..Const w:=800, h:=600Class Myapp Extends WindowField canvas:CanvasField icanvas:CanvasField scanvas:CanvasField image:ImageField sourceImage:ImageField targetImage:ImageField size:Int=16 ' try 8 16 32 64Field cx:IntField wx:IntField wy:IntField tilemap:Int[]' 512*512Field s:Int = 4Method New()Super.New("Myapp", w, h, WindowFlags.Resizable)For Local y:=0 To 511For Local x:=0 To 511tilemap[x + y * 512] = Int(Rnd(127))NextNextsourceImage=Image.Load("32x32sheet.png") ' 0,0,0 in Mx ! PixelFormat.RGBA8,TextureFlags.Dynamic)targetImage=New Image(sourceImage.Width,sourceImage.Height) ' ,,,0) 'MAYBE SAME AS BELOWimage=New Image(512,512,PixelFormat.RGBA8,TextureFlags.Dynamic) ' image=New Image(512,512,0,0,0) & Image.SetFlagsMask(Image.Managed)icanvas=New Canvas(image)scanvas=New Canvas(targetImage)' canvas=New Canvas() NOT NEEDED IN MONKEY2 ' image.Texture.Flags &=~ TextureFlags.FilterMipmapEnd' ---------------------------------------------------------------------------------------Method OnRender( canvas:Canvas ) OverrideApp.RequestRender()' canvas.Alpha=1.0scanvas.Clear(Color.Black) ' INSTEAD OF SHADER (just copies sourceImage to targetImage)scanvas.DrawImage(sourceImage,0,0) ' INSTEAD OF SHADER (just copies sourceImage to targetImage)scanvas.Flush() ' INSTEAD OF SHADER (just copies sourceImage to targetImage)' Init canvas & image'canvas.Viewport(0,0,w,h)'canvas.Scissor(0,0,w,h)'canvas.Projection2d(0,w,0,h)canvas.Clear(Color.Black)'icanvas.Viewport(0,0,w,h)'icanvas.Scissor(0,0,w,h)'icanvas.Projection2d(0,w,0,h)icanvas.Clear(Color.Black)' Draw to image' Scroll (by drawing onto itself)icanvas.Clear(Color.Black)icanvas.DrawImage(sourceImage,0,0)icanvas.DrawImage(targetImage,256,256)cx=(cx+2) Mod 512For Local temp:=0 To 127icanvas.DrawRect(64+temp,0,1,64,targetImage,0+temp+cx,0,1,64)Next' Plots something in the tiles (shows how to draw something to an image *after* shader have been applied)For Local temp:=1 To 16If Int(Rnd(1)) Then icanvas.Color = Color.Black Else icanvas.Color = Color.WhiteLocal xx:= Int(Rnd(7))Local yy:= Int(Rnd(7))icanvas.DrawRect(xx*4,yy*4,2*4,2*4)Nexticanvas.Flush() ' Finished drawing' Draw to canvas' Cookiecut 32x32 tiles from a 512x512 tilesheet and draw as tiles of any sizeLocal scrx:=wx Mod sizeLocal scry:=wy Mod sizeLocal mapx:=wx / sizeLocal mapy:=wy / sizeLocal cnty:= -scryFor Local y:=mapy To mapy+((h/size)+1)Local cntx:=-scrxFor Local x:=mapx To mapx+((w/size)+1)Local char:= tilemap[x + y * 512]Local tilex:= char & 15Local tiley:= char Shr 4canvas.DrawRect(cntx,cnty,size,size,image,tilex Shl 5,tiley Shl 5,32,32)cntx=cntx+sizeNextcnty=cnty+sizeNextcanvas.Flush() ' Finished drawingIf Keyboard.KeyReleased(Key.Escape) Then App.Terminate()End MethodEndFunction Main()New AppInstanceNew MyappApp.Run()End FunctionORIGINAL MONKEYX (works perfectly with all I’ve said, it would be a nice additional Monkey2 banana)
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110[crayon-5cba84528b50c146078588 inline="true" ]#GLFW_WINDOW_WIDTH = 800#GLFW_WINDOW_HEIGHT = 600#GLFW_WINDOW_FULLSCREEN = False#GLFW_WINDOW_RESIZABLE = True#GLFW_WINDOW_RENDER_WHILE_RESIZING = TrueImport mojo2Class MyApp Extends AppField canvas:CanvasField icanvas:CanvasField scanvas:CanvasField image:ImageField sourceImage:ImageField targetImage:ImageField size:Int=16 ' try 8 16 32 64Field cx:IntField wx:IntField wy:IntField tilemap:Int[512*512]Field s:Int = 4Method OnCreate()For Local y:=0 To 511For Local x:=0 To 511tilemap[x + y * 512] = Int(Rnd(127))NextNextsourceImage=Image.Load("32x32sheet.png",0,0,0)targetImage=New Image(sourceImage.Width,sourceImage.Height,,,0)image=New Image(512,512,0,0,0)Image.SetFlagsMask(Image.Managed)icanvas=New Canvas(image)scanvas=New Canvas(targetImage)canvas=New CanvasEndMethod OnUpdate()If KeyHit(KEY_ESCAPE) Then EndAppwx=wx-s*KeyDown(KEY_LEFT)+s*KeyDown(KEY_RIGHT)wy=wy-s*KeyDown(KEY_UP)+s*KeyDown(KEY_DOWN)wx=Max(0,Min(10000,wx)) ' Limit wx to 0 through 10000wy=Max(0,Min(10000,wy)) ' Limit wyx to 0 through 10000EndMethod OnRender()scanvas.Clear ' INSTEAD OF SHADER (just copies sourceImage to targetImage)scanvas.DrawImage sourceImage,0,0 ' INSTEAD OF SHADER (just copies sourceImage to targetImage)scanvas.Flush() ' INSTEAD OF SHADER (just copies sourceImage to targetImage)' Init canvas & imageLocal w=DeviceWidthLocal h=DeviceHeightcanvas.SetViewport 0,0,w,hcanvas.SetScissor 0,0,w,hcanvas.SetProjection2d 0,w,0,hcanvas.Clear 0,0,0icanvas.SetViewport 0,0,w,hicanvas.SetScissor 0,0,w,hicanvas.SetProjection2d 0,w,0,hicanvas.Clear 0,0,0' ' Draw to image' Scroll (by drawing onto itself)icanvas.Clear 0,0,0icanvas.DrawImage sourceImage,0,0icanvas.DrawImage targetImage,256,256cx=(cx+2) Mod 512For Local temp:=0 To 127icanvas.DrawRect 064+temp,0,1,64,targetImage,0+temp+cx,0,1,64Next' Plots something in the tiles (shows how to draw something to an image *after* shader have been applied)For Local temp:=1 To 16If Int(Rnd(1)) Then icanvas.SetColor 0,0,0 Else icanvas.SetColor 1,1,1Local xx:= Int(Rnd(7))Local yy:= Int(Rnd(7))icanvas.DrawRect xx*4,yy*4,2*4,2*4Nexticanvas.Flush ' Finished drawing' Draw to canvas' Cookiecut 32x32 tiles from a 512x512 tilesheet and draw as tiles of any sizeLocal scrx:=wx Mod sizeLocal scry:=wy Mod sizeLocal mapx:=wx / sizeLocal mapy:=wy / sizeLocal cnty:= -scryFor Local y:=mapy To mapy+((h/size)+1)Local cntx:=-scrxFor Local x:=mapx To mapx+((w/size)+1)Local char:= tilemap[x + y * 512] ; Local tilex:= char & 15 ; Local tiley:= char Shr 4canvas.DrawRect cntx,cnty,size,size,image,tilex Shl 5,tiley Shl 5,32,32cntx=cntx+sizeNextcnty=cnty+sizeNextcanvas.Flush ' Finished drawingEndEndFunction Main()New MyAppEndAttachments:
November 22, 2017 at 2:32 am #11903No direct X. It’s on macOS. I’ve tried windows (OpenGL via angle) and it shows the same amazing performance there. So both are equally good. It’s the same on both platforms.
November 22, 2017 at 2:54 am #11904Try building with debug on. You’re missing a ‘=New Int[512,512]’ and an “asset::blah.png”.
With these fixed I get a smooth scrolling sort of parallax charmap, with random chars all over the place, some sort of animated! Both monkey2 and monkeyx versions look the same to me.
November 22, 2017 at 3:02 am #11905Your kidding me? haha boy do I feel stupid, 16 hours straight and I miss that.
Could you list the code because I know not yet how to use “New Int[]”?
I already include Assets the whole directory?November 22, 2017 at 3:10 am #11906I already include Assets the whole directory?
You need to prefix file name with “asset::” when you load it, eg: Image.Load( “asset::32x32sheet.png” ).
For texture flags, always use TextureFlags.Dynamic for an image you’re updating frequently (I added some of these I think).
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105Namespace Myapp#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"#Import "assets/"Using std..Using mojo..Const w:=800, h:=600Class Myapp Extends WindowField canvas:CanvasField icanvas:CanvasField scanvas:CanvasField image:ImageField sourceImage:ImageField targetImage:ImageField size:Int=16 ' try 8 16 32 64Field cx:IntField wx:IntField wy:IntField tilemap:=New Int[512*512]Field s:Int = 4Method New()Super.New("Myapp", w, h, WindowFlags.Resizable)For Local y:=0 To 511For Local x:=0 To 511tilemap[x + y * 512] = Int(Rnd(127))NextNextsourceImage=Image.Load( "asset::32x32sheet.png",Null,Null )targetImage=New Image( sourceImage.Width,sourceImage.Height,PixelFormat.RGBA8,TextureFlags.Dynamic )image=New Image( 512,512,PixelFormat.RGBA8,TextureFlags.Dynamic )icanvas=New Canvas(image)scanvas=New Canvas(targetImage)' canvas=New Canvas() NOT NEEDED IN MONKEY2 ' image.Texture.Flags &=~ TextureFlags.FilterMipmapEnd' ---------------------------------------------------------------------------------------Method OnRender( canvas:Canvas ) OverrideApp.RequestRender()' canvas.Alpha=1.0scanvas.Clear(Color.Black) ' INSTEAD OF SHADER (just copies sourceImage to targetImage)scanvas.DrawImage(sourceImage,0,0) ' INSTEAD OF SHADER (just copies sourceImage to targetImage)scanvas.Flush() ' INSTEAD OF SHADER (just copies sourceImage to targetImage)' Init canvas & image'canvas.Viewport(0,0,w,h)'canvas.Scissor(0,0,w,h)'canvas.Projection2d(0,w,0,h)canvas.Clear(Color.None)'icanvas.Viewport(0,0,w,h)'icanvas.Scissor(0,0,w,h)'icanvas.Projection2d(0,w,0,h)icanvas.Clear(Color.None)' Draw to image' Scroll (by drawing onto itself)icanvas.Clear(Color.Black)icanvas.DrawImage(sourceImage,0,0)icanvas.DrawImage(targetImage,256,256)cx=(cx+2) Mod 512For Local temp:=0 To 127icanvas.DrawRect(64+temp,0,1,64,targetImage,0+temp+cx,0,1,64)Next' Plots something in the tiles (shows how to draw something to an image *after* shader have been applied)For Local temp:=1 To 16If Int(Rnd(1)) Then icanvas.Color = Color.Black Else icanvas.Color = Color.WhiteLocal xx:= Int(Rnd(7))Local yy:= Int(Rnd(7))icanvas.DrawRect(xx*4,yy*4,2*4,2*4)Nexticanvas.Flush() ' Finished drawing' Draw to canvas' Cookiecut 32x32 tiles from a 512x512 tilesheet and draw as tiles of any sizeLocal scrx:=wx Mod sizeLocal scry:=wy Mod sizeLocal mapx:=wx / sizeLocal mapy:=wy / sizeLocal cnty:= -scryFor Local y:=mapy To mapy+((h/size)+1)Local cntx:=-scrxFor Local x:=mapx To mapx+((w/size)+1)Local char:= tilemap[x + y * 512]Local tilex:= char & 15Local tiley:= char Shr 4canvas.DrawRect(cntx,cnty,size,size,image,tilex Shl 5,tiley Shl 5,32,32)cntx=cntx+sizeNextcnty=cnty+sizeNextcanvas.Flush() ' Finished drawingIf Keyboard.KeyReleased(Key.Escape) Then App.Terminate()End MethodEndFunction Main()New AppInstanceNew MyappApp.Run()End Function -
AuthorPosts
You must be logged in to reply to this topic.