• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
DaSimile

SDL Running Bugs

1 post in this topic

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]
0

Share this post


Link to post
Share on other sites
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().
1

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  
Followers 0