Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
commit 555ff14fa83b84e83e1aadf677355cb451118368 parent f8a72e4088bbe08ecff44a106ed82428800f728c Author: sentomk <sentomk@proton.me> Date: Mon, 23 Nov 2026 17:38:43 +0800 feat: add fixed-point Vec2 and use it in the demo Diffstat:
| A | src/core/vec2.h | | | 59 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | src/main.cpp | | | 46 | +++++++++++++++++++++------------------------- |
| A | tests/unit/vec2_test.cpp | | | 65 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 145 insertions(+), 25 deletions(-)
diff --git a/src/core/vec2.h b/src/core/vec2.h @@ -0,0 +1,59 @@ +/// @file core/vec2.h +/// @brief Two-dimensional fixed-point vector for the simulation. +#pragma once + +#include "core/fixed.h" + +namespace kumo { + +/// @brief A 2D position, offset, or velocity in 16.16 fixed point. +struct Vec2 { + Fixed x; + Fixed y; + + /// @brief The zero vector (0, 0). + static constexpr Vec2 zero() { return {}; } + + /// @brief A vector from whole integer coordinates. + static constexpr Vec2 from_ints(int x, int y) { + return {Fixed::from_int(x), Fixed::from_int(y)}; + } + + constexpr Vec2 operator-() const { return {-x, -y}; } + constexpr Vec2 operator+(Vec2 other) const { + return {x + other.x, y + other.y}; + } + constexpr Vec2 operator-(Vec2 other) const { + return {x - other.x, y - other.y}; + } + constexpr Vec2 operator*(Fixed scalar) const { + return {x * scalar, y * scalar}; + } + + constexpr Vec2& operator+=(Vec2 other) { + x += other.x; + y += other.y; + return *this; + } + constexpr Vec2& operator-=(Vec2 other) { + x -= other.x; + y -= other.y; + return *this; + } + constexpr Vec2& operator*=(Fixed scalar) { + x = x * scalar; + y = y * scalar; + return *this; + } + + constexpr bool operator==(const Vec2&) const = default; +}; + +constexpr Vec2 operator*(Fixed scalar, Vec2 vector) { return vector * scalar; } + +/// @brief Component-wise linear interpolation from @p a to @p b at @p t. +constexpr Vec2 lerp(Vec2 a, Vec2 b, Fixed t) { + return {lerp(a.x, b.x, t), lerp(a.y, b.y, t)}; +} + +} // namespace kumo diff --git a/src/main.cpp b/src/main.cpp @@ -1,13 +1,13 @@ #include <algorithm> #include <cstdio> #include <cstdlib> -#include <vector> - #include <ptn/patternia.hpp> +#include <vector> #include "core/clock.h" #include "core/fixed.h" #include "core/types.h" +#include "core/vec2.h" #include "platform/keyboard.h" #include "platform/window.h" #include "render/frame_buffer.h" @@ -25,15 +25,10 @@ constexpr int kBoxLifetimeTicks = 180; constexpr size_t kMaxSpawnedBoxes = 32; constexpr auto kPlayerSpeed = kumo::Fixed::from_int(2); // Pixels per tick. -struct Vec2 { - kumo::Fixed x; - kumo::Fixed y; -}; - struct BouncingBox { - Vec2 position; - Vec2 previous; - Vec2 velocity; + kumo::Vec2 position; + kumo::Vec2 previous; + kumo::Vec2 velocity; int ticks_left; }; @@ -46,20 +41,21 @@ void advance_axis(kumo::Fixed& position, kumo::Fixed& velocity, velocity = -velocity; return next; }; - position = match(position + velocity) | - on($[_ < kumo::Fixed{}] >> - [&](kumo::Fixed next) { return reflect(-next); }, - $[_ > limit] >> [&](kumo::Fixed next) { - return reflect(limit - (next - limit)); - }, - $ >> [](kumo::Fixed next) { return next; }); + position = + match(position + velocity) | + on( + $[_ < kumo::Fixed{}] >> + [&](kumo::Fixed next) { return reflect(-next); }, + $[_ > limit] >> + [&](kumo::Fixed next) { return reflect(limit - (next - limit)); }, + $ >> [](kumo::Fixed next) { return next; }); } -void draw_box(kumo::FrameBuffer& frame, Vec2 previous, Vec2 position, - kumo::Fixed alpha, int size, kumo::Color color) { - const int x = kumo::lerp(previous.x, position.x, alpha).round(); - const int y = kumo::lerp(previous.y, position.y, alpha).round(); - kumo::fill_rect(frame, x, y, size, size, color); +void draw_box(kumo::FrameBuffer& frame, kumo::Vec2 previous, + kumo::Vec2 position, kumo::Fixed alpha, int size, + kumo::Color color) { + const kumo::Vec2 drawn = kumo::lerp(previous, position, alpha); + kumo::fill_rect(frame, drawn.x.round(), drawn.y.round(), size, size, color); } /// @brief Exercises keyboard input, fixed updates and interpolation. @@ -95,7 +91,7 @@ class DemoGame final : public kumo::Game { // Consume press edges here so a frame with several ticks spawns each once. constexpr auto offset = Fixed::from_int((kPlayerSize - kSpawnedBoxSize) / 2); - const Vec2 origin{position_.x + offset, position_.y + offset}; + const kumo::Vec2 origin{position_.x + offset, position_.y + offset}; for (uint32_t presses = keyboard_.take_presses(Key::Space); presses > 0 && boxes_.size() < kMaxSpawnedBoxes; --presses) { boxes_.push_back({origin, @@ -118,8 +114,8 @@ class DemoGame final : public kumo::Game { private: kumo::Keyboard& keyboard_; - Vec2 position_{kumo::Fixed::from_int(40), kumo::Fixed::from_int(40)}; - Vec2 previous_ = position_; + kumo::Vec2 position_ = kumo::Vec2::from_ints(40, 40); + kumo::Vec2 previous_ = position_; std::vector<BouncingBox> boxes_; }; diff --git a/tests/unit/vec2_test.cpp b/tests/unit/vec2_test.cpp @@ -0,0 +1,65 @@ +#include "core/vec2.h" + +#include "check.h" +#include "core/fixed.h" + +using kumo::Fixed; +using kumo::lerp; +using kumo::Vec2; + +TEST(vec2_default_and_constructors) { + const Vec2 zero{}; + CHECK_EQ(zero, Vec2::zero()); + CHECK_EQ(zero.x, Fixed::from_int(0)); + CHECK_EQ(zero.y, Fixed::from_int(0)); + + const Vec2 point = Vec2::from_ints(3, -4); + CHECK_EQ(point.x, Fixed::from_int(3)); + CHECK_EQ(point.y, Fixed::from_int(-4)); +} + +TEST(vec2_arithmetic_and_negation) { + const Vec2 a = Vec2::from_ints(2, -3); + const Vec2 b = {Fixed::ratio(1, 2), Fixed::from_int(5)}; + + CHECK_EQ(-a, Vec2::from_ints(-2, 3)); + CHECK_EQ(a + b, (Vec2{Fixed::ratio(5, 2), Fixed::from_int(2)})); + CHECK_EQ(a - b, (Vec2{Fixed::ratio(3, 2), Fixed::from_int(-8)})); + + Vec2 acc = a; + acc += b; + CHECK_EQ(acc, a + b); + acc -= b; + CHECK_EQ(acc, a); +} + +TEST(vec2_scalar_multiplication) { + const Vec2 v = {Fixed::ratio(3, 2), Fixed::from_int(-4)}; + const Fixed half = Fixed::ratio(1, 2); + const Vec2 expected = {Fixed::ratio(3, 4), Fixed::from_int(-2)}; + + CHECK_EQ(v * half, expected); + CHECK_EQ(half * v, expected); + + Vec2 scaled = v; + scaled *= half; + CHECK_EQ(scaled, expected); +} + +TEST(vec2_lerp_interpolates_both_components) { + const Vec2 a = Vec2::from_ints(10, -20); + const Vec2 b = Vec2::from_ints(30, 20); + + CHECK_EQ(lerp(a, b, Fixed::from_int(0)), a); + CHECK_EQ(lerp(a, b, Fixed::from_int(1)), b); + CHECK_EQ(lerp(a, b, Fixed::ratio(1, 2)), Vec2::from_ints(20, 0)); + CHECK_EQ(lerp(a, b, Fixed::ratio(1, 4)), Vec2::from_ints(15, -10)); +} + +TEST(vec2_is_constexpr) { + constexpr Vec2 a = Vec2::from_ints(1, 2); + constexpr Vec2 b = Vec2::from_ints(5, 6); + constexpr Vec2 mid = lerp(a, b, Fixed::ratio(1, 2)); + static_assert(mid == Vec2::from_ints(3, 4)); + CHECK_EQ(mid, Vec2::from_ints(3, 4)); +}
