feat: Add Application Entry Point for Yaze

- Introduced app_main.cc as the main entry point for the Yaze application, separating it from the C API implementation.
- Implemented command-line flag parsing for ROM file loading, logging level, log file path, and log categories.
- Configured the logging system to enhance traceability and user feedback during application startup and execution.
- Integrated the core controller for managing application state and rendering, improving overall application structure and functionality.
This commit is contained in:
scawful
2025-10-05 14:38:32 -04:00
parent 9c8cdb677b
commit c6ba93fd33
5 changed files with 102 additions and 84 deletions

View File

@@ -1,101 +1,20 @@
// C API implementation - no heavy GUI/editor dependencies
#include "yaze.h"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <memory>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include "app/core/controller.h"
#include "app/core/platform/app_delegate.h"
#include "app/editor/message/message_data.h"
#include "app/rom.h"
#include "app/zelda3/overworld/overworld.h"
#include "util/flag.h"
#include "util/log.h"
#include "yaze_config.h"
DEFINE_FLAG(std::string, rom_file, "",
"Path to the ROM file to load. "
"If not specified, the app will run without a ROM.");
DEFINE_FLAG(
std::string, log_level, "info",
"Minimum log level to output (e.g., debug, info, warn, error, fatal).");
DEFINE_FLAG(std::string, log_file, "",
"Path to the log file. If empty, logs to stderr.");
DEFINE_FLAG(std::string, log_categories, "",
"Comma-separated list of log categories to enable.");
// Static variables for library state
static bool g_library_initialized = false;
int yaze_app_main(int argc, char** argv) {
yaze::util::FlagParser parser(yaze::util::global_flag_registry());
RETURN_IF_EXCEPTION(parser.Parse(argc, argv));
// --- Configure Logging System ---
auto string_to_log_level = [](const std::string& s) {
std::string upper_s;
std::transform(s.begin(), s.end(), std::back_inserter(upper_s),
::toupper);
if (upper_s == "YAZE_DEBUG") return yaze::util::LogLevel::YAZE_DEBUG;
if (upper_s == "INFO") return yaze::util::LogLevel::INFO;
if (upper_s == "WARN" || upper_s == "WARNING")
return yaze::util::LogLevel::WARNING;
if (upper_s == "ERROR") return yaze::util::LogLevel::ERROR;
if (upper_s == "FATAL") return yaze::util::LogLevel::FATAL;
return yaze::util::LogLevel::INFO; // Default
};
auto split_categories = [](const std::string& s) {
std::set<std::string> result;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, ',')) {
if (!item.empty()) {
result.insert(item);
}
}
return result;
};
yaze::util::LogManager::instance().configure(
string_to_log_level(FLAGS_log_level->Get()), FLAGS_log_file->Get(),
split_categories(FLAGS_log_categories->Get()));
LOG_INFO("App", "Yaze starting up...");
LOG_INFO("App", "Version: %s", YAZE_VERSION_STRING);
std::string rom_filename = "";
if (!FLAGS_rom_file->Get().empty()) {
rom_filename = FLAGS_rom_file->Get();
LOG_INFO("App", "Loading ROM file: %s", rom_filename);
}
#ifdef __APPLE__
return yaze_run_cocoa_app_delegate(rom_filename.c_str());
#endif
auto controller = std::make_unique<yaze::core::Controller>();
EXIT_IF_ERROR(controller->OnEntry(rom_filename))
while (controller->IsActive()) {
controller->OnInput();
if (auto status = controller->OnLoad(); !status.ok()) {
LOG_ERROR("App", "Controller OnLoad failed: %s", status.message());
break;
}
controller->DoRender();
}
controller->OnExit();
LOG_INFO("App", "Yaze shutting down.");
return EXIT_SUCCESS;
}
// Version and initialization functions
yaze_status yaze_library_init() {
if (g_library_initialized) {