我尝试通过SDL_OpenFont()
函数打开字体,但返回nullptr
,TTF_GetError()
返回“库未初始化”。
SDL_Init(SDL_INIT_EVERYTHING)
和TTF_Init()
都返回0。
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cout << "Couldn't initialize SDL: " << SDL_GetError() << std::endl;
return 1;
}
if(TTF_Init() < 0) {
std::cout << "Couldn't initialize TTF lib: " << TTF_GetError() << std::endl;
return 1;
}
TTF_Font* font = TTF_OpenFont("DroidSans.ttf", 25);
if(!font) {
std::cout << "Can't open font: " << TTF_GetError() << std::endl;
}
这是输出:
无法打开字体:库未初始化
这是我的CMakeLists.txt
:
cmake_minimum_required(VERSION 3.9)
project(proj)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${proj_SOURCE_DIR}/bin)
set(CMAKE_CXX_STANDARD 14)
include_directories(${SDL2_INCLUDE_DIRS})
INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2TTF REQUIRED SDL_ttf >= 2.0.0)
set(SOURCES main.cpp)
add_executable(proj ${SOURCES})
target_link_libraries(proj ${SDL2_LIBRARIES} ${SDL2TTF_LIBRARIES})
怎么了?