gpt4 book ai didi

c++ - “IMG_Load”未在此范围 SDL 2 中声明

转载 作者:太空狗 更新时间:2023-10-29 20:02:48 28 4
gpt4 key购买 nike

我正在尝试使用 SDL 创建一个应用程序,我想在我的窗口上添加一个图标,但是当我编译时我得到这个错误:

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

我正在使用 cmakemake 进行编译,这里是 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
)

这里是 ma​​in.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...

感谢您的帮助!

最佳答案

嗯...不确定,但也许试试这个?

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

按顺序包含两个标题。

关于c++ - “IMG_Load”未在此范围 SDL 2 中声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35210110/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com