Search code examples
c++filebitmapsdlsdl-2

Cannot get SDL_LoadBMP to display an image(C++)?


    void MainGame::drawGame() {
    glClearDepth(1.0);
    // clear colour and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    windowSurface = SDL_GetWindowSurface(_window);
    menuImage = SDL_LoadBMP("\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp");
    if (menuImage == NULL) {
        fatalError("Unable to load bitmap, 'ForhalenIntro_Screen.bmp'!");
    }
    //swap buffer/window displayed and draw to screen
    SDL_GL_SwapWindow(_window);
}

// Wont exit unless _gameState is equal to EXIT
void MainGame::gameLoop() {
    while (_gameState != GameState::EXIT) {
        procInput();
        drawGame();
    }

}

I am trying to display a bitmap image onto my window. I created an SDL_Surface for my window, and an SDL_Surface for my image initialized to NULL. My error, "Unable to load bitmap, 'ForhalenIntro_Screen.bmp'!" is returning, so I know the code is failing at the line where menuImage is assigned the bitmap function with the path to the image as the argument. I've double checked the file name, location and path. I've tried having just the filename as the path. The file is in the same folder as my vcrxproj file and main.cpp file. Where have I gone wrong? I am not getting any syntax errors and I have obviously included the necessary header files. EDIT: I've now also tried it with SDL_image but it still didn't work.


Solution

  • Your issue here is unintentional escaping (and most likely an incorrect path).

    menuImage = SDL_LoadBMP("\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp");
    

    Assuming this is in the default directory Visual Studio creates for you, the correct path would be: "C:\Users\Liam C\Documents\Visual Studio 2015\Projects\graphicsPractice\graphicsPractice\ForehalenIntro_Screen.bmp"

    In most programming languages, a \ is used to create a special character that cannot normally be typed, such as a null character ('\0') or a Unicode character (u16'\u263A' or u32'\U0001F60A'). So your use of the \ character in your string is (accidentally) trying to make special characters from '\L', '\D', '\V', '\P', '\g', '\g', and '\F'.

    You can either double up the backslashes ('\\' makes a \ character) or change the separators to forward slashes:

    menuImage = SDL_LoadBMP("C:\\Users\\Liam C\\Documents\\Visual Studio 2015\\Projects\\graphicsPractice\\graphicsPractice\\ForehalenIntro_Screen.bmp");
    // OR
    menuImage = SDL_LoadBMP("C:/Users/Liam C/Documents/Visual Studio 2015/Projects/graphicsPractice/graphicsPractice/ForehalenIntro_Screen.bmp");