• Advertisement
Sign in to follow this  

Tic Tac Toe Graphics

This topic is 2145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


#include"SDL/SDL.h"
#include"SDL/SDL_image.h"
#include<string>
using namespace std;

//video attribuets
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
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;
}

//if no errors
return true;
}

bool load_files()
{
background = load_image("board.png");
All = load_image("Picture.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 = 175;
board[ 0 ].h = 155;

board[ 1 ].x = 182;
board[ 1 ].y = 0;
board[ 1 ].w = 175;
board[ 1 ].h = 155;

board[ 2 ].x = 367;
board[ 2 ].y = 0;
board[ 2 ].w = 175;
board[ 2 ].h = 155;

board[ 3 ].x = 0;
board[ 3 ].y = 160;
board[ 3 ].w = 175;
board[ 3 ].h = 155;

board[ 4 ].x = 182;
board[ 4 ].y = 160;
board[ 4 ].w = 175;
board[ 4 ].h = 155;

board[ 5 ].x = 367;
board[ 5 ].y = 160;
board[ 5 ].w = 175;
board[ 5 ].h = 155;

board[ 6 ].x = 0;
board[ 6 ].y = 325;
board[ 6 ].w = 175;
board[ 6 ].h = 155;

board[ 7 ].x = 182;
board[ 7 ].y = 325;
board[ 7 ].w = 175;
board[ 7 ].h = 155;

board[ 8 ].x = 367;
board[ 8 ].y = 325;
board[ 8 ].w = 175;
board[ 8 ].h = 155;
}

class PictureX
{
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();

PictureX();
};

PictureX::PictureX()
{
m_pImage = load_image("X.png");
}

bool PictureX::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 PictureX::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 PictureX::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 PictureO : public PictureX
{
private:
SDL_Surface* m_pImage;
SDL_Rect* pSurface[9];

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

PictureO();
};

PictureO::PictureO()
{
m_pImage = load_image("O.png");
}

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

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

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

bool PictureO::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 PictureO::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 TicTacBoard
{
private:
PictureO boardO;
PictureX boardX;
int playerTurn;

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

TicTacBoard(int turn = 1);
};

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

void TicTacBoard::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();

TicTacBoard myBoard;

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))
{
myBoard.playGame();

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

}

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

clean_up();

return 0;

}


So I'm trying to make a tic tac toe game using sdl, but I'm having troubles changing which player turn it is. It changes players turn but it doesn't do it properly. Like X will go 2 times then O goes once. Any idea how I could ? Also if there's way to make this code better let me know smile.png

Share this post


Link to post
Share on other sites
Advertisement
I didn't see your code fully yet, But for the mouse click, you could wrap everything in a function like,


bool MouseOnTop( int objectX, int objectY, int objectW, int objectH, int mouseX, int mouseY )
{
if( mouseX < objectX || mouseY < objectY )
{
return false;
}
if( mouseX > objectX+objectW || mouseY > objectY+objectH )
{
return false;
}

return true;
}

//And then call the function whenever wanted like, MouseOnTop(board[0].x, board[0].y .... whatever);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement