Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

‘IMG_Load’ was not declared in this scope SDL 2

I'm trying to make an application with SDL and I would like to add an icon on my window but when I compile I get this error :

error: ‘IMG_Load’ was not declared in this scope
  SDL_Surface * icon_surface = IMG_Load(icon);
                                            ^

I'am using cmake and make for the compilation, here is the CMakeLists.txt

cmake_minimum_required (VERSION 2.6)

set(PROJECT_NAME "Engine")


project (${PROJECT_NAME})


SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c++0x")

if( CMAKE_SIZEOF_VOID_P EQUAL 8 )

    MESSAGE( "64 bits compiler detected" )
    SET( EX_PLATFORM 64 )
    SET( EX_PLATFORM_NAME "x64" )

    SET( EXECUTABLE_NAME ${PROJECT_NAME}-x64 )

else( CMAKE_SIZEOF_VOID_P EQUAL 8 ) 

    MESSAGE( "32 bits compiler detected" )
    SET( EX_PLATFORM 32 )
    SET( EX_PLATFORM_NAME "x86" )

    SET( EXECUTABLE_NAME ${PROJECT_NAME}-x86 )

endif( CMAKE_SIZEOF_VOID_P EQUAL 8 )

# The executable file will be generate in the bin/ directory
set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})


# All .h files are in the include/ directory
include_directories(
    ../include/
)


# All .cpp files are in the src/ directory
file(
    GLOB_RECURSE
    SOURCE_FILES
    ../src/*
)

add_executable(
    ${EXECUTABLE_NAME}
    ${HEADER_FILES}
    ${SOURCE_FILES}
)

target_link_libraries(
    ${EXECUTABLE_NAME}
    GL
    GLEW 
    SDL2
)

and here the main.cpp

#include <stdio.h>
#include <stdlib.h>

#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <glm/glm.hpp>

using namespace glm;
using namespace std;

SDL_Window * CreateWindow(
    const char * title,
    int x,
    int y,
    int w,
    int h,
    Uint32 flags,
    const char * icon
);

SDL_Renderer * CreateRenderer(
    SDL_Window * window,
    Uint32 flags,
    int r,
    int g,
    int b
);

void Init(Uint32 flags);

int main(void)
{

    Init(SDL_INIT_EVERYTHING);

    SDL_Window * window = CreateWindow("Prism", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, /*SDL_WINDOW_FULLSCREEN_DESKTOP*/ SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL, "../../assets/icon.png");

    SDL_Renderer * renderer = CreateRenderer(window, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC, 0, 0, 0);

    bool end = false;
    SDL_Event events;


    while(!end) {
        SDL_WaitEvent(&events);

        if(events.window.event == SDL_WINDOWEVENT_CLOSE)
            end = true;


        SDL_RenderClear(renderer); // On nettoie l'écran


        SDL_RenderPresent(renderer); // Et on effectue le rendu
    }

    SDL_DestroyWindow(window);

    SDL_Quit();

    return(EXIT_SUCCESS);
}

void Init(Uint32 flags){

    if(SDL_Init(flags) < 0){
        printf("Erreur lors de l'initialisation de la SDL : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
}

SDL_Window * CreateWindow(const char * title, int x, int y, int w, int h, Uint32 flags, const char * icon)
{
    SDL_Window * window = SDL_CreateWindow(title, x, y, w, h, flags);

    if(window == NULL){
        printf("Erreur lors de la création de la fenêtre : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    SDL_Surface * icon_surface = IMG_Load(icon);

    if(icon_surface == NULL){
        printf("Erreur lors de la récupération de l'icone : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    SDL_SetWindowIcon(window, icon_surface);
    SDL_FreeSurface(icon_surface);

    SDL_ShowCursor(SDL_DISABLE);
    return window;
}

SDL_Renderer * CreateRenderer(SDL_Window * window, Uint32 flags, int r, int g, int b)
{
    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, flags);

    if(renderer == NULL){
        printf("Erreur lors de la création du renderer : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    if(SDL_SetRenderDrawColor(renderer, r, g, b, 255) < 0){
        printf("Erreur lors de la modification de la couleur de fond du renderer : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }


    return renderer;
}

I tried to add SDL_image.h in the include directory but I get an error because the compiler isn't able to find the SDL.h specified in the SDL_image.h...

Thank you for your helps !

like image 761
geauser Avatar asked Dec 24 '22 09:12

geauser


2 Answers

Hmm... not sure, but maybe try this?

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

Include both headers, in that order.

like image 130
Justin Time - Reinstate Monica Avatar answered Dec 28 '22 10:12

Justin Time - Reinstate Monica


Aside from include <SDL2/SDL_image.h> as suggested by Justin Time, you would also have to supply the library (SDL2_image) to link against your executable in your cmake file, like so;

target_link_libraries(
    ${EXECUTABLE_NAME}
    GL
    GLEW 
    SDL2
    SDL2_image
)

I just tested your code and it runs fine with above modification. Make sure your "../../assets/icon.png" is accessible to your executable. Hope that helps.

like image 32
share Avatar answered Dec 28 '22 09:12

share