c3860cc1d3
Added `window::stop()` (sets GLFW close flag). Updated signal/timer handlers and Q key check to call it instead of using a separate `quit` bool in main(). This encapsulates the close state in the window class (no more external flag + manual checks). The render loop is now simpler. (The process_signals lambda and ASIO duration timer are retained.)
97 lines
2.3 KiB
C++
97 lines
2.3 KiB
C++
#define GLFW_INCLUDE_NONE
|
|
|
|
#include <csignal>
|
|
#include <chrono>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
#include "GLFW/glfw3.h"
|
|
#include "asio.hpp"
|
|
#include "asio/steady_timer.hpp"
|
|
#include "fmt/std.h"
|
|
|
|
#include "cbt/window.hpp"
|
|
#include "cbt/opengl/context.hpp"
|
|
#include "scenes/cube.hpp"
|
|
|
|
auto main(int argc, char const* argv[]) -> int {
|
|
float max_duration_seconds = 0.0f;
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
std::string_view arg = argv[i];
|
|
if (arg == "--help" || arg == "-h") {
|
|
fmt::print("Usage: {} [--duration <seconds>] [--help|-h]\n", argv[0]);
|
|
fmt::print(" --duration <seconds> Auto-terminate after N seconds (for testing/CI)\n");
|
|
return 0;
|
|
}
|
|
if (arg == "--duration" && i + 1 < argc) {
|
|
max_duration_seconds = std::stof(std::string(argv[++i]));
|
|
continue;
|
|
}
|
|
}
|
|
|
|
auto win = cbt::window("cuber", 1280, 720);
|
|
|
|
if (!win.valid()) {
|
|
return 1;
|
|
}
|
|
|
|
auto ctx = cbt::opengl::context(win);
|
|
|
|
if (!ctx.valid()) {
|
|
return 1;
|
|
}
|
|
|
|
auto scn = cbt::scenes::cube();
|
|
if (!scn.init()) {
|
|
return 1;
|
|
}
|
|
|
|
// signal handling + optional duration timer (via ASIO)
|
|
asio::io_context io;
|
|
asio::signal_set signals(io, SIGINT, SIGTERM);
|
|
|
|
signals.async_wait([&](auto, auto) {
|
|
win.stop();
|
|
io.stop();
|
|
});
|
|
|
|
asio::steady_timer duration_timer(io);
|
|
if (max_duration_seconds > 0.0f) {
|
|
duration_timer.expires_after(std::chrono::milliseconds(
|
|
static_cast<long long>(max_duration_seconds * 1000.0f)));
|
|
duration_timer.async_wait([&win](auto ec) {
|
|
if (!ec) {
|
|
win.stop();
|
|
}
|
|
});
|
|
}
|
|
|
|
auto process_signals = [&]() -> void {
|
|
while (io.poll()) {}
|
|
};
|
|
|
|
// render loop
|
|
auto prev = std::chrono::steady_clock::now();
|
|
|
|
while (!win.should_close()) {
|
|
process_signals();
|
|
|
|
if (glfwGetKey(win.raw(), GLFW_KEY_Q) == GLFW_PRESS) {
|
|
win.stop();
|
|
}
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
auto dt = std::chrono::duration<float>(now - prev).count();
|
|
prev = now;
|
|
|
|
scn.update(dt);
|
|
scn.render();
|
|
|
|
win.swap_buffers();
|
|
win.poll_events();
|
|
}
|
|
|
|
return 0;
|
|
}
|