enable fonts to be loaded when windows has been installed other than default location

This commit is contained in:
jettis
2024-12-06 13:03:57 +02:00
parent 38931cf65e
commit ca423994ea
7 changed files with 29 additions and 14 deletions

View File

@@ -1,3 +1,5 @@
#include "../SDLHelpers.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
@@ -5,8 +7,6 @@ using namespace std;
#include <SDL3_ttf/SDL_ttf.h> #include <SDL3_ttf/SDL_ttf.h>
#include "../SDLHelpers.h"
#include "Button.h" #include "Button.h"
Button::Button(string text, SDL_Color color, SDL_Rect pos) : Item(pos) { Button::Button(string text, SDL_Color color, SDL_Rect pos) : Item(pos) {

View File

@@ -1,3 +1,5 @@
#include "../SDLHelpers.h"
#include <string> #include <string>
using namespace std; using namespace std;
@@ -5,8 +7,6 @@ using namespace std;
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h> #include <SDL3_ttf/SDL_ttf.h>
#include "../SDLHelpers.h"
#include "Checkbox.h" #include "Checkbox.h"
bool isBetween(float a, float b, float c) { bool isBetween(float a, float b, float c) {

View File

@@ -1,3 +1,5 @@
#include "../SDLHelpers.h"
#include <vector> #include <vector>
using namespace std; using namespace std;
@@ -6,8 +8,6 @@ using namespace std;
#include <SDL3_image/SDL_image.h> #include <SDL3_image/SDL_image.h>
#include <SDL3_ttf/SDL_ttf.h> #include <SDL3_ttf/SDL_ttf.h>
#include "../SDLHelpers.h"
#include "Item.h" #include "Item.h"
#include "Scrollbar.h" #include "Scrollbar.h"
#include "Listbox.h" #include "Listbox.h"

View File

@@ -1,3 +1,5 @@
#include "../SDLHelpers.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
@@ -12,12 +14,9 @@ using namespace std;
#include <SDL3_ttf/SDL_ttf.h> #include <SDL3_ttf/SDL_ttf.h>
#include <SDL3/SDL_storage.h> #include <SDL3/SDL_storage.h>
#include "../SDLHelpers.h"
#include "Item.h" #include "Item.h"
#include "Button.h" #include "Button.h"
#include "Dialog.h" #include "Dialog.h"
#include "Frame.h"
#include "DlgMainMenu.h" #include "DlgMainMenu.h"

View File

@@ -1,3 +1,5 @@
#include "../SDLHelpers.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
@@ -6,8 +8,6 @@ using namespace std;
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h> #include <SDL3_ttf/SDL_ttf.h>
#include "../SDLHelpers.h"
#include "DropDownList.h" #include "DropDownList.h"
#include "DlgDropDownList.h" #include "DlgDropDownList.h"

View File

@@ -1,3 +1,5 @@
#include "../SDLHelpers.h"
#include <string> #include <string>
using namespace std; using namespace std;
@@ -5,8 +7,6 @@ using namespace std;
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h> #include <SDL3_ttf/SDL_ttf.h>
#include "../SDLHelpers.h"
#include "Listbox.h" #include "Listbox.h"
Listbox::Listbox(vector<string> list, int item_size, SDL_Rect pos) : Item(pos) { Listbox::Listbox(vector<string> list, int item_size, SDL_Rect pos) : Item(pos) {

View File

@@ -8,6 +8,10 @@
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#ifdef SDL_PLATFORM_WINDOWS
#include <windows.h>
#endif
static const char *font_paths[] = {"C:/windows/fonts/", "/usr/share/fonts/TTF/", "./"}; static const char *font_paths[] = {"C:/windows/fonts/", "/usr/share/fonts/TTF/", "./"};
static const short font_path_count = 3; static const short font_path_count = 3;
@@ -31,12 +35,24 @@ static SDL_Color getColor(Uint8 r, Uint8 g, Uint8 b) {
static TTF_Font *openFont(std::vector<std::string> fonts, int size) { static TTF_Font *openFont(std::vector<std::string> fonts, int size) {
TTF_Font *font = NULL; TTF_Font *font = NULL;
for (int x = 0; x < fonts.size(); x++) { 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++) { for (int y = 0; y < font_path_count; y++) {
std::string val(font_paths[y]); std::string val(font_paths[y]);
val.append(fonts[x]); val.append(fonts[x]);
font = TTF_OpenFont(val.c_str(), size); font = TTF_OpenFont(val.c_str(), size);
if (font) if (font) {
return font; return font;
}
} }
} }
return NULL; return NULL;