mirror of
				https://github.com/lov3b/Pong.git
				synced 2025-11-03 23:00:19 +01:00 
			
		
		
		
	lookup linux font on runtime
This commit is contained in:
		@@ -11,9 +11,8 @@ the following command.
 | 
				
			|||||||
apt install cmake build-essential libsdl2-dev libsdl2-gfx-dev libsdl2-ttf-dev
 | 
					apt install cmake build-essential libsdl2-dev libsdl2-gfx-dev libsdl2-ttf-dev
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
DejaVuSans is also expected to be in the same place as it is under compile time. You will need to make sure to compile
 | 
					DejaVuSans is also expected to be present at the default location under linux whilst Arial is under Windows and macOS. 
 | 
				
			||||||
for the same distro that you're using, and also have the font installed. The package under debian is
 | 
					The package under debian is called  `fonts-dejavu-core`
 | 
				
			||||||
called  `fonts-dejavu-core`
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Compiling
 | 
					## Compiling
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,30 +3,35 @@
 | 
				
			|||||||
//
 | 
					//
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <filesystem>
 | 
					#include <filesystem>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const char* getLinuxFilePath() {
 | 
					const char *getLinuxFilePath() {
 | 
				
			||||||
    const char *fonts[] = {
 | 
					    const char *fonts[] = {"/usr/share/fonts/truetype/DejaVuSans-Bold.ttf", // openSUSE
 | 
				
			||||||
            "/usr/share/fonts/truetype/DejaVuSans-Bold.ttf", // openSUSE
 | 
					 | 
				
			||||||
                           "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", // Debian
 | 
					                           "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", // Debian
 | 
				
			||||||
                           "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", // Arch
 | 
					                           "/usr/share/fonts/TTF/DejaVuSans-Bold.ttf", // Arch
 | 
				
			||||||
                           "/usr/share/fonts/dejavu-sans-fonts/DejaVuSans-Bold.ttf", // Fedora
 | 
					                           "/usr/share/fonts/dejavu-sans-fonts/DejaVuSans-Bold.ttf", // Fedora
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
    for (const char *font : fonts)
 | 
					    for (const char *font: fonts)
 | 
				
			||||||
        if (std::filesystem::exists(font))
 | 
					        if (std::filesystem::exists(font))
 | 
				
			||||||
            return font;
 | 
					            return font;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return nullptr;
 | 
					    return nullptr;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const char *getDefaultFontPath() {
 | 
				
			||||||
#if defined(_WIN32) || defined(_WIN64)
 | 
					#if defined(_WIN32) || defined(_WIN64)
 | 
				
			||||||
const char* defaultFontPath = "C:\\Windows\\Fonts\\Arial.ttf";
 | 
					    return "C:\\Windows\\Fonts\\Arial.ttf";
 | 
				
			||||||
#elif defined(__linux__)
 | 
					#elif defined(__linux__)
 | 
				
			||||||
const char *defaultFontPath = getLinuxFilePath();
 | 
					    return getLinuxFilePath();
 | 
				
			||||||
#elif defined(__APPLE__) || defined(__MACH__)
 | 
					#elif defined(__APPLE__) || defined(__MACH__)
 | 
				
			||||||
const char *defaultFontPath = "/System/Library/Fonts/Supplemental/Arial.ttf";
 | 
					    return "/System/Library/Fonts/Supplemental/Arial.ttf";
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
const char* defaultFontPath = nullptr;
 | 
					    return nullptr;
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -41,6 +41,7 @@ public:
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    TextScreen(const std::string &text, SDL_Point *screenSize, std::optional<SDL_Point> basePosition) : hasUpdated(
 | 
					    TextScreen(const std::string &text, SDL_Point *screenSize, std::optional<SDL_Point> basePosition) : hasUpdated(
 | 
				
			||||||
            false), screenSize(screenSize), basePosition(basePosition) {
 | 
					            false), screenSize(screenSize), basePosition(basePosition) {
 | 
				
			||||||
 | 
					        const char *defaultFontPath = getDefaultFontPath();
 | 
				
			||||||
        if (defaultFontPath == nullptr) {
 | 
					        if (defaultFontPath == nullptr) {
 | 
				
			||||||
            std::cerr << "Font path is not set for this platform (null)" << std::endl;
 | 
					            std::cerr << "Font path is not set for this platform (null)" << std::endl;
 | 
				
			||||||
            exit(-1);
 | 
					            exit(-1);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user