Loading 2D Tile-Based Maps?
Moderator: PC Supremacists
-
- Chaos Rift Newbie
- Posts: 10
- Joined: Thu Dec 01, 2011 11:15 am
Loading 2D Tile-Based Maps?
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?
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?
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Loading 2D Tile-Based Maps?
That's pretty much all there is to it. Check out LazyFoo's article: http://lazyfoo.net/SDL_tutorials/lesson29/index.phpTheCompBoy 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?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
-
- Chaos Rift Newbie
- Posts: 10
- Joined: Thu Dec 01, 2011 11:15 am
Re: Loading 2D Tile-Based Maps?
Thanks!
- Van-B
- Chaos Rift Regular
- Posts: 125
- Joined: Tue Aug 10, 2010 7:17 am
- Current Project: iPhone puzzle game
- Favorite Gaming Platforms: All - Except Amiga
- Programming Language of Choice: DBPro, ObjC++
- Location: Scotland
Re: Loading 2D Tile-Based Maps?
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.
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.
Health, ammo.... and bacon and eggs.
-
- Chaos Rift Newbie
- Posts: 10
- Joined: Thu Dec 01, 2011 11:15 am
Re: Loading 2D Tile-Based Maps?
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?
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?
- Light-Dark
- Dreamcast Developer
- Posts: 307
- Joined: Sun Mar 13, 2011 7:57 pm
- Current Project: 2D RPG & NES Platformer
- Favorite Gaming Platforms: NES,SNES,N64,Genesis,Dreamcast,PC,Xbox360
- Programming Language of Choice: C/++
- Location: Canada
Re: Loading 2D Tile-Based Maps?
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 dataTheCompBoy 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?
<tpw_rules> LightDark: java is a consequence of inverse moore's law: every 18 months, the average program will be twice as slow. therefore, computers always run at the same percevied speed. java's invention was a monumental step
-
- Chaos Rift Cool Newbie
- Posts: 78
- Joined: Mon Feb 21, 2011 2:55 am
- Current Project: Aleios Engine
- Favorite Gaming Platforms: PC, Dreamcast
- Programming Language of Choice: C++
- Location: Melbourne, Australia
Re: Loading 2D Tile-Based Maps?
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.Light-Dark wrote: There is a better way to load tiles, not in a array but instead from a text file
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()).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
- Light-Dark
- Dreamcast Developer
- Posts: 307
- Joined: Sun Mar 13, 2011 7:57 pm
- Current Project: 2D RPG & NES Platformer
- Favorite Gaming Platforms: NES,SNES,N64,Genesis,Dreamcast,PC,Xbox360
- Programming Language of Choice: C/++
- Location: Canada
Re: Loading 2D Tile-Based Maps?
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.Aleios wrote: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.Light-Dark wrote: There is a better way to load tiles, not in a array but instead from a text file
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!
<tpw_rules> LightDark: java is a consequence of inverse moore's law: every 18 months, the average program will be twice as slow. therefore, computers always run at the same percevied speed. java's invention was a monumental step
- Van-B
- Chaos Rift Regular
- Posts: 125
- Joined: Tue Aug 10, 2010 7:17 am
- Current Project: iPhone puzzle game
- Favorite Gaming Platforms: All - Except Amiga
- Programming Language of Choice: DBPro, ObjC++
- Location: Scotland
Re: Loading 2D Tile-Based Maps?
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.
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.
Health, ammo.... and bacon and eggs.
-
- Chaos Rift Newbie
- Posts: 10
- Joined: Thu Dec 01, 2011 11:15 am
Re: Loading 2D Tile-Based Maps?
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
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