SDL3 TTF produces error "Text has zero width"
This piece of code:
#include
#include
#include
#include
#include
#include
int main() {
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
char* mem = nullptr;
size_t size = 0;
{
std::ifstream font_load{"test_font.ttf", std::ios_base::in | std::ios_base::binary};
font_load.seekg(0, std::ios_base::end);
size = font_load.tellg();
font_load.seekg(0);
mem = new char[size];
font_load.read(mem, size);
}
SDL_IOStream* io = SDL_IOFromConstMem(mem, size);
TTF_Font* f = TTF_OpenFontIO(io, true, 24.0f);
delete mem;
if( f == nullptr ) {
std::cout << "Font load error: " << SDL_GetError() << std::endl;
} else {
SDL_Color f_color{255,255,255,255};
std::string test_text{ "Example text" };
SDL_Surface* surf = TTF_RenderText_Blended(
f,
test_text.c_str(),
test_text.size(),
f_color
);
if( surf == nullptr ) {
std::cout << "Surface creation error: " << SDL_GetError() << std::endl;
} else {
std::cout << "Surface created!" << std::endl;
SDL_DestroySurface( surf );
}
TTF_CloseFont( f );
}
TTF_Quit();
SDL_Quit();
return 0;
}
...after compiling under MSVC on Windows prints:
Surface creation error: Text has zero width
What may be the cause of "text has zero width" error? I tried also loading font directly with TTF_Font* f = TTF_OpenFont("test_font.ttf", 24.0f); which works. However in my production code sometimes I have to load font thru SDL's IO.