Sign in to follow this  
  • entries
    686
  • comments
    1952
  • views
    386210

Fixing some stuff...

Sign in to follow this  
Stompy9999

57 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

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