#include "evo/client/application.hpp" #include "render/sdl_renderer.hpp" #include #include #include #include #include #include #include #include #include void client_controls_tests(); namespace { using namespace evo::client; void check(bool ok, std::source_location at = std::source_location::current()) { if (!ok) throw std::runtime_error("client lifecycle line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) template bool refused(const std::expected& result, E error) { return !result && result.error() == error; } evo::render::Frame fixture() { const std::array rectangles{{{0, 0, 32, 32, {255, 0, 0, 255}}}}; auto result = evo::render::Frame::create(rectangles); CHECK(result); return std::move(result.value()); } SDL_Window* window() { int count{}; SDL_Window** windows = SDL_GetWindows(&count); SDL_Window* result = count == 1 ? windows[0] : nullptr; SDL_free(static_cast(windows)); CHECK(result); return result; } void send(SDL_EventType type, SDL_WindowID id) { SDL_Event event{}; event.type = type; if (type >= SDL_EVENT_RENDER_TARGETS_RESET && type <= SDL_EVENT_RENDER_DEVICE_LOST) event.render.windowID = id; else event.window.windowID = id; CHECK(SDL_PushEvent(&event)); } void frame_contract() { using namespace evo::render; std::array r{{{1, 2, 3, 4, {5, 6, 7, 255}}}}; const auto frame = Frame::create(r); CHECK(frame); r[0].x = 100; CHECK(frame->rectangles()[0].x == 1); // Owns copy; caller mutation cannot alter it. r[0].x = std::numeric_limits::quiet_NaN(); CHECK(refused(Frame::create(r), FrameError::invalid_geometry)); r[0].x = 0; r[0].width = -1; CHECK(refused(Frame::create(r), FrameError::invalid_geometry)); r[0].width = std::numeric_limits::infinity(); CHECK(refused(Frame::create(r), FrameError::invalid_geometry)); std::vector full(max_rectangles); CHECK(Frame::create(full)); full.emplace_back(); CHECK(refused(Frame::create(full), FrameError::too_many_primitives)); } void pixels(SDL_Window* native, const evo::render::Frame& frame, WindowState state) { auto* renderer = SDL_GetRenderer(native); CHECK(renderer); // Exercise non-unit, anisotropic drawable scaling without requiring a HiDPI monitor. state.logical_width = state.pixel_width / 2; state.logical_height = state.pixel_height; CHECK(detail::draw(renderer, frame, state)); const std::unique_ptr surface{ SDL_RenderReadPixels(renderer, nullptr), SDL_DestroySurface}; CHECK(surface); Uint8 r{}, g{}, b{}, a{}; CHECK(SDL_ReadSurfacePixel(surface.get(), 48, 8, &r, &g, &b, &a)); CHECK(r == 255 && g == 0 && b == 0 && a == 255); CHECK(SDL_ReadSurfacePixel(surface.get(), 80, 8, &r, &g, &b, &a)); CHECK(r == 0 && g == 0 && b == 0 && a == 255); CHECK(SDL_ReadSurfacePixel(surface.get(), 8, 48, &r, &g, &b, &a)); CHECK(r == 0 && g == 0 && b == 0 && a == 255); } void lifecycle() { const auto frame = fixture(); CHECK(refused(Application::open({0, 64, false, "software"}), Error::invalid_config)); CHECK(refused(Application::open({64, 64, false, "evo-invalid"}), Error::renderer)); CHECK(SDL_WasInit(0) == 0); // Failed partial initialization released SDL. for (int round = 0; round < 32; ++round) { auto result = Application::open({160, 120, false, "software"}); CHECK(result); auto app = std::move(result.value()); CHECK(refused(Application::open({}), Error::already_open)); CHECK(app->step(frame) == Step::rendered); auto* native = window(); pixels(native, frame, app->state()); const auto id = SDL_GetWindowID(native); // Foreign close/reset must not affect the one owned window. send(SDL_EVENT_WINDOW_CLOSE_REQUESTED, id + 1); send(SDL_EVENT_RENDER_DEVICE_LOST, id + 1); send(SDL_EVENT_WINDOW_FOCUS_GAINED, id); CHECK(app->step(frame) == Step::rendered); CHECK(app->state().accepts_input()); send(SDL_EVENT_WINDOW_FOCUS_LOST, id); CHECK(app->step(frame) == Step::rendered); CHECK(!app->state().accepts_input()); send(SDL_EVENT_WINDOW_MINIMIZED, id); CHECK(app->step(frame) == Step::paused); send(SDL_EVENT_WINDOW_RESTORED, id); CHECK(app->step(frame) == Step::rendered); CHECK(SDL_SetWindowSize(native, 240, 180)); send(SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED, id); send(SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED, id); CHECK(app->step(frame) == Step::rendered); int lw{}, lh{}, pw{}, ph{}; CHECK(SDL_GetWindowSize(native, &lw, &lh)); CHECK(SDL_GetWindowSizeInPixels(native, &pw, &ph)); const auto state = app->state(); CHECK(state.logical_width == lw && state.logical_height == lh); CHECK(state.pixel_width == pw && state.pixel_height == ph); CHECK(std::isfinite(state.content_scale) && state.content_scale > 0); send(SDL_EVENT_RENDER_TARGETS_RESET, id); CHECK(app->step(frame) == Step::rendered); send(SDL_EVENT_RENDER_DEVICE_RESET, id); CHECK(app->step(frame) == Step::rendered); CHECK(SDL_HideWindow(native)); send(SDL_EVENT_WINDOW_HIDDEN, id); CHECK(app->step(frame) == Step::paused); CHECK(SDL_ShowWindow(native)); send(SDL_EVENT_WINDOW_SHOWN, id); CHECK(app->step(frame) == Step::rendered); if (round % 2 == 0) { send(SDL_EVENT_RENDER_DEVICE_LOST, id); CHECK(refused(app->step(frame), Error::device_lost)); CHECK(refused(app->step(frame), Error::device_lost)); CHECK(app->state().failed && !app->state().accepts_input()); } else { send(SDL_EVENT_WINDOW_CLOSE_REQUESTED, id); CHECK(app->step(frame) == Step::closed); CHECK(app->step(frame) == Step::closed); } app.reset(); CHECK(SDL_WasInit(0) == 0); } auto hidden = Application::open({64, 64, true, "software"}); CHECK(hidden); CHECK((*hidden)->step(frame) == Step::paused); send(SDL_EVENT_QUIT, 0); CHECK((*hidden)->step(frame) == Step::closed); } Intention required(std::expected result) { CHECK(result); return result.value(); } void native_input() { const auto frame = fixture(); auto result = Application::open({320, 240, false, "software"}); CHECK(result); auto app = std::move(result.value()); CHECK(app->poll_events()); const auto id = SDL_GetWindowID(window()); send(SDL_EVENT_WINDOW_FOCUS_GAINED, id); auto key = [id](SDL_Scancode scancode, bool down, bool repeat = false) { SDL_Event event{}; event.type = down ? SDL_EVENT_KEY_DOWN : SDL_EVENT_KEY_UP; event.key.windowID = id; event.key.scancode = scancode; event.key.repeat = repeat; CHECK(SDL_PushEvent(&event)); }; key(SDL_SCANCODE_W, true); key(SDL_SCANCODE_RIGHT, true); CHECK(app->poll_events()); const auto first = app->sample_intention(40); CHECK(first && first->move_y == 1000 && first->sequence == 1 && first->tick == 40); CHECK(app->take_navigation().x == 1); key(SDL_SCANCODE_W, false); CHECK(app->poll_events()); CHECK(required(app->sample_intention(41)).move_y == 0); key(SDL_SCANCODE_W, true); send(SDL_EVENT_WINDOW_FOCUS_LOST, id); send(SDL_EVENT_WINDOW_FOCUS_GAINED, id); key(SDL_SCANCODE_W, true, true); CHECK(app->poll_events()); CHECK(required(app->sample_intention(42)).move_y == 0 && app->take_navigation().x == 0); key(SDL_SCANCODE_W, true); CHECK(app->poll_events()); app->set_ui_capture(true); CHECK(required(app->sample_intention(43)).move_y == 0); key(SDL_SCANCODE_W, true); CHECK(app->poll_events()); app->set_ui_capture(false); CHECK(required(app->sample_intention(44)).move_y == 0); CHECK(app->poll_events()); key(SDL_SCANCODE_TAB, true); key(SDL_SCANCODE_W, true); CHECK(app->poll_events()); CHECK(app->ui_captured() && required(app->sample_intention(45)).move_y == 0); key(SDL_SCANCODE_TAB, true, true); CHECK(app->poll_events()); CHECK(app->ui_captured()); key(SDL_SCANCODE_TAB, false); key(SDL_SCANCODE_TAB, true); key(SDL_SCANCODE_W, true); CHECK(app->poll_events()); CHECK(!app->ui_captured() && required(app->sample_intention(46)).move_y == 1000); // A saturated event batch cancels motion until the backlog drains. for (int i = 0; i < 300; ++i) key(SDL_SCANCODE_RIGHT, true); CHECK(app->poll_events()); CHECK(required(app->sample_intention(47)).move_y == 0 && app->take_navigation().x == 0); CHECK(app->poll_events()); CHECK(required(app->sample_intention(48)).move_y == 0 && app->take_navigation().x == 0); SDL_Event wheel{}; wheel.type = SDL_EVENT_MOUSE_WHEEL; wheel.wheel.windowID = id; wheel.wheel.y = 2; wheel.wheel.mouse_x = 123; wheel.wheel.mouse_y = 45; wheel.wheel.direction = SDL_MOUSEWHEEL_FLIPPED; CHECK(SDL_PushEvent(&wheel)); CHECK(app->poll_events()); const auto navigation = app->take_navigation(); CHECK(navigation.wheel == -2 && navigation.pointer.x == 123 && navigation.pointer.y == 45); app->set_ui_capture(true); CHECK(SDL_PushEvent(&wheel)); CHECK(app->poll_events()); CHECK(app->take_navigation().wheel == 0); app->set_ui_capture(false); key(SDL_SCANCODE_W, true); CHECK(app->poll_events()); key(SDL_SCANCODE_W, true); send(SDL_EVENT_WINDOW_MINIMIZED, id); CHECK(app->poll_events()); CHECK(required(app->sample_intention(49)).move_y == 0); send(SDL_EVENT_WINDOW_RESTORED, id); CHECK(app->poll_events()); CHECK(required(app->sample_intention(50)).move_y == 0); app->set_ui_capture(true); key(SDL_SCANCODE_W, true); // Queued while captured; releasing capture must not replay it. app->set_ui_capture(false); CHECK(app->poll_events()); CHECK(required(app->sample_intention(51)).move_y == 0); CHECK(app->present(frame) == Step::rendered); } } // namespace int main() try { client_controls_tests(); frame_contract(); lifecycle(); native_input(); return 0; } catch (const std::exception& e) { std::fprintf(stderr, "%s: %s\n", e.what(), SDL_GetError()); return 1; } catch (...) { return 1; }