#include #include #include #include #include "core/clock.h" #include "core/fixed.h" #include "core/types.h" #include "core/vec2.h" #include "ecs/entity.h" #include "ecs/world.h" #include "platform/keyboard.h" #include "platform/window.h" #include "render/frame_buffer.h" #include "render/raster.h" #include "runtime/game.h" #include "runtime/loop.h" namespace { constexpr int kScreenWidth = 320; constexpr int kScreenHeight = 180; constexpr int kPlayerSize = 20; constexpr int kSpawnedBoxSize = 8; constexpr int kBoxLifetimeTicks = 180; constexpr size_t kMaxSpawnedBoxes = 32; constexpr auto kPlayerSpeed = kumo::Fixed::from_int(2); // Pixels per tick. struct Transform { kumo::Vec2 position; kumo::Vec2 previous; }; struct Velocity { kumo::Vec2 value; }; struct BoxSprite { int size; kumo::Color color; }; struct Lifetime { int ticks_left; }; struct PlayerTag {}; struct BounceTag {}; /// @brief Advances one axis and reflects any overshoot at either boundary. void advance_axis(kumo::Fixed& position, kumo::Fixed& velocity, kumo::Fixed limit) { using namespace ptn; const auto reflect = [&velocity](kumo::Fixed next) { 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; }); } void save_previous_transforms(kumo::World& world) { world.each( [](kumo::Entity /*e*/, Transform& t) { t.previous = t.position; }); } void player_input_system(kumo::World& world, const kumo::Keyboard& keyboard) { using kumo::Fixed; using kumo::Key; const int dx = keyboard.is_down(Key::Right) - keyboard.is_down(Key::Left); const int dy = keyboard.is_down(Key::Down) - keyboard.is_down(Key::Up); world.each( [dx, dy](kumo::Entity /*e*/, PlayerTag&, Transform& t, const BoxSprite& sprite) { t.position.x = std::clamp(t.position.x + kPlayerSpeed * Fixed::from_int(dx), Fixed{}, Fixed::from_int(kScreenWidth - sprite.size)); t.position.y = std::clamp(t.position.y + kPlayerSpeed * Fixed::from_int(dy), Fixed{}, Fixed::from_int(kScreenHeight - sprite.size)); }); } void bounce_movement_system(kumo::World& world) { using kumo::Fixed; world.each( [](kumo::Entity /*e*/, BounceTag&, Transform& t, Velocity& v, const BoxSprite& sprite) { advance_axis(t.position.x, v.value.x, Fixed::from_int(kScreenWidth - sprite.size)); advance_axis(t.position.y, v.value.y, Fixed::from_int(kScreenHeight - sprite.size)); }); } void lifetime_system(kumo::World& world) { world.each([&world](kumo::Entity e, Lifetime& life) { --life.ticks_left; if (life.ticks_left <= 0) world.destroy_deferred(e); }); world.flush_destroyed(); } void draw_sprite(kumo::FrameBuffer& frame, const Transform& t, const BoxSprite& sprite, kumo::Fixed alpha) { const kumo::Vec2 drawn = kumo::lerp(t.previous, t.position, alpha); kumo::fill_rect(frame, drawn.x.round(), drawn.y.round(), sprite.size, sprite.size, sprite.color); } /// @brief Exercises ECS, keyboard input, fixed updates and interpolation. class DemoGame final : public kumo::Game { public: explicit DemoGame(kumo::Keyboard& keyboard) : keyboard_(keyboard) { player_ = world_.create(); const kumo::Vec2 start = kumo::Vec2::from_ints(40, 40); world_.emplace(player_); world_.emplace(player_, start, start); world_.emplace(player_, kPlayerSize, kumo::make_color(250, 210, 70)); } void fixed_update(const kumo::Tick& /*tick*/) override { using kumo::Fixed; using kumo::Key; save_previous_transforms(world_); player_input_system(world_, keyboard_); bounce_movement_system(world_); lifetime_system(world_); // Consume press edges here so a frame with several ticks spawns each once. constexpr auto offset = Fixed::from_int((kPlayerSize - kSpawnedBoxSize) / 2); const kumo::Vec2 player_pos = world_.get(player_).position; const kumo::Vec2 origin{player_pos.x + offset, player_pos.y + offset}; for (uint32_t presses = keyboard_.take_presses(Key::Space); presses > 0 && world_.count() < kMaxSpawnedBoxes; --presses) { const kumo::Entity box = world_.create(); world_.emplace(box); world_.emplace(box, origin, origin); world_.emplace( box, kumo::Vec2{Fixed::ratio(5, 2), Fixed::ratio(7, 4)}); world_.emplace(box, kSpawnedBoxSize, kumo::make_color(100, 195, 250)); world_.emplace(box, kBoxLifetimeTicks); } } void render(kumo::FrameBuffer& frame, kumo::Fixed alpha) const override { kumo::clear(frame, kumo::make_color(24, 28, 40)); world_.each( [&](kumo::Entity /*e*/, const PlayerTag&, const Transform& t, const BoxSprite& sprite) { draw_sprite(frame, t, sprite, alpha); }); world_.each( [&](kumo::Entity /*e*/, const BounceTag&, const Transform& t, const BoxSprite& sprite) { draw_sprite(frame, t, sprite, alpha); }); } private: kumo::Keyboard& keyboard_; kumo::World world_; kumo::Entity player_; }; } // namespace int main() { kumo::Window* window = kumo::window_create({"Kumo", 1280, 720}); if (window == nullptr) { std::fprintf(stderr, "could not create the window\n"); return EXIT_FAILURE; } kumo::FrameBuffer frame(kScreenWidth, kScreenHeight); DemoGame game(kumo::window_keyboard(window)); kumo::SystemClock clock; kumo::Loop loop(clock); int64_t shown_seconds = -1; for (;;) { kumo::window_poll_events(window); if (kumo::window_should_close(window)) break; loop.step_frame(game, frame); const int64_t seconds = loop.simulated_time() / kumo::kNanosecondsPerSecond; if (seconds != shown_seconds) { shown_seconds = seconds; char title[64]; std::snprintf(title, sizeof(title), "Kumo - %lld s", static_cast(seconds)); kumo::window_set_title(window, title); } // Waits for the display, which paces the loop. kumo::window_present(window, frame); } kumo::window_destroy(window); return 0; }