Sign in to follow this  
darenking

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[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);

Share this post


Link to post
Share on other sites
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... :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
*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?


hahaha.. oh well. i've never had any problems with char*'s.
'new' and 'delete' work fine. but i guess in a string intensive program, i might consider using std::strings.

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