Loading Graphics with the help of extern files

Started by
6 comments, last by Brother Bob 10 years, 6 months ago

Hey,

I always create an huge enum in my project and one function where all the 'loadTexture' functions are called. I think its not the best way so i thougt i could easy read the img src from an extern file (url:foo.png name:TEXTURE_TREE).

The only problem is: I still have to declare the enum. ( In there are some values like 'TEXTURE_TREE' and so on that are called on orher places). Is there a way to automatically fill this enum, or do you have a better idea?

thanks,

Daniel

Advertisement

Hey,

I always create an huge enum in my project and one function where all the 'loadTexture' functions are called. I think its not the best way so i thougt i could easy read the img src from an extern file (url:foo.png name:TEXTURE_TREE).

The only problem is: I still have to declare the enum. ( In there are some values like 'TEXTURE_TREE' and so on that are called on orher places). Is there a way to automatically fill this enum, or do you have a better idea?

thanks,

Daniel

The only problem is: I still have to declare the enum.

No, you only think you do.

Try using an associative container such as map or unordered_map and refer to the textures as strings:

// load resources
map<string,texture> atlas;
for each ( line in file ) {
    atlas[line.name] = texture::load(line.uri);
}

// get resource
const texture& imgTree = atlas["TEXTURE_TREE"];

Typically you would use an associative container (std::map or similar) to map the filename to an ID which is assigned sequentially by the texture loader, then you get the ID back after loading by looking it up in the map. You should only get the ID once and remember it in a variable though, to avoid doing the string to ID lookup all the time.

So

LoadTexture("TEXTURE_TREE"); // this loads the texture and automatically assigns the next available ID if it isn't already loaded and already got an ID

after loading

tex_tree_id = GetTextureID("TEXTURE_TREE");

and then use tex_tree_id wherever you would use the enum value. You can't use variables in switch statements, however, so you will need to refactor any code that does that.

And don't double post, ask a mod to delete your other thread.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I made a reply similar to the above in the other identical thread in For Beginners. One of these threads needs to be deleted or they need to be merged. Don't double post threads in different forums.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

And don't double post, ask a mod to delete your other thread.

How to delete or merge a post?

Dunno, the best way is not to do it in the first place ;)

You could PM rip-off who is a moderator and is on the forum right now, or you could report a post in this thread and ask for it to be closed, not sure if that is seen as a valid reason to report though. Or you could wait until a moderator sees the thread and closes one of them (probably this one since it only has my reply and discussion about double posting).

EDIT: Looks like it has been merged already. Now don't do it again!

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I have merged the two threads. In the future, don't make multiple threads for the same question.

This topic is closed to new replies.

Advertisement