Jump to content
  • Advertisement
Sign in to follow this  
Woad

C++ Allegro Problem

This topic is 4061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just started using Allegro and i ran into a problem. I'm using VS C++ Express. Sorry I don't see any tag helper. Here's the code:
#include <allegro.h>
#include <string>
#include <sstream>

BITMAP *AnimationSpr;
using namespace std;

int main(){
 
	int i = 1;
	string str;
	stringstream out;
	out << i;
	str = out.str();


    allegro_init();
    install_keyboard();
    set_color_depth(16);
    set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    
    AnimationSpr = load_bitmap( "animation" + str + ".bmp", NULL);

    acquire_screen();
    
    draw_sprite( screen, AnimationSpr, 320-32, 240-32);
        
    release_screen();

    readkey();
    
    return 0;
    
}   
END_OF_MAIN();
I get the errors/warnings. [size=7]c:\program files\microsoft visual studio 8\vc\include\allegro\internal\alconfig.h(397) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned char *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\internal\alconfig.h(404) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned char *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline\draw.inl(421) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned char *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline\draw.inl(435) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned char *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline\draw.inl(446) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned short *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline\draw.inl(460) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned short *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline\draw.inl(471) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned short *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline\draw.inl(485) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned short *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline\draw.inl(521) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned int *' of greater size c:\program files\microsoft visual studio 8\vc\include\allegro\inline\draw.inl(535) : warning C4312: 'type cast' : conversion from 'unsigned int' to 'unsigned int *' of greater size c:\documents and settings\woadraiders\my documents\allegrotest\allegrotest\main.cpp(22) : error C2664: 'load_bitmap' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'const char *' with [ _Elem=char, _Traits=std::char_traits<char>, _Ax=std::allocator<char> ] No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Share this post


Link to post
Share on other sites
Advertisement

string str;
...
str = out.str();
...
AnimationSpr = load_bitmap( "animation" + str + ".bmp", NULL);








The problem is that you're passing a std::string as the first argument to load_bitmap() when the first argument should be of type char*.

Do this instead:


stringstream out;
out << "animation" << i << ".bmp";
AnimationSpr = load_bitmap( out.str().c_str(), NULL);







The first thing you'll notice is that you don't really need the string object.
(In fact, I am not totally sure that operator + is defined with a left hand operand of type char* and a right hand operand of std::string as you had it in your original code, but I could be wrong).

out.str() returns a std::string object that contains the contents of your stringstream. Calling c_str() on the std::string() that was returned retuns the contents of the string as type char*

The warnings on the other hand are a different issue.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!