Anyways, been having trouble with my program because it still lets the player pick the same spot again after its already been filled, the debugger is not helping due to my poor knowledge with it. I am almost positive that the problem is in the handle_events() function, the debugger tells me that the bool value is returning true to check if the spot is taken but a simple if statement is still letting the player pick the same spot, occasionally if you keep clicking it won't let pick the same spot. Criticism well appreciated on any other bad coding in the program.
Source (Scroll to handle events for the problem):
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <sstream>
using namespace std;
const int SCREEN_H = 480;
const int SCREEN_W = 755;
const int SCREEN_BPP = 32;
SDL_Surface *current_turn = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *target_X = NULL;
SDL_Surface *target_O = NULL;
SDL_Event event;
bool Turn = true;
class Target
{
private:
SDL_Rect box;
public:
bool taken;
Target(int x,int y,int h, int w);
void handle_events();
};
SDL_Surface *clarify_image( std::string filename)
{
SDL_Surface *loaded_image = NULL;
SDL_Surface *optimized_image = NULL;
loaded_image = IMG_Load( filename.c_str());
if ( loaded_image != NULL)
{
optimized_image = SDL_DisplayFormat( loaded_image );
SDL_FreeSurface(loaded_image);
if (optimized_image != NULL)
{
Uint32 colorkey = SDL_MapRGB(optimized_image->format,0,0xFF,0xFF);
SDL_SetColorKey( optimized_image,SDL_SRCCOLORKEY,colorkey);
}
}
return optimized_image;
}
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, NULL,destination, &offset);
}
bool load_files()
{
target_X = clarify_image("target_X.png");
target_O = clarify_image("target_O.png");
background = clarify_image("background.png");
current_turn = target_X;
if ((background == NULL) || (target_X == NULL) || (target_O == NULL)) {return false;}
else
return true;
}
bool init()
{
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) { return false;}
screen = SDL_SetVideoMode( SCREEN_W, SCREEN_H, SCREEN_BPP, SDL_SWSURFACE );
if( screen == NULL ) {return false;}
SDL_WM_SetCaption( "Jake's program", NULL );
if(TTF_Init() == -1) {return false;}
return true;
}
void cleanup()
{
SDL_FreeSurface( background );
SDL_FreeSurface( target_X );
SDL_FreeSurface( target_O );
SDL_Quit();
}
Target::Target(int x,int y,int w,int h)
{
box.x = x;
box.y = y;
box.w = w;
box.h = h;
}
void Target::handle_events()
{
int x = 0,y = 0;
if (event.type == SDL_MOUSEBUTTONDOWN)
{
x = event.motion.x;
y = event.motion.y;
if( event.button.button == SDL_BUTTON_LEFT )
{
x = event.button.x;
y = event.button.y;
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
if (Turn == true)
{
if (Target::taken == false)
{
apply_surface(box.x,box.y,target_X,screen);
Turn = false;
Target::taken = true;
}
}
else
if (Turn == false)
{
if (Target::taken == false)
{
apply_surface(box.x,box.y,target_O,screen);
Turn = true;
Target::taken = true;
}
}
else
if (Target::taken == true)
{
if (Turn == true)
{
Turn = false;
}
else
if (Turn == false)
{
Turn = true;
}
}
}
}
}
}
int main ( int argc, char *args[] )
{
//check for errors
bool quit = false;
if( init() == false ) { return 1;}
if( load_files() == false ) { return 2;}
apply_surface( 0,0,background,screen);
//game loop
while (quit == false)
{
Target myTarget1(2,2,247,119);
Target myTarget2(251,2,247,119);
Target myTarget3(502,2,247,119);
Target myTarget4(2,127,247,119);
Target myTarget5(251,127,247,119);
Target myTarget6(502,127,247,119);
Target myTarget7(2,249,247,119);
Target myTarget8(251,249,247,119);
Target myTarget9(502,249,247,119);
myTarget1.taken = false;
myTarget2.taken = false;
myTarget3.taken = false;
myTarget4.taken = false;
myTarget5.taken = false;
myTarget6.taken = false;
myTarget7.taken = false;
myTarget8.taken = false;
myTarget9.taken = false;
if(SDL_PollEvent(&event))
{
myTarget1.handle_events();
myTarget2.handle_events();
myTarget3.handle_events();
myTarget4.handle_events();
myTarget5.handle_events();
myTarget6.handle_events();
myTarget7.handle_events();
myTarget8.handle_events();
myTarget9.handle_events();
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
if (SDL_Flip( screen ) == -1)
{
return 3;
}
}
//cleanup
cleanup();
return 0;
}