Question (difficult to explain)

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey everyone, I'm not quite sure how to explain my problem, so I apologize if I come off as a retard. I'm writing a texture manager that *gasp* manages all of my textures. I wrote a function for the manager that renders a texture to the screen mapped on a quad. The function requires , among other things, the texture that is used on the quad. Now I could just send the function an integer that corresponds to the place of the texture in the texture array, but then it gets easy to accidently use the wrong texture, so I created some define's to help (see below)
#define GROUND 0
#define TREE 1
//etc....

TextureManager manager;

//This is alot easier to see what's going on than...
manager.RenderTexture(GROUND, xposition , yposition);

//... this one
manager.RenderTexture(0, xposition , yposition);


Problem is, I don't want to have to update the define's everytime an artist adds a texture, which brings me to the meat of the question: Is there anyway to #define something after the program executes? I want to have the manager read from a file what textures to load, and what to name them in the manager, but I have no idea how to create the name in code. The file that specifies the textures is a plain text file that looks like this
Number of Textures = 2
Texture Name = GROUND
Texture Filename = "Data/Ground.bmp"
Texture Name = TREE
Texture Filename = "Data/Tree.bmp"


Then I would want to be able to say in code
manager.RenderTexture(TREE,xposition, yposition);


but not have to manually #define TREE in the code. Again, let me know if I am babbling incoherently, I have no idea how to explain this.

Share on other sites
Quote:
 Hey everyone, I'm not quite sure how to explain my problem, so I apologize if I come off as a retard.

I hope this answer doesn't come across that way either [wink]

Why can't you just use variables to accompish this? This concept is 'data-driven design', which allows for non-harcoded logic and operations based on external data. For example, in your case you can use a container that stores either a struct or a class that holds all the information.

For example:

struct tTexture{   int id;   int xpos;   int ypos;};class TextureManager {  ...  map<string, tTexture> texMap;  ...  RenderTexture(string str)  {     index = texMap[str].id;     x = texMap[str].xpos;     y = texMap[str].ypos;     .. rest of logics that uses those variables  }};// a lot easier to read now isnt it?manager.RenderTexture("ground");// or even if you wanted to tell the coordsmanager.RenderTexture("ground", X, Y);

Now that's not complete, done that correct, or anything like that, but it should give you an idea of what I am talking about. Kind of see what I mean?

Share on other sites
map<string, tTexture> texMap;

Highlighted for being the key idea that the OP probably wasn't aware of.

Variable names "don't exist" at runtime. However, text strings can. We can use the standard library class std::map to establish a mapping between runtime text "names" for textures, and the actual texture objects.

(By the way, don't use #define for constants. Use const {typename goes here}. That allows your constant to have a name and a scope, which are both incredibly useful - and are not relying on a dumb text-substitution engine. Shouldn't you trust your compiler more than you trust a search-and-replace function?)

Share on other sites
not to get off topic but is map faster than keeping a list of strings and searching until an identical string is found ? if so how?

Share on other sites
Thank you so much, I got something working. I've never used STL before, but this is a good way to start off :D

Share on other sites
Quote:
 Original post by MTclipnot to get off topic but is map faster than keeping a list of strings and searching until an identical string is found ? if so how?

It really depends on the implementation of the map as if it is faster or not, but with a map, you can use iterators and store that variable and use it over and over when needed throughout the function so you do not have to reaccess the map each time. Either way, it doesn't matter, there exist Find functions for vectors and maps which I both assume have an O(n) complexity, so essentially they are the same. You could speed up a map's access if you used a hash map instead though, although in a case like this, not sure if you will see any benefits to doing that.

Share on other sites
Quote:
Original post by Drew_Benton
Quote:
 Original post by MTclipnot to get off topic but is map faster than keeping a list of strings and searching until an identical string is found ? if so how?

It really depends on the implementation of the map as if it is faster or not, but with a map, you can use iterators and store that variable and use it over and over when needed throughout the function so you do not have to reaccess the map each time. Either way, it doesn't matter, there exist Find functions for vectors and maps which I both assume have an O(n) complexity, so essentially they are the same. You could speed up a map's access if you used a hash map instead though, although in a case like this, not sure if you will see any benefits to doing that.

std::map is O(log n) worst case IIRC, which should be fast enough unless you have a lot of types of textures or lookup a lot per scene, and a lot faster than if you rolled your own just going through all n texture types.

Share on other sites
Quote:
 Original post by etothexstd::map is O(log n) worst case IIRC

Oh wow, I guess you're right [smile] That's a lot better than I had always thought.

Share on other sites
Quote:
Original post by Drew_Benton
Quote:
 Original post by etothexstd::map is O(log n) worst case IIRC

Oh wow, I guess you're right [smile] That's a lot better than I had always thought.
Yeah it's just a red-black tree so it's not too bad.

An unordered map is on average constant time, and when it comes to a big map with strings as the index, switching to a hashed container makes a huge difference.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 23
• 12
• 12
• 11
• Forum Statistics

• Total Topics
634779
• Total Posts
3019240
×