Tilemaps in GLBasic

Previous topic - Next topic

Leon

Hello,
I have a question about GLBasic. Is there an easy way to create a tilemap?
Thanks for your answers :)

erico

#1
Create a 2 dimension array, let´s say 100x100 with DIM tile[99][99].
That is the board, each tile let´s say, 16x16 pixels.
Create the tiles, maybe 8 tiles, in a single bmp side by side.

Load tiles with LOADANIM "tiles.bmp",0,16,16.

Do a FOR/NEXT loop to draw the board. For example:

FOR X=0 to 99
FOR Y=0 to 99
  DRAWANIM 0,frame#,x*16,y*16
NEXT Y
NEXT X

This will read the array and populate the tile FRAME# on its screen position.
You should have the array filled with 0 so it uses the first tile.

If you would like to populate the map before drawing, you can use DATA and RESTORE.
I think there are several examples on the forum about this.

EDIT: these 2 topics might help you out too:
http://www.glbasic.com/forum/index.php?topic=10134.0
http://www.glbasic.com/forum/index.php?topic=10414.0

quangdx

I usually create a screen first, draw my tiled background to that from a 2 dimensional array,
then use DRAWSPRITE to draw the background.
Asobi tech - the science of play.
Spare time indiegame developer.

Leon

Thanks for your answers :)
I just don't understand how I can select the tile I want.

nabz32

you mean checking which tile ID is at a certain coordinate?

Just divide your 2D coordinates by your Tile Size.
Make sure your coordinates are inside of the max size of your tile map
otherwise you get an access out of array bounds.

erico

Quote from: Leon on 2015-Sep-10
Thanks for your answers :)
I just don't understand how I can select the tile I want.

What is it that you want to do? It is hard to understand the context of "select the tile I want".

If you want to set a tile to a position for a map:
When you LOADANIM, you automatically create an ID for each tile starting at 0.
If you create your map with data, just use the ID numbers.

If you want, let´s say, a selector to hover the tiles:
Create variables for its coordinates, like SELECTX and SELECTY.
Then DRAWSPRITE 1,SELECTX,SELECTY
You can then check which tile is under the selector with:
IF TILE[SELECTX][SELECTY]=7 THEN TCHANS!

I hope it helps, but both linked examples should get you there, if you are still having trouble you need to better describe what you want.

GarBenjamin

#6
If you look in the 2D snippets forum you will find a Tiled Map Loader and generic tile map renderer I wrote.

It sounds like you may want to use a map editor such as Tiled: http://mapeditor.org
Tiled is free and you can easily draw your stages.

Then you just use the modules I posted to load the Tiled maps and display them.

You can try it out if that is what you mean. And if you run into any issues with it just let me know (PM) and I will try to help you sort it out.

If you mean doing it all in code then the others already are giving great support.

spacefractal

#7
Im thinks this is a great tiled based game to been tryout to see how the code wise is working:
http://www.glbasic.com/forum/index.php?topic=10461.0

Im also used Tiled (http://mapeditor.org) for both Genius Greedy Mouse and Karma Miwa (but not CatchOut), but in a somewhere more complicered form (using dual layers and many items using remapping constatly, like the dirt in Greedy Mouse). Karma Miwa even used flipped and mirrored tiles as well.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

Leon

Wow, your help is really good :)
I'll try the map editor.
Thanks for your help :)