Files
SDLRousku/SDLHelpers.h
2025-09-23 00:27:56 +03:00

80 lines
1.9 KiB
C++

#ifndef SDLHELPERS_H
#define SDLHELPERS_H
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <vector>
#include <string>
#include <iostream>
#include <cmath>
#ifdef SDL_PLATFORM_WINDOWS
#include <windows.h>
#endif
#ifdef SDL_PLATFORM_ANDROID
#include <SDL3/SDL_iostream.h>
#endif
static const char *font_paths[] = {"C:/windows/fonts/", "/usr/share/fonts/TTF/", "./", "./assets/"};
static const short font_path_count = 3;
static SDL_Rect getRect(Sint16 x, Sint16 y, Uint16 w, Uint16 h) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
return rect;
}
static SDL_Color getColor(Uint8 r, Uint8 g, Uint8 b) {
SDL_Color color;
color.r = r;
color.g = g;
color.b = b;
return color;
}
static TTF_Font *openFont(std::vector<std::string> fonts, int size) {
TTF_Font *font = NULL;
#ifdef SDL_PLATFORM_ANDROID
SDL_IOStream* rw = SDL_IOFromFile("FreeSans.ttf", "rb");
if (!rw) {
SDL_Log("SDL_IOFromFile failed: %s", SDL_GetError());
return nullptr;
}
font = TTF_OpenFontIO(rw, 1, size); // 1 = auto-free the IOStream
if (!font) {
SDL_Log("TTF_OpenFontRW failed: %s", SDL_GetError());
return nullptr;
}
return font;
#endif
for (int x = 0; x < fonts.size(); x++) {
#ifdef SDL_PLATFORM_WINDOWS
TCHAR windir[MAX_PATH];
GetWindowsDirectory(windir, MAX_PATH);
std::string windir_s(windir);
windir_s.append("/fonts/");
windir_s.append(fonts[x]);
font = TTF_OpenFont(windir_s.c_str(), size);
if (font) {
return font;
}
#endif
for (int y = 0; y < font_path_count; y++) {
std::string val(font_paths[y]);
val.append(fonts[x]);
font = TTF_OpenFont(val.c_str(), size);
if (font) {
return font;
}
}
}
return NULL;
}
#endif