Author Topic: Tilemaps in GLBasic  (Read 2899 times)

Offline Leon

  • Mc. Print
  • *
  • Posts: 42
    • View Profile
Tilemaps in GLBasic
« on: 2015-Sep-09 »
Hello,
I have a question about GLBasic. Is there an easy way to create a tilemap?
Thanks for your answers :)

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4218
    • View Profile
    • Portfolio
Re: Tilemaps in GLBasic
« Reply #1 on: 2015-Sep-09 »
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
« Last Edit: 2015-Sep-09 by erico »

Offline quangdx

  • Mr. Polyvector
  • ***
  • Posts: 233
  • work hard / play hard
    • View Profile
    • Asobi tech
Re: Tilemaps in GLBasic
« Reply #2 on: 2015-Sep-09 »
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.

Offline Leon

  • Mc. Print
  • *
  • Posts: 42
    • View Profile
Re: Tilemaps in GLBasic
« Reply #3 on: 2015-Sep-10 »
Thanks for your answers :)
I just don't understand how I can select the tile I want.

Offline nabz32

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 307
    • View Profile
Re: Tilemaps in GLBasic
« Reply #4 on: 2015-Sep-10 »
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.

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4218
    • View Profile
    • Portfolio
Re: Tilemaps in GLBasic
« Reply #5 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.

Offline GarBenjamin

  • Mr. Drawsprite
  • **
  • Posts: 81
    • View Profile
Re: Tilemaps in GLBasic
« Reply #6 on: 2015-Sep-10 »
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.
« Last Edit: 2015-Sep-10 by GarBenjamin »

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3679
    • View Profile
    • Space Fractal
Re: Tilemaps in GLBasic
« Reply #7 on: 2015-Sep-11 »
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.
« Last Edit: 2015-Sep-11 by spacefractal »
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

Offline Leon

  • Mc. Print
  • *
  • Posts: 42
    • View Profile
Re: Tilemaps in GLBasic
« Reply #8 on: 2015-Sep-14 »
Wow, your help is really good :)
I'll try the map editor.
Thanks for your help :)