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