Sign in to follow this  
Mizipzor

I need some advice

Recommended Posts

Im trying to do a tilebased engine. And I dont know if I should make the drawtile() ask for a std::string or just a char. A string is easier cause then I can store the textures in std::map <std::string, Sdl_Surface*> textures; and access a texture with its own filename. With a char its trickier, then I must come up with a unique char for every texture and keep track of them. The upside is that its faster to send chars then strings and I can store the maps in char arrays. Is the difference in speed negletable? What should I do? I know how to program it and stuff it just I can decide on which system to use.

Share this post


Link to post
Share on other sites
For simplicity's sake, it's probably better to stick with the version that you have already. Giving the textures human-readable names such as "grass" or "floor-tile" will probably make debugging easier than if you called them "a", "b", "c", etc, since you can see at a glance which texture is where, rather than having to look at the char and remember which texture it represents. The C++ STL is generally very well implemented, so you shouldn't experience huge speed problems when using std::string. In the wider scheme of things, using std::string will make the other pieces of your code which access textures a lot easier to deal with, due to built in bounds checking, resizing, etc.

Share this post


Link to post
Share on other sites
Have you considered using an enumeration?


enum { GRASS, FLOOR_TILE, /* etc. */ } tile_t;

std::map<tile_t, Sdl_Surface*> textures;


This way you could use an indexed container instead, as well (such as vector or deque), which may be more efficient (and straightforward; but it depends on what exactly you're trying to do with the textures). The enumeration values will implicitly convert to integers, you see (but not the other way around), with the first item mapping to 0, the next to 1 etc. (by default; you can override that - look it up).

Share this post


Link to post
Share on other sites
The problem with using an enum is that you can't easily add textures without a recompile. Using verbatim names (in the form of strings), allows textures to be added dynamically on a per-map basis.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mizipzor
Im trying to do a tilebased engine. And I dont know if I should make the drawtile() ask for a std::string or just a char. A string is easier cause then I can store the textures in

std::map <std::string, Sdl_Surface*> textures;

and access a texture with its own filename. With a char its trickier, then I must come up with a unique char for every texture and keep track of them. The upside is that its faster to send chars then strings and I can store the maps in char arrays.

Is the difference in speed negletable? What should I do? I know how to program it and stuff it just I can decide on which system to use.


As long as you only do this lookup at the image's creation and not every frame, I doubt it will cause much problems as the full string. There's also other options you could do such as pointers to strings or the hash of a string as a middle ground. I'm not sure if these are any benefit in a map though. You'd have to see for yourself.

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