【发布时间】:2021-06-20 16:53:41
【问题描述】:
我想将 SDL2 添加到 Cmake 项目中,在 Windows 上使用 VSCode 中的 C++,但使用来自 Msys2 (g++) 的 Mingw64。
这是我当前的 CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(TileGameStudio_Runtime LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(PROJ_SRC
${PROJECT_SOURCE_DIR}/src
)
add_subdirectory(${PROJECT_SOURCE_DIR}/SDL2)
include_directories(${PROJECT_SOURCE_DIR}/SDL2/include)
file(GLOB PROJECT_SOURCES CONFIGURE_DEPENDS
${PROJ_SRC}/*.cpp
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}\\Game")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}\\Game")
# add the executable
add_executable(TileGameStudio_Runtime
${PROJECT_SOURCES}
)
target_link_libraries(TileGameStudio_Runtime PRIVATE
SDL2-static
SDL2main
)
set_target_properties(
TileGameStudio_Runtime
PROPERTIES
OUTPUT_NAME "Game"
SUFFIX ".exe"
)
This is my current Project Structure (img)
如您所见。我从https://github.com/libsdl-org/SDL 克隆了回购作为我项目的子目录。而这个,我是通过 cmake add_subdirectory 添加的。
我的 Main.cpp 就是这样的:
#include <iostream>
#include <vector>
#include <string>
#include <SDL.h>
#ifdef main
# undef main
#endif /* main */
using namespace std;
int main(int argc, char *argv[]) {
SDL_Window *win = NULL;
SDL_Renderer *renderer = NULL;
int posX = 100, posY = 100, width = 320, height = 240;
SDL_Init(SDL_INIT_VIDEO);
win = SDL_CreateWindow("Hello World", posX, posY, width, height, 0);
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
//================================================ Draw a Text
//this opens a font style and sets a size
TTF_Font* Sans = TTF_OpenFont("Sans.ttf", 24);
// this is the color in rgb format,
// maxing out all would give you the color white,
// and it will be your text's color
SDL_Color White = {255, 255, 255};
// as TTF_RenderText_Solid could only be used on
// SDL_Surface then you have to create the surface first
SDL_Surface* surfaceMessage = TTF_RenderText_Solid(Sans, "put your text here", White);
SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
SDL_Rect Message_rect; //create a rect
Message_rect.x = 0; //controls the rect's x coordinate
Message_rect.y = 0; // controls the rect's y coordinte
Message_rect.w = 100; // controls the width of the rect
Message_rect.h = 100; // controls the height of the rect
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
//================================================ Draw a Text End
while (1) {
SDL_Event e;
if (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
应该创建一个 Window.. 并可能在其中绘制一个文本。 但是,找不到 SDL.h...所以我不能在这里构建 Stuff..
【问题讨论】:
-
你在 MSYS2 中安装了 SDL 包吗?您正在运行哪种 MSYS2 模式? (在终端提示符中以洋红色表示
MINGW64或MSYS或其他什么?) -
SDL2 不是通过 msys 或其他任何方式加载的。 Repo(在原始帖子中链接)直接克隆到项目文件夹中,并通过 cmake 的 add_subdirectories 和 target_link_libraries 链接。 Msys2 用于获取 mingw64 和 gcc 编译器。终端中的错误正在发生导致无法找到 SDL.h.. 根据 SDL.h 的来源,这会产生更多错误
-
抱歉,没注意到。您是否有理由不想使用预建的?您需要安装一堆依赖项才能正确构建它......(这不是导致您的问题的原因,但没有依赖项,生成的构建可能会不如官方构建。)
-
也找不到 msys 和 tarball 版本.. + 构建 tarball 版本的 make 命令有问题.. 这就是我想尝试 Repo 版本的原因。
-
如果两者都不起作用,请返回到 MSYS2 包。请告诉我您安装的确切软件包,以及您正在运行的 MSYS2 模式(有关如何确定模式,请参阅我的第一条评论)。
标签: c++ visual-studio-code cmake sdl-2 mingw-w64