Search code examples
csdlsdl-2

SDL2 > PNG IMAGE > couldn't load the image


SITUATION

I want to load and use my image (which is .png) in my app. However when I try to load the image I get the following error

Error at ./sources/texture.c:9: 'The texture is NULL'

> Couldn't open pawn.png

CODE

texture.c

texture_t*
texture_load(renderer_t *renderer, const char *path) {
    texture_t *tex = IMG_LoadTexture(renderer, path);

    if (tex == NULL)
        error_print(AT, "The texture is NULL");

    return tex;
}

game.c

game_t*
game_create() {
    ...
    IMG_Init(IMG_INIT_PNG);
    ...
    texture_load(renderer, "pawn.png")
    ...
}

FOLDER STRUCTURE

.
├── build
│   ├── app
│   └── pawn.png
├── headers
│   ...
│   ├── game.h
|   ...
│   ├── texture.h
│   ...
├── resources
│   └── pawn.png
└── sources
    ...
    ├── game.c
    ...
    ├── texture.c
    ...

BONUS

if You want to see the whole "image" of my project then You can click here


Solution

  • Maybe it's a silly hypothesis, but are you sure the file is in the current application directory?

    On a POSIX system you can check it with:

    if (access("pawn.png", F_OK) == 0) {
        printf("File exists\n");
    } else {
        printf("File doesn't exist\n");
    }
    

    If it prints File doesn't exist, then you need to specify a full path to the file.