5

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 !

geauser
  • 1,005
  • 8
  • 20

3 Answers3

5

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

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

Include both headers, in that order.

  • Nope doesn't work... compiler can't find it... I tried to modify all the include in the **SDL_image.h** remplacing `#include "SDL.h"` by `#include` but i get another error – geauser Feb 04 '16 at 20:01
  • @LittleStrongMind Don't change `SDL_image.h`. Change `main.cpp`. `SDL.h` and `SDL_image.h` should be in the same folder, so `SDL_image.h` is correct in including `SDL.h` instead of `SDL/SDL.h`. – Justin Time - Reinstate Monica Feb 04 '16 at 20:17
  • I'm trying to change the way my folders are organized and `CMakeLists. txt` to avoid the use of libs installed on the distribution, i'll test your solution in a few minutes. – geauser Feb 04 '16 at 20:29
  • You should change some settings in CMakelists.txt. try to use sdl2 with pkgconfig in the file so it sets you up with the correct search paths. You must do that for both sdl2 and sdl_image2 – Gerhard Stein Feb 04 '16 at 20:30
  • @Gerstrong do you have an example for me ? It's my first time with cmake so i'm not comfortable with – geauser Feb 04 '16 at 20:40
  • Sure: https://github.com/gerstrong/Commander-Genius/blob/master/src/CMakeLists.txt. but please edit for your sources first... – Gerhard Stein Feb 04 '16 at 20:43
4

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.

share
  • 315
  • 2
  • 6
  • Is your icon image, `"../../assets/icon.png"` accessible from your build directory? Otherwise your executable won't find it and will crash. – share Feb 04 '16 at 20:29
3

Ok So I'm an idiot because I didn't install libsdl2-image-2.0-0... I looked at what you sent me Gestrong and I changed my CMakeList.txt to the following :

cmake_minimum_required (VERSION 2.6)

SET(PROJECT_NAME "Engine")


PROJECT(${PROJECT_NAME})


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

INCLUDE(FindPkgConfig)
INCLUDE(FindOpenGL REQUIRED)
INCLUDE(FindGLEW REQUIRED)
INCLUDE(FindSDL REQUIRED)


PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image)

INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR})

SET(EXECUTABLE_OUTPUT_PATH bin/)

INCLUDE_DIRECTORIES(../include/)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${SDL2IMAGE_INCLUDE_DIRS})

FILE(
    GLOB_RECURSE
    SOURCE_FILES
    ../src/*
)


ADD_EXECUTABLE(
    ${PROJECT_NAME}
    ${SOURCE_FILES}
)


TARGET_LINK_LIBRARIES(
    ${PROJECT_NAME}
    ${SDL2_LIBRARIES}
    ${SDL2IMAGE_LIBRARIES}
    ${OPENGL_LIBRARIES}
    ${GLEW_LIBRARIES} 
)

I added #include <SDL2/SDL_image.h> in my code and now it WORKS :) thank you !

geauser
  • 1,005
  • 8
  • 20