3

I try to open a font via the SDL_OpenFont() function, but it returns nullptr and TTF_GetError() returns "Library not initialized".

SDL_Init(SDL_INIT_EVERYTHING) and TTF_Init() both return 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;
}

This is the output:

Can't open font: Library not initialized

This is my 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})

What's wrong?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
svloyso
  • 133
  • 1
  • 6

0 Answers0