Sign in to follow this  
DaSimile

SDL Running Bugs

Recommended Posts

DaSimile    106
When I run my program all it does is run for about 0.235 or so seconds and the the program closes itself, any help on finding the possible bug would be greatly appreciated. The code itself was much of my own but some parts were puzzle pieced together from around the internet.

[source lang="cpp"]#include"SDL/SDL.h"
#include"SDL/SDL_image.h"
#include<string>
using namespace std;

//video attribuets
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

//surface
SDL_Surface* screen = NULL;
SDL_Surface* background = NULL;
SDL_Surface* All = NULL;

//events
SDL_Event event;

//rectangle
SDL_Rect board[9];

SDL_Surface* load_image(string filename)
{
//temporarily store image
SDL_Surface* loadedImage = NULL;

//optimize Image
SDL_Surface* optimizedImage = NULL;

//load image
loadedImage = IMG_Load(filename.c_str());

if(loadedImage != NULL)
{
//optimize Image
optimizedImage = SDL_DisplayFormat(loadedImage);

//free old surface
SDL_FreeSurface(loadedImage);

if(optimizedImage != NULL)
{
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}

//return image
return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
//create rectangel
SDL_Rect offset;

offset.x = x;
offset.y = y;

//blit screen
SDL_BlitSurface(source, NULL, destination, &offset);
}

bool init()
{
//initlize everyting
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}

screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

//if screen didn't work
if(screen == NULL)
{
return false;
}

//Set window caption
SDL_WM_SetCaption( "Jakobs Final Project: Tic Tac Toe", NULL );

//if no errors
return true;
}

bool load_files()
{
background = load_image("board.png");
All = load_image("null.png");

if(background == NULL)
{
return false;
}

if(All == NULL)
{
return false;
}

//if no errors
return true;
}

void clean_up()
{
//quit
SDL_Quit();
}

void set_clips()
{
board[ 0 ].x = 0;
board[ 0 ].y = 0;
board[ 0 ].w = 200;
board[ 0 ].h = 200;

board[ 1 ].x = 200;
board[ 1 ].y = 0;
board[ 1 ].w = 200;
board[ 1 ].h = 200;

board[ 2 ].x = 400;
board[ 2 ].y = 0;
board[ 2 ].w = 200;
board[ 2 ].h = 200;

board[ 3 ].x = 0;
board[ 3 ].y = 200;
board[ 3 ].w = 200;
board[ 3 ].h = 200;

board[ 4 ].x = 200;
board[ 4 ].y = 200;
board[ 4 ].w = 200;
board[ 4 ].h = 200;

board[ 5 ].x = 400;
board[ 5 ].y = 200;
board[ 5 ].w = 200;
board[ 5 ].h = 200;

board[ 6 ].x = 0;
board[ 6 ].y = 400;
board[ 6 ].w = 200;
board[ 6 ].h = 200;

board[ 7 ].x = 200;
board[ 7 ].y = 400;
board[ 7 ].w = 200;
board[ 7 ].h = 200;

board[ 8 ].x = 400;
board[ 8 ].y = 400;
board[ 8 ].w = 200;
board[ 8 ].h = 200;
}

class PicX
{
private:
SDL_Surface* m_pImage; //pointer to the image
SDL_Rect* pSurface[9];

public:
void testImage(int x, int y, SDL_Surface* destination);
void handle_events();
bool checkWinner();

PicX();
};

PicX::PicX()
{
m_pImage = load_image("x.png");
}

bool PicX::checkWinner()
{
if( (pSurface[0] == &board[0]) && (pSurface[1] == &board[1]) && (pSurface[2] == &board[2]) )
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[3] == &board[3]) && (pSurface[4] == &board[4]) && (pSurface[5] == &board[5]))
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[6] == &board[6]) && (pSurface[7] == &board[7]) && (pSurface[8] == &board[8]) )
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[0] == &board[0]) && (pSurface[3] == &board[3]) && (pSurface[6] == &board[6]) )
{
apply_surface(325, 320, All, screen);
return true;
}
if( (pSurface[1] == &board[1]) && (pSurface[4] == &board[4]) && (pSurface[7] == &board[7]) )
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[2] == &board[2]) && (pSurface[5] == &board[5]) && (pSurface[8] == &board[8]) )
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[2] == &board[2]) && (pSurface[4] == &board[4]) && (pSurface[6] == &board[6]) )
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[0] == &board[0]) && (pSurface[4] == &board[4]) && (pSurface[8] == &board[8]) )
{
apply_surface(325, 320, All, screen);
return true;
}
}

