[SOLVED] SDL problem no errors are being returned.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

[SOLVED] SDL problem no errors are being returned.

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

Last edited by jakobnator on Mon Apr 04, 2011 9:39 pm, edited 1 time in total.
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: SDL problem no errors are being returned.

Post by xiphirx »

Your gameloop is pointless, the event handling is done outside of it ;)
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

Re: SDL problem no errors are being returned.

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

Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
User avatar
jaybee
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 27
Joined: Thu Jan 13, 2011 12:53 pm
Current Project: 2d platformer
Favorite Gaming Platforms: NES, SNES, Genesis, DOS
Programming Language of Choice: C++
Location: Running Springs, CA

Re: SDL problem no errors are being returned.

Post 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.
"Do I really have to spell this out? What if a bunch of punk kids go into the woods with a bullet proof vest and strap it on a grizzly bear? Then what have you got? Invincible bears. Is that what you want? Invincible bears running around; raping your churches and burning your women?"
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

Re: SDL problem no errors are being returned.

Post 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;
}
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
User avatar
jaybee
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 27
Joined: Thu Jan 13, 2011 12:53 pm
Current Project: 2d platformer
Favorite Gaming Platforms: NES, SNES, Genesis, DOS
Programming Language of Choice: C++
Location: Running Springs, CA

Re: SDL problem no errors are being returned.

Post 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.
"Do I really have to spell this out? What if a bunch of punk kids go into the woods with a bullet proof vest and strap it on a grizzly bear? Then what have you got? Invincible bears. Is that what you want? Invincible bears running around; raping your churches and burning your women?"
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

Re: SDL problem no errors are being returned.

Post 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.
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
Post Reply