Ooops
March 05, 2021, 06:45:45 PM

Author Topic: cut & display tiles with array variables  (Read 136 times)

Offline drfloyd

cut & display tiles with array variables
« on: February 03, 2021, 03:03:40 PM »
Hello, need again some help to display tiles

to cut tiles on an image i do that :

' download image
Global fulltiles:TImage=LoadImage ("test.png")
' display image
DrawImage fulltiles,0,0
' cut first tile
Global tile1:TPixmap=GrabPixmap(0,0,20,20)
....
' display
DrawPixmap tile1,250,10

IT WORKS

But i want to save the tiles on array INT variables.... tile[1] tile[2], etc.... without succes.... :/ I have done that :

Global tile[1]:TPixmap=GrabPixmap(0,0,20,20)
DrawPixmap tile[1],250,10

NOT WORKS, i suppose i do not understand arrays structures (not usual as in old basics)

If you can help :)

Offline TomToad

Re: cut & display tiles with array variables
« Reply #1 on: February 03, 2021, 04:01:30 PM »
Code: [Select]
Local fullimages:TImage = LoadImage("test.png")

Local tile:TPixmap[10] 'defines the number of elements
tile[1] = GrabPixmap(0,0,20,20) 'puts pixmap into element 1
Note that arrays are 0 based, which means a 10 element array is indexed from 0-9.  If you want to index from 1-10 instead, you would need to either subtract 1 form index (tile[i-1]) or create an 11 element array and just ignore element 0 (Global tile:TPixmap[11]).

Another thing, drawing pixmaps to the screen is far slower than images, it is better to either initialize the array to several blank images, then GrabImage to the array,
Code: [Select]
Local fullimages:TImage = LoadImage("test.png")

Local tile:TImage[10]
For Local i:int = 0 to 9
    tile[i] = CreateImage(20,20)
Next

DrawImage fulltiles,0,0

tile[1] = GrabImage(0,0)

DrawImage tile[1],250,10

 or use LoadAnimImage and just specify the frame.
Code: [Select]
Local fulltiles:TImage = LoadAnimImage("test.png",20,20,0,10)

DrawImage fulltiles,250,10,1
Another thing, it is best to avoid Global unless you have a very specific reason to use them.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline drfloyd

Re: cut & display tiles with array variables
« Reply #2 on: February 03, 2021, 04:34:59 PM »
Thanks, what is wrong :

Code: [Select]
Global tile:TImage[10]
Global fulltiles:TImage=LoadImage ("test.png")
tile[1]=CreateImage(20,20)
DrawImage fulltiles,0,0
tile[1]=GrabImage(0,0)

DrawImage tile[1],100,100

Error message  : Unable to find overload for grabimage(int,int) argument 1 is int but declaration is Timage

Offline TomToad

Re: cut & display tiles with array variables
« Reply #3 on: February 03, 2021, 05:03:38 PM »
Sorry, that should be GrabImage(tile[1],20,20).  I typed in those examples from memory.

If you type in a command, you can press F1 and see the correct syntax at the bottom of the editor.  Press F2 twice, and you'll get the help page on that command.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Derron

Re: cut & display tiles with array variables
« Reply #4 on: February 03, 2021, 09:24:50 PM »
Instead of drawing and grabbing ..

There is DrawSubImageRect() ... which draws a portion of a image. Perfect for "sprite atlases" (differently sized images placed on a big image).


bye
Ron

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal