Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    686
  • comments
    1952
  • views
    387445

Fixing some stuff...

Sign in to follow this  
Stompy9999

75 views

I've been looking over the code for Stompy 2, and I noticed one thing I really wanted to change.

In any tile based game I do, I like to rename the filenames of the tile graphics 0, 1, 2 and so forth, so that I can load them all into an array with one for-loop:

// Load in tileset
for(int i = 0; i < NUM_TILES; i++)
{
char filename[20];
sprintf(filename, "gfx/Tiles/%d.bmp", i);
tiles = SDL_LoadBMP(filename);

if(!tiles)
// error
}



Although this works, I want to use std::string whenever I need to do any string related processing. Plus, I've heard using sprintf() is a definite no-no. So this would be the new version:

// Load in tileset
for(int i = 0; i < NUM_TILES; i++)
{
std::ostringstream filename;
filename << "gfx/Tiles/" << i << ".bmp";
tiles = SDL_LoadBMP(filename.str().c_str());

if(!tiles)
// error
}



Does the same thing, but it is now very C++-ified and it fits in well with the rest of my STL heavy code.
Sign in to follow this  


3 Comments


Recommended Comments

If you're curious, a similar method of concatenating numbers onto strings is to use boost::lexical_cast.

It can be used thusly:

std::string = "gfx/Tiles/" + boost::lexical_cast< std::string >( i ) + ".bmp";

Share this comment


Link to comment
That also looks like a good solution. I haven't used any boost yet in my code, but it sounds like something I should try.

Share this comment


Link to comment
I've never had any trouble with sprintf. Maybe if you're doing some intensive string manipulation it is disadvantageous, I don't know. You could also use atoi() to convert integers to characters, though if you're using std::string chars might not help you much.

Share this comment


Link to comment

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
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!