Could someone help a newby solve a problem in SDL

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
VelloCretic
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Mon Nov 29, 2010 9:03 pm

Could someone help a newby solve a problem in SDL

Post by VelloCretic »

Ok well I've been following lazyfoo.net but I just can't do the "event driven programming" tutorial so here's my code, what is it that I am doing wrong?
load_files() keeps returning 2 for me?
im using Code::Blocks IDE and GCC compiler if that helps

Code: Select all

//Headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
//Properties of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface * screen = NULL;
SDL_Surface * image = NULL;
//the event structure
SDL_Event event;

SDL_Surface * load_image ( std::string filename )
{
    //image that will be loaded
    SDL_Surface * loadedImage = NULL;
    //image that we will use
    SDL_Surface * optimizedImage = NULL;
    //Load the image
    loadedImage = IMG_Load ( filename.c_str() );
    if ( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat ( loadedImage);
        SDL_FreeSurface (loadedImage);
    }
    return optimizedImage;
}

void apply_surface ( int x , int y , SDL_Surface * source , SDL_Surface * destination)
{
    //make a temporary rect to hold the offsets
    SDL_Rect offset;
    //get the offsets
    offset.x = x;
    offset.y = y;
    //blit the surface
    SDL_BlitSurface ( source , NULL , destination , &offset);
}

bool init()
{
    //initalize SDL
    if ( SDL_Init (SDL_INIT_EVERYTHING) == -1 )
    {
        return false;
    }
    //Set up the screen
    screen = SDL_SetVideoMode ( SCREEN_WIDTH , SCREEN_HEIGHT , SCREEN_BPP , SDL_SWSURFACE);
    if ( screen = NULL )
    {
        return false;
    }
    //set up the caption
    SDL_WM_SetCaption( "Event test", NULL);
    return true;
}

bool load_files ()
{
    image = load_image ( "x.png");
    if ( image == NULL)
    {
        return false;
    }
    return true;
}

void clean_up ()
{
    SDL_FreeSurface ( image );
    SDL_Quit();
}


int main (int argc , char* args [])
{
    //make bool quit false
    bool quit = false;
    //initialize
    if ( init() == false)
    {
        return 1;
    }
    if ( load_files() == false);
    {
        return 1 ;
    }
    //aply the surface to the screen
    apply_surface ( 0 , 0 , image , screen);
    //update the screen
    if (SDL_Flip ( screen ) == -1 )
    {
        return 1;
    }
    while (quit == false)
    {
        while (SDL_PollEvent ( &event ) )
        {
            //if the user has Xed out of the window
            if ( event.type = SDL_QUIT)
            {
                //set quit to true
                quit = true;
            }
        }
    }
    //free the surface and quit SDL
    clean_up();
    return 0;
}
User avatar
eaane74
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Tue Feb 23, 2010 1:43 pm

Re: Could someone help a newby solve a problem in SDL

Post by eaane74 »

VelloCretic wrote:Ok well I've been following lazyfoo.net but I just can't do the "event driven programming" tutorial so here's my code, what is it that I am doing wrong?
load_files() keeps returning 2 for me?
im using Code::Blocks IDE and GCC compiler if that helps

Code: Select all

//Headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
//Properties of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface * screen = NULL;
SDL_Surface * image = NULL;
//the event structure
SDL_Event event;

SDL_Surface * load_image ( std::string filename )
{
    //image that will be loaded
    SDL_Surface * loadedImage = NULL;
    //image that we will use
    SDL_Surface * optimizedImage = NULL;
    //Load the image
    loadedImage = IMG_Load ( filename.c_str() );
    if ( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat ( loadedImage);
        SDL_FreeSurface (loadedImage);
    }
    return optimizedImage;
}

void apply_surface ( int x , int y , SDL_Surface * source , SDL_Surface * destination)
{
    //make a temporary rect to hold the offsets
    SDL_Rect offset;
    //get the offsets
    offset.x = x;
    offset.y = y;
    //blit the surface
    SDL_BlitSurface ( source , NULL , destination , &offset);
}

