ywbird
ywbird

Reputation: 105

How can I open .ttf font in SDL2_ttf?

I'm building simple SDL2 project on Windows, and I want to print some text on SDL2 screen.
It works with absolute path, but I want to use relative path. I've tried relative path on TTF_OpenFont but it returns nullptr.

Project directory:

Project Directory
├─bin
│  └─debug
│      main.exe
│      SDL2.dll
│      SDL2_image.dll
│      SDL2_ttf.dll
│
├─res
│      font.ttf
│
└─src
       main.cpp
       renderwindow.cpp
       renderwindow.hpp

renderwindow.cpp:

#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>

//...

TTF_Font *font = TTF_OpenFont("../res/font.ttf", 16);
if (font == nullptr)
{
    std::cout << "Font couldn't load." << std::endl;
}

//... font displaying stuff

Output:

Font couldn't load.

I don't know weather relative path starts at wherer cpp file is or executable file is so, I tried both.
TTF_OpenFont("../res/font.ttf", 16) and TTF_OpenFont("../../res/font.ttf", 16)
And, sicne I'm on Windows, I tired with different directory separator by replacing / with \\. But none of them worked.

Upvotes: 0

Views: 282

Answers (0)

Related Questions