Files
aevolution/apps/evo-client/main.cpp
T

127 lines
5.6 KiB
C++

#include "evo/client/application.hpp"
#include <algorithm>
#include <array>
#include <charconv>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <exception>
#include <string_view>
#include <thread>
int main(int argc, char** argv) try {
evo::client::Config config;
unsigned frames = 0;
for (int i = 1; i < argc; ++i) {
const std::string_view arg{argv[i]};
if (arg == "--hidden")
config.hidden = true;
else if (arg == "--renderer" && i + 1 < argc)
config.renderer = argv[++i];
else if (arg == "--frames" && i + 1 < argc) {
const std::string_view value{argv[++i]};
const auto [end, error] =
std::from_chars(value.data(), value.data() + value.size(), frames);
if (error != std::errc{} || end != value.data() + value.size() || frames == 0 ||
frames > 100000)
return 2;
} else {
std::fputs("usage: evo-client [--renderer NAME] [--hidden] [--frames 1..100000]\n",
stderr);
return 2;
}
}
auto app = evo::client::Application::open(config);
if (!app) {
std::fprintf(stderr, "client initialization error=%d\n", static_cast<int>(app.error()));
return 1;
}
// A minimized native window may start with no drawable area. Adopt the
// native viewport before projecting; until then use the requested size.
auto camera =
evo::client::Camera::create({config.width, config.height, config.width, config.height});
if (!camera)
return 1;
std::puts("Arrows: pan | wheel: zoom | WASD: intention indicator | Tab: UI capture (orange)");
std::uint64_t count = 0;
auto previous = std::chrono::steady_clock::now();
while (frames == 0 || count < frames) {
if (auto result = (*app)->poll_events(); !result) {
std::fprintf(stderr, "client event error=%d\n", static_cast<int>(result.error()));
return 1;
}
const auto state = (*app)->state();
if (state.closed)
break;
const auto now = std::chrono::steady_clock::now();
const double seconds =
std::clamp(std::chrono::duration<double>(now - previous).count(), 0.0, 0.1);
previous = now;
const auto navigation = (*app)->take_navigation();
// Fixture ticks only. No server session or simulation clock exists here.
const auto intention = (*app)->sample_intention(count);
if (!intention)
return 1;
std::array<evo::render::Rectangle, 3> boxes{};
if (state.logical_width > 0 && state.logical_height > 0 && state.pixel_width > 0 &&
state.pixel_height > 0) {
if (!camera->resize({state.logical_width, state.logical_height, state.pixel_width,
state.pixel_height}))
return 1;
if (auto result =
camera->pan({navigation.x * 400 * seconds, navigation.y * 400 * seconds});
!result && result.error() != evo::client::CameraError::out_of_range)
return 1;
const evo::client::ScreenPoint anchor{
std::clamp(navigation.pointer.x, 0.0, static_cast<double>(state.logical_width)),
std::clamp(navigation.pointer.y, 0.0, static_cast<double>(state.logical_height))};
if (auto result = camera->zoom(std::exp2(navigation.wheel / 4), anchor);
!result && result.error() != evo::client::CameraError::out_of_range)
return 1;
// Local-mm presentation fixture only. Clip before float conversion.
const auto upper = camera->world_to_screen({-80, 48});
const auto lower = camera->world_to_screen({80, -48});
if (!upper || !lower)
return 1;
const double left = std::clamp(upper->x, 0.0, static_cast<double>(state.logical_width));
const double top = std::clamp(upper->y, 0.0, static_cast<double>(state.logical_height));
const double right =
std::clamp(lower->x, 0.0, static_cast<double>(state.logical_width));
const double bottom =
std::clamp(lower->y, 0.0, static_cast<double>(state.logical_height));
boxes[0] = {static_cast<float>(left),
static_cast<float>(top),
static_cast<float>(right - left),
static_cast<float>(bottom - top),
{64, 180, 140, 255}};
}
boxes[1] = {8, 8, 64, 8,
(*app)->ui_captured() ? evo::render::Color{240, 140, 32, 255}
: evo::render::Color{64, 180, 140, 255}};
boxes[2] = {36 + static_cast<float>(intention->move_x) / 50,
48 - static_cast<float>(intention->move_y) / 50,
8,
8,
{240, 240, 240, 255}};
const auto frame = evo::render::Frame::create(boxes, {16, 20, 28, 255});
if (!frame)
return 1;
const auto result = (*app)->present(*frame);
if (!result) {
std::fprintf(stderr, "client frame error=%d\n", static_cast<int>(result.error()));
return 1;
}
if (*result == evo::client::Step::closed)
break;
++count;
std::this_thread::sleep_for(std::chrono::milliseconds{16});
}
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "client exception: %.200s\n", e.what());
return 1;
} catch (...) {
std::fputs("client exception\n", stderr);
return 1;
}