load_bitmap problem in allegro
DrummaBoyFB

Please I need help with this I asked yesterday and I got no reply
Here's my problem
I got a Player Class

// Player.h
class Player {
Player (const char &name , int WalkFrames);
BITMAP *WalkImages [12];
};

// Player.cpp
#include <allegro>
#include "Player.h"

Player::Player (const char &name , int WalkFrames) {

for (int i = 0 ; i < WalkFrames ; i++) {
WalkImages [i] = load_bitmap (name + "Walk" + i + ".bmp",NULL); // heres the problem

}

}

whenever I do this, the code won't compile. and it keeps on saying
expression must have integral or enum type what can I do to fix this?

kingcools

try one thing please:

combine this expression: "name + "Walk" + i + ".bmp"" to one string before using it in the function and then try to use that string for loading the bitmap.
As far as i remember i had a similar problem doing this and i fixed it by using strings.

edit:
strings have the method "c_str()" which returns the string as a char* type, which allows using it in functions that only take that type as a parameter(which is the case with loadbitmap as far as i remember)

Jeff Bernard

To elaborate on kingcools' post, C and C++ don't have an integral string type. C++ uses std::string and C uses a char array (sometimes called a C-style string).

So, when you surround something with double quotes, it's actually an array of `char`s, so the operator+ is not going to do what you want. You want to use C++'s std::string class instead of const char& (which isn't even a C-style string, it's a constant reference to a char).

LennyLen

I posted this in the original thread the OP made on this topic (DrummaBoyFB: Making two threads about the same problem is a good way of getting yourself ostracized, so don't do it.), but here it is for this one...

The problem is that the first parameter of load_bitmap() is supposed to be a const char * (also known as a string in C, which is different from a C++ string) and what you're passing to it isn't. If you want to dynamically load a bitmap, you will need to construct the string yourself, then pass that to the load_bitmap() function.

Here's an example of how you can do that in C++:

std::stringstream str;
str << name << "Walk" << i << ".bmp";
    
WalkImage[i] = load_bitmap((str.str()).c_str(), NULL);
if(!WalkImage [i]) {

    allegro_message("Error loading %s.", (str.str()).c_str());
    exit(EXIT_FAILURE);

}

The C method of doing this is actually simpler, IMO:

char filename[256];
sprintf(filename, "%sWalk%d.bmp", name, i);
WalkImage[i] = load_bitmap(filename, NULL);
if(!WalkImage [i]) {

    allegro_message("Error loading %s.", (filename);
    exit(EXIT_FAILURE);

}

bamccaig
LennyLen said:

The C method of doing this is actually simpler, IMO...

Using Boost's Format library can even the score a little bit:

#SelectExpand
1#include <allegro.h> 2#include <boost/format.hpp> 3#include <stdexcept> 4 5... 6 7std::string filename = boost::format("%sWalk%d.bmp") % name % i; 8 9WalkImage[i] = load_bitmap(filename.c_str(), 0); 10 11if(!WalkImage[i]) 12{ 13 throw std::runtime_error(boost::format( 14 "Failed to load '%s'!" % filename); 15}

On the one hand, the C is perhaps easier to read (at least, for a C programmer), but the C++ is safer and not much worse (if not better). Note that I used the printf style of format, but Boost supports a few others (one of them being its own format, which is safer than the printf-style, but I'm most familiar with that so it's more likely to be used correctly[1]).

The OP shouldn't be worrying about all of this yet. He should be following some simple C or C++ tutorials that cover strings, which can be a pretty difficult subject in both languages, especially coming from higher-level languages (and his use of + as a concatenation operator suggests just that). You'll get to Allegro in no time, but first you need to understand C and/or C++. If nothing else, look into a C string tutorial (you'll need this to use Allegro because it's a C library). Then, if you want, find a C++ std::string tutorial, preferably one that covers std::stringstream also.

References

  1. Though untested.
Jeff Bernard

Overloading mod like that looks weird.

bamccaig

Overloading mod like that looks weird.

Yes, but so does overloading bit shift operators for IO and the addition operator for concatenation. :) As long as the meaning is clear (I'm sure they chose % because it's used for placeholders in format strings) I think that it works fine. And it's even a little bit more 1337 than a plain old method call. ;)

Jeff Bernard

I don't think overloading plus for concatenation looks weird. I want to make a new string that is string one plus string two. std::string newStr = str1+str2, it makes perfect sense. I think it makes more sense than that boost alternative, which is basically printf style, because it's inline from left to right.

I do agree that the bitshift operator overloading is weird. I guess they just wanted something that was like an arrow.

Thread #604785. Printed from Allegro.cc