A probably stupid SDL problem
Posted: Sat Dec 27, 2008 2:51 pm
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.
here is the working code written by lazyfoo:
and here is the code written by me:
("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
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.
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.
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.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.
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;
}
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;
}
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
It is probably just some stupid syntax error, but i really cant find it.