Page 1 of 2

A probably stupid SDL problem

Posted: Sat Dec 27, 2008 2:51 pm
by ismetteren
I am following lazyfoo's sdl tutorial.

I am in lesson 2, wich is about drawing some stuff on the screen. When i am copying the code from the tutorial it works just fine, but my own code is just making a empty window pop up and then disapear instantly.
If you run the program and the images don't show up or the window flashes for a second and you find in stderr.txt:
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

It's because the program tried to access memory it wasn't supposed to. Odds are its because it tried to access NULL when apply_surface() was called. This means you need to make sure the bitmap files are in the same directory as the program.

If the window pops up and the image doesn't show up, again make sure the bitmaps are in the same folder as the program or in the project directory.
I have checked my stderr.txt, and there are nothing in it, and since lazyfoo's code works, and i have checked alot of times that i have the same file path, it seems like this isent the problem.

here is the working code written by lazyfoo:

Code: Select all

/*This source code copyrighted by Lazy Foo' Productions (2004-2008) and may not
be redestributed without written permission.*/

//The headers
#include "SDL/SDL.h"
#include <string>

//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces that will be used
SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *load_image( std::string filename )
{
    //Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = SDL_LoadBMP( filename.c_str() );

    //If nothing went wrong in loading the image
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );
    }

    //Return the optimized image
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;

    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;

    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}

int main( int argc, char* args[] )
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return 1;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Hello World", NULL );

    //Load the images
    message = load_image( "hello_world.bmp" );
    background = load_image( "background.bmp" );

    //Apply the background to the screen
    apply_surface( 0, 0, background, screen );

    //Apply the message to the screen
    apply_surface( 180, 140, message, screen );

    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }

    SDL_Delay( 2000 );

    //Free the surfaces
    SDL_FreeSurface( message );
    SDL_FreeSurface( background );

    //Quit SDL
    SDL_Quit();

    return 0;
}
and here is the code written by me:

Code: Select all

#include"SDL/SDL.h"
#include<string>
#include<iostream>

using namespace std;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface * message = NULL;
SDL_Surface * background = NULL;
SDL_Surface * screen = NULL;

SDL_Surface * loadImage(std::string fileName) {
	SDL_Surface * loadedImage = NULL;
	SDL_Surface * optimizedImage = NULL;

	loadedImage = SDL_LoadBMP(fileName.c_str());

	if(loadedImage != NULL){
		optimizedImage = SDL_DisplayFormat(loadedImage);
		SDL_FreeSurface(loadedImage);
	}
	else{
		cout << "fjel i loadbmp" << endl;
	}

	return optimizedImage;
}

void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	cout << "checkpoint4.1" << endl;

	SDL_BlitSurface(source, NULL, destination, &offset);

	cout << "checkpoint4.2" << endl;
}

int main(int argc, char* args[])
{
	cout << "start" << endl;
	if(SDL_Init( SDL_INIT_EVERYTHING ) == -1){
		cout << "fjel i init" << endl;
		return 1;
	}

	cout << "checkpoint1" << endl;

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
	if(screen = NULL){
		cout << "fjel i setvideomode" << endl;
		return 1;
	}

	cout << "checkpoint2" << endl;

	SDL_WM_SetCaption("Hello world", NULL);

	cout << "checkpoint3" << endl;

	message = loadImage("hello_world.bmp");
	background = loadImage("background.bmp");

	cout << "checkpoint4" << endl;

	applySurface(0, 0, background, screen);
	applySurface(180, 140, message, screen);

	cout << "checkpoint5" << endl;

	if(SDL_Flip(screen) == -1){
		cout << "fjel i flip" << endl;
		return 1;
	}

	cout << "checkpoint6" << endl;

	SDL_Delay(10000);

	cout << "checkpoint7" << endl;

	SDL_FreeSurface(background);
	SDL_FreeSurface(message);

	cout << "checkpoint8" << endl;

    SDL_Quit();

    cout << "Success" << endl;
    return 0;
}
("fjel i" means "error in" in danish... or actuly it should be "fejl i"... my bad)

