Public Group

# Beginner SDL app just quits as soon as it starts

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

## Recommended Posts

[font=Verdana, sans-serif]I am a beginner-to-intermediate C++ programmer in the process of teaching himself SDL, using a series of decent tutorials by Lazy Foo[/font][font=Verdana, sans-serif]. I got through the first lesson just fine, but then when I got to lesson two, "Optimized Surface Loading and Blitting[/font][font=Verdana, sans-serif]," a small amount of the terminologies started to confuse me, as some of it was over my head.[/font]

[font=Verdana, sans-serif]The program is supposed to display a tiled background with a message in the center, delay for two seconds then quit. But when I went to run the program it compiled just fine, with no errors and no warnings, only it just exits as soon as it starts, and I don't see the images loaded or anything. I want to make it very clear that 'sdl.dll' is in the program's directory, and so are the files 'img_bg.bmp' and 'img_message.bmp' so it's not a matter of missing files. [/font]

[font=Verdana, sans-serif]My only theory as to why it's not displaying the images and waiting for 2000 milliseconds is because it ran into an error and one of the return[/font][font=Verdana, sans-serif] statements (see my code below,) and as such returned a one to exit the program. I don't know what else it could be, and I would love some help debugging my program.[/font]

[font=Verdana, sans-serif]http://www.lazyfoo.n...son02/index.php - Here's the tutorial I was following[/font]

[font=Verdana, sans-serif]My code is below, but it's rather tl;dr at the moment, so if you think you can help me without looking at the code, be my guest. I had to include the whole file because I don't know what line is causing the crash[/font]

[font=Verdana, sans-serif]PROTIP: If you are going to be a sarcastic prick and tell me something like 'rtfm' or 'lrn 2 debug' then I will not appreciate it. Instead, say it nicely and I will gladly go out and research what I need to in order to solve my own problem, at is, if you are disgusted with a beginner who is utterly lost and does not know where to go next (even some pointers, not the programming kind, to what I can do next on my own would be most helpful) and prefer to take your disgust out on me, it will not be tolerated.[/font]

 /***************************************** * OPTIMIZED SURFACE LOADING AND BLITTING * * by Nick Hagen * * 6/3/2012 * ****************************************** * * Followed Lazy Foo's SDL tutorial here: * http://www.lazyfoo.net/SDL_tutorials/lesson02/index.php * * Made with SDL 1.2.15 * and Code::Blocks 10.05 */ /// *** INIT SECTION ************************************ // Required headers #include "SDL/SDL.h" #include <string> /* 'string' included because Lazy Foo prefers 'string' to 'char' */ using namespace std; // Screen attributes, as global constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32; /* In all of Lazy Foo's tutorials, 32 BPP will be used */ /* I can sorta see how putting these constants into functions would be preferably to typing '640' and '480' and '32' over and over again. */ // Now we're declaring all the surfaces we will be using SDL_Surface *message = NULL; // Foreground image SDL_Surface *background = NULL; // Background image SDL_Surface *screen = NULL; /* Wait...I thought declaring global variables was bad programming practice! I guess not in this case... */ /// *** FUNCTION DEFS *********************************** /* Apparently 'loadImage()' is already a taken function name. I will have to settle for calling it 'loadImages()' instead. Also, notice the asterisk. I think that means that the function 'loadImages()' returns a pointer to a variable of type 'SDL_Surface', but I could be wrong. Anyways onwards! */ /// -- loadImages() ------------------------------------- SDL_Surface *loadImages(string filename) { // The argument 'filename' will hold the path of the image to be loaded SDL_Surface* loadedImage = NULL; // This pointer stores the first loaded image SDL_Surface* optimizedImage = NULL; // This one is for the optimized image // Load the image (not QUITE sure about the '.c_str()' at the end of 'filename' loadedImage = SDL_LoadBMP(filename.c_str()); /* My guess is that the '.c_str()' appends a filetype which is a C-string to the end of the 'filename' string variable, but I could be mistaken. */ // If nothing went wrong in loading the image if(loadedImage != NULL) { // Create an optimized image optimizedImage = SDL_DisplayFormat(loadedImage); /* What 'SDL_DisplayFormat()' does is convert whatever BPP the loaded image was to the BPP of the screen, in this case 32 BPP. Otherwise if you don't convert the image's BPP when you load them here, SDL tries to convert on- the-fly, which costs you speed and processing power. This, it's a much better idea to convert images when you first load them.THAT is what 'optimize' means, by the way. */ SDL_FreeSurface(loadedImage); // Get rid of the old, non-32 BPP image from memory } // End 'if' statement return optimizedImage; // Return the new image, optimized at 32 BPP } // End function definition /// -- applySurface() ----------------------------------- void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination) { // Takes in the coordinates of where you want to blit the surface, what surface to blit, and what surface to blit to SDL_Rect offset; // 'SDL_Rect' is either a struct or a class. This instance will hold the offsets offset.x = x; offset.y = y; // Actually blit the surface SDL_BlitSurface(source, NULL, destination, &offset); /* The fourth argument holds the offsets to where on the destination the source is going to be applied. I'm not sure why it has a '&' in front of it though... */ } // End function definition /// *** MAIN FUNCTION *********************************** int main(int argc, char* args[]) { /// -- Initializations ------------------------------ /* Initialize all SDL subsystems (I guess putting it in an 'if' statement still works...) NOTES: SDL subsystems are things like the video, audio, timers, etc that are the individual engine components used to make a game. */ if( SDL_Init(SDL_INIT_EVERYTHING) == -1){ return 1; /* If SDL can't initialize, it returns -1. In this case we handle the error by returning 1, which will end the program */ } // End 'if' statement SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); // Set up the screen if(screen == NULL){ return 1;} //If there was an error in setting up the screen, end the program SDL_WM_SetCaption("Hello, world!", NULL); // Set the window caption /// -- Load and Apply ------------------------------- message = loadImages("img_hello.bmp"); background = loadImages("img_bg.bmp"); // Apply the background images! Tile them! applySurface(0, 0, background, screen); applySurface(320, 0, background, screen); applySurface(0, 240, background, screen); applySurface(320, 240, background, screen); // Apply the message image! applySurface(180, 140, message, screen); // Flip the buffers, and update the screen if(SDL_Flip(screen) == -1){ return 1;} // If there's an error then end the program SDL_Delay(2000); // Wait 2 seconds /// -- Finishing Up --------------------------------- SDL_FreeSurface(message); SDL_FreeSurface(background); SDL_Quit(); // Quit SDL return 0; // End of the program }  Edited by Da5ch

