Passing a string into an load_bitmap() [Allegro]
If I was writing a game engine and wanted to be able to load custom sprites how could I pass it to the load_bitmap() function?
i.e.
void GameEngine::Get_Sprites( ~Pass a String~ ) {
BITMAP *~passed string~ = load_bitmap("~passed string~.EXTENSION", NULL);
}
Or maybe there is a better way? Thanks :) I guess what is confusing me are those quote marks.
Your load_bitmap() function would accept the filename as an argument. I would reccomend putting these such utility functions in a header file, so if you want to use it then you can just call the header. So when you declare your function in the header: void load_bitmap(std::string filename). Then define this function in the cpp. So when you want to load a bitmap, just type: (type your variable)=load_bitmap("test.bmp");
That assumes hardcoding though.
Say I set this up.
ex of output.
"What file would you like to display?"
_
Repsonse = title
What is the files extension?
_
response = .bmp
_
~display title.bmp~
I want something like.
void GameEngine::Get_Sprites(std::string1, std::string2) {
BITMAP *std::string1 = load_bitmap("std::string1, std::string2", NULL);
}
Say I set this up.
ex of output.
"What file would you like to display?"
_
Repsonse = title
What is the files extension?
_
response = .bmp
_
~display title.bmp~
I want something like.
void GameEngine::Get_Sprites(std::string1, std::string2) {
BITMAP *std::string1 = load_bitmap("std::string1, std::string2", NULL);
}
concatenate the strings together and pass with:
myString.c_str();
here's the ref page for std::string:
http://www.sgi.com/tech/stl/basic_string.html
-me
myString.c_str();
here's the ref page for std::string:
http://www.sgi.com/tech/stl/basic_string.html
-me
I don't know how to pass it though, the file name is in quotes, so it wouldn't recognize a variable. :-/
Is your problem that you need to pass a character array to load_bmp instead of a string? Because that's what the c_str() function does to strings. So you can do this:
std::string userInput;
cout << "Please enter the filename (with extension) to load: ";
cin >> userInput;
BITMAP* map = load_bmp(userInput.c_str(), NULL);
I'm not especially familiar with using bitmaps, so my syntax may be off. I'm also not certain what your "Get_Sprites" function is supposed to do. What are its arguments? It evidently doesn't return anything because it's a void function, so what does it do?
std::string userInput;
cout << "Please enter the filename (with extension) to load: ";
cin >> userInput;
BITMAP* map = load_bmp(userInput.c_str(), NULL);
I'm not especially familiar with using bitmaps, so my syntax may be off. I'm also not certain what your "Get_Sprites" function is supposed to do. What are its arguments? It evidently doesn't return anything because it's a void function, so what does it do?
From what i see, the OP tries to create new variables at runtime
noticed the std::string1 ?
it is impossible to create new named variables at runtime like this. The only thing i know that could do what you want is using an
more on std::map (msdn.microsoft.com)
hope that helps !
matchu
Quote:
void GameEngine::Get_Sprites(std::string1, std::string2) {
BITMAP *std::string1 = load_bitmap("std::string1, std::string2", NULL);
}
noticed the std::string1 ?
it is impossible to create new named variables at runtime like this. The only thing i know that could do what you want is using an
std::map<std::string, BITMAP *>
more on std::map (msdn.microsoft.com)
hope that helps !
matchu
ah, I get what that function does now thank you.
the function would load my bitmaps at the initialization of the program and allow me to load more at a later time if wanted.. or reuseable code.
for instance.. let me see if i have this understood.
usage:
Well.. I thought I knew where I was going but I don't understand stdlib string functions very well. How would I go about concatenating them?
Well anyway that is the idea of the function.
the function would load my bitmaps at the initialization of the program and allow me to load more at a later time if wanted.. or reuseable code.
for instance.. let me see if i have this understood.
class GameEngine { private: std::string file_name; std::string file_ext; std::string concat; public: void Get_Sprites(std::string file_name, std::string file_ext);};extern GameEngine geC;
void GameEngine::Get_Sprites(std::string file_name, std::string file_ext) { BITMAP *file_name.c_str(); ~need to concatenate file_name + "." + file_ext~ file_name.c_str() = load_bitmap(concat.c_str(), NULL);}
usage:
std::string files[40];std::string ext = "bmp";int n = 39;for(n;n>-1;n--){ geC.Get_Sprites(files[n], ext);}
Well.. I thought I knew where I was going but I don't understand stdlib string functions very well. How would I go about concatenating them?
Well anyway that is the idea of the function.
file_name.c_str() = load_bitmap(concat.c_str(), NULL);
c_str() returns a constant value, not a variable you can assign to. First and foremost, your code is incorrect C++, so whatever you're doing, let's get it in general terms and then code the algorithm from there, because what you've got now is just plain flat-out impossible. You can assign to file_name, but not file_name.c_str(). The filename usually already has the file extension on it, but if you really need to add it, it's just file_name += ".bmp".If you want to be able to load bitmaps and remember their filenames as well to display them, you could make a class consisting of a bitmap pointer and a string. You could also make a std::vector or std::list of them if you plan on creating a variable number of them on the fly. You could also store them in a std::map, using the filename for a key and the bitmap for the value. It all works, depending on what exactly you're driving at. But the above posters are correct; you can't just make new variables at runtime. That's what arrays and (better yet) container classes are for.
[shameless plug]
Here, read these.
http://pixwiki.bafsoft.com/mags/5/articles/stl/part1.html
http://pixwiki.bafsoft.com/mags/6/articles/stl2/part1.html
http://pixwiki.bafsoft.com/mags/8/articles/stl3/stl3.html
http://pixwiki.bafsoft.com/mags/8/articles/strings/page01.html
[/shameless plug]
So more like..
[Edited by - Lempface on June 7, 2006 6:54:55 PM]
class GameEngine { private: std::string file_name; std::string file_ext; std::string concat; BITMAP *spritesbmp[40]; int n1; public: void Get_Sprites(std::string file_name, std::string file_ext, int n1);};extern GameEngine geC;
void GameEngine::Get_Sprites(const std::string &file_name,const std::string &file_ext, int &n1) { concat = file_name + "." + file_ext; spritesbmp[n1] = load_bitmap(concat.c_str(), NULL);}
std::string files[40];std::string ext = "bmp";int n = 0;for(n;n<40;n++) geC.Get_Sprites(files[n], ext, n);
[Edited by - Lempface on June 7, 2006 6:54:55 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement