feat: add --scene and --screenshot CLI flags

Added --scene <cube|sphere> to select the initial scene from
the command line (useful for headless/CI testing where key
presses aren't possible).

Added --screenshot to render a single frame, save a PNG,
and exit immediately. Combined with --duration or used
alone, this allows fully automated screenshot capture
without relying on interactive key presses.

Updated help text to reflect new options.
This commit is contained in:
2026-05-05 23:59:54 +02:00
parent 78d0515e8b
commit 6f696d377b
+23 -2
View File
@@ -17,12 +17,16 @@
auto main(int argc, char const* argv[]) -> int { auto main(int argc, char const* argv[]) -> int {
float max_duration_seconds = 0.0f; float max_duration_seconds = 0.0f;
std::string_view scene_name = "cube";
bool take_screenshot = false;
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
std::string_view arg = argv[i]; std::string_view arg = argv[i];
if (arg == "--help" || arg == "-h") { if (arg == "--help" || arg == "-h") {
fmt::print("Usage: {} [--duration <seconds>] [--help|-h]\n", argv[0]); fmt::print("Usage: {} [options]\n", argv[0]);
fmt::print(" --duration <seconds> Auto-terminate after N seconds (for testing/CI)\n"); fmt::print(" --duration <seconds> Auto-terminate after N seconds (for testing/CI)\n");
fmt::print(" --scene <cube|sphere> Select initial scene (default: cube)\n");
fmt::print(" --screenshot Render one frame, save screenshot, and exit\n");
fmt::print(" S key Take screenshot (saved as screenshot.png)\n"); fmt::print(" S key Take screenshot (saved as screenshot.png)\n");
fmt::print(" 1/2 key Switch between cube/sphere scene\n"); fmt::print(" 1/2 key Switch between cube/sphere scene\n");
return 0; return 0;
@@ -31,6 +35,14 @@ auto main(int argc, char const* argv[]) -> int {
max_duration_seconds = std::stof(std::string(argv[++i])); max_duration_seconds = std::stof(std::string(argv[++i]));
continue; continue;
} }
if (arg == "--scene" && i + 1 < argc) {
scene_name = argv[++i];
continue;
}
if (arg == "--screenshot") {
take_screenshot = true;
continue;
}
} }
auto win = cbt::window("cuber", 1280, 720); auto win = cbt::window("cuber", 1280, 720);
@@ -52,7 +64,11 @@ auto main(int argc, char const* argv[]) -> int {
if (!cube_scn.init() || !sphere_scn.init()) { if (!cube_scn.init() || !sphere_scn.init()) {
return 1; return 1;
} }
active_scene = &cube_scn; if (scene_name == "sphere" || scene_name == "2") {
active_scene = &sphere_scn;
} else {
active_scene = &cube_scn;
}
// signal handling + optional duration timer (via ASIO) // signal handling + optional duration timer (via ASIO)
asio::io_context io; asio::io_context io;
@@ -106,6 +122,11 @@ auto main(int argc, char const* argv[]) -> int {
win.swap_buffers(); win.swap_buffers();
win.poll_events(); win.poll_events();
if (take_screenshot) {
win.screenshot();
win.stop();
}
} }
return 0; return 0;