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.

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 SDL_GetError() 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.