Sign in to follow this  

First steps...

This topic is 1966 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

[CODE]
#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;
}
[/CODE]
Thanks!

Share this post


Link to post
Share on other sites
It looks like you're missing a few pairs of parentheses:
[code]
SDL_Quit; // Does nothing
[/code]
[code]
if (!init) // Does not behave as expected; same as writing: if ( init == null )
return 1;
[/code]

And it doesn't hurt to let know if your application doesn't load resources correctly:
[code]
image = load_image("background.bmp");
if ( !image )
return -2;
[/code]

Share this post


Link to post
Share on other sites
[quote name='fastcall22' timestamp='1343141156' post='4962609']
It looks like you're missing a few pairs of parentheses:
[code]
SDL_Quit; // Does nothing
[/code]
[code]
if (!init) // Does not behave as expected; same as writing: if ( init == null )
return 1;
[/code]

And it doesn't hurt to let know if your application doesn't load resources correctly:
[code]
image = load_image("background.bmp");
if ( !image )
return -2;
[/code]
[/quote]
For the init function... if init == false then return 1.
Am I doing that wrong?

PS: Still not working... hmm... Edited by Krishath

Share this post


Link to post
Share on other sites
In case it is not clear, the code [tt]if ( !init );[/tt] tests the address of the [tt]init[/tt] function and not the result of calling the init function as [tt]if ( !init() );[/tt] does. [tt]SDL_Quit;[/tt] is a statement that just does nothing with the address of [tt]SDL_Quit[/tt], whereas [tt]SDL_Quit();[/tt] calls [tt]SDL_Quit[/tt].

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

Share this post


Link to post
Share on other sites
[quote name='fastcall22' timestamp='1343143943' post='4962625']
In case it is not clear, the code [tt]if ( !init );[/tt] tests the address of the [tt]init[/tt] function and not the result of calling the init function as [tt]if ( !init() );[/tt] does. [tt]SDL_Quit;[/tt] is a statement that just does nothing with the address of [tt]SDL_Quit[/tt], whereas [tt]SDL_Quit();[/tt] calls [tt]SDL_Quit[/tt].

If it isn't working, you should try using your debugger within your IDE and step through the problem.
[/quote]
Oh God I'm so stupid. Haven't been programming for a while... thanks! Soooooooooooooooolved!

Share this post


Link to post
Share on other sites
Your image is most likely not getting loaded.

[CODE]
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;
}
[/CODE]

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

Try this:
[CODE]
image = load_image("background.bmp");
if(image==NULL)std::cout<<"Failed to load image";
[/CODE]

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

Share this post


Link to post
Share on other sites
[quote name='Johnell' timestamp='1343144322' post='4962628']
Your image is most likely not getting loaded.

[CODE]
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;
}
[/CODE]

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

Try this:
[CODE]
image = load_image("background.bmp");
if(image==NULL)std::cout<<"Failed to load image";
[/CODE]

If that message is printed you need to fix the search path for your image.
[/quote]
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.

[CODE]
#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;
}
[/CODE]

Share this post


Link to post
Share on other sites
You need to initialize SDL_Image by calling [url=http://www.libsdl.org/projects/SDL_image/docs/SDL_image_8.html][tt]IMG_Init[/tt][/url], 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 [url=http://sdl.beuc.net/sdl.wiki/SDL_GetError]SDL_GetError[/url]; to check any SDL_Image-related errors, use [url=http://www.libsdl.org/projects/SDL_image/docs/SDL_image_45.html]IMG_GetError[/url]. Edited by fastcall22

Share this post


Link to post
Share on other sites

This topic is 1966 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this