Skip to content

Commit 279c008

Browse files
authored
Add Game class, lighthouse controls foundation (#3)
* Update le2d to v0.1.1 * Add `class Game` * Add `App::m_game`
1 parent 1942048 commit 279c008

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

ext/src.zip

30.7 KB
Binary file not shown.

src/app.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <imgui.h>
22
#include <app.hpp>
33
#include <djson/json.hpp>
4+
#include <game.hpp>
5+
#include <klib/visitor.hpp>
46
#include <log.hpp>
57

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

2123
void App::run() {
22-
while (m_context.is_running()) {
23-
m_context.next_frame();
24+
auto game = Game{&m_services};
2425

25-
ImGui::ShowDemoWindow();
26+
auto const event_visitor = klib::SubVisitor{
27+
[&game](le::event::CursorPos const& cursor_pos) { game.on_cursor_pos(cursor_pos); },
28+
};
2629

30+
auto delta_time = kvf::DeltaTime{};
31+
while (m_context.is_running()) {
32+
m_context.next_frame();
33+
auto const dt = delta_time.tick();
34+
for (auto const& event : m_context.event_queue()) { std::visit(event_visitor, event); }
35+
game.tick(dt);
36+
if (auto renderer = m_context.begin_render()) { game.render(renderer); }
2737
m_context.present();
2838
}
39+
m_context.wait_idle();
2940
}
3041

3142
void App::bind_services() {

src/game.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <game.hpp>
2+
#include <glm/gtx/norm.hpp>
3+
#include <le2d/context.hpp>
4+
#include <cmath>
5+
6+
namespace miracle {
7+
Game::Game(gsl::not_null<le::ServiceLocator const*> services) : m_services(services) {
8+
m_triangle.vertices = {
9+
le::Vertex{.position = {-50.0f, -50.0f}},
10+
le::Vertex{.position = {+50.0f, -50.0f}},
11+
le::Vertex{.position = {+0.0f, +75.0f}},
12+
};
13+
m_circle.create(50.0f);
14+
}
15+
16+
void Game::on_cursor_pos(le::event::CursorPos const& cursor_pos) {
17+
auto const framebuffer_size = m_services->get<le::Context>().framebuffer_size();
18+
m_cursor_pos = cursor_pos.normalized.to_target(framebuffer_size);
19+
}
20+
21+
void Game::tick([[maybe_unused]] kvf::Seconds const dt) {
22+
m_circle.transform.position = m_cursor_pos;
23+
24+
auto const dist_sq = glm::length2(m_cursor_pos);
25+
if (dist_sq > 0.1f) {
26+
auto const dist = std::sqrt(dist_sq);
27+
auto const normalized = m_cursor_pos / dist;
28+
static constexpr auto up_v = glm::vec2{0.0f, 1.0f};
29+
auto const dot = glm::dot(normalized, up_v);
30+
auto const angle = glm::degrees(std::acos(dot));
31+
m_triangle.transform.orientation = m_cursor_pos.x > 0.0f ? -angle : angle;
32+
}
33+
}
34+
35+
void Game::render(le::Renderer& renderer) const {
36+
m_triangle.draw(renderer);
37+
m_circle.draw(renderer);
38+
}
39+
} // namespace miracle

src/game.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include <kvf/time.hpp>
3+
#include <le2d/drawable/shape.hpp>
4+
#include <le2d/event.hpp>
5+
#include <le2d/renderer.hpp>
6+
#include <le2d/service_locator.hpp>
7+
8+
namespace miracle {
9+
class Game {
10+
public:
11+
explicit Game(gsl::not_null<le::ServiceLocator const*> services);
12+
13+
void on_cursor_pos(le::event::CursorPos const& cursor_pos);
14+
15+
void tick(kvf::Seconds dt);
16+
void render(le::Renderer& renderer) const;
17+
18+
private:
19+
gsl::not_null<le::ServiceLocator const*> m_services;
20+
21+
le::drawable::Triangle m_triangle{};
22+
le::drawable::Circle m_circle{};
23+
24+
glm::vec2 m_cursor_pos{};
25+
};
26+
} // namespace miracle

0 commit comments

Comments
 (0)