Code:
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <string>
using namespace std;
//if the user has not quit
bool quit = false;
//intergers
int x;
int y;
int player = 1;
//font stuff
TTF_Font *font = NULL;
SDL_Color textColor = {43, 250, 250};
//surfaces
SDL_Surface *screen;
SDL_Surface *background;
SDL_Surface *writing;
SDL_Surface *O;
SDL_Surface *X;
SDL_Surface *b1;
SDL_Surface *b2;
SDL_Surface *b3;
SDL_Surface *b4;
SDL_Surface *b5;
SDL_Surface *b6;
SDL_Surface *b7;
SDL_Surface *b8;
SDL_Surface *b9;
//event
SDL_Event event;
void handle_events(){
if(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT){
quit = true;
}
if(event.type == SDL_MOUSEMOTION){
x = event.motion.x;
y = event.motion.y;
}
if(event.type == SDL_MOUSEBUTTONUP){
if(event.button.button == SDL_BUTTON_LEFT){
if(x <= 200 && y <= 147){
if(b1 == NULL){
if(player == 1){
b1 = X;
player = 2;
}
else if(player == 2){
b1 = O;
player = 1;
}
}
}
if(x <= 411 && x >= 213 && y <= 147){
if(b2 == NULL){
if(player == 1){
b2 = X;
player = 2;
}
else if(player == 2){
b2 = O;
player = 1;
}
}
}
if(x <= 624 && x >= 424 && y <= 147){
if(b3 == NULL){
if(player == 1){
b3 = X;
player = 2;
}
else if(player == 2){
b3 = O;
player = 1;
}
}
}
if(x <= 200 && y <= 305 && y >= 160){
if(b4 == NULL){
if(player == 1){
b4 = X;
player = 2;
}
else if(player == 2){
b4 = O;
player = 1;
}
}
}
if(x <= 410 && x >= 213 && y <= 305 && y >= 160){
if(b5 == NULL){
if(player == 1){
b5 = X;
player = 2;
}
else if(player == 2){
b5 = O;
player = 1;
}
}
}
if(x <= 623 && x >= 423 && y <= 307 && y >= 160){
if(b6 == NULL){
if(player == 1){
b6 = X;
player = 2;
}
else if(player == 2){
b6 = O;
player = 1;
}
}
}
if(x <= 199 && y <= 464 && y >= 318){
if(b7 == NULL){
if(player == 1){
b7 = X;
player = 2;
}
else if(player == 2){
b7 = O;
player = 1;
}
}
}
if(x <= 410 && x >= 212 && y <= 465 && y >= 318){
if(b8 == NULL){
if(player == 1){
b8 = X;
player = 2;
}
else if(player == 2){
b8 = O;
player = 1;
}
}
}
if(x <= 623 && x >= 423 && y <= 466 && y >= 320){
if(b9 == NULL){
if(player == 1){
b9 = X;
player = 2;
}
else if(player == 2){
b9 = O;
player = 1;
}
}
}
}
}
}
}
SDL_Surface *load_image(string filename){
SDL_Surface *surface = NULL;
surface = IMG_Load(filename.c_str());
return surface;
}
bool load_files(){
//open font
font = TTF_OpenFont("lazy.ttf", 28);
if(font == NULL){
return false;
}
return true;
}
void apply_surface(int X, int Y, SDL_Surface *image, SDL_Surface *destination){
SDL_Rect offset;
offset.x = X;
offset.y = Y;
SDL_BlitSurface(image, NULL, destination, &offset);
}
bool init(){
if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
return false;
}
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
if(screen == NULL){
return false;
}
if(TTF_Init() == -1){
return false;
}
SDL_WM_SetCaption("Tic Tac Toe", NULL);
return true;
}
void clean_up(){
//free surface
SDL_FreeSurface(background);
SDL_FreeSurface(O);
SDL_FreeSurface(X);
SDL_FreeSurface(b1);
SDL_FreeSurface(b2);
SDL_FreeSurface(b3);
SDL_FreeSurface(b4);
SDL_FreeSurface(b5);
SDL_FreeSurface(b6);
SDL_FreeSurface(b7);
SDL_FreeSurface(b8);
SDL_FreeSurface(b9);
TTF_CloseFont(font);
SDL_Quit();
TTF_Quit();
}
int main(int argc, char *args[]){
if(init() == false){
return 1;
}
if(load_files == false){
return 1;
}
background = load_image("background.png");
X = load_image("X.png");
O = load_image("O.png");
if(background == NULL){
return 1;
}
while(!quit){
if(player == 1){
writing = TTF_RenderText_Solid(font, "It is Player 1's turn.", textColor);
if(writing == NULL){
return 1;
}
while(player == 1){
apply_surface(234, 18, writing, screen);
}
SDL_FreeSurface(writing);
}
else if(player == 2){
writing = TTF_RenderText_Solid(font, "It is Player 2's turn.", textColor);
if(writing == NULL){
return 1;
}
while(player == 2){
apply_surface(234, 18, writing, screen);
}
SDL_FreeSurface(writing);
}
handle_events();
//make screen color
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
//draw picture on screen
apply_surface(0, 0, background, screen);
apply_surface(0,0, b1, screen);
apply_surface(213, 0, b2, screen);
apply_surface(424, 0, b3, screen);
apply_surface(0, 160, b4, screen);
apply_surface(213, 160, b5, screen);
apply_surface(423, 160, b6, screen);
apply_surface(0, 318, b7, screen);
apply_surface(212, 318, b8, screen);
apply_surface(423, 320, b9, screen);
if(SDL_Flip(screen) == -1){
return 1;
}
}
clean_up();
return 0;
}
something that might help pin point the problem is that the code worked fine until I added the text things.