Page 1 of 1

[SOLVED] SDL problem no errors are being returned.

Posted: Thu Mar 31, 2011 8:23 pm
by jakobnator
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;
}


Re: SDL problem no errors are being returned.

Posted: Thu Mar 31, 2011 11:23 pm
by xiphirx
Your gameloop is pointless, the event handling is done outside of it ;)

Re: SDL problem no errors are being returned.

Posted: Fri Apr 01, 2011 7:13 pm
by jakobnator
OK I fixed it, but now the X comes up but not when I click it just starts with it up

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);
    //game loop
    while (quit == false)
    {
        Target myTarget(170,120,320,240);
        if(SDL_PollEvent(&event))
        {
            myTarget.handle_events();
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }
        myTarget.show();
        if (SDL_Flip( screen ) == -1)
            {
            return 3;
            }
    }
    //cleanup
    cleanup();
    return 0;
}


Re: SDL problem no errors are being returned.

Posted: Fri Apr 01, 2011 10:08 pm
by jaybee
jakobnator wrote: //game loop
while (quit == false)
{
Target myTarget(170,120,320,240);
if(SDL_PollEvent(&event))
{
myTarget.handle_events();
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
myTarget.show();
if (SDL_Flip( screen ) == -1)
{
return 3;
}
}
//cleanup
cleanup();
return 0;
}

[/code]
just before you call SDL_Flip(screen) you are calling myTarget.show(), which is unconditionally blitting the image to the screen.

Re: SDL problem no errors are being returned.

Posted: Sat Apr 02, 2011 2:38 pm
by jakobnator
I moved it inside the event, but how can I make the show conditional if the condition is in another function?

Code: Select all

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 myTarget(170,120,320,240);
        if(SDL_PollEvent(&event))
        {
            myTarget.handle_events();
            myTarget.show();
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }
        if (SDL_Flip( screen ) == -1)
            {
            return 3;
            }
    }
    //cleanup
    cleanup();
    return 0;
}

Re: SDL problem no errors are being returned.

Posted: Sat Apr 02, 2011 3:14 pm
by jaybee

Code: Select all

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);
}

Look at your Target::handle_events() code, now look at your Target::show() code. Handle events is checking a certain set of conditions and if they are met, you call apply_surface(box.x, box.y, target_X, screen);. Your Target::show() is just calling apply_surface without doing any conditionals at all. Just remove Target::show() from your game loop that should do it.

Re: SDL problem no errors are being returned.

Posted: Sat Apr 02, 2011 4:08 pm
by jakobnator
Yea I tried and it just went back to not showing it at all can you make sure my handle_events() function is working which it is not.