I've been trying to make a synthesis game for my Organic Chemistry class, but when I try to draw some buttons on the screen I get a segmentation fault. I am using some functions for loading taken from Tim Jones's tutorial. I have built a Button class that has the size, position and image associated with the button. I plan to use this as my superclass so that I can create more buttons with more functionality. The problem is that when I initialise my button and configure it, I get a segmentation fault. By using the debugger I found out the program crashes when I try to load the image for the button. I include some relevant files with this post to see if anyone can help me. It's been a while since I've coded anything so any other comment/suggestion regarding style or program structure are welcome.
Here is the Button.h file:
# ifndef BUTTON_H
#define BUTTON_H
#include <SDL.h>
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <string>
#include "Logger.h"
#include "CSurface.h"
class Button
{
protected:
int Width;
int Height;
int X;
int Y;
SDL_Surface* Picture;
Logger Log;
bool IsActive;
public:
Button();
bool Configure(int width, int height, int x, int y, char PictureFile[20], bool Active);
~Button();
int GetWidth();
int GetHeight();
int GetX();
int GetY();
void ChangePicture();
void Activate();
bool DrawButton(SDL_Surface* Destination);
//virtual void Function1();
//virtual void Function2();
};
#endif
Here is the Button.cpp file:
#include "Button.h"
bool Button::Configure(int width, int height, int x, int y, char* PictureFile, bool Active)
{
Width=width;
Height=height;
X=x;
Y=y;
if((Picture = CSurface::OnLoad(PictureFile)) == NULL) {Log<<"No hay foto";
return false;
}
IsActive=true;
}
Button::Button(){
Width=0;
Height=0;
X=0;
Y=0;
Picture=NULL;
IsActive=false;
}
Button::~Button()
{
SDL_FreeSurface(Picture);
}
int Button::GetWidth()
{
return Width;
}
int Button::GetHeight()
{
return Height;
}
int Button::GetX()
{
return X;
}
int Button::GetY()
{
return Y;
}
bool Button::DrawButton(SDL_Surface* Destination)
{
if (IsActive){
return CSurface::OnDraw(Destination,Picture,X,Y);
Log<<"NO llego hasta el if";}
}
I will also include the main App files:
#ifndef _CAPP_H_
#define _CAPP_H_
#include <SDL.h>
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "CEvent.h"
#include "CSurface.h"
#include "Logger.h"
#include "Button.h"
//==============================================================================
class CApp : public CEvent {
private:
bool Running;
SDL_Surface* Surf_Display;
SDL_Surface* Surf_Test;
Logger Log;
Button StartButton;
public:
CApp();
int OnExecute();
public:
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnExit();
void OnLoop();
void OnRender();
void OnCleanup();
};
//==============================================================================
#endif
//==============================================================================
#include "CApp.h"
//==============================================================================
CApp::CApp() {
Surf_Test = NULL;
Surf_Display = NULL;
Running = true;
StartButton.Configure(134, 10, 500, 500,"PlayGameButton.png", true);
}
//------------------------------------------------------------------------------
int CApp::OnExecute() {
if(OnInit() == false) {
return -1;
}
SDL_Event Event;
while(Running) {
while(SDL_PollEvent(&Event)) {
OnEvent(&Event);
}
OnLoop();
OnRender();
}
OnCleanup();
return 0;
}
//==============================================================================
int main(int argc, char* argv[]) {
CApp theApp;
return theApp.OnExecute();
}
Just in case here is the image load function
SDL_Surface* CSurface::OnLoad( char* filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 255, 9, 255 ) );
}
}
//Return the optimized surface
return optimizedImage;
}
Thank you.
Thank you all for your help and if there is another file youneed, just ask