SDL_LoadBMP() is successful, but the window becomes entirely black

672 views Asked by At

I apologize if this question has already been asked but I've been researching for about a week now and cannot find the answer any where.

The problem that I am having is that while SDL_LoadBMP() is loading the image successfully, The window does not render an image at all, and instead renders an entirely black screen. However I do know something is being loaded (not just because there is no error being returned by SDL_LoadBMP() but also) because when I run the program with the SDL_LoadBMP() call commented out the window stays entirely white.

if it helps I have been writing along with the Lazyfoo tutorial located here. Code below...

From Main.cpp

int main(int argc, char* args[])
{
    //the surface that we will be applying an image on
    SDL_Surface* ImageSurface = NULL;

    //try to initalize SDL
    try
    {
        initSDL();
    }
    //if an error is caught
    catch (string Error)
    {
        //print out the error
        cout << "SDL error occurred! SDL Error: " << Error << endl;
        //return an error
        return -1;
    }

    //try loading an image on to the ImageSurface
    try
    {
        loadMedia(ImageSurface, "ImageTest.bmp");
    }
    //if an error is caught
    catch(string Error)
    {
        //print the error out
        cout << "SDL error occurred! SDL Error: " << Error << endl;
        //return an error
        SDL_Delay(6000);
        return -1;
    }
    //Apply Image surface to the main surface
    SDL_BlitSurface(ImageSurface, NULL, Surface, NULL);

    //upadte the surface of the main window
    SDL_UpdateWindowSurface(Window);

    //wait for 2 seconds (2000 miliseconds)
    SDL_Delay(10000);

    //close SDL
    close();

    //return
    return 0;
}

From SDLBackend.cpp (I will only be posting the code relevant to the image loading process)

void loadMedia(SDL_Surface* surface, string path)
{
    cout << "Attempting to load an image!" << endl;
    //load the image at path into our surface
    surface = SDL_LoadBMP(path.c_str());
    //if there was an error in the loading procdure
    if(surface == NULL)
    {
        //make a string to store our error in
        string Error = SDL_GetError();
        //throw our error
        throw Error;
    }
    cout << "Successfully loaded an image!" << endl;
    cout << "Pushing surface into the Surface List" << endl;
    //Put the surface in to our list
    SurfaceList.push_back(surface);
    return;
}

I am compiling using visual studio 2013, and the image ImageTest.bmp is located in the same directory as the vcxproj file.

1

There are 1 answers

1
Johnny Cage On BEST ANSWER

The problem is in loadMedia(). The loaded surface is being assigned to a local variable. You'll need to use a reference to pointer,

void loadMedia(SDL_Surface*& surface, string path)
{
    surface = SDL_LoadBMP(path.c_str());
}

Or a double pointer (maybe preferred, clarifies intent),

void loadMedia(SDL_Surface** surface, string path)
{
    *surface = SDL_LoadBMP(path.c_str());
}

Alternately, you could return it or even extract it from SurfaceList.back().