Page 1 of 1
Could someone help a newby solve a problem in SDL
Posted: Mon Nov 29, 2010 9:13 pm
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;
}
Re: Could someone help a newby solve a problem in SDL
Posted: Wed Dec 01, 2010 6:38 pm
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?
Re: Could someone help a newby solve a problem in SDL
Posted: Wed Dec 01, 2010 7:49 pm
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).
Re: Could someone help a newby solve a problem in SDL
Posted: Wed Dec 01, 2010 8:53 pm
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,
( i think i misplaced the semi colon there)
but when load_files() is called from main it still returns 1. any ideas?
Re: Could someone help a newby solve a problem in SDL
Posted: Thu Dec 02, 2010 12:46 am
by adikid89
the program couldn't spot "x.png"...
Re: Could someone help a newby solve a problem in SDL
Posted: Thu Dec 02, 2010 11:48 am
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.
Re: Could someone help a newby solve a problem in SDL
Posted: Thu Dec 02, 2010 3:47 pm
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.
Re: Could someone help a newby solve a problem in SDL
Posted: Thu Dec 02, 2010 4:40 pm
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.
Re: Could someone help a newby solve a problem in SDL
Posted: Thu Dec 02, 2010 4:53 pm
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
Re: Could someone help a newby solve a problem in SDL
Posted: Thu Dec 02, 2010 9:35 pm
by VelloCretic
hmm well ok i fixed it now it seems xD
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;
}
Re: Could someone help a newby solve a problem in SDL
Posted: Fri Dec 03, 2010 2:17 pm
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/
Re: Could someone help a newby solve a problem in SDL
Posted: Fri Dec 03, 2010 2:50 pm
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
.
Re: Could someone help a newby solve a problem in SDL
Posted: Fri Dec 03, 2010 4:41 pm
by eaane74
It does focus more graphics than the SDL api, which is what makes lazyfoo's tutorials so good.