About Monkey 2 › Forums › Monkey 2 Programming Help › Draw image
This topic contains 31 replies, has 6 voices, and was last updated by 
 juliocmfj 1 year, 7 months ago.
- 
		AuthorPosts
 - 
		
			
				
August 30, 2017 at 3:31 pm #10117
Ex: I have an image drawn on the screen, when moving to the wireside or up side, when touching the corners, the image is automatically erased from the screen. When moving to the right or bottom corner, this does not happen. It is as if you where cutting the image when you hit the left and top corner.
Any idea what that might be?
Follow the code below:
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283#Import "<std>"#Import "<mojo>"#Import "TiledImport"' json#Import "data/maps/map.json"' png#Import "data/maps/Outside_A2.png"#Import "data/maps/Outside_A4.png"#Import "data/maps/Outside_A5.png"#Import "data/maps/Outside_B.png"Using std..Using mojo..Global FixScreenResolution:Vec2i = New Vec2i(1024, 768)Class MyWindow Extends WindowField timer:TimerField tiled:TiledImportField xy:Vec2i = New Vec2i(0, 0)Method New()Super.New("Tiled Import - Test", 1024, 768, WindowFlags.Resizable)Layout = "letterbox"timer = New Timer(60, OnUpdate)tiled = New TiledImport(, "map.json")EndMethod OnMeasure:Vec2i() OverrideReturn FixScreenResolutionEndMethod OnUpdate()For Local i:UInt = 0 Until tiled.layers.Lengthtiled.layers[i].x = xy.Xtiled.layers[i].y = xy.YNextIf (Keyboard.KeyDown(Key.Left))xy.X -= 2Else If (Keyboard.KeyDown(Key.Right))xy.X += 2EndIf (Keyboard.KeyDown(Key.Up))xy.Y -=2Else If (Keyboard.KeyDown(Key.Down))xy.Y += 2EndApp.RequestRender()EndMethod OnRender(canvas:Canvas) Overridecanvas.Clear(Color.Black)canvas.Color = Color.Whitetiled.Render(canvas)EndMethod OnKeyEvent(event:KeyEvent) OverrideSelect (event.Type)Case EventType.KeyDownSelect (event.Key)Case Key.EscapeApp.Terminate()EndEndEndEndFunction Main()New AppInstance()New MyWindow()App.Run()EndAttachments:
August 30, 2017 at 11:22 pm #10121I could probably figure it out but I would probably burn my two remaining neurons trying to solve. It would be a bit easier if you post complete run-able code. A zip file maybe?
August 31, 2017 at 12:03 am #10122I can post codes here. I get an illegal operation error. The I send the two code via download.
https://drive.google.com/open?id=0B2lZ_pN3kwOJLUJTYWUwUjA2dVk
August 31, 2017 at 11:04 am #10133There is no playable demo in archive – no images and map file. So I can’t test it.
(My advices about your code were eaten by antispam, even splitted by parts)
August 31, 2017 at 11:14 am #10134I forgot to send the whole project. Below is the link
https://drive.google.com/open?id=0B2lZ_pN3kwOJVm9fTVBqT1hWWTQAugust 31, 2017 at 11:15 am #10135August 31, 2017 at 11:28 am #10136Great tips. I still have not much knowledge about Monkey2. Only now have I realized the rean benefit of overloading an operator…
I’ll folow some of your tips.
On the cut in the images when moving to the left or top corner, if I use the canvas.Translate(x, y) method, the same “error” does not happen, however everything on the screen is moved, and I want to do just scroll of the map (in this case).
August 31, 2017 at 12:22 pm #10137Actually your problem is not in your main file. It’s in your TiledImport in the RenderLayer Method. you have to figure out how to keep on drawing the image once the image position gets less than 0 and until the image position is less then the with of the tile. Right now you are not drawing any tiles if the position of the image is less than 0 so if the position of the image becomes -1 then you stop displaying it but you still have say 31 pixels(of a 32 pixel width image) to display and that is why you get that blank image on the side.
I hope that’s clear to you.August 31, 2017 at 2:10 pm #10139Problema solved. I changed the coordinates to Float and, like magic, the problem ended. I do not know if it’s a Monkey2 bug, but for me, that should not happen.
Anyway, I appreciate everyone’s help and it’s the tip for anyone who has the same problem.
August 31, 2017 at 2:44 pm #10140The problem here is mixing UInt with Int.
When you sum int with uint you got uint which have non-negative value only.
If you replace all UInt with Int (including “for local x:uint = …”) your map will render normally.
August 31, 2017 at 2:56 pm #10141Nerobot, this makes all sense, however, the only change I made was in the x and y of the TiledLayer class, I was Int and I moved to Float.
The x and y found in: map[x, y], are UInt, since there is no image with id less than zero in the array.
August 31, 2017 at 3:04 pm #10142Right, and you got Float+UInt=Float types conversion. The only (one) Float in expression make whole result as Float.
And with float map get its negative coords.
The x and y found in: map[x, y], are UInt, since there is no image with id less than zero in the array.
yes, they are, but them “break” you expectation because of type conversion UInt+Int=UInt
Monkey1canvas.DrawImage(tileArray[layers[layerId].map[x, y]].image, layers[layerId].x + x * tileWidth, layers[layerId].y + y * tileHeight)August 31, 2017 at 3:16 pm #10143Got it. So I must have done something wrong, but I’ll try to explain my logic behind this rendering loop.
Monkey123456789Method RenderLayer(canvas:Canvas, layerId:UInt)For Local y:UInt = 0 Until layers[layerId].heightFor Local x:UInt = 0 Until layers[layerId].widthIf (layers[layerId].map[x, y] > -1)canvas.DrawImage(tileArray[layers[layerId].map[x, y]].image, layers[layerId].x + x * tileWidth, layers[layerId].y + y * tileHeight)EndNextNextEndIn the first For Local y, I put it as UInt because it only traverses the array with the id’s of the images that represent the tiles, since there is none with id under 0. And so I did with For Local x.
When drawing the tiles, I did this based on: x of the tile(float) + x of the loop For(UInt) * tileWidth and in the same way with the y coordinate.
Does this seem to make sense or dir I really make a mistake?
August 31, 2017 at 3:34 pm #10144Does this seem to make sense or dir I really make a mistake?
Using float for coords is OK.
My posts above just answering for your
and, like magic, the problem ended. I do not know if it’s a Monkey2 bug, but for me, that should not happen.
Answer is – there is no magic, it’s just a type conversion rules.
August 31, 2017 at 3:44 pm #10145I made several basic mistakes there. Lol
That’s the problem coming from BlitzMax, which did not give you that many options. There it only had Int and not UInt. It was all more “simple”.
I hope Ia do not make the same mistakes and thank you for the tips.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.