as you can see i have added some checkpoints(and some other cout's)
here is the stdout.txt

Code: Select all

start
checkpoint1
checkpoint2
checkpoint3
checkpoint4
checkpoint4.1
checkpoint4.2
checkpoint4.1
checkpoint4.2
checkpoint5
to me, it seems like it just magically stops at checkpoint 5...

It is probably just some stupid syntax error, but i really cant find it.

Re: A probably stupid SDL problem

Posted: Sat Dec 27, 2008 3:11 pm
by andrew
Common problem.

Line 56:

Code: Select all

   if(screen = NULL){

Re: A probably stupid SDL problem

Posted: Sat Dec 27, 2008 3:34 pm
by ismetteren
andrew wrote:Common problem.

Line 56:

Code: Select all

   if(screen = NULL){
Tanks! :worship:

I knew it was something stupid...

Re: A probably stupid SDL problem

Posted: Sun Dec 28, 2008 2:04 am
by bugmenot
To stop this from happening in the future, get into the habit of putting your constants/literals on the left of equality tests:

Code: Select all

if( NULL == screen )
So when you make the mistake of:

Code: Select all

if( NULL = screen )
Your compiler will spit out an actual error.

Re: A probably stupid SDL problem

Posted: Sun Dec 28, 2008 6:22 am
by programmerinprogress
that's a hard one to spot, I don't how many times I used an assignment '=' instead of an equality '==' when I started programming...

Re: A probably stupid SDL problem

Posted: Sun Dec 28, 2008 12:30 pm
by Falco Girgis
bugmenot wrote:To stop this from happening in the future, get into the habit of putting your constants/literals on the left of equality tests:

Code: Select all

if( NULL == screen )
So when you make the mistake of:

Code: Select all

if( NULL = screen )
Your compiler will spit out an actual error.
Interesting. That's stupidly obvious, but I have never heard anybody mention doing that, and I have never stopped and thought of it.

Clevar.

Re: A probably stupid SDL problem

Posted: Mon Dec 29, 2008 6:55 am
by programmerinprogress
Interesting. That's stupidly obvious, but I have never heard anybody mention doing that, and I have never stopped and thought of it.
maybe we're all just so ashamed of admitting we did something so stupid, we deny it at all costs :lol:

*Programmers FBI agents smash through door and take me to a secure location*

Re: A probably stupid SDL problem

Posted: Mon Dec 29, 2008 2:02 pm
by dandymcgee
programmerinprogress wrote:that's a hard one to spot, I don't how many times I used an assignment '=' instead of an equality '==' when I started programming...
Yea.. Me too :roll:

@bugmenot: You, my friend, are a freaking genius. :worship: :lol:

Re: A probably stupid SDL problem

Posted: Mon Jan 12, 2009 9:03 pm
by Ginto8
Personally, I have my own safeguard against mixing up assignment and comparison operators. I don't really use

Code: Select all

if( [variable] == NULL )

or

Code: Select all

if( [variable] != NULL )
and you could replace NULL with 0 in those cases (NULL is a constant for 0, if I am not mistaken). Instead I use (or don't use) the unary logical NOT operator (!).

example (for what you were talking about before):

Code: Select all

if( !screen ) // ![variable] means [variable] == NULL or [variable] == false or something like that...
     return 1;
or

Code: Select all

if( screen ) // OMG NO OPERATOR -- means [variable] != NULL or [variable] != false or other stuff like that
     // do stuff
BTW to all the dev team (programmers mainly), you guys are my 1D0LZ!!!!!111!1!1!111oneoneone :worship:

But in all seriousness, you guys are awesome! ;)

Re: A probably stupid SDL problem

Posted: Mon Jan 12, 2009 9:16 pm
by dandymcgee
@Ginto8 - Interesting method I've seen that used before a few times. Welcome to the forums :mrgreen:

Re: A probably stupid SDL problem

Posted: Mon Jan 12, 2009 9:52 pm
by MarauderIIC
I do that all the time myself. Welcome to the forums! :)

Re: A probably stupid SDL problem

Posted: Wed Jan 14, 2009 5:42 am
by Ginto8
Thanks; I'm glad to be here. :) You know, it's funny how much the if( [variable] ) thing makes sense. All the comparison operators (>, ==, etc) return a bool value. So if you were to do if( [var1] == [var2] ), the == would either return true or false. Since there isn't a !, it will do whatever's in the if statement if == returns true. So, when using a comparison operator, condition statements check "Does [var1] [comparison operator] [var2] return true?" or, with !, "Does [var1] [comparison operator] [var2] return false?" (yes, I know I repeated myself. Just trying to make it kinda easier for the newbs ;) ).

-Ginto8

Re: A probably stupid SDL problem

Posted: Wed Jan 14, 2009 7:39 am
by programmerinprogress
I think it's matter of preference and style.

Personally I use if(!variable) or if(variable) most of the time, but I mainly use it when I'm trying to create a game loop like this while(!Quit) or something like that.

When you think about it, you're taking away the need for a binary operator, and the need for an operand, so in theory, the statement should be easier to comprehend (that is unless you don't understand the zero and non-zero relationship of boolean values)

I would definitely choose to use if(!variable) as opposed to if(variable == NULL) (or some other variation).

Re: A probably stupid SDL problem

Posted: Wed Jan 14, 2009 11:55 am
by MarauderIIC
I should mention that if... == NULL is "good practice" "in case they decide to change NULL from 0 to some other number" HA
Also, in C#, (IIRC) NULL is not a number, so you have to do if.. == null. You get a compilation error if you do if (variablename).

Re: A probably stupid SDL problem

Posted: Wed Jan 14, 2009 12:39 pm
by programmerinprogress
...you don't have to do NULL in C/C++ because it kicks ass!

Seriously though, it's logical that using the word NULL is better practice, it's easier to read, and if you use equality operators, I guess it helps self-documentation of your code.

But in practice, why waste time using equality operators and NULL if the statement already does it for you?, any competent programmer who has knowledge of C/C++ operators will understand the code.

Could you imagine the chaos if the C++ standardisation committee decided one day to start using different numbers for true and false? all hell would break loose!