I've been going through a couple of tutorials on SDL and having moderate success. But this one has me stumped. The tutorial from LazyFoo.com has me creating a dot that emits sparks. I followed along and wrote my code which returns my .bmp as NULL. Hmmm, I must have done something wrong. So I downloaded his source code and copied it almost verbatim. The same problem, image is still NULL. I compiled and ran his code and it worked just fine. I moved on to another tutorial by aaroncox.net. He has a demo to be compiled and run so he can go through Classes and OOP discussion. I copy/pasted his code with the only change being

#include "SDL.h"
#include "SDL_TTF.h"

changed to

#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"

His code is giving a segmentation fault that gdb says comes from bitmap being NULL.

Here is the original code from LazyFoo that executes properly:

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

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

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

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );

        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }

    //Return the optimized surface
    return optimizedImage;
}

//
//More Function Definitions Here
//

bool load_files()
{
    //Load the dot image
    dot = load_image( "dot.bmp" );

    //If there was a problem in loading the dot
    if( dot == NULL )
    {
        return false;
    }

    //Load the particles
    red = load_image( "red.bmp" );
    green = load_image( "green.bmp" );
    blue = load_image( "blue.bmp" );
    shimmer = load_image( "shimmer.bmp" );

    //If there was a problem in loading the images
    if( ( shimmer == NULL ) || ( blue == NULL ) || ( green == NULL ) || ( red == NULL ) )
    {
        return false;
    }

    //Set alpha
    SDL_SetAlpha( red, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
    SDL_SetAlpha( blue, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
    SDL_SetAlpha( green, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
    SDL_SetAlpha( shimmer, SDL_SRCALPHA | SDL_RLEACCEL, 192 );

    //If everything loaded fine
    return true;
}

//
//More Code Here
//



int main( int argc, char* args[] )
{
    //Quit flag
    bool quit = false;

    //The dot that will be used
    Dot myDot;

    //The frame rate regulator
    Timer fps;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Load the files
    if( load_files() == false )
    {
        return 1;
    }

    //While the user hasn't quit
    while( quit == false )
    {
     //
     //Rest of Main Function Here
     //

And my code which returns '2':

SDL_Surface *load_image(std::string filename){
	SDL_Surface* loadedImage = NULL;
	SDL_Surface* optimizedImage = NULL;
	
	loadedImage = IMG_Load(filename.c_str());
	if(loadedImage != NULL){
		printf("!");
		optimizedImage = SDL_DisplayFormat( loadedImage );
		SDL_FreeSurface(loadedImage);
		if( optimizedImage != NULL ){
			printf("@");
			SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF));
		}
	}
	return optimizedImage;
}
//More Functions and classes
bool load_files(){
	dot = load_image("dot.bmp");
	if(dot == NULL){
		return false;
	}
	red = load_image("red.bmp");
	green = load_image("green.bmp");
	blue = load_image("blue.bmp");
	shimmer = load_image("shimmer.bmp");
	if((shimmer == NULL) || (blue == NULL) || (red == NULL) || (green == NULL)){
		return false;
	}
	SDL_SetAlpha(red, SDL_SRCALPHA | SDL_RLEACCEL, 192);
	SDL_SetAlpha(blue, SDL_SRCALPHA | SDL_RLEACCEL, 192);
	SDL_SetAlpha(green, SDL_SRCALPHA | SDL_RLEACCEL, 192);
	SDL_SetAlpha(shimmer, SDL_SRCALPHA | SDL_RLEACCEL, 192);
	
	return true;
}
//More Code Here
int main(int argc, char** argv)
{
	bool quit = false;
	Dot myDot;
	Timer fps;
	
	if(init() == false){
		return 1;
	}
	if(load_files() == false){
		return 2;
	}
	while(quit == false){
                //The Rest of Main Here

I had load_files return 2 instead of 1 to verify where the problem lies. Both sets of code are in the same folder with the .bmp files, and I even tried giving the full path to the .bmps. I blew it off and moved on to aaroncox, but it seems to have the same problem:

const char *DOOMGUY_BITMAP_FILE_NAME = "doomguy.bmp";


SDL_Surface* g_doomguyBitmap = NULL;

SDL_Surface* loadBitmap(const char* bitmapFileName,
                        int transparentRed, int transparentGreen, int transparentBlue)
{
   SDL_Surface* bitmap = SDL_LoadBMP(bitmapFileName);

   SDL_SetColorKey(bitmap,
                   SDL_SRCCOLORKEY,
                   SDL_MapRGB(bitmap->format, transparentRed, transparentGreen, transparentBlue));

   return bitmap;
}
int main(int argc, char **argv)
{
   g_screen = initGraphics(WINDOW_WIDTH, WINDOW_HEIGHT,
                           WINDOW_TITLE);

   g_doomguyBitmap = loadBitmap(DOOMGUY_BITMAP_FILE_NAME,
                                TRANSPARENT_RED, TRANSPARENT_GREEN, TRANSPARENT_BLUE);

   SDL_Init(SDL_INIT_TIMER);

   float deltaTime = 0.0;
   int thisTime = 0;
   int lastTime = 0;

   while (g_gameIsRunning)
   {

Anyone know why I can't load .bmp files? Compiled with:
g++ -Wall -g -o "%e" "%f" -lSDL -lSDL_image -lSDL_ttf
Where %f = current file name
%e = file name without extension

Recommended Answers

All 5 Replies

I strongly recommend using SDL_Image and ditching BMP for better formats, like PNG where you can ditch color keying. It is described a few tutos later on LazyFoo.

Although the reference doesn't mention it, I wonder if SDL_Init() shouldn't be called before a call to SDL_DisplayFormat() .

Found the problem. I went back and looked at init() and found that I forgot SDL_SetVideoMode(). That fixed the LazyFoo problem. LazyFoo does use SDL_image.h and IMG_Load() for image handling, but the aaroncox program is just a copy/paste of his code. But further toying with gdb pointed me towards a TTF error. Specifically:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff772481b in TTF_SizeUNICODE () from /usr/lib/libSDL_ttf-2.0.so.0
(gdb)

Should this be a whole new thread?
Anyway, thanksfor the input jaskij

Expected something of the kind.

I suppose telling you what the docs say will not help you, but they mention SIGSEGV after getting a null font.

commented: Thanks for the help. +3

Aah-ha! Nice clue about the null font. I played around and found that for some reason SDL wants the full path to the font. Now it works as it should. Thanks for the help jaskij.

You're welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.