Skip to content

Commit

Permalink
Improve render input font args
Browse files Browse the repository at this point in the history
  • Loading branch information
XorTroll committed Jul 4, 2024
1 parent 2af5676 commit 64fa19f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Example/source/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main() {
auto renderer_opts = pu::ui::render::RendererInitOptions(SDL_INIT_EVERYTHING, pu::ui::render::RendererHardwareFlags);
renderer_opts.UseImage(pu::ui::render::IMGAllFlags);
renderer_opts.UseAudio(pu::ui::render::MixerAllFlags);
renderer_opts.UseTTF();
renderer_opts.AddDefaultAllSharedFonts();
auto renderer = pu::ui::render::Renderer::New(renderer_opts);

// Create our main application from the renderer
Expand Down
46 changes: 24 additions & 22 deletions Plutonium/include/pu/ui/render/render_Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#pragma once
#include <pu/ui/ui_Types.hpp>
#include <pu/ui/render/render_SDL2.hpp>
#include <pu/ttf/ttf_Font.hpp>
#include <vector>

namespace pu::ui::render {
Expand All @@ -31,31 +32,32 @@ namespace pu::ui::render {
u32 sdl_render_flags;
u32 width;
u32 height;
bool init_ttf;
std::vector<PlSharedFontType> default_shared_fonts;
std::vector<std::string> default_font_paths;
std::vector<u32> extra_default_font_sizes;
std::string default_font_path;
bool init_mixer;
u32 audio_mixer_flags;
bool init_img;
u32 sdl_img_flags;
bool init_pl;
bool init_romfs;

RendererInitOptions(const u32 sdl_flags, const u32 sdl_render_flags, const u32 w = ScreenWidth, const u32 h = ScreenHeight) : sdl_flags(sdl_flags), sdl_render_flags(sdl_render_flags), width(w), height(h), init_ttf(false), extra_default_font_sizes(), default_font_path(), init_mixer(false), audio_mixer_flags(0), init_img(false), sdl_img_flags(0), init_pl(false), init_romfs(false) {}
RendererInitOptions(const u32 sdl_flags, const u32 sdl_render_flags, const u32 w = ScreenWidth, const u32 h = ScreenHeight) : sdl_flags(sdl_flags), sdl_render_flags(sdl_render_flags), width(w), height(h), default_shared_fonts(), default_font_paths(), extra_default_font_sizes(), init_mixer(false), audio_mixer_flags(0), init_img(false), sdl_img_flags(0), init_romfs(false) {}

inline void UseTTF(const std::string &default_font_path = "") {
this->init_ttf = true;
inline void AddDefaultSharedFont(const PlSharedFontType type) {
this->default_shared_fonts.push_back(type);
}

// Empty font path = using shared font
if(!default_font_path.empty()) {
this->default_font_path = default_font_path;
}
else {
this->init_pl = true;
inline void AddDefaultAllSharedFonts() {
for(u32 i = 0; i < PlSharedFontType_Total; i++) {
this->default_shared_fonts.push_back(static_cast<PlSharedFontType>(i));
}
}

inline void SetExtraDefaultFontSize(const u32 font_size) {
inline void AddDefaultFontPath(const std::string &font_path) {
this->default_font_paths.push_back(font_path);
}

inline void AddExtraDefaultFontSize(const u32 font_size) {
this->extra_default_font_sizes.push_back(font_size);
}

Expand Down Expand Up @@ -110,6 +112,7 @@ namespace pu::ui::render {
class Renderer {
private:
RendererInitOptions init_opts;
bool ttf_init;
bool ok_romfs;
bool ok_pl;
bool initialized;
Expand Down Expand Up @@ -183,20 +186,19 @@ namespace pu::ui::render {

std::pair<u32, u32> GetDimensions();

// Text rendering
// Font loading

bool AddSharedFont(const std::string &font_name, const u32 font_size, const PlSharedFontType type);
bool AddAllSharedFonts(const std::string &font_name, const u32 font_size);
bool AddFontFile(const std::string &font_name, const u32 font_size, const std::string &path);
bool AddFont(const std::string &font_name, std::shared_ptr<ttf::Font> &font);

inline void AddDefaultFontFromShared(const u32 font_size) {
AddAllSharedFonts(MakeDefaultFontName(font_size), font_size);
}
bool LoadSingleSharedFontInFont(std::shared_ptr<ttf::Font> &font, const PlSharedFontType type);
bool LoadAllSharedFontsInFont(std::shared_ptr<ttf::Font> &font);

inline void AddDefaultFontFromFile(const u32 font_size, const std::string &path) {
AddFontFile(MakeDefaultFontName(font_size), font_size, path);
inline void AddDefaultFont(std::shared_ptr<ttf::Font> &font) {
AddFont(MakeDefaultFontName(font->GetFontSize()), font);
}

// Text rendering

sdl2::Texture RenderText(const std::string &font_name, const std::string &text, const Color clr);
i32 GetTextWidth(const std::string &font_name, const std::string &text);
i32 GetTextHeight(const std::string &font_name, const std::string &text);
Expand Down
102 changes: 33 additions & 69 deletions Plutonium/source/pu/ui/render/render_Renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <pu/ui/render/render_Renderer.hpp>
#include <pu/ttf/ttf_Font.hpp>

namespace pu::ui::render {

Expand All @@ -13,18 +12,6 @@ namespace pu::ui::render {
// Global font object
std::vector<std::pair<std::string, std::shared_ptr<ttf::Font>>> g_FontTable;

inline bool DoAddSharedFont(std::shared_ptr<ttf::Font> &font, const PlSharedFontType type) {
// Let's assume pl services are initialized, and return if anything unexpected happens
PlFontData data = {};
if(R_FAILED(plGetSharedFontByType(&data, type))) {
return false;
}
if(!ttf::Font::IsValidFontFaceIndex(font->LoadFromMemory(data.address, data.size, ttf::Font::EmptyFontFaceDisposingFunction))) {
return false;
}
return true;
}

inline bool ExistsFont(const std::string &font_name) {
for(const auto &[name, font]: g_FontTable) {
if(name == font_name) {
Expand All @@ -39,11 +26,13 @@ namespace pu::ui::render {

void Renderer::Initialize() {
if(!this->initialized) {
this->ttf_init = false;

if(this->init_opts.init_romfs) {
this->ok_romfs = R_SUCCEEDED(romfsInit());
}

if(this->init_opts.init_pl) {
if(!this->init_opts.default_shared_fonts.empty()) {
// TODO: choose pl service type?
this->ok_pl = R_SUCCEEDED(plInitialize(PlServiceType_User));
}
Expand All @@ -61,24 +50,25 @@ namespace pu::ui::render {
IMG_Init(this->init_opts.sdl_img_flags);
}

if(this->init_opts.init_ttf) {
if(!this->init_opts.default_shared_fonts.empty() || !this->init_opts.default_font_paths.empty()) {
TTF_Init();
if(!this->init_opts.default_font_path.empty()) {
for(const auto size: DefaultFontSizes) {
AddDefaultFontFromFile(size, this->init_opts.default_font_path);
}
for(const auto size: this->init_opts.extra_default_font_sizes) {
AddDefaultFontFromFile(size, this->init_opts.default_font_path);
}
}
else {
for(const auto size: DefaultFontSizes) {
AddDefaultFontFromShared(size);
}
for(const auto size: this->init_opts.extra_default_font_sizes) {
AddDefaultFontFromShared(size);
}
this->ttf_init = true;

#define _CREATE_DEFAULT_FONT_FOR_SIZES(sizes) { \
for(const auto size: sizes) { \
auto default_font = std::make_shared<ttf::Font>(size); \
for(const auto &path: this->init_opts.default_font_paths) { \
default_font->LoadFromFile(path); \
} \
for(const auto type: this->init_opts.default_shared_fonts) { \
LoadSingleSharedFontInFont(default_font, type); \
} \
AddDefaultFont(default_font); \
} \
}

_CREATE_DEFAULT_FONT_FOR_SIZES(DefaultFontSizes);
_CREATE_DEFAULT_FONT_FOR_SIZES(this->init_opts.extra_default_font_sizes);
}

if(this->init_opts.init_mixer) {
Expand All @@ -98,7 +88,7 @@ namespace pu::ui::render {
// Close all the fonts before closing TTF
g_FontTable.clear();

if(this->init_opts.init_ttf) {
if(this->ttf_init) {
TTF_Quit();
}
if(this->init_opts.init_img) {
Expand Down Expand Up @@ -267,60 +257,34 @@ namespace pu::ui::render {
return { static_cast<u32>(w), static_cast<u32>(h) };
}

bool AddSharedFont(const std::string &font_name, const u32 font_size, const PlSharedFontType type) {
bool AddFont(const std::string &font_name, std::shared_ptr<ttf::Font> &font) {
if(ExistsFont(font_name)) {
return false;
}

auto font = std::make_shared<ttf::Font>(font_size);
if(!DoAddSharedFont(font, type)) {
return false;
}


g_FontTable.push_back(std::make_pair(font_name, std::move(font)));
return true;
}

bool AddAllSharedFonts(const std::string &font_name, const u32 font_size) {
if(ExistsFont(font_name)) {
return false;
}

auto font = std::make_shared<ttf::Font>(font_size);
if(!DoAddSharedFont(font, PlSharedFontType_Standard)) {
bool LoadSingleSharedFontInFont(std::shared_ptr<ttf::Font> &font, const PlSharedFontType type) {
// Assume pl services are initialized, and return if anything unexpected happens
PlFontData data = {};
if(R_FAILED(plGetSharedFontByType(&data, type))) {
return false;
}
if(!DoAddSharedFont(font, PlSharedFontType_NintendoExt)) {
return false;
}
if(!DoAddSharedFont(font, PlSharedFontType_ChineseSimplified)) {
return false;
}
if(!DoAddSharedFont(font, PlSharedFontType_ExtChineseSimplified)) {
return false;
}
if(!DoAddSharedFont(font, PlSharedFontType_ChineseTraditional)) {
return false;
}
if(!DoAddSharedFont(font, PlSharedFontType_KO)) {
if(!ttf::Font::IsValidFontFaceIndex(font->LoadFromMemory(data.address, data.size, ttf::Font::EmptyFontFaceDisposingFunction))) {
return false;
}

g_FontTable.push_back(std::make_pair(font_name, std::move(font)));
return true;
}

bool AddFontFile(const std::string &font_name, const u32 font_size, const std::string &path) {
if(ExistsFont(font_name)) {
return false;
}

auto font = std::make_shared<ttf::Font>(font_size);
if(!ttf::Font::IsValidFontFaceIndex(font->LoadFromFile(path))) {
return false;
bool LoadAllSharedFontsInFont(std::shared_ptr<ttf::Font> &font) {
for(u32 i = 0; i < PlSharedFontType_Total; i++) {
if(!LoadSingleSharedFontInFont(font, static_cast<PlSharedFontType>(i))) {
return false;
}
}

g_FontTable.push_back(std::make_pair(font_name, std::move(font)));
return true;
}

Expand Down

0 comments on commit 64fa19f

Please sign in to comment.