Sign in to follow this  
Prodigal Squirrel

Texturing 2d tile map

Recommended Posts

I was curious of what strategies are use for texturing a 2d tile map. Currently, I assumed to just give each visible object an image variable, that is assigned a certain graphic file. Is this a valid strategy, or are there more efficient approaches to texturing a 2d tile map?

Share this post


Link to post
Share on other sites
Let's say you store your map as a 2d array.

One method, like you said, is to give each tile its own image. That would work, but it's slow, and it's a massive waste of memory. (100 tiles might share the same image) Another method is to give each tile a pointer to whatever texture it is drawn with. It's good on memory, but when you go to save your map to disk, you can't save a bunch of pointers and expect them to still be valid when you start up.

A third method, and IMO the best and easiest, is to have each tile store an integer, which will act as a texture index. In your drawMap function, the function would look up each tile's index in, say, a vector of images and draw that one. That's the method I used for SDL stuff back when I used it.

Now, that's good, but looking up a pointer to an image inside a vector can take enough time to slow you down if you're drawing a lot of tiles. A more advanced implementation is to use both the second and third method: Keep an array of integers so you can save the map to disk, and when it loads, fill a second array of pointers to the actual texture. That way when you're drawing you can just grab the texture pointer right from the map array, instead of using a vector lookup.

Share this post


Link to post
Share on other sites
Quote:
Original post by Foobar of Integers
In your drawMap function, the function would look up each tile's index in, say, a vector of images and draw that one.


Thanks for the help, and I like how used drawMap as an example since it has the exact same notation and name as the map drawing function in my engine.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this