Failed to init SDL image

The code is:
> #include “main.h”
>
> SDL_Window* gWindow = nullptr;
>
> int main() {
> int errcode;
> if ((errcode = SDL_Init(SDL_INIT_VIDEO))) {
> printf(“error: sdl failed to init. (%d)\n”,errcode);
> goto end;
> }
>
> if((errcode = IMG_Init( IMG_INIT_PNG )) != IMG_INIT_PNG) {
> printf(“error: failed to init image. (%d) (%s)\n”,errcode, SDL_GetError());
> goto end;
> }
>
> gWindow = SDL_CreateWindow(“wtf just test”, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
> if(gWindow == nullptr) {
> printf(“error: failed to create window.”);
> goto end;
> }
> SDL_Delay(5000);
> end:
> return false;
> }

And output is:

error: failed to init image. (0) (PNG images are not supported)

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(foo)

set(ToBuild “bin/foo”)

set(CMAKE_EXE_LINKER_FLAGS “-static-libgcc -static-libstdc++”)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} “${CMAKE_SOURCE_DIR}/cmake/”)

aux_source_directory(src DIR_SRC)

find_package(SDL2 REQUIRED)

find_package(SDL2_image REQUIRED)

include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS})

add_executable(${ToBuild} ${DIR_SRC})

target_link_libraries(${ToBuild} ${SDL2_LIBRARY} ${SDL2_IMAGE_LIBRARY})

Anyway to fix it?

(I’m using Solus Linux)

The code example given in the docs is:

// load support for the JPG and PNG image formats
int flags=IMG_INIT_JPG|IMG_INIT_PNG;
int initted=IMG_Init(flags);
if((initted&flags) != flags) {
printf(“IMG_Init: Failed to init required jpg and png support!\n”);
printf(“IMG_Init: %s\n”, IMG_GetError());
// handle error
}

So maybe you should be testing if IMG_Init is returning an int with the IMG_INIT_PNG bit set, rather than if it is equal to IMG_INIT_PNG or not?

I’ve gotten away with not even initializing it most of the time.
Even the docs say:
“No initialization is needed nor performed when using the IMG_isJPG , IMG_isPNG , and IMG_isTIF functions.”
This documentation is for the old version of SDL I think, but it remained the same even when
SDL2 came out so I assume it’s still valid.
https://www.libsdl.org/projects/SDL_image/docs/SDL_image_frame.html