2

While trying to load a .png file with IMG_LoadTexture(renderer, "idle.png") SDL_GetError() says: "Couldn't open idle.png" There are no compiler errors, just a black window appears.

This is my main.cpp

#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>

int main(int argc,  char** argv) {
    SDL_Event event;
    SDL_Renderer *renderer = NULL;
    SDL_Texture *texture = NULL;
    SDL_Window *window = NULL;

    SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
    SDL_CreateWindowAndRenderer(
            800, 600,
            0, &window, &renderer
    );
    IMG_Init(IMG_INIT_PNG);
    texture = IMG_LoadTexture(renderer, "idle.png");
    std::cout << SDL_GetError();

    while (1) {
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderPresent(renderer);
        if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
            break;
    }
    SDL_DestroyTexture(texture);
    IMG_Quit();
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return EXIT_SUCCESS;
}

But I guess the problem is the way I link the library. I installed sdl2, sdl2_image and libpng.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
project(untitled)

set(CMAKE_CXX_STANDARD 17)

add_executable(untitled main.cpp)

INCLUDE(FindPkgConfig)

PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)

INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(untitled ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES})
Karol
  • 23
  • 4
  • 3
    Are you sure the file is in your current working directory of your process (since it seems that's where you try to load it from - the CWD is not necessarily the same directory as the executable is stored in)? – Jesper Juhl Nov 28 '18 at 16:49
  • idle.png, main.cpp and CMakeLists.txt are in folder untitled (name of the project) – Karol Nov 28 '18 at 16:52
  • 3
    Source location is irrelevant. What matters is the location of the file at runtime compared to the CWD of the process. 3 easy ways to fix: 1) change the cwd at runtime to where the file is and load as you do now. 2) provide an absolute path to the file when loading so cwd is irrelevant. 3) obtain the path to the executable at runtime and then construct a path to the file relative to where the executable is. – Jesper Juhl Nov 28 '18 at 16:58
  • It worked with absolute path. Thanks. – Karol Nov 28 '18 at 17:03
  • The problem with an absolute path is that it'll beeak when you move your files. I prefer constructing a executable relative path to resources. – Jesper Juhl Nov 28 '18 at 17:05

1 Answers1

2

You are loading the image from the current working directory (CWD) of your application. That is not necessarily the same directory as your executable is in (it depends on how it is launched), which you seem to assume.

3 easy ways to fix:

  1. change the cwd at runtime to where the file is and load as you do now.
  2. provide an absolute path to the file when loading, so cwd is irrelevant.
  3. obtain the path to the executable at runtime and then construct a path to the file relative to where the executable is. (best option in my opinion since it's robust against moving your project around/installing to a different location).
genpfault
  • 51,148
  • 11
  • 85
  • 139
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70