void PicX::handle_events()
{
//mouse offset
int x = 0, y = 0;

if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
//get mouse offsets
x = event.motion.x;
y = event.motion.y;

if( (x > board[0].x) && (x < board[0].x + board[0].w) && (y > board[0].y) && (y < board[0].y + board[0].h) )
{
pSurface[0] = &board[0];
testImage(board[0].x, board[0].y, screen);
}

if( (x > board[1].x) && (x < board[1].x + board[1].w) && (y > board[1].y) && (y < board[1].y + board[1].h) )
{
pSurface[1] = &board[1];
testImage(board[1].x, board[1].y, screen);
}

if( (x > board[2].x) && (x < board[2].x + board[2].w) && (y > board[2].y) && (y < board[2].y + board[2].h) )
{
pSurface[2] = &board[2];
testImage(board[2].x, board[2].y, screen);
}

if( (x > board[3].x) && (x < board[3].x + board[3].w) && (y > board[3].y) && (y < board[3].y + board[3].h) )
{
pSurface[3] = &board[3];
testImage(board[3].x, board[3].y, screen);
}

if( (x > board[4].x) && (x < board[4].x + board[4].w) && (y > board[4].y) && (y < board[4].y + board[4].h) )
{
pSurface[4] = &board[4];
testImage(board[4].x, board[4].y, screen);
}

if( (x > board[5].x) && (x < board[5].x + board[5].w) && (y > board[5].y) && (y < board[5].y + board[5].h) )
{
pSurface[5] = &board[5];
testImage(board[5].x, board[5].y, screen);
}

if( (x > board[6].x) && (x < board[6].x + board[6].w) && (y > board[6].y) && (y < board[6].y + board[6].h) )
{
pSurface[6] = &board[6];
testImage(board[6].x, board[6].y, screen);
}

if( (x > board[7].x) && (x < board[7].x + board[7].w) && (y > board[7].y) && (y < board[7].y + board[7].h) )
{
pSurface[7] = &board[7];
testImage(board[7].x, board[7].y, screen);
}

if( (x > board[8].x) && (x < board[8].x + board[8].w) && (y > board[8].y) && (y < board[8].y + board[8].h) )
{
pSurface[8] = &board[8];
testImage(board[8].x, board[8].y, screen);
}
}
}
}

void PicX::testImage(int x, int y, SDL_Surface* destination)
{
//create rect offset
SDL_Rect offset;

offset.x = x;
offset.y = y;

//blit
SDL_BlitSurface(m_pImage, NULL, screen, &offset);
}

class PicO : public PicX
{
private:
SDL_Surface* m_pImage;
SDL_Rect* pSurface[9];

public:
void testImage(int x, int y, SDL_Surface* destination);
void handle_events();
bool checkWinner();

PicO();
};

PicO::PicO()
{
m_pImage = load_image("o.png");
}

void PicO::testImage(int x, int y, SDL_Surface* destination)
{
//create rect
SDL_Rect offset;

offset.x = x;
offset.y = y;

SDL_BlitSurface(m_pImage, NULL, destination, &offset);
}

bool PicO::checkWinner()
{
if( (pSurface[0] == &board[0]) && (pSurface[1] == &board[1]) && (pSurface[2] == &board[2]) )
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[3] == &board[3]) && (pSurface[4] == &board[4]) && (pSurface[5] == &board[5]))
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[6] == &board[6]) && (pSurface[7] == &board[7]) && (pSurface[8] == &board[8]) )
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[0] == &board[0]) && (pSurface[3] == &board[3]) && (pSurface[6] == &board[6]) )
{
apply_surface(325, 320, All, screen);
return true;
}
if( (pSurface[1] == &board[1]) && (pSurface[4] == &board[4]) && (pSurface[7] == &board[7]) )
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[2] == &board[2]) && (pSurface[5] == &board[5]) && (pSurface[8] == &board[8]) )
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[2] == &board[2]) && (pSurface[4] == &board[4]) && (pSurface[6] == &board[6]) )
{
apply_surface(325, 320, All, screen);
return true;
}

