2

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.

3
  • It might have to do with it being a jpg. Does it work if you convert it into a png instead?
    – Blaze
    Commented Feb 6, 2019 at 8:53
  • Thanks for your help. Yes, I tried PNG, but it did not work
    – Thomas
    Commented Feb 6, 2019 at 8:56
  • 1
    Executable's directory is not what we commonly refer to as current working directory. Relative paths are relative to CWD. You might want to launch process in "correct" working directory, change working directory by process itself (chdir), or use absolute path to your file. For a start, check your working directory is what you expect it yo be with getcwd/GetCurrentDirectory.
    – keltar
    Commented Feb 6, 2019 at 10:48

1 Answer 1

1

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;
3
  • 1
    Did you try this with an absolute path? How did you specify the absolute path?
    – Emil Vatai
    Commented Feb 6, 2019 at 10:18
  • Thanks ! One step closer! Now i get no error message about the picture. I get only in a window a black screen.
    – Thomas
    Commented Feb 6, 2019 at 14:09
  • See update above! If this doesn't work, please expand your question with an updated version of your code.
    – Emil Vatai
    Commented Feb 6, 2019 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.