A probably stupid SDL problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

A probably stupid SDL problem

Post 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.
Image ImageImage Image
andrew
Chaos Rift Regular
Chaos Rift Regular
Posts: 121
Joined: Mon Dec 08, 2008 2:12 pm

Re: A probably stupid SDL problem

Post by andrew »

Common problem.

Line 56:

Code: Select all

   if(screen = NULL){
User avatar
ismetteren
Chaos Rift Junior
Chaos Rift Junior
Posts: 276
Joined: Mon Jul 21, 2008 4:13 pm

Re: A probably stupid SDL problem

Post by ismetteren »

andrew wrote:Common problem.

Line 56:

Code: Select all

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

I knew it was something stupid...
Image ImageImage Image
bugmenot
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Sun Dec 07, 2008 7:05 pm

Re: A probably stupid SDL problem

Post 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.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: A probably stupid SDL problem

Post 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...
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: A probably stupid SDL problem

Post 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.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: A probably stupid SDL problem

Post 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*
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: A probably stupid SDL problem

Post 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:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: A probably stupid SDL problem

Post 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! ;)
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: A probably stupid SDL problem

Post by dandymcgee »

@Ginto8 - Interesting method I've seen that used before a few times. Welcome to the forums :mrgreen:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: A probably stupid SDL problem

Post by MarauderIIC »

I do that all the time myself. Welcome to the forums! :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: A probably stupid SDL problem

Post 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
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: A probably stupid SDL problem

Post 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).
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: A probably stupid SDL problem

Post 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).
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: A probably stupid SDL problem

Post 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!
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
Post Reply