bool init()
{
    //initalize SDL
    if ( SDL_Init (SDL_INIT_EVERYTHING) == -1 )
    {
        return false;
    }
    //Set up the screen
    screen = SDL_SetVideoMode ( SCREEN_WIDTH , SCREEN_HEIGHT , SCREEN_BPP , SDL_SWSURFACE);
    if ( screen = NULL )
    {
        return false;
    }
    //set up the caption
    SDL_WM_SetCaption( "Event test", NULL);
    return true;
}

bool load_files ()
{
    image = load_image ( "x.png");
    if ( image == NULL)
    {
        return false;
    }
    return true;
}

void clean_up ()
{
    SDL_FreeSurface ( image );
    SDL_Quit();
}


int main (int argc , char* args [])
{
    //make bool quit false
    bool quit = false;
    //initialize
    if ( init() == false)
    {
        return 1;
    }
    if ( load_files() == false);
    {
        return 1 ;
    }
    //aply the surface to the screen
    apply_surface ( 0 , 0 , image , screen);
    //update the screen
    if (SDL_Flip ( screen ) == -1 )
    {
        return 1;
    }
    while (quit == false)
    {
        while (SDL_PollEvent ( &event ) )
        {
            //if the user has Xed out of the window
            if ( event.type = SDL_QUIT)
            {
                //set quit to true
                quit = true;
            }
        }
    }
    //free the surface and quit SDL
    clean_up();
    return 0;
}
load_files() cannot possibly return 2 since it returns a boolean value( 0 or 1 )
I see two other mistakes.

Code: Select all

if ( screen = NULL )
{
	return false;
}
should be

Code: Select all

if ( screen == NULL )
{
	return false;
}
and

Code: Select all

//if the user has Xed out of the window
if ( event.type = SDL_QUIT)
{
	//set quit to true
	quit = true;
}
should be

Code: Select all

//if the user has Xed out of the window
if ( event.type == SDL_QUIT)
{
	//set quit to true
	quit = true;
}
I am suprised your compiler didnt catch those errors.
do you have -enable all compiler warnings set?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Could someone help a newby solve a problem in SDL

Post by dandymcgee »

VelloCretic wrote: I am suprised your compiler didnt catch those errors.
do you have -enable all compiler warnings set?
Code::Blocks doesn't have the luxury of catching such common mistakes by default. Dunno if changing warning level would fix that, but there are so many other reasons to switch from Code::Blocks to Visual Studio does it even matter (I was a long-time Code::Blocks fan, but am incredibly happy to have given VS an honest chance).
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
VelloCretic
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Mon Nov 29, 2010 9:03 pm

Re: Could someone help a newby solve a problem in SDL

Post by VelloCretic »

yes you're right it returns 1 not 2 ( i made a typo in the OP) and i fixed the areas where i had put = instead of ==,
i also found a if statement which was wrong being this,

Code: Select all

if ( load_files() == false);
( i think i misplaced the semi colon there)
but when load_files() is called from main it still returns 1. any ideas?
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: Could someone help a newby solve a problem in SDL

Post by adikid89 »

the program couldn't spot "x.png"...
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
eaane74
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Tue Feb 23, 2010 1:43 pm

Re: Could someone help a newby solve a problem in SDL

Post by eaane74 »

I really dont see anything wrong with your load_files() function.
Have you checked the return value? Your problem could also reside in the load_image() function.
It is almost always better to use asset() statements to determine if you are dealling with a NULL pointer.

One other thought:
Since you created the screen in system memory, SDL_Flip() is actually calling SDL_UpdateRects().

Also try commenting out your error checking and just call the function as is and see if the debugger kicks in.
VelloCretic
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Mon Nov 29, 2010 9:03 pm

Re: Could someone help a newby solve a problem in SDL

Post by VelloCretic »

ok well i've just tried recompiling the code into VC++ 2010 and...
load_files returns true and the program continues but now when i try and run it, it executes the program fine until

Code: Select all

if ( SDL_Flip ( screen ) == -1)
	{
		return 1;
	}
what happens is when the program gets to this part of the code i get an error saying
"Unhandled exception at 0x6812a1ba in dep.exe: 0xC0000005: Access violation reading location 0x00000138."
i really have no idea what this error is or means , when i comment out the if statement calling SDL_Flip though the program returns 0 although it seems it skips over the loop but maybe that has something to do with refreshing the screen.
User avatar
eaane74
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Tue Feb 23, 2010 1:43 pm

