[SOLVED] SDL problem no errors are being returned.
Posted: Thu Mar 31, 2011 8:23 pm
Ok so I am trying to make tictactoe and I am just trying to get an x to come up when you click the middle slot but nothing comes up and there are no errors sorry for long code and no idea were to look for error, I think its inside the handle_events function.
Code: Select all
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <sstream>
const int SCREEN_H = 480;
const int SCREEN_W = 640;
const int SCREEN_BPP = 32;
const int TARGET_X = 0;
const int TARGET_O = 1;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *target_X = NULL;
SDL_Surface *target_O = NULL;
SDL_Rect *clips[2];
SDL_Event event;
class Target
{
private:
SDL_Rect box;
public:
Target(int x,int y,int h, int w);
void handle_events();
void show();
};
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");
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_MOUSEMOTION)
{
x = event.motion.x;
y = event.motion.y;
if (event.type == SDL_MOUSEBUTTONDOWN)
{
x = event.motion.x;
y = event.motion.y;
if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )
{
apply_surface(box.x,box.y,target_X,screen);
}
}
}
}
void Target::show()
{
apply_surface(box.x,box.y,target_X,screen);
}
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);
Target myTarget(170,120,320,240);
myTarget.handle_events();
//game loop
while (quit == false)
{
if(SDL_PollEvent(&event))
{
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
if (SDL_Flip( screen ) == -1)
{
return 3;
}
}
//cleanup
cleanup();
return 0;
}