I just started learning SDL so I can make games in C++,
but for some reason my code fails to compile.
The code:

#include "SDL/SDL.h"
#include <string>
using std::string;

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 *load_image(string filename)
{
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    loadedImage = SDL_LoadBMP(filename.c_str());
    if(loadedImage != NULL)
    {
        optimizedImage = SDL_Display_Format(loadedImage);
        SDL_FreeSurface(loadedImage);
    }

    return optimizedImage;
}
void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(source, NULL, destination, &offset);
}

int main(int argc, char* args[])
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return 1;
    }
    screen = SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    if(screen == NULL) { return 1; }
    SDL_WM_SetCaption("Hello World", NULL);
    message = load_image("message.bmp");
    background = load_image("background.bmp");
    apply_surface(0, 0, background, screen);
    apply_surface(200, 200, message, screen);
    if(SDL_Flip(screen) == -1) { return 1; }
    SDL_Delay(5000);
    SDL_FreeSurface(message);
    SDL_FreeSurface(background);

    SDL_Quit();
    return 0;
}

Here's the errors I got:

||=== Test_SDL, Release ===|
C:\Programming\C++\Newbie\TutorialStuff\Hello_SDL\main.cpp||In function `SDL_Surface* load_image(std::string)'
C:\Programming\C++\Newbie\TutorialStuff\Hello_SDL\main.cpp|20|error: `SDL_Display_Format' was not declared in this scope|
C:\Programming\C++\Newbie\TutorialStuff\Hello_SDL\main.cpp|20|warning: unused variable 'SDL_Display_Format'|
C:\Programming\C++\Newbie\TutorialStuff\Hello_SDL\main.cpp||In function `int SDL_main(int, char**)'
C:\Programming\C++\Newbie\TutorialStuff\Hello_SDL\main.cpp|40|error: `SetVideoMode' was not declared in this scope|
C:\Programming\C++\Newbie\TutorialStuff\Hello_SDL\main.cpp|45|error: `apply_surface' was not declared in this scope|
C:\Programming\C++\Newbie\TutorialStuff\Hello_SDL\main.cpp|40|warning: unused variable 'SetVideoMode'|
||=== Build finished: 3 errors, 2 warnings ===|

I don't know what went wrong, it's either the code or SDL isn't set up properly...
I would like someone to tell me what's wrong, and
be able to fix it and explain.

Any help appreciated!

~ Xarver

P.S. I have attached a zip file with the things.

Dani AI

Generated

Quick follow‑up and practical notes for this thread: the original compile-time failures were down to simple misspellings of SDL functions and missing SDL prefixes (see the helpful pointers from and ). After those were fixed, a runtime load failure turned out to be an image-format problem rather than a blit bug; ’s advice to check the SDL error string made that obvious. The checklist below helps avoid the same traps and is useful for readers coming to this topic long after it was posted.

Practical checklist

  • Always verify every loader/conversion call returns a non-NULL surface and record the SDL error string when it fails; that usually tells whether the problem is a path, format, or permission issue.
  • Confirm the program’s working directory (IDEs often run the exe from a different folder). Try an absolute path or run the binary from a console to be sure the file is reachable.
  • SDL_LoadBMP expects standard, uncompressed Windows BMPs. Some editors/exporters (or renamed files) produce variants SDL won’t accept. Export as a true 24‑bit, uncompressed Windows BMP, or switch to SDL_image (IMG_Load) and PNG/JPEG to avoid BMP quirks.
  • For images with transparency use an alpha‑aware conversion (or keep the surface’s alpha) so the background renders correctly. A quick isolation test is to copy a known‑good BMP over the failing filename to see whether the loader or the file is at fault.

A modern note: the tutorial APIs are SDL 1.2 era (SetVideoMode/DisplayFormat). New projects should use SDL2 (CreateWindow/CreateRenderer and textures) and SDL_image for broader format support. Also double‑check that linked libraries match the headers (SDL 1.2 vs SDL2) when encountering undefined references.

Recommended Answers

All 12 Replies

Read SDL tutorials carefully then correct misprints in you sources. Must be SDL_DisplayFormat, SDL_SetVideoMode etc...

Are you sure you added the library files from project settings?

Are you sure you added the library files from project settings?

IMHO, it's of no importance: there are obviously misspelled SDL function names in the source...

Ok.


optimizedImage = SDL_Display_Format(loadedImage);

should be
optimizedImage = SDL_DisplayFormat(loadedImage);

----------------------------------------------------------------------------------------------

screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

You forgot SDL here.

----------------------------------------------------------------------------------------------

apply_surface(0, 0, background, screen);
apply_surface(200, 200, message, screen);

Your function's name is applySurface :)

It should be working now.

I fixed my program, but still a small problem.
It loads the message.bmp picture but doesn't load the background.
:(

#include "SDL/SDL.h"
#include <string>
using std::string;

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 *load_image(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);
    }

    return optimizedImage;
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(source, NULL, destination, &offset);
}

int main(int argc, char* args[])
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return 1;
    }
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    if(screen == NULL) { return 1; }
    SDL_WM_SetCaption("Hello World", NULL);
    message = load_image("message.bmp");
    background = load_image("background.bmp");
    apply_surface(0, 0, background, screen);
    apply_surface(100, 100, message, screen);
    if(SDL_Flip(screen) == -1) { return 1; }
    SDL_Delay(5000);
    SDL_FreeSurface(message);
    SDL_FreeSurface(background);

    SDL_Quit();
    return 0;
}

Here's a zip:

I don't know why it won't work. :|

Anyone know why it only loads message.bmp?

I need help here. :)

I still need help here. :|

I still need help here. :|

Assuming that SDL is unable to load the image, you might place a call to immediately after the line: background = load_image("background.bmp"); to get a description of the cause of the failure.

I put SDL_GetError after it and it does nothing.
There's no errors, but the background image doesn't show.
Download the second zip I uploaded and maybe you can figure out the problem...
I need help with this. :)

Well, by doing the following ...

...
background = load_image("background.bmp");

if(background == NULL)
{
    // load_image() failed ...
    cout << "load_image() failed, error: " << SDL_GetError() << endl;
}

apply_surface(100, 100, message, screen);
...

I got output as follows:

load_image() failed, error: File is not a Windows BMP file

commented: Thanks +1

Is it an image you changed manually from .jpg (for example) to bmp by just renaming the file?

commented: Thanks +1

Ah, GIMP didn't support BMP, but I saved it anyway... I'm going to make a new background.
Thanks for the solution! :)

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.