So I have a a Player class that holds an ALLEGRO_BITMAP pointer, and in the constructor, I Have:
and in main I have:
al_set_target_bitmap(player.getSprite());
getSprite passes the ALLEGRO_BITMAP pointer by reference when called, and it seg faults before it even gets the chance to to get to the if(!sprite) line in the constructor. What is going on?
Thanks for the help!
Make sure the constructor for Player isn't being called before al_init() and if you want the bitmap to draw fast, make sure its called after al_create_display as well.
One thing some people don't know, when you create an object on the stack in a function like below, it calls the constructor before the code in the function runs:
void foo() { Player player; // constructor is called when foo is entered al_init(); // allegro is initialized here, after the player object is constructed. ... }
Thank you! The API failed to mention this but I guess it should've been obvious..appreciate the really quick reply!
I think the api mentions you can't call most/all allegro functions before al_init has been called.
In fact, on the docs for al_init, it says its just like al_install_system, and `al_install_system`s docs say:
Initialize the Allegro system. No other Allegro functions can be called before this (with one or two exceptions).
Also, if you create bitmaps before you've created a display, you'll end up with memory bitmaps, which can be slower than normal video bitmaps (they are stored as textures on the video card, so are really fast to draw).