##### Share on other sites
you could print some debug messages (just a simple cout <<"Failed to init SDL"; or cout <<"Failed to flip buffers";) before each of your return 1s to figure out which one it is hitting.

also, in your post you said you had the files img_bg.bml and img_message.bmp but in the code you got img_hello.bmp, did you just make a mistake writing the post or could this be the cause of the problem ?

##### Share on other sites

I want to make it very clear that 'sdl.dll' is in the program's directory, and so are the files 'img_bg.bmp' and 'img_message.bmp' so it's not a matter of missing files.
[/quote]
This is an assumption. The file might appear to be present, but it could be corrupted. In addition, if you are using Windows then the file explorer will hide the file extensions of "known" files by default. You might call a file image.bmp, but there could be an implicit, invisible ".bmp" extension, making the full file name "image.bmp.bmp".

I don't think these are your actual problems, but I'm just pointing this out for future reference. You should put error checking code in for this anyway, for reasons similar to what I have to say next.

My only theory as to why it's not displaying the images and waiting for 2000 milliseconds is because it ran into an error and one of the return statements (see my code below,) and as such returned a one to exit the program. I don't know what else it could be, and I would love some help debugging my program.
[/quote]
This is very likely. There are a couple of ways of trying to diagnose this, such as outputting a message to a persistent log just before returning, using an OS API call to generate a message box, or placing a breakpoint on each of the return statements and running the program in your debugger.

The latter will find it fastest, but you should implement one of the former too, because someday your code will be crashing on someone else's machine - one that you can't attach a debugger to. To make it easy for you to figure out what is happening in such cases, you should log or display what failed, and the error message (i.e. SDL_GetError()).

PROTIP: If you are going to be a sarcastic prick and tell me something like 'rtfm' or 'lrn 2 debug' then I will not appreciate it. Instead, say it nicely and I will gladly go out and research what I need to in order to solve my own problem, at is, if you are disgusted with a beginner who is utterly lost and does not know where to go next (even some pointers, not the programming kind, to what I can do next on my own would be most helpful) and prefer to take your disgust out on me, it will not be tolerated.
[/quote]
There should be no need to place such a message in your posts. We strongly discourage such unhelpful responses in general, but in particular in For Beginners we have a much stricter policy towards being helpful. If you feel someone has crossed the line, bring it to the attention of a moderator via the "report" button (we don't see everything).

Speaking of discouraging behaviour, drop that pre-emptively defensive attitude in future. It will put people off answering, and it starts the thread on an aggressive tone.

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632949
• Total Posts
3009414
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!