if( (pSurface[0] == &board[0]) && (pSurface[4] == &board[4]) && (pSurface[8] == &board[8]) )
{
apply_surface(325, 320, All, screen);
return true;
}
}

void PicO::handle_events()
{
//mouse offset
int x = 0, y = 0;

if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
//get mouse offsets
x = event.motion.x;
y = event.motion.y;

if( (x > board[0].x) && (x < board[0].x + board[0].w) && (y > board[0].y) && (y < board[0].y + board[0].h) )
{
pSurface[0] = &board[0];
testImage(board[0].x, board[0].y, screen);
}

if( (x > board[1].x) && (x < board[1].x + board[1].w) && (y > board[1].y) && (y < board[1].y + board[1].h) )
{
pSurface[1] = &board[1];
testImage(board[1].x, board[1].y, screen);
}

if( (x > board[2].x) && (x < board[2].x + board[2].w) && (y > board[2].y) && (y < board[2].y + board[2].h) )
{
pSurface[2] = &board[2];
testImage(board[2].x, board[2].y, screen);
}

if( (x > board[3].x) && (x < board[3].x + board[3].w) && (y > board[3].y) && (y < board[3].y + board[3].h) )
{
pSurface[3] = &board[3];
testImage(board[3].x, board[3].y, screen);
}

if( (x > board[4].x) && (x < board[4].x + board[4].w) && (y > board[4].y) && (y < board[4].y + board[4].h) )
{
pSurface[4] = &board[4];
testImage(board[4].x, board[4].y, screen);
}

if( (x > board[5].x) && (x < board[5].x + board[5].w) && (y > board[5].y) && (y < board[5].y + board[5].h) )
{
pSurface[5] = &board[5];
testImage(board[5].x, board[5].y, screen);
}

if( (x > board[6].x) && (x < board[6].x + board[6].w) && (y > board[6].y) && (y < board[6].y + board[6].h) )
{
pSurface[6] = &board[6];
testImage(board[6].x, board[6].y, screen);
}

if( (x > board[7].x) && (x < board[7].x + board[7].w) && (y > board[7].y) && (y < board[7].y + board[7].h) )
{
pSurface[7] = &board[7];
testImage(board[7].x, board[7].y, screen);
}

if( (x > board[8].x) && (x < board[8].x + board[8].w) && (y > board[8].y) && (y < board[8].y + board[8].h) )
{
pSurface[8] = &board[8];
testImage(board[8].x, board[8].y, screen);
}
}
}
}

class GameBoard
{
private:
PicO boardO;
PicX boardX;
int playerTurn;

public:
void playGame();
void checkWinner();

GameBoard(int turn = 1);
};

GameBoard::GameBoard(int turn):
playerTurn(turn)
{}

void GameBoard::playGame()
{
if(playerTurn == 1)
{
boardO.handle_events();
playerTurn += 1;
}

else if(playerTurn == 2)
{
boardX.handle_events();
playerTurn -= 1;
}
}

int main(int argc, char* args[])
{
bool quit = false;

if(init() == false)
{
return 1;
}

if(load_files() == false)
{
return 1;
}

set_clips();

GameBoard theBoard;

apply_surface(0, 0, background, screen);

//mouse offsets
int x = 0, y = 0;

while(quit == false)
{
//while there's evetns to handle
while(SDL_PollEvent(&event))
{
theBoard.playGame();

if(event.type == SDL_QUIT)
{
quit = true;
}

}


if(SDL_Flip(screen) == -1)
{
return 1;
}
}

clean_up();

return 0;

}[/source]

Share this post


Link to post
Share on other sites
juliano7s    121
Looking at your main() it seems to be quitting before the while(quit == false). Since init() is quite simple, check the files you're trying to load on load_files().

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this