我正在尝试使用SDL创建应用程序,我想在我的窗口上添加一个图标但是当我编译时出现此错误:
error: ‘IMG_Load’ was not declared in this scope
SDL_Surface * icon_surface = IMG_Load(icon);
^
我正在使用 cmake 和 make 进行编译,这里是 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
)
这里是 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;
}
我尝试在include目录中添加SDL_image.h,但是我收到错误,因为编译器无法找到SDL_image.h中指定的SDL.h ...
感谢您的帮助!
答案 0 :(得分:3)
嗯...不确定,但也许试试这个?
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
按顺序包含两个标题。
答案 1 :(得分:3)
除了Justin Time建议的包含<SDL2/SDL_image.h>
之外,您还必须提供库(SDL2_image
)以链接您的cmake文件中的可执行文件,如此;
target_link_libraries(
${EXECUTABLE_NAME}
GL
GLEW
SDL2
SDL2_image
)
我刚刚测试了您的代码,并且通过上述修改运行良好。确保您的可执行文件可以访问"../../assets/icon.png"
。希望有所帮助。
答案 2 :(得分:1)
好
所以我是个白痴,因为我没有安装libsdl2-image-2.0-0
...我看了你发给我的是Gestrong,我将 CMakeList.txt 更改为以下内容:
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}
)
我在我的代码中添加了#include <SDL2/SDL_image.h>
,现在它可以工作:)谢谢!