First steps...

Started by
7 comments, last by GSnake 11 years, 8 months ago
Hello guys, I'm following the tutorials by LazyFoo but I tried to "design" the code by my own way... BUT (obviously) it crashes.
What am I doing wrong?


#include "SDL/SDL.h"
#include <string>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 32
SDL_Surface * screen = NULL;
SDL_Surface * image = NULL;
SDL_Event event;
bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
return false;
if ((screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE)) == NULL)
return false;
SDL_WM_SetCaption("LOL", NULL);
return true;
}
SDL_Surface * load_image ( std::string filename )
{
SDL_Surface * loadedImage = NULL;
SDL_Surface * optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename.c_str());
if (loadedImage)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void apply_surface (int x, int y, SDL_Surface * source, SDL_Surface * destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
void clean_up (SDL_Surface * image)
{
SDL_FreeSurface(image);
SDL_Quit;
}
int main (int argc, char * argv[])
{
bool quit = false;
if (!init)
return 1;
image = load_image("background.bmp");
apply_surface(0, 0, image, screen);
SDL_Flip(screen);
while (!quit)
while (SDL_PollEvent(&event))
if (event.type == SDL_QUIT)
quit = true;
clean_up(image);
return 0;
}

Thanks!
Advertisement
It looks like you're missing a few pairs of parentheses:

SDL_Quit; // Does nothing


if (!init) // Does not behave as expected; same as writing: if ( init == null )
return 1;


And it doesn't hurt to let know if your application doesn't load resources correctly:

image = load_image("background.bmp");
if ( !image )
return -2;

It looks like you're missing a few pairs of parentheses:

SDL_Quit; // Does nothing


if (!init) // Does not behave as expected; same as writing: if ( init == null )
return 1;


And it doesn't hurt to let know if your application doesn't load resources correctly:

image = load_image("background.bmp");
if ( !image )
return -2;


For the init function... if init == false then return 1.
Am I doing that wrong?

PS: Still not working... hmm...
In case it is not clear, the code if ( !init ); tests the address of the init function and not the result of calling the init function as if ( !init() ); does. SDL_Quit; is a statement that just does nothing with the address of SDL_Quit, whereas SDL_Quit(); calls SDL_Quit.

If it isn't working, you should try using your debugger within your IDE and step through the problem.

In case it is not clear, the code if ( !init ); tests the address of the init function and not the result of calling the init function as if ( !init() ); does. SDL_Quit; is a statement that just does nothing with the address of SDL_Quit, whereas SDL_Quit(); calls SDL_Quit.

If it isn't working, you should try using your debugger within your IDE and step through the problem.

Oh God I'm so stupid. Haven't been programming for a while... thanks! Soooooooooooooooolved!
Your image is most likely not getting loaded.


SDL_Surface * load_image ( std::string filename )
{
SDL_Surface * loadedImage = NULL;
SDL_Surface * optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename.c_str());
if (loadedImage)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}


If your if statement is false you will return a null pointer.

Try this:

image = load_image("background.bmp");
if(image==NULL)std::cout<<"Failed to load image";


If that message is printed you need to fix the search path for your image.

Your image is most likely not getting loaded.


SDL_Surface * load_image ( std::string filename )
{
SDL_Surface * loadedImage = NULL;
SDL_Surface * optimizedImage = NULL;
loadedImage = SDL_LoadBMP(filename.c_str());
if (loadedImage)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}


If your if statement is false you will return a null pointer.

Try this:

image = load_image("background.bmp");
if(image==NULL)std::cout<<"Failed to load image";


If that message is printed you need to fix the search path for your image.

No the problem was caused by those damned parenthesis..

Got another problem though. I've installed the SDL_image library BUT it cannot load my image... (tried to make it print errors in the "loading" function and it doesn't pop up any error).

It always return -2.


#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 32
SDL_Surface * screen = NULL;
SDL_Surface * dots = NULL;
SDL_Event event;
SDL_Rect clip[4];

bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
return false;
if ((screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE)) == NULL)
return false;
SDL_WM_SetCaption("LOL", NULL);
return true;
}
SDL_Surface * load_image ( std::string filename )
{
SDL_Surface * loadedImage = NULL;
SDL_Surface * optimizedImage = NULL;
loadedImage = IMG_Load(filename.c_str());
if (loadedImage)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
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, clip, destination, &offset);
}
void clean_up (SDL_Surface * dots)
{
SDL_FreeSurface(dots);
SDL_Quit();
}
int main (int argc, char * argv[])
{
bool quit = false;
clip[0].x = 0;
clip[0].y = 0;
clip[0].w = 100;
clip[0].h = 100;
clip[1].x = 100;
clip[1].y = 0;
clip[1].w = 100;
clip[1].h = 100;
clip[2].x = 0;
clip[2].y = 100;
clip[2].w = 100;
clip[2].h = 100;
clip[3].x = 100;
clip[3].y = 100;
clip[3].w = 100;
clip[3].h = 100;
if (!init())
return 1;
dots = load_image("dots.png");
if (!dots)
return -2;
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
apply_surface(0, 0, dots, screen, &clip[0]);
SDL_Flip(screen);
while (!quit)
while (SDL_PollEvent(&event))
if (event.type == SDL_QUIT)
quit = true;
clean_up(dots);
return 0;
}
You need to initialize SDL_Image by calling IMG_Init, before you can use any of SDL_Image's features.

Although both sport similar-looking APIs, always keep in mind that SDL and SDL_Image are completely separate libraries. For instance, to check any SDL-related errors, use SDL_GetError; to check any SDL_Image-related errors, use IMG_GetError.
Solved this too!

This topic is closed to new replies.

Advertisement