Skip to main content
gamedev.net
🔒 Locked

storing tile map

Started by CodeBoy504 Dec 17, 2004 at 12:50 PM 4 replies 1.5k views
Original Post
CodeBoy504
CodeBoy504
hey everyone, it's my first post here on gamedev. Anyways, I am working on a 2D tile-based RPG in OpenGL and I've run into a small problem. I was planning on having my tile structs point to a Sprite class that draws most of my game's 2d sprites. I think this will work except when I save the map to a file the pointer will become invalid. I don't have my code at the moment but here's a small example. struct CTile { CSprite* tile bool walkable } CTile map[5][5] //map array //load map from file... map[0][0].tile->Draw(0, 0); //draw a tile How would I retrieve a valid pointer when I load the map tiles from file?
BouncePup
BouncePup
You really can't to the best of my knowledge. I'd suggest you break it down and use either integers or unsigned char to store what tile to draw at what position (X,Y) and save that out to a file.


-Nate S.
"Always forgive your enemies, nothing annoys them more.."
CodeBoy504
CodeBoy504
yeah, that was the only other thing i could think of. It just seemed a bit hard to manage because you would have to designate an int or char to each sprite and keep track of which is which. I'm not too sure of a good way to do this.
tentoid
tentoid
In my game each tile has an graphic ID value (integer). They are loaded from an XML file which defines the ID and a filename which corresponds to the image.

If you want to save an object you can't just save the pointer (it just stores the address in memory). For example, I have methods GameObject::Serialize(std::fstream file) and GameObject::Unserialize(std::fstream file). When serializing the Serialize() method writes the object's values (ints, floats, bools etc.) into the file one by one. When unserializing it Unserialize() reads these in the same order.

I hope that made some sense..

[Edited by - tentoid on December 17, 2004 1:18:45 PM]
Corvwyn
Corvwyn
Yes, serialization would be the way to go. I had some trouble with that some time ago as well. I finally got into it because a fruny helped me out in these forums. Here: http://www.gamedev.net/community/forums/topic.asp?topic_id=207487

If you use managed code (which I don't) you could take the easy way out and use premade functions.

Hope this helps.
krez
krez
Quote:
Original post by CodeBoy504
yeah, that was the only other thing i could think of. It just seemed a bit hard to manage because you would have to designate an int or char to each sprite and keep track of which is which. I'm not too sure of a good way to do this.

yes, and then when you load your map you look up (from a list of valid sprites) which one to load, load it (elsewhere, see below), and then get a pointer to it.

the place it is actually loaded can vary, but i would use a STL container of some sort and manage it (loading, cleaning up afterwards) with another class and just make the pointer available as soon as the first tile of that type is requested.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.