Page 1 of 1
Loading 2D Tile-Based Maps?
Posted: Thu Dec 01, 2011 11:20 am
by TheCompBoy
I need to ask for help on this one.. Its at the moment the biggest problem of my 2D side scroller project im making in C++..
I found out that it would be better to make a big tile sheet and load the tiles like one by one and the render them to the screen instead of using like the whole map in one Image.. But i have no idea how the best way to load theese tiles one by one..
Should i make like an Map array which like 1's mean like grass and 2's means stone?
Or maybe a text file with just the numbers?.. Whats the best way to solve this?
Re: Loading 2D Tile-Based Maps?
Posted: Thu Dec 01, 2011 1:05 pm
by dandymcgee
TheCompBoy wrote:Should i make like an Map array which like 1's mean like grass and 2's means stone?
Or maybe a text file with just the numbers?
That's pretty much all there is to it. Check out LazyFoo's article:
http://lazyfoo.net/SDL_tutorials/lesson29/index.php
Re: Loading 2D Tile-Based Maps?
Posted: Thu Dec 01, 2011 1:24 pm
by TheCompBoy
Thanks!
Re: Loading 2D Tile-Based Maps?
Posted: Thu Dec 01, 2011 6:07 pm
by Van-B
Using CHAR or byte values is quite handy for storing maps. For instance, you could make a tile set, a grid of 16x16 images. That tends to be plenty for most level graphics, 256 different images. With that, I would tend to make a byte array for whatever grid size the map is. Then saving and loading map files is just a case of loading the bytes into the array. If your using OpenGL, you can make good use of the single image, because that means you can construct a vertex array instead of just a quad, so the whole map stored as a 2D mesh, then rendering it is really quick and efficient. You would have to work on a vertex format, like an extension of what you would do with a quad. You can make a big array of vertex data, with UV map coordinates set to suit the tile. I just work out the X and Y coordinate of a tile number... tiley=int(tile/16) tilex=tile-(tiley*16) - I know there's probably a hundred ways of optimizing that, but anyway I like to keep the vertex stuff clear and well commented, it can become a headache when figuring out vertex orders, best to be well organized with that stuff.
Maybe an idea to adopt an established map editor like Mappy
http://tilemap.co.uk/mappy.php
Theres probably some great source code for it for C++, no point in reinventing the wheel if you don't have to. I guess it depends on how complex your game is, often it's better to know your map format and rendering inside and out - then its possible to adapt it. I mean, on the surface a game like Super Mario appears simple, a long grid of bytes holding tile numbers... but then all the fancy stuff like variable tile heights, and moving platforms get quite complex. It's best to consider everything your game needs, and how the map format should be designed to handle that.
Re: Loading 2D Tile-Based Maps?
Posted: Sat Dec 03, 2011 12:17 pm
by TheCompBoy
I tried writing something that loads tiles from an array using XNA C#..
I get a problem tho.. I have an array that holds the X, Y positions to draw on.. And i have a loop that draws a tile to each position in the array..
But after that i found out that if i draw inside a for loop the pictures will only stay while the loop is active.. Is there some better idea to do this?
Is it possible to in my loop that i create a Texture2D out of the tiles my loop makes me?
Re: Loading 2D Tile-Based Maps?
Posted: Sat Dec 03, 2011 5:27 pm
by Light-Dark
TheCompBoy wrote:I tried writing something that loads tiles from an array using XNA C#..
I get a problem tho.. I have an array that holds the X, Y positions to draw on.. And i have a loop that draws a tile to each position in the array..
But after that i found out that if i draw inside a for loop the pictures will only stay while the loop is active.. Is there some better idea to do this?
Is it possible to in my loop that i create a Texture2D out of the tiles my loop makes me?
There is a better way to load tiles, not in a array but instead from a text file, you can start out with a text file and have a equation that you can extract the x,y and type of the tile from a number written to the text file, i don't know much C# i don't know if it does file I/O but if so i recomend you use file I/O for storing tile/map data
Re: Loading 2D Tile-Based Maps?
Posted: Sat Dec 03, 2011 10:30 pm
by Aleios
Light-Dark wrote:
There is a better way to load tiles, not in a array but instead from a text file
It's going to be loaded into an array from the text file as constantly reading from a text file is stupid and slow, you load the data from the text file into an array, close the file and loop from that array.
TheCompBoy wrote:
But after that i found out that if i draw inside a for loop the pictures will only stay while the loop is active
It seems that you are talking about the drawing part. You need to constantly keep looping over the tiles since every time there is a call to your drawing function i have no doubt that you are clearing the buffer, which is what is usually done. You can probably save the content of the screen to a Texture2D somehow, but you should probably just make an image outside your game if you are going to do that. Even with a Texture2D you would still be needing to clear the buffer to get rid of old data and bring in new data. Simple solution: Keep looping through the array of tiles as it's what most people do. If there are no changes occurring to the scene then just don't call the clear function (probably GraphicsDevice.Clear()).
Re: Loading 2D Tile-Based Maps?
Posted: Sat Dec 03, 2011 10:55 pm
by Light-Dark
Aleios wrote:Light-Dark wrote:
There is a better way to load tiles, not in a array but instead from a text file
It's going to be loaded into an array from the text file as constantly reading from a text file is stupid and slow, you load the data from the text file into an array, close the file and loop from that array.
Not at all, the way i do it is it places the tiles right after it loads one its quick and painless, im guessing he has it so it places the tiles as the camera hits them and deletes them as it passes,The way i have it is it loads the entire area and it will render the tiles as they come within the camera area, not slow at all takes about a second.
Example
Code: Select all
for (int i = 0; i < TOTAL_TILES; i++)
{
MAPFILE >> TileAttribute;
TileAttribute = TileAtribute/10/20/30 //Example of a decyrption/encyrption method
tilearray[i] = new Tile(x,y,TileAttribute); //Create a new instance of Tile
}
MAPFILE.close(); // Close the file, and were done!
Re: Loading 2D Tile-Based Maps?
Posted: Sun Dec 04, 2011 4:36 am
by Van-B
Why is that better than using an array?
What happens if you want to scroll backwards? - seems like loading tiles like that is pretty limited. For one thing, you shouldn't draw your tiles one by one. For another thing, that would really fricken complicate the level editor.
Re: Loading 2D Tile-Based Maps?
Posted: Sun Dec 04, 2011 7:59 am
by TheCompBoy
I solved the problem with drawing the tiles.. Now i have even more stupid problem..
I now have two arrays per tile.. One array for the tiles X position to be drawn on and one for the tiles Y position..
Any suggestions on how to make it more readable / easier or other coders to look at it?
EDIT:
I did it. And im proud of myself :D