Re: Could someone help a newby solve a problem in SDL

Post by eaane74 »

Are the SDL libs compiled for VS or mingw?
I am sure that makes a difference.

Some things to try:

Call SDL_GetError() and see if it gives you any more info
Or implicitly call SDL_UpdateRects( screen, 0, 0, 0, 0 ) which is what SDL_Flp is doing.
VelloCretic
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Mon Nov 29, 2010 9:03 pm

Re: Could someone help a newby solve a problem in SDL

Post by VelloCretic »

I think it's or VS, I'll try SDL_GetError in a sec
Edit: alright i replace SDL_Flip with SDL_Rect, instead of testing the load_files() function im just calling it and everything is working fine except the picture is not showing up, so maybe there is something wrong with either my load_image/load_files functions
VelloCretic
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Mon Nov 29, 2010 9:03 pm

Re: Could someone help a newby solve a problem in SDL

Post by VelloCretic »

hmm well ok i fixed it now it seems xD :lol:
load_files returns true and i used SDL_UpdateRects to refresh the screen
here's the code i have now that works

Code: Select all

     //get the offsets
    offset.x = x;
    offset.y = y;
    //blit the surface
    SDL_BlitSurface ( source , NULL , destination , &offset);
}

bool init()
{
    //initalize SDL
    if ( SDL_Init (SDL_INIT_EVERYTHING) == -1 )
    {
        return false;
    }
    //Set up the screen
    screen = SDL_SetVideoMode ( SCREEN_WIDTH , SCREEN_HEIGHT , SCREEN_BPP , SDL_SWSURFACE);
    if ( screen == NULL )
    {
        return false;
    }
    //set up the caption
    SDL_WM_SetCaption( "Event test", NULL);
    return true;
}

bool load_files ()
{
    image = load_image ( "x.png");
    if ( image == NULL)
    {
        return false;
    }
    return true;
}

void clean_up ()
{
    SDL_FreeSurface ( image );
    SDL_Quit();
}


int main (int argc , char* args [])
{
    //make bool quit false
    bool quit = false;
    //initialize
	if ( init() == false)
	{
		return 1;
	}
	//im actually not sure how I fixed load_files :S
	if ( load_files() == false )
	{
		return 1;
	}
    //aply the surface to the screen
    apply_surface ( 0 , 0 , image , screen);
    //update the screen
	SDL_UpdateRect ( screen , 0 , 0 , 0 , 0 );
	//using SDL_UpdateRect instead of SDL_Flip this time!
	while (quit == false)
    {
        while (SDL_PollEvent ( &event ) )
        {
            //if the user has Xed out of the window
            if ( event.type == SDL_QUIT)
            {
                //set quit to true
                quit = true;
            }
        }
    }
    //free the surface and quit SDL
    clean_up();
    return 0;
}
User avatar
eaane74
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Tue Feb 23, 2010 1:43 pm

Re: Could someone help a newby solve a problem in SDL

Post by eaane74 »

Sweet. I am glad you got it working.

I have nothing against lazyfoo, it is a excellent site to learn SDL and only SDL.
My only problem is the he does it with bad programming habits

Here is another good SDL tutorial site:( where I learned SDL ) http://iki.fi/sol/gp/
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: Could someone help a newby solve a problem in SDL

Post by adikid89 »

eaane74 wrote:Sweet. I am glad you got it working.

I have nothing against lazyfoo, it is a excellent site to learn SDL and only SDL.
My only problem is the he does it with bad programming habits

Here is another good SDL tutorial site:( where I learned SDL ) http://iki.fi/sol/gp/
Come on... you can't hold that against him...He's not teaching programming, you should already know that if you going into game programming. His tutorials are about a graphics library that's all. His examples could just as well have been in pseudo-code.

P.S. Thosesol tutorials are awesome.. too bad I didn't understand anything from them :( .
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
eaane74
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 7
Joined: Tue Feb 23, 2010 1:43 pm

Re: Could someone help a newby solve a problem in SDL

Post by eaane74 »

It does focus more graphics than the SDL api, which is what makes lazyfoo's tutorials so good.
Post Reply