Jump to content
  • Advertisement
Sign in to follow this  

Tic-Tac-Toe help in SDL

This topic is 3797 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

I am creating a simple game of Tic-Tac-Toe in SDL and I have been through about half the tutorials on lazy foo's site and decided to give a go at creating Tic-Tac-Toe with SDL. I first started with just basic function just displayed a background I made with paint that is a Tic-Tac-Toe board and just a X piece. And i took the user input in which he clicked on just the top left sqaure to see if I could get the piece to display when the user clicked on that specific spot on the board. I got that to work. Now I addeded in functionality for the other squares but I can get the program to compile but it crashes and I dont know whats wrong. I'm not to great with the debugger and honstley dont know how to use it to figure out these problems. I also get this message when I try to use it to step into the program. No symbols are loaded from any call stack frame. The source code cannot be loaded. And I have no idea what that means. Also another problem I have with my progarm is that when I apply a piece to the board in my first run is that when the user clicked any where else after they have done there first piece the piece would then disspaer. I was wondering if anyone could help. This is going to be my first game in SDL or with any graphics API. I still havn't added functionally for determining the winnder or anything like that and its gonna be person vs person not person vs computer. I have done enough just to get pieces on the board and thats it. Any comments or suggestion would be appericated. Thank you.
// headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>

// constants for screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

// the event structure
SDL_Event event;

// the tic-tac-toe board squares
SDL_Rect board_tiles[8];

// the surface
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *pieceX = NULL;

// load the images into the surfaces
SDL_Surface *load_image(std::string filename)
	SDL_Surface *loadedImage = NULL;
	SDL_Surface *optimizedImage = NULL;

	loadedImage = IMG_Load(filename.c_str());

	if(loadedImage != NULL)
		optimizedImage = SDL_DisplayFormat(loadedImage);


		if(optimizedImage != NULL)

	return optimizedImage;

void apply_surface(int x, int y, SDL_Surface *source,SDL_Surface *destination,SDL_Rect *clip = NULL)
	SDL_Rect offset;

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


bool init()
		return false;


	if(screen == NULL)
		return false;

	SDL_WM_SetCaption("Tic-Tac-Toe 1.0",NULL);

	return true;

bool load_files()
	background = load_image("background.png");

	pieceX = load_image("pieceX.png");

	if(background == NULL)
		return false;

	if(pieceX == NULL)
		return false;

	return true;

void clean_up()


// initalizes all the square piece of the Tic-Tac-Toe board
void init_tiles()
	// top left
	board_tiles[0].x = 80;
	board_tiles[0].y = 115;
	board_tiles[0].w = 165;
	board_tiles[0].h = 185;

	// top middle
	board_tiles[1].x = 180;
	board_tiles[1].y = 115;
	board_tiles[1].w = 345;
	board_tiles[1].h = 185;

	// top right
	board_tiles[2].x = 350;
	board_tiles[2].y = 115;
	board_tiles[2].w = 440;
	board_tiles[2].h = 185;

	// middle left
	board_tiles[3].x = 75;
	board_tiles[3].y = 190;
	board_tiles[3].w = 170;
	board_tiles[3].h = 280;

	// middle middle
	board_tiles[4].x = 175;
	board_tiles[4].y = 190;
	board_tiles[4].w = 345;
	board_tiles[4].h = 280;

	// middle right
	board_tiles[5].x = 350;
	board_tiles[5].y = 190;
	board_tiles[5].w = 440;
	board_tiles[5].h = 280;

	// bottom left
	board_tiles[6].x = 75;
	board_tiles[6].y = 285;
	board_tiles[6].w = 165;
	board_tiles[6].h = 340;

	// bottom middle
	board_tiles[7].x = 175;
	board_tiles[7].y = 285;
	board_tiles[7].w = 345;
	board_tiles[7].h = 340;
	// bottom right
	board_tiles[8].x = 350;
	board_tiles[8].y = 285;
	board_tiles[8].w = 445;
	board_tiles[8].h = 340;

int main(int argc, char *args[])

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

	bool quit = false;

	// initalize SDL
	if(init() == false)
		return 1;

	// initalize and load all the files for us
	if(load_files() == false)
		return 1;

	// load the Tic-Tac-Toe board

	while(quit == false)
			if(event.type == SDL_MOUSEBUTTONDOWN)
				if(event.button.button == SDL_BUTTON_LEFT)
					x = event.button.x;
					y = event.button.y;

			else if(event.type == SDL_QUIT)
				quit = true;

		// apply the background

		for(int i = 0; i < 9; ++i)
			if((x > board_tiles.x) && (x < board_tiles.w) && (y > board_tiles.y) && (y < board_tiles.h))

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



	return 0;

Share this post

Link to post
Share on other sites
Just a bit of advice from someone who's been there (though not with SDL).

Write a command line version first, using printf() to display the board. When you've got the kinks worked out, THEN use SDL to make a gui.

One problem at a time. :)

Share this post

Link to post
Share on other sites

I just tried your code.

First of all, you made a big but simple mistake. In init_tiles() you init 9 tiles (which makes sense) but you only declared an array of 8 tiles, so you should get a segmentation fault.

Another point:
though I never used SDL, I think you have to rethink your init_tiles() function. Usually you define the rectangle width and height not in absolute coordinates. So e.g. a rect which covers the space from (100,100) to (200,200) has not the width/height 200 but 100.

Hope I could help you :)

Share this post

Link to post
Share on other sites
wow big mistake I wantn't thinking when I definied my array. I fixed that and my program dosn't crash and everything is working it's just that the surfaces wont stick and I'm not sure why. I apply the surface when the user clicks and then when they select another box. I'm not sure why its doing this any suggestions.

Share this post

Link to post
Share on other sites
Just move your apply_surface(...,background...) function call out of your outer loop. This way you do not draw over your previous Xs ;)

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!