SDL bitmap problem

I have tried to follow the second Lazyfoo SDL tutorial (accessible by the following link): http://lazyfoo.net/SDL_tutorials/lesson02/index.php

#i have encountered a problem during the program's runtime: the images will not print to the screen. I have figured out that this was a problem with the "load_image" function (as I had changed the initialization function of the variables using it to SDL_LoadBMP and it worked fine). I modified the program so that both the load_image and apply_surface functions were placed inside a class - still no improvement. The code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <string>
#include "SDL/SDL.h"

using namespace std;

class functions{
    public:
SDL_Surface* load_image(string filename)
{
    SDL_Surface* unoptimisedLoadedImage = NULL;
    SDL_Surface* optimisedLoadedImage = NULL;
    unoptimisedLoadedImage = SDL_LoadBMP( filename.c_str() );
    if(unoptimisedLoadedImage != NULL)
    {
        optimisedLoadedImage = SDL_DisplayFormat(unoptimisedLoadedImage);
        SDL_FreeSurface(unoptimisedLoadedImage);
    }
    return optimisedLoadedImage;
}


void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
//Temporary rectangle to hold offsets.
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface(source, NULL, destination, &offset);
}
};
int main(int argc, char* args[])
{
    functions Fo;
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;
    }
    SDL_Surface* Message = Fo.load_image("EthanChromeCrystal.bmp");
    SDL_Surface* Background = Fo.load_image("SDLTestBg1.bmp");
    SDL_Surface* Screen = SDL_SetVideoMode(960,600,32,SDL_SWSURFACE);
    if(Screen == NULL)
    {
        return 1;
    }
    SDL_WM_SetCaption("Example Program!", NULL);
    Fo.apply_surface(0,0,Background,Screen);
    Fo.apply_surface(480,0,Background,Screen);
    Fo.apply_surface(0,300,Background,Screen);
    Fo.apply_surface(480,300,Background,Screen);
    Fo.apply_surface(240,150,Message,Screen);
    if(SDL_Flip(Screen)==-1)
    {
        return 1;
    }
    SDL_Delay(5000);
    SDL_FreeSurface(Message);
    SDL_FreeSurface(Background);
    SDL_Quit();
    return 0;
}


What have I done wrong this time? Thanks!
Last edited on
Make sure that your program can find the image files. load_image returns NULL if it failed to load the image.
It can find the images. It is an error with the load_image() function (I know this because I changed one of the load_image() functions in main() to Load_BMP() and that image loaded fine). I removed the SDL_FreeSurface() and set it to return unoptimisedLoadedImage. That worked. I compiled and ran the code that I downloaded from the website and it worked a treat. What have I done wrong?
I think the problem is that SDL_SetVideoMode has to be called before calling SDL_DisplayFormat otherwise SDL_DisplayFormat doesn't know what format it should change the surface to.
Last edited on
Topic archived. No new replies allowed.