Skip to content

Commit

Permalink
Updated resources
Browse files Browse the repository at this point in the history
Now app resources (fonts, textures) are embedded.
  • Loading branch information
baderouaich committed Oct 19, 2024
1 parent 55e6c88 commit 81e30cc
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 134 deletions.
16 changes: 10 additions & 6 deletions src/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include <Scenes/MainMenuScene.hpp>
#include <Utility/OpenGLUtils.hpp>

#include <Logo.hpp>


NS_ENIGMA_BEGIN

Application::Application(const WindowSettings& window_settings)
Expand All @@ -21,10 +24,10 @@ Application::Application(const WindowSettings& window_settings)
m_current_frame_time(0.0f),
m_delta_time(0.0f),
// Analytics
m_hardware_info_timer(0.0f),
m_FPS(nullptr),
m_ram_info(nullptr),
m_cpu_info(nullptr),
m_hardware_info_timer(0.0f) {
m_cpu_info(nullptr) {
// Check if there is an enigma process already running or not.
if (!SingleProcessInstance::IsUnique()) {
DialogUtils::Warn("Another instance of Enigma is already running!");
Expand Down Expand Up @@ -64,13 +67,14 @@ void Application::InitWindow(const WindowSettings& window_settings) {
m_window->SetEventCallback(ENIGMA_BIND_FUN(Application::OnEvent));

// Set Window runtime icon
m_window->SetIcon(Constants::Resources::Textures::ENIGMA_LOGO_PNG_PATH.string());
m_window->SetIcon(Enigma::Textures::Logo_png, Enigma::Textures::Logo_png_len);

// Set window top left position at center
const auto [monitor_width, monitor_height] = m_window->GetMonitorSize();
const auto& [window_width, window_height] = m_window->GetSize();
m_window->SetPosition(static_cast<std::int32_t>((monitor_width - window_width) / 2), static_cast<std::int32_t>((monitor_height - window_height) / 2));

m_window->Focus();
// Set window's default cursor mode
//m_window->SetCursor(CursorMode::CrossHair);
} catch (const std::exception& e) {
Expand All @@ -89,11 +93,11 @@ void Application::InitImGuiRenderer() {
}

void Application::InitHardwareInfo(const WindowSettings& window_settings) {
if (window_settings.is_show_fps)
if (window_settings.show_fps)
m_FPS = std::make_unique<std::uint32_t>(0);
if (window_settings.is_show_ram_usage)
if (window_settings.show_ram_usage)
m_ram_info = std::make_unique<RAMInfo>();
if (window_settings.is_show_cpu_usage)
if (window_settings.show_cpu_usage)
m_cpu_info = std::make_unique<CPUInfo>();
}

Expand Down
33 changes: 10 additions & 23 deletions src/Core/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ namespace Enigma {
}
}

namespace Config {
static const fs::path WINDOW_CONFIG_FILE_PATH = ::Enigma::ResourceManager::getResourcesDir() / "config" / "WindowSettings.ini";
}

namespace ENV // Environment Variable
{
static constexpr const char *ENIGMA_DIR_ENV_KEY = "ENIGMA_DIR"; // Holds the directory path of Enigma
}

namespace Logger {
static const fs::path LOG_FILE_PATH = ::Enigma::ResourceManager::getResourcesDir() / "logs" / "Enigma.log";
}

namespace Database {
static const fs::path DATABASE_FILE_PATH = ::Enigma::ResourceManager::getResourcesDir() / "database" / "Enigma.db";
// Unfortunately, u cant create multiple tables at once..
Expand Down Expand Up @@ -110,16 +97,16 @@ namespace Enigma {
}

namespace Resources {
namespace Textures {
// window runtime icon
static const fs::path ENIGMA_LOGO_PNG_PATH = ::Enigma::ResourceManager::getResourcesDir() / "branding" / "Logo.png";
}

namespace Fonts {
static const fs::path AUDIOWIDE_FONT_PATH = ::Enigma::ResourceManager::getResourcesDir() / "fonts" / "Audiowide-Regular.ttf";
static const fs::path MONTSERRAT_FONT_PATH = ::Enigma::ResourceManager::getResourcesDir() / "fonts" / "Montserrat-Medium.ttf";
static const fs::path UBUNTU_FONT_PATH = ::Enigma::ResourceManager::getResourcesDir() / "fonts" / "Ubuntu-Regular.ttf";
}
// namespace Textures {
// // window runtime icon
// static const fs::path ENIGMA_LOGO_PNG_PATH = ::Enigma::ResourceManager::getResourcesDir() / "branding" / "Logo.png";
// }

// namespace Fonts {
// static const fs::path AUDIOWIDE_FONT_PATH = ::Enigma::ResourceManager::getResourcesDir() / "fonts" / "Audiowide-Regular.ttf";
// static const fs::path MONTSERRAT_FONT_PATH = ::Enigma::ResourceManager::getResourcesDir() / "fonts" / "Montserrat-Medium.ttf";
// static const fs::path UBUNTU_FONT_PATH = ::Enigma::ResourceManager::getResourcesDir() / "fonts" / "Ubuntu-Regular.ttf";
// }
}

namespace Colors {
Expand Down
27 changes: 3 additions & 24 deletions src/Database/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,19 @@ NS_ENIGMA_BEGIN

void Database::initialize() {
ENIGMA_TRACE_CURRENT_FUNCTION();
{
const fs::path resDir = ::Enigma::ResourceManager::getResourcesDir();
if (!fs::is_directory(resDir)) {
ENIGMA_CRITICAL("Couldn't find resources directory at {}", resDir.string());
std::exit(EXIT_FAILURE);
}
#if defined(ENIGMA_DEBUG)
ENIGMA_INFO("Resources Dir Path: {}", resDir.string());
#endif
}

#if defined(ENIGMA_DEBUG)
ENIGMA_INFO("SQLite3 version {}", SQLite::VERSION);
#endif

try {
//Create db dir if not exists
const fs::path dbDir = ::Enigma::ResourceManager::getResourcesDir() / "database";
if (!fs::is_directory(dbDir)) {
ENIGMA_INFO("Creating Database Directory {} ...", dbDir.string());
if (!fs::create_directory(dbDir)) {
ENIGMA_CRITICAL("Failed to create database directory: {}", dbDir.string());
std::exit(EXIT_FAILURE);
}
}

// Create or open db file
const fs::path database_file_path = Constants::Database::DATABASE_FILE_PATH;
m_database = std::make_unique<SQLite::Database>(
database_file_path.string(),
//database_file_path.string(),
ResourceManager::getDatabaseFilepath(),
SQLite::OPEN_CREATE | SQLite::OPEN_READWRITE // create if not exists
);
#if defined(ENIGMA_DEBUG)
ENIGMA_INFO("Database File Path: {0}", database_file_path.string());
ENIGMA_INFO("Database File Path: {0}", m_database->getFilename());
#endif

// Create Tables If Not Exists
Expand Down
9 changes: 6 additions & 3 deletions src/Logger/Logger.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <pch.hpp>
#include "Logger.hpp"

#include <Utility/DateTimeUtils.hpp>
#include <Utility/FileUtils.hpp>

NS_ENIGMA_BEGIN
Expand All @@ -13,8 +15,9 @@ void Logger::initialize() {

// File Logger
//log_sinks[1] = std::make_shared<spdlog::sinks::basic_file_sink_mt>(Constants::Logger::LOG_FILE_PATH, true);
const fs::path log_file_path = Constants::Logger::LOG_FILE_PATH;
log_sinks[1] = std::make_shared<spdlog::sinks::basic_file_sink_mt>(log_file_path.string(), true);
const fs::path logsDir = ResourceManager::getLogsDir();
const fs::path logFilepath = logsDir / DateTimeUtils::now("Enigma-%Y%m%d%H%M%S.log");
log_sinks[1] = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logFilepath.string(), true);

// Set pattern of the console logger => [time] [trace/info/debug...]: msg]
log_sinks[0]->set_pattern("%^[%T] [%l]: %v%$"); // regex like pattern output format https://github.com/gabime/spdlog/wiki/3.-Custom-formatting
Expand All @@ -38,7 +41,7 @@ void Logger::initialize() {

ENIGMA_TRACE_CURRENT_FUNCTION();
#if defined(ENIGMA_DEBUG)
ENIGMA_INFO("Log File Path: {0}", log_file_path.string());
ENIGMA_INFO("Log File Path: {0}", logFilepath.string());
#endif
}

Expand Down
4 changes: 1 addition & 3 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ int main(int argc, char *argv[]) {

std::int32_t exit_code = -1;
try {
const Enigma::Config windowConfig(Enigma::Constants::Config::WINDOW_CONFIG_FILE_PATH);
const Enigma::WindowSettings windowSettings(windowConfig);
Enigma::Application app(windowSettings);
Enigma::Application app{};
app.Run();
exit_code = EXIT_SUCCESS;
} catch (const std::exception& e) {
Expand Down
114 changes: 92 additions & 22 deletions src/ResourceManager/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Application/Application.hpp>
#include <Logger/Logger.hpp>
#include <System/ENV/ENV.hpp>
#include <Ubuntu-Regular.hpp>

NS_ENIGMA_BEGIN

Expand All @@ -12,8 +13,66 @@ void ResourceManager::initialize() {
}

void ResourceManager::shutdown() {
// DO NOT FREE fonts, ImGui will do so.
}

fs::path ResourceManager::getAppDataDir() {
fs::path appDataPath;
const std::string appName = "Enigma";
#if defined(ENIGMA_PLATFORM_WINDOWS)
// Windows: Use APPDATA or fall back to a local directory
const char *appData = std::getenv("APPDATA");
appDataPath = appData ? fs::path(appData) / appName : fs::path("C:/ProgramData") / appName;
#elif defined(ENIGMA_PLATFORM_LINUX)
// Linux and others: Use HOME or fall back to the current directory
const char *homeDir = std::getenv("HOME");
appDataPath = homeDir ? fs::path(homeDir) / ".local/share" / appName : fs::current_path() / appName;
#elif __APPLE__
// macOS: Library/Application Support or fall back to home directory
appDataPath = fs::path(std::getenv("HOME")) / "Library/Application Support" / appName;
if (!fs::exists(appDataPath)) {
appDataPath = fs::path(std::getenv("HOME")) / appName; // Fall back to home directory
}
#endif
// Create the directory if it doesn't exist
if (!fs::is_directory(appDataPath))
fs::create_directories(appDataPath);
return appDataPath;
}

fs::path ResourceManager::getDatabaseFilepath() {
const fs::path resDir = getResourcesDir();
const fs::path dbDir = resDir / "database";
if (!fs::is_directory(dbDir))
fs::create_directories(dbDir);
return dbDir / "Enigma.db";
}

fs::path ResourceManager::getLogsDir() {
const fs::path resDir = getResourcesDir();
const fs::path logsDir = resDir / "logs";
if (!fs::is_directory(logsDir))
fs::create_directories(logsDir);
return logsDir;
}

// fs::path ResourceManager::getConfigDir() {
// const fs::path resDir = getResourcesDir();
// const fs::path configDir = resDir / "config";
// if (!fs::is_directory(configDir))
// fs::create_directories(configDir);
// return configDir;
// }

fs::path ResourceManager::getResourcesDir() {
static const fs::path resDir = getAppDataDir() / "res";
if (!fs::is_directory(resDir))
fs::create_directories(resDir);
return resDir;
}


#if 0
fs::path ResourceManager::getInstallBaseDir() {
// /home/$(whoami)/Enigma in Linux
// C:\Program Files\Enigma in Windows
Expand Down Expand Up @@ -43,8 +102,9 @@ fs::path ResourceManager::getResourcesDir() {
return localResDir;
}

#endif

ImFont*& ResourceManager::getFont(std::string_view name) {
ImFont *& ResourceManager::getFont(std::string_view name) {
ENIGMA_ASSERT(m_fonts.contains(name), "Font was not loaded");
return m_fonts.at(name);
}
Expand All @@ -56,27 +116,37 @@ void ResourceManager::loadFonts() {

const auto& io = ImGui::GetIO();

// const fs::path font_audiowide_path = Constants::Resources::Fonts::AUDIOWIDE_FONT_PATH;
// const fs::path font_montserrat_path = Constants::Resources::Fonts::MONTSERRAT_FONT_PATH;

ImFontConfig fontConfig = ImFontConfig();
fontConfig.FontDataOwnedByAtlas = false;

// m_fonts["Audiowide-Regular-60"] = io.Fonts->AddFontFromFileTTF(font_audiowide_path.string().c_str(), 60.0f, &fontConfig);
// m_fonts["Audiowide-Regular-45"] = io.Fonts->AddFontFromFileTTF(font_audiowide_path.string().c_str(), 45.0f, &fontConfig);
// m_fonts["Audiowide-Regular-30"] = io.Fonts->AddFontFromFileTTF(font_audiowide_path.string().c_str(), 30.0f, &fontConfig);
// m_fonts["Audiowide-Regular-20"] = io.Fonts->AddFontFromFileTTF(font_audiowide_path.string().c_str(), 20.0f, &fontConfig);
m_fonts["Ubuntu-Regular-60"] = io.Fonts->AddFontFromMemoryTTF(Enigma::Fonts::Ubuntu_Regular_ttf, Enigma::Fonts::Ubuntu_Regular_ttf_len, 60.0f, &fontConfig);
m_fonts["Ubuntu-Regular-45"] = io.Fonts->AddFontFromMemoryTTF(Enigma::Fonts::Ubuntu_Regular_ttf, Enigma::Fonts::Ubuntu_Regular_ttf_len, 45.0f, &fontConfig);
m_fonts["Ubuntu-Regular-30"] = io.Fonts->AddFontFromMemoryTTF(Enigma::Fonts::Ubuntu_Regular_ttf, Enigma::Fonts::Ubuntu_Regular_ttf_len, 30.0f, &fontConfig);
m_fonts["Ubuntu-Regular-20"] = io.Fonts->AddFontFromMemoryTTF(Enigma::Fonts::Ubuntu_Regular_ttf, Enigma::Fonts::Ubuntu_Regular_ttf_len, 20.0f, &fontConfig);
m_fonts["Ubuntu-Regular-18"] = io.Fonts->AddFontFromMemoryTTF(Enigma::Fonts::Ubuntu_Regular_ttf, Enigma::Fonts::Ubuntu_Regular_ttf_len, 18.0f, &fontConfig);
m_fonts["Ubuntu-Regular-16"] = io.Fonts->AddFontFromMemoryTTF(Enigma::Fonts::Ubuntu_Regular_ttf, Enigma::Fonts::Ubuntu_Regular_ttf_len, 16.0f, &fontConfig);
m_fonts["Ubuntu-Regular-14"] = io.Fonts->AddFontFromMemoryTTF(Enigma::Fonts::Ubuntu_Regular_ttf, Enigma::Fonts::Ubuntu_Regular_ttf_len, 14.0f, &fontConfig);
m_fonts["Ubuntu-Regular-12"] = io.Fonts->AddFontFromMemoryTTF(Enigma::Fonts::Ubuntu_Regular_ttf, Enigma::Fonts::Ubuntu_Regular_ttf_len, 12.0f, &fontConfig);

#if 0
// const fs::path font_audiowide_path = Constants::Resources::Fonts::AUDIOWIDE_FONT_PATH;
// const fs::path font_montserrat_path = Constants::Resources::Fonts::MONTSERRAT_FONT_PATH;
const fs::path font_ubuntu_path = Constants::Resources::Fonts::UBUNTU_FONT_PATH;
// ENIGMA_ASSERT_OR_THROW(fs::exists(font_audiowide_path), "Font " + font_audiowide_path.string() + " not found");
// ENIGMA_ASSERT_OR_THROW(fs::exists(font_montserrat_path), "Font " + font_montserrat_path.string() + " not found");
// ENIGMA_ASSERT_OR_THROW(fs::exists(font_audiowide_path), "Font " + font_audiowide_path.string() + " not found");
// ENIGMA_ASSERT_OR_THROW(fs::exists(font_montserrat_path), "Font " + font_montserrat_path.string() + " not found");
ENIGMA_ASSERT_OR_THROW(fs::exists(font_ubuntu_path), "Font " + font_ubuntu_path.string() + " not found");

#ifdef ENIGMA_DEBUG
// ENIGMA_LOG("Font Audiowide-Regular path: {}", font_audiowide_path.string());
// ENIGMA_LOG("Font Montserrat-Medium path: {}", font_montserrat_path.string());
// ENIGMA_LOG("Font Audiowide-Regular path: {}", font_audiowide_path.string());
// ENIGMA_LOG("Font Montserrat-Medium path: {}", font_montserrat_path.string());
ENIGMA_LOG("Font Ubuntu-Regular path: {}", font_ubuntu_path.string());
#endif

ImFontConfig fontConfig = ImFontConfig();
//fontConfig.FontDataOwnedByAtlas = true;

// m_fonts["Audiowide-Regular-60"] = io.Fonts->AddFontFromFileTTF(font_audiowide_path.string().c_str(), 60.0f, &fontConfig);
// m_fonts["Audiowide-Regular-45"] = io.Fonts->AddFontFromFileTTF(font_audiowide_path.string().c_str(), 45.0f, &fontConfig);
// m_fonts["Audiowide-Regular-30"] = io.Fonts->AddFontFromFileTTF(font_audiowide_path.string().c_str(), 30.0f, &fontConfig);
// m_fonts["Audiowide-Regular-20"] = io.Fonts->AddFontFromFileTTF(font_audiowide_path.string().c_str(), 20.0f, &fontConfig);

m_fonts["Ubuntu-Regular-60"] = io.Fonts->AddFontFromFileTTF(font_ubuntu_path.string().c_str(), 60.0f, &fontConfig);
m_fonts["Ubuntu-Regular-45"] = io.Fonts->AddFontFromFileTTF(font_ubuntu_path.string().c_str(), 45.0f, &fontConfig);
m_fonts["Ubuntu-Regular-30"] = io.Fonts->AddFontFromFileTTF(font_ubuntu_path.string().c_str(), 30.0f, &fontConfig);
Expand All @@ -85,13 +155,13 @@ void ResourceManager::loadFonts() {
m_fonts["Ubuntu-Regular-16"] = io.Fonts->AddFontFromFileTTF(font_ubuntu_path.string().c_str(), 16.0f, &fontConfig);
m_fonts["Ubuntu-Regular-14"] = io.Fonts->AddFontFromFileTTF(font_ubuntu_path.string().c_str(), 14.0f, &fontConfig);
m_fonts["Ubuntu-Regular-12"] = io.Fonts->AddFontFromFileTTF(font_ubuntu_path.string().c_str(), 12.0f, &fontConfig);

// m_fonts["Montserrat-Medium-45"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 45.0f, &fontConfig);
// m_fonts["Montserrat-Medium-20"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 20.0f, &fontConfig);
// m_fonts["Montserrat-Medium-18"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 18.0f, &fontConfig);
// m_fonts["Montserrat-Medium-16"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 16.0f, &fontConfig);
// m_fonts["Montserrat-Medium-14"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 14.0f, &fontConfig);
// m_fonts["Montserrat-Medium-12"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 12.0f, &fontConfig);
#endif
// m_fonts["Montserrat-Medium-45"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 45.0f, &fontConfig);
// m_fonts["Montserrat-Medium-20"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 20.0f, &fontConfig);
// m_fonts["Montserrat-Medium-18"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 18.0f, &fontConfig);
// m_fonts["Montserrat-Medium-16"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 16.0f, &fontConfig);
// m_fonts["Montserrat-Medium-14"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 14.0f, &fontConfig);
// m_fonts["Montserrat-Medium-12"] = io.Fonts->AddFontFromFileTTF(font_montserrat_path.string().c_str(), 12.0f, &fontConfig);

// Build added fonts atlas --> imgui issue #3643
io.Fonts->Build();
Expand Down
12 changes: 11 additions & 1 deletion src/ResourceManager/ResourceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ namespace Enigma {
static void initialize();
static void shutdown();

static fs::path getAppDataDir();

static fs::path getDatabaseFilepath();
static fs::path getLogsDir();
//static fs::path getConfigDir();

/// @brief Returns path to res/ folder
/// Example: /home/$(whoami)/Enigma/res/
static fs::path getResourcesDir();
#if 0
/// @brief Returns path to res/ folder
/// Example: /home/$(whoami)/Enigma/res/
static fs::path getResourcesDir();

/// @brief Returns path to installed Enigma/ folder
/// Example: /home/$(whoami)/Enigma/
static fs::path getInstallBaseDir();

#endif
/// @brief Returns ImFont by name
static ImFont*& getFont(std::string_view name);

Expand Down
Loading

0 comments on commit 81e30cc

Please sign in to comment.