Add font loading functionality and refactor font handling in core
This commit is contained in:
@@ -16,7 +16,6 @@
|
||||
#include "imgui/backends/imgui_impl_sdl2.h"
|
||||
#include "imgui/backends/imgui_impl_sdlrenderer2.h"
|
||||
#include "imgui/imgui.h"
|
||||
#include "imgui/imgui_internal.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace app {
|
||||
@@ -180,7 +179,7 @@ absl::Status Controller::OnEntry(std::string filename) {
|
||||
#else
|
||||
platform_ = Platform::kUnknown;
|
||||
#endif
|
||||
RETURN_IF_ERROR(CreateSDL_Window())
|
||||
RETURN_IF_ERROR(CreateWindow())
|
||||
RETURN_IF_ERROR(CreateRenderer())
|
||||
RETURN_IF_ERROR(CreateGuiContext())
|
||||
RETURN_IF_ERROR(LoadAudioDevice())
|
||||
@@ -264,7 +263,7 @@ void Controller::OnExit() {
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
absl::Status Controller::CreateSDL_Window() {
|
||||
absl::Status Controller::CreateWindow() {
|
||||
auto sdl_flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
|
||||
if (flags()->kUseNewImGuiInput) {
|
||||
sdl_flags |= SDL_INIT_GAMECONTROLLER;
|
||||
@@ -273,25 +272,26 @@ absl::Status Controller::CreateSDL_Window() {
|
||||
if (SDL_Init(sdl_flags) != 0) {
|
||||
return absl::InternalError(
|
||||
absl::StrFormat("SDL_Init: %s\n", SDL_GetError()));
|
||||
} else {
|
||||
SDL_DisplayMode displayMode;
|
||||
SDL_GetCurrentDisplayMode(0, &displayMode);
|
||||
int screenWidth = displayMode.w * 0.8;
|
||||
int screenHeight = displayMode.h * 0.8;
|
||||
|
||||
window_ = std::unique_ptr<SDL_Window, core::SDL_Deleter>(
|
||||
SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
|
||||
SDL_WINDOWPOS_UNDEFINED, // initial x position
|
||||
SDL_WINDOWPOS_UNDEFINED, // initial y position
|
||||
screenWidth, // width, in pixels
|
||||
screenHeight, // height, in pixels
|
||||
SDL_WINDOW_RESIZABLE),
|
||||
core::SDL_Deleter());
|
||||
if (window_ == nullptr) {
|
||||
return absl::InternalError(
|
||||
absl::StrFormat("SDL_CreateWindow: %s\n", SDL_GetError()));
|
||||
}
|
||||
}
|
||||
|
||||
SDL_DisplayMode display_mode;
|
||||
SDL_GetCurrentDisplayMode(0, &display_mode);
|
||||
int screen_width = display_mode.w * 0.8;
|
||||
int screen_height = display_mode.h * 0.8;
|
||||
|
||||
window_ = std::unique_ptr<SDL_Window, core::SDL_Deleter>(
|
||||
SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
|
||||
SDL_WINDOWPOS_UNDEFINED, // initial x position
|
||||
SDL_WINDOWPOS_UNDEFINED, // initial y position
|
||||
screen_width, // width, in pixels
|
||||
screen_height, // height, in pixels
|
||||
SDL_WINDOW_RESIZABLE),
|
||||
core::SDL_Deleter());
|
||||
if (window_ == nullptr) {
|
||||
return absl::InternalError(
|
||||
absl::StrFormat("SDL_CreateWindow: %s\n", SDL_GetError()));
|
||||
}
|
||||
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
@@ -305,22 +305,17 @@ absl::Status Controller::CreateGuiContext() {
|
||||
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||
|
||||
if (flags()->kUseNewImGuiInput) {
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
|
||||
}
|
||||
|
||||
// Initialize ImGui for SDL
|
||||
// Initialize ImGui based on the backend
|
||||
ImGui_ImplSDL2_InitForSDLRenderer(window_.get(),
|
||||
Renderer::GetInstance().renderer());
|
||||
ImGui_ImplSDLRenderer2_Init(Renderer::GetInstance().renderer());
|
||||
|
||||
// Check if the assets/fonts directory exists in our CWD
|
||||
// Otherwise, load the system fonts.
|
||||
// const auto assets_path = std::filesystem::path("assets");
|
||||
// if (std::filesystem::is_directory(assets_path)) {
|
||||
RETURN_IF_ERROR(LoadFontFamilies());
|
||||
// } else {
|
||||
// #ifdef __APPLE__
|
||||
// LoadSystemFonts();
|
||||
// #else
|
||||
@@ -328,7 +323,6 @@ absl::Status Controller::CreateGuiContext() {
|
||||
// "Could not find assets/fonts directory in the current working "
|
||||
// "directory");
|
||||
// #endif
|
||||
//}
|
||||
|
||||
// Set the default style
|
||||
gui::ColorsYaze();
|
||||
@@ -340,6 +334,32 @@ absl::Status Controller::CreateGuiContext() {
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status Controller::LoadFontFamilies() const {
|
||||
// LoadSystemFonts();
|
||||
return LoadPackageFonts();
|
||||
}
|
||||
|
||||
absl::Status Controller::LoadAudioDevice() {
|
||||
SDL_AudioSpec want, have;
|
||||
SDL_memset(&want, 0, sizeof(want));
|
||||
want.freq = audio_frequency_;
|
||||
want.format = AUDIO_S16;
|
||||
want.channels = 2;
|
||||
want.samples = 2048;
|
||||
want.callback = NULL; // Uses the queue
|
||||
audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
|
||||
if (audio_device_ == 0) {
|
||||
return absl::InternalError(
|
||||
absl::StrFormat("Failed to open audio: %s\n", SDL_GetError()));
|
||||
}
|
||||
// audio_buffer_ = new int16_t[audio_frequency_ / 50 * 4];
|
||||
audio_buffer_ = std::make_shared<int16_t>(audio_frequency_ / 50 * 4);
|
||||
SDL_PauseAudioDevice(audio_device_, 0);
|
||||
editor_manager_.emulator().set_audio_buffer(audio_buffer_.get());
|
||||
editor_manager_.emulator().set_audio_device_id(audio_device_);
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status Controller::LoadConfigFiles() {
|
||||
// Create and load a dotfile for the application
|
||||
// This will store the user's preferences and settings
|
||||
@@ -368,124 +388,6 @@ absl::Status Controller::LoadConfigFiles() {
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status Controller::LoadFontFamilies() const {
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
|
||||
static const char *KARLA_REGULAR = "Karla-Regular.ttf";
|
||||
static const char *ROBOTO_MEDIUM = "Roboto-Medium.ttf";
|
||||
static const char *COUSINE_REGULAR = "Cousine-Regular.ttf";
|
||||
static const char *DROID_SANS = "DroidSans.ttf";
|
||||
static const char *NOTO_SANS_JP = "NotoSansJP.ttf";
|
||||
static const char *IBM_PLEX_JP = "IBMPlexSansJP-Bold.ttf";
|
||||
static const float FONT_SIZE_DEFAULT = 16.0f;
|
||||
static const float FONT_SIZE_DROID_SANS = 18.0f;
|
||||
static const float ICON_FONT_SIZE = 18.0f;
|
||||
|
||||
// Icon configuration
|
||||
static const ImWchar icons_ranges[] = {ICON_MIN_MD, 0xf900, 0};
|
||||
ImFontConfig icons_config;
|
||||
icons_config.MergeMode = true;
|
||||
icons_config.GlyphOffset.y = 5.0f;
|
||||
icons_config.GlyphMinAdvanceX = 13.0f;
|
||||
icons_config.PixelSnapH = true;
|
||||
|
||||
// Japanese font configuration
|
||||
ImFontConfig japanese_font_config;
|
||||
japanese_font_config.MergeMode = true;
|
||||
icons_config.GlyphOffset.y = 5.0f;
|
||||
icons_config.GlyphMinAdvanceX = 13.0f;
|
||||
icons_config.PixelSnapH = true;
|
||||
|
||||
// List of fonts to be loaded
|
||||
std::vector<const char *> font_paths = {
|
||||
KARLA_REGULAR, ROBOTO_MEDIUM, COUSINE_REGULAR, IBM_PLEX_JP, DROID_SANS};
|
||||
|
||||
// Load fonts with associated icon and Japanese merges
|
||||
for (const auto &font_path : font_paths) {
|
||||
float font_size =
|
||||
(font_path == DROID_SANS) ? FONT_SIZE_DROID_SANS : FONT_SIZE_DEFAULT;
|
||||
|
||||
std::string actual_font_path;
|
||||
#ifdef __APPLE__
|
||||
#if TARGET_OS_IOS == 1
|
||||
const std::string kBundlePath = GetBundleResourcePath();
|
||||
actual_font_path = kBundlePath + font_path;
|
||||
#else
|
||||
actual_font_path = absl::StrCat(GetBundleResourcePath(),
|
||||
"Contents/Resources/font/", font_path);
|
||||
#endif
|
||||
#else
|
||||
actual_font_path = std::filesystem::absolute(font_path).string();
|
||||
#endif
|
||||
|
||||
if (!io.Fonts->AddFontFromFileTTF(actual_font_path.data(), font_size)) {
|
||||
return absl::InternalError(
|
||||
absl::StrFormat("Failed to load font from %s", actual_font_path));
|
||||
}
|
||||
|
||||
// Merge icon set
|
||||
std::string actual_icon_font_path = "";
|
||||
const char *icon_font_path = FONT_ICON_FILE_NAME_MD;
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
#if TARGET_OS_IOS == 1
|
||||
const std::string kIconBundlePath = GetBundleResourcePath();
|
||||
actual_icon_font_path = kIconBundlePath + "MaterialIcons-Regular.ttf";
|
||||
#else
|
||||
actual_icon_font_path =
|
||||
absl::StrCat(GetBundleResourcePath(),
|
||||
"Contents/Resources/font/MaterialIcons-Regular.ttf");
|
||||
#endif
|
||||
#else
|
||||
actual_icon_font_path = std::filesystem::absolute(icon_font_path).string();
|
||||
#endif
|
||||
io.Fonts->AddFontFromFileTTF(actual_icon_font_path.data(), ICON_FONT_SIZE,
|
||||
&icons_config, icons_ranges);
|
||||
|
||||
// Merge Japanese font
|
||||
std::string actual_japanese_font_path = "";
|
||||
const char *japanese_font_path = NOTO_SANS_JP;
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
#if TARGET_OS_IOS == 1
|
||||
const std::string kJapaneseBundlePath = GetBundleResourcePath();
|
||||
actual_japanese_font_path = kJapaneseBundlePath + japanese_font_path;
|
||||
#else
|
||||
actual_japanese_font_path =
|
||||
absl::StrCat(GetBundleResourcePath(), "Contents/Resources/font/",
|
||||
japanese_font_path);
|
||||
#endif
|
||||
#else
|
||||
actual_japanese_font_path =
|
||||
std::filesystem::absolute(japanese_font_path).string();
|
||||
#endif
|
||||
io.Fonts->AddFontFromFileTTF(actual_japanese_font_path.data(), 18.0f,
|
||||
&japanese_font_config,
|
||||
io.Fonts->GetGlyphRangesJapanese());
|
||||
}
|
||||
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status Controller::LoadAudioDevice() {
|
||||
SDL_AudioSpec want, have;
|
||||
SDL_memset(&want, 0, sizeof(want));
|
||||
want.freq = audio_frequency_;
|
||||
want.format = AUDIO_S16;
|
||||
want.channels = 2;
|
||||
want.samples = 2048;
|
||||
want.callback = NULL; // Uses the queue
|
||||
audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
|
||||
if (audio_device_ == 0) {
|
||||
return absl::InternalError(
|
||||
absl::StrFormat("Failed to open audio: %s\n", SDL_GetError()));
|
||||
}
|
||||
// audio_buffer_ = new int16_t[audio_frequency_ / 50 * 4];
|
||||
audio_buffer_ = std::make_shared<int16_t>(audio_frequency_ / 50 * 4);
|
||||
SDL_PauseAudioDevice(audio_device_, 0);
|
||||
editor_manager_.emulator().set_audio_buffer(audio_buffer_.get());
|
||||
editor_manager_.emulator().set_audio_device_id(audio_device_);
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
} // namespace core
|
||||
} // namespace app
|
||||
} // namespace yaze
|
||||
|
||||
Reference in New Issue
Block a user