From: Daniel Karbach Date: Wed, 11 Mar 2015 13:50:10 +0000 (+0100) Subject: control (some) application parameters via cmdline X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=f01ebd81ff047eb3209e25e183564f72f587915f;p=blank.git control (some) application parameters via cmdline --- diff --git a/building b/building index 4163862..e10bb28 100644 --- a/building +++ b/building @@ -46,3 +46,19 @@ CPPFLAGS, CXXFLAGS, LDXXFLAGS: DEBUG_FLAGS, PROFILE_FLAGS, RELEASE_FLAGS: flags for building binaries in debug, profile, and release mode + + +Running +======= + +blank + normal execution + +blank + terminate after frames + +blank -t + terminate after milliseconds + +blank -t + terminate after n frames, assume milliseconds pass each frame diff --git a/src/app.cpp b/src/app.cpp index d8f1e21..7ddbbf1 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -40,6 +40,34 @@ Application::Application() } +void Application::RunN(size_t n) { + Uint32 last = SDL_GetTicks(); + for (size_t i = 0; i < n; ++i) { + Uint32 now = SDL_GetTicks(); + int delta = now - last; + Loop(delta); + last = now; + } +} + +void Application::RunT(size_t t) { + Uint32 last = SDL_GetTicks(); + Uint32 finish = last + t; + while (last < finish) { + Uint32 now = SDL_GetTicks(); + int delta = now - last; + Loop(delta); + last = now; + } +} + +void Application::RunS(size_t n, size_t t) { + for (size_t i = 0; i < n; ++i) { + Loop(t); + } +} + + void Application::Run() { running = true; Uint32 last = SDL_GetTicks(); diff --git a/src/app.hpp b/src/app.hpp index 0b0e64e..cb71411 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -23,9 +23,17 @@ public: Application(const Application &) = delete; Application &operator =(const Application &) = delete; + /// run until user quits void Run(); void Loop(int dt); + /// run for n frames + void RunN(size_t n); + /// run for t milliseconds + void RunT(size_t t); + /// run for n frames, assuming t milliseconds for each + void RunS(size_t n, size_t t); + void HandleEvents(); void Update(int dt); void Render(); diff --git a/src/main.cpp b/src/main.cpp index 06771fe..f9bc154 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,85 @@ #include "app.hpp" +#include +#include +#include + using namespace blank; +namespace { + +enum Mode { + NORMAL, + FRAME_LIMIT, + TIME_LIMIT, + FIXED_FRAME_LIMIT, +}; + +} + + int main(int argc, char *argv[]) { + Mode mode = NORMAL; + size_t n = 0, t = 0; + + bool error = false; + for (int i = 1; i < argc; ++i) { + if (argv[i] == nullptr || argv[i][0] == '\0') continue; + if (argv[i][0] == '-') { + if (argv[i][1] == 't' && argv[i][2] == '\0') { + ++i; + if (i >= argc) { + std::cerr << "missing argument to -t" << std::endl; + error = true; + } else { + t = std::strtoul(argv[i], nullptr, 10); + } + } else { + std::cerr << "unable to interpret argument " + << i << " (" << argv[i] << ")" << std::endl; + error = true; + } + } else if (std::isdigit(*argv[i])) { + n = std::strtoul(argv[i], nullptr, 10); + } else { + std::cerr << "unable to interpret argument " + << i << " (" << argv[i] << ")" << std::endl; + error = true; + } + } + + if (error) { + return 1; + } + + if (n > 0) { + if (t > 0) { + mode = FIXED_FRAME_LIMIT; + } else { + mode = FRAME_LIMIT; + } + } else if (t > 0) { + mode = TIME_LIMIT; + } + Application app; - app.Run(); + switch (mode) { + default: + case NORMAL: + app.Run(); + break; + case FRAME_LIMIT: + app.RunN(n); + break; + case TIME_LIMIT: + app.RunT(t); + break; + case FIXED_FRAME_LIMIT: + app.RunS(n, t); + break; + } return 0;