Skip to content

Add Game class, lighthouse controls foundation #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified ext/src.zip
Binary file not shown.
17 changes: 14 additions & 3 deletions src/app.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <imgui.h>
#include <app.hpp>
#include <djson/json.hpp>
#include <game.hpp>
#include <klib/visitor.hpp>
#include <log.hpp>

namespace miracle {
Expand All @@ -19,13 +21,22 @@ App::App() : m_context(context_ci), m_data_loader(le::FileDataLoader::upfind("as
}

void App::run() {
while (m_context.is_running()) {
m_context.next_frame();
auto game = Game{&m_services};

ImGui::ShowDemoWindow();
auto const event_visitor = klib::SubVisitor{
[&game](le::event::CursorPos const& cursor_pos) { game.on_cursor_pos(cursor_pos); },
};

auto delta_time = kvf::DeltaTime{};
while (m_context.is_running()) {
m_context.next_frame();
auto const dt = delta_time.tick();
for (auto const& event : m_context.event_queue()) { std::visit(event_visitor, event); }
game.tick(dt);
if (auto renderer = m_context.begin_render()) { game.render(renderer); }
m_context.present();
}
m_context.wait_idle();
}

void App::bind_services() {
Expand Down
39 changes: 39 additions & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <game.hpp>
#include <glm/gtx/norm.hpp>
#include <le2d/context.hpp>
#include <cmath>

namespace miracle {
Game::Game(gsl::not_null<le::ServiceLocator const*> services) : m_services(services) {
m_triangle.vertices = {
le::Vertex{.position = {-50.0f, -50.0f}},
le::Vertex{.position = {+50.0f, -50.0f}},
le::Vertex{.position = {+0.0f, +75.0f}},
};
m_circle.create(50.0f);
}

void Game::on_cursor_pos(le::event::CursorPos const& cursor_pos) {
auto const framebuffer_size = m_services->get<le::Context>().framebuffer_size();
m_cursor_pos = cursor_pos.normalized.to_target(framebuffer_size);
}

void Game::tick([[maybe_unused]] kvf::Seconds const dt) {
m_circle.transform.position = m_cursor_pos;

auto const dist_sq = glm::length2(m_cursor_pos);
if (dist_sq > 0.1f) {
auto const dist = std::sqrt(dist_sq);
auto const normalized = m_cursor_pos / dist;
static constexpr auto up_v = glm::vec2{0.0f, 1.0f};
auto const dot = glm::dot(normalized, up_v);
auto const angle = glm::degrees(std::acos(dot));
m_triangle.transform.orientation = m_cursor_pos.x > 0.0f ? -angle : angle;
}
}

void Game::render(le::Renderer& renderer) const {
m_triangle.draw(renderer);
m_circle.draw(renderer);
}
} // namespace miracle
26 changes: 26 additions & 0 deletions src/game.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <kvf/time.hpp>
#include <le2d/drawable/shape.hpp>
#include <le2d/event.hpp>
#include <le2d/renderer.hpp>
#include <le2d/service_locator.hpp>

namespace miracle {
class Game {
public:
explicit Game(gsl::not_null<le::ServiceLocator const*> services);

void on_cursor_pos(le::event::CursorPos const& cursor_pos);

void tick(kvf::Seconds dt);
void render(le::Renderer& renderer) const;

private:
gsl::not_null<le::ServiceLocator const*> m_services;

le::drawable::Triangle m_triangle{};
le::drawable::Circle m_circle{};

glm::vec2 m_cursor_pos{};
};
} // namespace miracle