Skip to content

Commit

Permalink
Refactor: Separated the main function into smaller functions to help …
Browse files Browse the repository at this point in the history
…readability.
  • Loading branch information
Pixfri committed Dec 22, 2024
1 parent fded7c2 commit d460566
Showing 1 changed file with 61 additions and 34 deletions.
95 changes: 61 additions & 34 deletions Source/FlashlightEngine/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,85 @@
#include <FlashlightEngine/Types.hpp>

#include <FlashlightEngine/EngineApplication.hpp>
#include <FlashlightEngine/Core/Filesystem.hpp>

#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdexcept>

int main(const int argc, char* argv[]) {
spdlog::set_pattern("[%H:%M:%S %z] [%^---%L---%$] [thread %t] %v");

try {
FlashlightEngine::UInt32 width = 1280;
FlashlightEngine::UInt32 height = 720;

// If argc (argument count) is one, it means the program was run without arguments
// (argv[0] = the name of the program, hence argc = 1 when there are no arguments).
if (argc > 1) {
for (FlashlightEngine::Int32 i = 1; i < argc - 1; i++) {
if (std::strcmp(argv[i], "--width") == 0 || std::strcmp(argv[i], "-w") == 0) {
char* end;
const long value = std::strtol(argv[i + 1], &end, 10);

if (*end != '\0' || value <= 0) {
throw std::invalid_argument("Invalid width value provided after --width.");
}

width = static_cast<FlashlightEngine::UInt32>(value);
}
FlashlightEngine::UInt32 g_Width = 1280;
FlashlightEngine::UInt32 g_Height = 720;

if (std::strcmp(argv[i], "--height") == 0 || std::strcmp(argv[i], "-h") == 0) {
char* end;
const long value = std::strtol(argv[i + 1], &end, 10);
void SetupLogger();
void ParseArguments(int argc, char* argv[]);

if (*end != '\0' || value <= 0) {
throw std::invalid_argument("Invalid height value provided after --height.");
}

height = static_cast<FlashlightEngine::UInt32>(value);
}
}
}
int main(const int argc, char* argv[]) {
try {
SetupLogger();
ParseArguments(argc, argv);

FlashlightEngine::EngineApplication app(width, height);
FlashlightEngine::EngineApplication app(g_Width, g_Height);

app.Run();

} catch (const std::invalid_argument& e) {
std::cerr << "Invalid argument: " << e.what() << std::endl;
return EXIT_FAILURE;
} catch (const std::runtime_error& e) {
std::cerr << "An error occurred while running the application: " << e.what() << std::endl;
return EXIT_FAILURE;
} catch (const spdlog::spdlog_ex& ex) {
std::cerr << "Failed to initialize logger: " << ex.what() << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

void SetupLogger() {
const auto logFile = FlashlightEngine::Filesystem::GetLogsDirectory() / "FlashlightEngine.log";

std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>(logFile.string(), true));

const auto logger = std::make_shared<spdlog::logger>("FlashlightEngine", begin(sinks), end(sinks));
spdlog::register_logger(logger);
spdlog::set_default_logger(logger);

spdlog::set_pattern("[%H:%M:%S %z] [%^---%L---%$] [thread %t] %v");
}

void ParseArguments(const int argc, char* argv[]) {
// If argc (argument count) is one, it means the program was run without arguments
// (argv[0] = the name of the program, hence argc = 1 when there are no arguments).
if (argc > 1) {
for (FlashlightEngine::Int32 i = 1; i < argc - 1; i++) {
if (std::strcmp(argv[i], "--width") == 0 || std::strcmp(argv[i], "-w") == 0) {
char* end;
const long value = std::strtol(argv[i + 1], &end, 10);

if (*end != '\0' || value <= 0) {
throw std::invalid_argument("Invalid width value provided after --width.");
}

g_Width = static_cast<FlashlightEngine::UInt32>(value);
}

if (std::strcmp(argv[i], "--height") == 0 || std::strcmp(argv[i], "-h") == 0) {
char* end;
const long value = std::strtol(argv[i + 1], &end, 10);

if (*end != '\0' || value <= 0) {
throw std::invalid_argument("Invalid height value provided after --height.");
}

g_Height = static_cast<FlashlightEngine::UInt32>(value);
}
}
}
}

0 comments on commit d460566

Please sign in to comment.