Seg fault when creating bitmap outside of main
So I have a a Player class that holds an ALLEGRO_BITMAP pointer, and in the constructor, I Have:
2 //Starting pos
5 SCREEN_W =
6 SCREEN_H =
xStartPos = SCREEN_W / 2
size / 2
yStartPos = SCREEN_H / 2
size / 2
10 //Sprite set
sprite = al_create_bitmap(
cout << "ERROR 20: COULD NOT CREATE PLAYER SPRITE!" <<
and in main I have:
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:
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!
Thread #612610. Printed from Allegro.cc
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).