|
Seg fault when creating bitmap outside of main |
ntaylorbishop
Member #15,129
May 2013
|
So I have a a Player class that holds an ALLEGRO_BITMAP pointer, and in the constructor, I Have: 1Player::Player(float mass, int size, int screenWidth, int screenHeight) {
2 //Starting pos
3 this->mass = mass;
4 this->spriteSize = size;
5 SCREEN_W = screenWidth;
6 SCREEN_H = screenHeight;
7 xStartPos = SCREEN_W / 2.0 - size / 2.0;
8 yStartPos = SCREEN_H / 2.0 - size / 2.0;
9
10 //Sprite set
11 sprite = al_create_bitmap(spriteSize, spriteSize);
12
13 if(!sprite)
14 cout << "ERROR 20: COULD NOT CREATE PLAYER SPRITE!" << endl;
15}
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! |
Thomas Fjellstrom
Member #476
June 2000
|
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. ... }
-- |
ntaylorbishop
Member #15,129
May 2013
|
Thank you! The API failed to mention this but I guess it should've been obvious..appreciate the really quick reply! |
Thomas Fjellstrom
Member #476
June 2000
|
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: Quote: 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). -- |
|