#ifndef SDLHELPERS_H #define SDLHELPERS_H #include #include #include #include #include #include #ifdef SDL_PLATFORM_WINDOWS #include #endif #ifdef SDL_PLATFORM_ANDROID #include #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 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