From dbab645c94f128180e6066645e81b7927f111503 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 2 Oct 2025 19:40:05 +0300 Subject: [PATCH] add 'tps' command line argument --- src/engine/Engine.hpp | 1 + src/engine/ServerMainloop.cpp | 4 +--- src/util/ArgsReader.hpp | 9 +++++++++ src/util/command_line.cpp | 33 +++++++++++++++++++++++---------- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/engine/Engine.hpp b/src/engine/Engine.hpp index 98bcd497..1f9438c0 100644 --- a/src/engine/Engine.hpp +++ b/src/engine/Engine.hpp @@ -50,6 +50,7 @@ struct CoreParameters { std::filesystem::path userFolder = "."; std::filesystem::path scriptFile; std::filesystem::path projectFolder; + int tps = 20; }; using OnWorldOpen = std::function, int64_t)>; diff --git a/src/engine/ServerMainloop.cpp b/src/engine/ServerMainloop.cpp index 27c35bdd..2f446533 100644 --- a/src/engine/ServerMainloop.cpp +++ b/src/engine/ServerMainloop.cpp @@ -15,8 +15,6 @@ using namespace std::chrono; static debug::Logger logger("mainloop"); -inline constexpr int TPS = 20; - ServerMainloop::ServerMainloop(Engine& engine) : engine(engine) { } @@ -39,7 +37,7 @@ void ServerMainloop::run() { "script:" + coreParams.scriptFile.filename().u8string() ); - double targetDelta = 1.0 / static_cast(TPS); + double targetDelta = 1.0 / static_cast(coreParams.tps); double delta = targetDelta; auto begin = system_clock::now(); auto startupTime = begin; diff --git a/src/util/ArgsReader.hpp b/src/util/ArgsReader.hpp index 90d159f5..4c845376 100644 --- a/src/util/ArgsReader.hpp +++ b/src/util/ArgsReader.hpp @@ -33,5 +33,14 @@ namespace util { last = argv[pos]; return argv[pos++]; } + + int nextInt() { + auto text = next(); + try { + return std::stoi(text); + } catch (const std::exception& e) { + throw std::runtime_error(e.what()); + } + } }; } diff --git a/src/util/command_line.cpp b/src/util/command_line.cpp index 8f8c981b..6d84ab28 100644 --- a/src/util/command_line.cpp +++ b/src/util/command_line.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "io/engine_paths.hpp" #include "util/ArgsReader.hpp" @@ -15,10 +16,17 @@ class ArgC { public: std::string keyword; std::function execute; + std::string args; std::string help; - ArgC(const std::string& keyword, std::function execute, const std::string& help) { + ArgC( + const std::string& keyword, + std::function execute, + const std::string& args, + const std::string& help + ) { this->keyword = keyword; this->execute = execute; + this->args = args; this->help = help; } }; @@ -31,42 +39,47 @@ static bool perform_keyword( ArgC("--res", [¶ms, &reader]() -> bool { params.resFolder = reader.next(); return true; - }, " - set resources directory."), + }, "", "set resources directory."), ArgC("--dir", [¶ms, &reader]() -> bool { params.userFolder = reader.next(); return true; - }, " - set userfiles directory."), + }, "", "set userfiles directory."), ArgC("--project", [¶ms, &reader]() -> bool { params.projectFolder = reader.next(); return true; - }, " - set project directory."), + }, "", "set project directory."), ArgC("--test", [¶ms, &reader]() -> bool { params.testMode = true; params.scriptFile = reader.next(); return true; - }, " - test script file."), + }, "", "test script file."), ArgC("--script", [¶ms, &reader]() -> bool { params.testMode = false; params.scriptFile = reader.next(); return true; - }, " - main script file."), + }, "", "main script file."), ArgC("--headless", [¶ms]() -> bool { params.headless = true; return true; - }, "- run in headless mode."), + }, "", "run in headless mode."), + ArgC("--tps", [¶ms, &reader]() -> bool { + params.tps = reader.nextInt(); + return true; + }, "", "headless mode tick-rate (default - 20)."), ArgC("--version", []() -> bool { std::cout << ENGINE_VERSION_STRING << std::endl; return false; - }, "- display the engine version."), + }, "", "display the engine version."), ArgC("--help", []() -> bool { std::cout << "VoxelCore v" << ENGINE_VERSION_STRING << "\n\n"; std::cout << "Command-line arguments:\n"; for (auto& a : argumentsCommandline) { - std::cout << a.keyword << " " << a.help << std::endl; + std::cout << std::setw(20) << std::left << (a.keyword + " " + a.args); + std::cout << "- " << a.help << std::endl; } std::cout << std::endl; return false; - }, "- display this help.") + }, "", "display this help.") }; for (auto& a : argumentsCommandline) { if (a.keyword == keyword) {