Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 17 Oct 2011
Offline Last Active Jun 03 2012 04:06 PM

#4945919 Beginner SDL app just quits as soon as it starts

Posted by on 03 June 2012 - 03:40 PM

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

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.

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.

http://www.lazyfoo.n...son02/index.php - Here's the tutorial I was following

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

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.

* 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

	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_Quit(); // Quit SDL
	return 0;   // End of the program