lang="en-US"> Error FIX: Segmentation Fault in Allegro OOP Classes –  Design1online.com, LLC

Error FIX: Segmentation Fault in Allegro OOP Classes

Okay so I’ve been setting up the skeleton of my code for this game. What I’m trying to do is setup one class that will manage all the resources of all other classes in the game. That way I can call one function to pause the game, start the game, launch menus, play sounds, and so forth and so on. To do this I’m making all the sub classes of the game write to a buffer in the manging class which I call the game engine. The game engine will then call other subclasses as the game is being played so the appropriate information is written to the buffer at different parts of the game. Everything was going great until I tried to allocate the bitmap in the game engine class using the create_bitmap(int width, int height) function.

Problematic Code

Game Engine .h File

#include <allegro.h> 
#include <linalleg.h> 

class gameegine { 

public: 
gameengine(); //default constructor 
BITMAP *getBuffer(); //returns the gameengine buffer 

private: 
BITMAP *buffer; 

};

Game Engine .cpp File

#include "gameengine.h" 

gameengine::gameengine() 
{ 

buffer; //this will work just fine 

buffer = create_bitmap(SCREENWIDTH, SCREENHEIGHT); //this crashes and burns 
clear(buffer); //this crashes and burns because buffer wasn't allocated 

}

Main .cpp File

#include <allegro.h> 
#include <linalleg.h> 
#include "gameengine.h" 

int main() 
{ 

init_alleg(); //start allegro 

gameengine *plex = new gameengine(); //start the game engine 

plex->loading(); //show the loading screen until everything is setup to start 

while (!key[KEY_ESC]) //play the game until ESC key is pressed 
{ 

scare_mouse(); //hide the mouse 

plex->draw(); //all the game stuff goes on in here 

vsync(); //remove flicker 
acquire_screen(); ///lock the screen for drawing 
show_mouse(plex->getBuffer()); //draw the mouse to the buffer 
blit(plex->getBuffer(), screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); //draw the buffer to the screen 
release_screen(); //unlock the screen 
unscare_mouse(); //show the mouse before 

} //the game is done 

if (plex->mode() == BUILDMODE) //they are in the online game 
plex->disconnect(); //disconnect from the database 

deinit(); //release all allocated resources 

} END_OF_MAIN()

What I found is that Allegro will let you declare bitmaps in classes but it can’t allocate them. When I try to allocate the bitmap I get a shared resources of threads when compiling (on my Linux box). After several days of tearing my hair out I finally found a way to get this working. Instead of allocating the bitmap I’m going to pass it into the class constructor. That way the allocation is done outside of the class itself. The same will have to be done with the graphics class which deals with loading and drawing the graphics of the game to the game engine buffer. Not exactly the optimal solution but I’m not ready to give up on using classes in this project yet. I would have to change a lot of the program’s skeleton structure if I converted it all over to c and quite frankly I don’t have the time for that. I have a lot to get done in only a few weeks!

Working Solution

Game Engine .h File

#include <allegro.h> 
#include <linalleg.h> 

class gameegine { 

public: 
gameengine(BITMAP *newbuffer); //default constructor 
BITMAP *getBuffer(); //returns the gameengine buffer 

private: 
BITMAP *buffer; 

};

Game Engine .cpp File

#include "gameengine.h" 

gameengine::gameengine(BITMAP *newbuffer) 
{ 

buffer = newbuffer; //now this all works 
clear(buffer);  

}

Main .cpp File

#include <allegro.h> 
#include <linalleg.h> 
#include "gameengine.h" 

int main() 
{ 

init_alleg(); //start allegro 
BITMAP *buffer = create_bitmap(SCREEN_W, SCREEN_H); 

gameengine *plex = new gameengine(buffer); //start the game engine 

plex->loading(); //show the loading screen until everything is setup to start 

while (!key[KEY_ESC]) //play the game until ESC key is pressed 
{ 

scare_mouse(); //hide the mouse 

plex->draw(); //all the game stuff goes on in here 

vsync(); //remove flicker 
acquire_screen(); ///lock the screen for drawing 
show_mouse(plex->getBuffer()); //draw the mouse to the buffer 
blit(plex->getBuffer(), screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); //draw the buffer to the screen 
release_screen(); //unlock the screen 
unscare_mouse(); //show the mouse before 

} //the game is done 

if (plex->mode() == BUILDMODE) //they are in the online game 
plex->disconnect(); //disconnect from the database 

deinit(); //release all allocated resources 

} END_OF_MAIN()

You may also like...

3 Responses

  1. Jogos Online says:

    sweet, a blog with code!

  2. Jogos says:

    This code dont compile on my Code::Block

Leave a Reply