ALLEGRO: how can I use load_bitmap with various filenames? level1.bmp level2.bmp etc

Started by
10 comments, last by darkzerox 18 years, 10 months ago
Hmm. I have this: from = load_bitmap("data/spritesheet001.bmp", NULL); ...to load in my spritesheet for level 1. But what about for level 2, 3 etc? I need to be able to change the filename to data/spritesheetx.bmp where x is the current level. I tried creating a string like this: std::string filename="data/spritesheet001.bmp"; If I can get it to use that filename I can easily change the string as needed. But when I do this: BITMAP *from; from = load_bitmap(filename, NULL); ...I get "cannot convert 'std::string' to 'const char' for argument 1". Any ideas?
Advertisement
[edit] filename.c_string() should be filename.c_str() [/edit]

pass filename.c_str() instead of just filename. It converts std::string to const char. So use something like this:

BITMAP *from;
from = load_bitmap(filename.c_str(), NULL);
your are trying to convert from a string to the char*. Unfortunately it is impossible. But to solve the problem you should write:

BITMAP *from;
from = load_bitmap(filename.c_str(), NULL);

c_str() method returns char* so that the function "load_bitmap", which is a simple C function is able to execute.

I think that it should help... :)

Se my programming blog: Code and Graphics

Allegro is a C API, not a C++ API. It knows nothing of std::string. You need to pass it a character array. You need to use the c_str() method of the string class to get at the character array:

from = load_bitmap(filename.c_str(), NULL);

In the future, consider posting Allegro-specific topics in the Alternative Game Libraries forum here at GDNet, or the forums at allegro.cc.
Thanks all, it works perfectly:
from = load_bitmap(filename.c_str(), NULL);

Will post Allegro questions on Alternative Game Libraries in future, for sure.
this is why i hate std::strings :) i've never really found the benefit of them... you can do most string manipulations on const char*'s using the string library...
Quote:Original post by joebarnslondon
Thanks all, it works perfectly:
from = load_bitmap(filename.c_str(), NULL);

Will post Allegro questions on Alternative Game Libraries in future, for sure.


*just incase*
Don't forget to release the bitmap data between bitmap loads! I forget the allegro function name, but it you keep loading without releasing, you'll get leaks!
Quote:Original post by Vampyre_Dark

*just incase*
Don't forget to release the bitmap data between bitmap loads! I forget the allegro function name, but it you keep loading without releasing, you'll get leaks!


Talking of which, why does a person have to destroy bitmaps at the end of their program? Isn't all memory freed when the program ends?
Quote:Original post by darkzerox
this is why i hate std::strings :) i've never really found the benefit of them... you can do most string manipulations on const char*'s using the string library...
*Sigh*, not this again. The reason people use std::strings is because they are easier (for a lot of things). It could save you lines of code (no need for malloc, realloc or free), and you're complaining about needing to type an extra 8 characters when you need compatibility with C functions?
Quote:Original post by joebarnslondon
Talking of which, why does a person have to destroy bitmaps at the end of their program? Isn't all memory freed when the program ends?


In theory yes, but things are not always that simple. Anything you allocate or acquire, you should deallocate or release, unless otherwise specified.

(PS. Moving to Alternative Game Libraries forum.)

This topic is closed to new replies.

Advertisement