Search code examples
c++visual-studiosdl

SDL-Image: Couldn't open image


Unfortunately I do not get a picture but only a white screen. I am currently learning c ++ and sdl.

error message:

SDL_image Error: Couldn't open test.jpg

I am using Visual Studio on a Windows 10 computer.

#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;;

string ExePath() {
    char buffer[MAX_PATH];
    GetModuleFileName(NULL, buffer, MAX_PATH);
    string::size_type pos = string(buffer).find_last_of("\\/");
    return string(buffer).substr(0, pos);
}
int main(int argc, char* args[]) {
    cout << "my directory is " << ExePath() << "\n";

if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
    std::cout << "SDL load fail" << std::endl;
    return -1;
}
SDL_Window* window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN);
if (window == NULL) {
    std::cout << "Window load fail." << std::endl;
    return -1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
    std::cout << "Renderer load fail." << std::endl;
    return -1;
}

SDL_Texture* background = IMG_LoadTexture(renderer, "test.jpg");
if (background == NULL) {
    printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
    SDL_Delay(5000);
    return -1;
}
SDL_Rect pos;
pos.x = 20;
pos.y = 30;
pos.w = 460;
pos.h = 300;

SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background, NULL, &pos);
SDL_RenderPresent(renderer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
atexit(SDL_Quit);
return 0;
}

I have already tried the absolute path. The picture is also in the correct folder. Smaller pictures, jpeg, png. I do not know.


Solution

  • SDL Image is a separate library. You should initialize it befor using it. Something like this:

        ////...
        int imgFlags = IMG_INIT_JPG; // or IMG_INIT_PNG; 
        // not sure about the imgFlags parameter, read the docs.
        if( !( IMG_Init( imgFlags ) & imgFlags ) )
        {
            printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
        }
        /// the rest is the same...
        SDL_Texture* background = IMG_LoadTexture(renderer, "test.jpg");
        if (background == NULL) {
            printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
            SDL_Delay(5000);
            return -1;
        }
        /// ...
    

    Some reference https://discourse.libsdl.org/t/help-with-initializing-sdl-image/23601

    EDIT: And for the "one step closer": I think your delay is in the wrong place.

        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, background, NULL, &pos);
        SDL_RenderPresent(renderer);
        SDL_Delay(5000);  //////// For example try putting int here
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        atexit(SDL_Quit);
        std::cout << "Bye!" << std::endl;
        return 0;