Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475#include "sc2k/ui/view_motion.hpp"
#include <algorithm>#include <array>#include <cmath>#include <cstdio>#include <vector>
#include "test_harness.hpp"
using sc2k::ui::ViewMotion;
static double pan_impulse(const std::vector<double>& frameTimes) { ViewMotion motion; motion.add_pan(800.0, 0.0); double position = 0.0; for (const double elapsed : frameTimes) position += motion.advance_pan(elapsed).x; return position;}
static void refresh_rate_does_not_change_response() { const std::vector<double> sixty(60, 1.0 / 60.0); const std::vector<double> oneTwenty(120, 1.0 / 120.0); std::vector<double> uneven; for (int i = 0; i < 60; ++i) { uneven.push_back(1.0 / 200.0); uneven.push_back(1.0 / 60.0 - 1.0 / 200.0); } const double at60 = pan_impulse(sixty); const double at120 = pan_impulse(oneTwenty); const double unevenResult = pan_impulse(uneven); CHECK(std::abs(at60 - at120) < 0.000001); CHECK(std::abs(at60 - unevenResult) < 0.000001);}
static void sixty_hz_input_is_even_on_a_120_hz_display() { ViewMotion motion; std::array<double, 120> movement{}; for (int frame = 0; frame < 120; ++frame) { if ((frame & 1) == 0) motion.add_pan(12.0, 0.0); movement[std::size_t(frame)] = motion.advance_pan(1.0 / 120.0).x; } double worstRatio = 1.0; for (int frame = 40; frame + 1 < 120; frame += 2) { const double larger = std::max(movement[std::size_t(frame)], movement[std::size_t(frame + 1)]); const double smaller = std::min(movement[std::size_t(frame)], movement[std::size_t(frame + 1)]); worstRatio = std::max(worstRatio, larger / smaller); } CHECK(worstRatio < 1.25); // the former half-per-frame filter alternated at exactly 2:1}
static void zoom_is_time_based_and_bounded() { ViewMotion sixty; ViewMotion oneTwenty; sixty.add_zoom(1.0, 3.0, 0.5, 2.0, 320, 240); oneTwenty.add_zoom(1.0, 3.0, 0.5, 2.0, 320, 240); double scale60 = 1.0; double scale120 = 1.0; for (int frame = 0; frame < 60; ++frame) scale60 = sixty.advance_zoom(scale60, 1.0 / 60.0).scale; for (int frame = 0; frame < 120; ++frame) scale120 = oneTwenty.advance_zoom(scale120, 1.0 / 120.0).scale; CHECK(std::abs(scale60 - scale120) < 0.000001); CHECK(scale60 == 2.0); CHECK(!sixty.zooming() && !oneTwenty.zooming());}
int main() { refresh_rate_does_not_change_response(); sixty_hz_input_is_even_on_a_120_hz_display(); zoom_is_time_based_and_bounded(); std::printf("view_motion: time-based pan and zoom smoothing OK\n"); return failures == 0 ? 0 : 1;}