About Monkey 2 › Forums › Monkey 2 Programming Help › DrawRect usage?
This topic contains 3 replies, has 2 voices, and was last updated by
Ethernaut
1 year, 5 months ago.
-
AuthorPosts
-
November 16, 2017 at 2:24 pm #11744
If you try this code with any sprite-sheet of 32×32 tiles then you’ll notice the pixels will bleed.
Is this the classic bleeding OpenGL bug in some sense or am I just using it wrong?[/crayon]Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283[crayon-5cba8456aa50d064509835 inline="true" ]Namespace bunnies'#Import "assets/32x32sheet.png"#Import ""#Import ""Using std..Using mojo..Const VWIDTH:=1920, VHEIGHT:=1080Class Bunnymark Extends WindowField frames: Int = 1Field elapsed: Int = 1Field bunnies:Stack[] = New Stack[4]' 512x512 tilesheet of 32x32 tilesField 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 lastMilli := Millisecs()Method New()Super.New("Bunnymark", VWIDTH, VHEIGHT, WindowFlags.Resizable )Local tmpimage:=Floor( random.Rnd( 3 ))For Local i:=0 Until bunnies.Lengthbunnies[i] = New StackEndEndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()If Keyboard.KeyReleased(Key.Escape) Then App.Terminate()Local bunnycount:IntFor Local s:=Eachin bunniesFor Local bunny:=Eachin sbunny.Update(); bunny.Draw(canvas);bunnycount+=1NextNextcanvas.Color = Color.White ;canvas.DrawRect( 0, 0, VWIDTH, 25 )'canvas.Color = Color.Black ;canvas.DrawText("The Bunnymark ( " + asbunnycount + " )",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 = 1Local tmpimage:IntFor Local i:=1 Until _len + 1tmpimage = Floor( random.Rnd( 3 ))bunnies[ tmpimage ].Add( New Bunny( Mouse.X, Mouse.Y, images[ tmpimage ] ) )EndEndEnd MethodEndClass BunnyField x: Float, y: Float, xspeed: Float, yspeed: Float, texture: ImageGlobal gravity := 0.5Method New( x: Float, y: Float, texture:Image )Self.x = x ; Self.y = y ; Self.texture = texture ; xspeed = random.Rnd(10)EndMethod Update:Void( )'yspeed += gravity ; y += yspeed ; x += xspeed'If y >= VHEIGHT Then y = VHEIGHT ; yspeed = -random.Rnd( 35 )'If x < 0 Or x > VWIDTH Then xspeed *= -1 ; x = Clamp(x, 0.0, Float(VWIDTH) )x=x+xspeedEndMethod Draw(canvas:Canvas)canvas.TextureFilteringEnabled = Falsecanvas.DrawRect(x,y,256,256,texture,5*32,1*32,32,32)EndEndFunction Main()New AppInstanceNew BunnymarkApp.Run()End FunctionNovember 18, 2017 at 3:39 am #11790November 18, 2017 at 3:43 am #11792I think I solved it myself, the x & y needs to be Int instead of Float.
November 19, 2017 at 9:09 am #11817Using ints will solve it, but sometimes you want fractional coordinates (i.e. HD sprites, non “retro” 2D games).
For those cases, the only solution I know of is to add a gap between each tile, and copy the color of each edge pixel to the corresponding gap pixel.
I used to do this using Photoshop actions, but it was really annoying since you had to keep two sets of images – the original ones, and the ones with the colored gap…
…So I made a Monkey module to deal with that “on the fly”! (this feature is still alpha)
https://github.com/DoctorWhoof/spriteToolsHere’s how it works:
Once you load a PNG file, the Atlas class internally generates the gaps between tiles, even if they were created without gaps, and creates a texture atlas from that. You can then use Atlas.Cells[ index ] to access each individual image.And as a bonus, it converts any source image file into a “Power of Two” sized atlas, more guaranteed to work in all kinds of GPUs without artifacts like unwanted scaling.

Totally untested in real production! Let me know if it works. -
AuthorPosts
You must be logged in to reply to this topic.