Tic-Tac-Toe help in SDL

Started by
3 comments, last by kohma 16 years, 3 months ago
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);

		SDL_FreeSurface(loadedImage);

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

	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;

	SDL_BlitSurface(source,clip,destination,&offset);
}

bool init()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
		return false;

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

	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()
{
	SDL_FreeSurface(background);
	SDL_FreeSurface(pieceX);

	SDL_Quit();
}

// 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
	init_tiles();

	while(quit == false)
	{
		while(SDL_PollEvent(&event))
		{
			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
		apply_surface(0,0,background,screen);

		for(int i = 0; i < 9; ++i)
		{
			if((x > board_tiles.x) && (x < board_tiles.w) && (y > board_tiles.y) && (y < board_tiles.h))
			{
				apply_surface(board_tiles.x,board_tiles.y,pieceX,screen);
			}
		}

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

	}

	clean_up();

	return 0;
}


Advertisement
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. :)
Hi,

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 :)
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.
Just move your apply_surface(...,background...) function call out of your outer loop. This way you do not draw over your previous Xs ;)

This topic is closed to new replies.

Advertisement