diff --git a/ext/src.zip b/ext/src.zip index a88489d..19d9888 100644 Binary files a/ext/src.zip and b/ext/src.zip differ diff --git a/src/app.cpp b/src/app.cpp index cd12bb8..7f17df4 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include namespace miracle { @@ -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() { diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..c5ed010 --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +namespace miracle { +Game::Game(gsl::not_null 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().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 diff --git a/src/game.hpp b/src/game.hpp new file mode 100644 index 0000000..1601115 --- /dev/null +++ b/src/game.hpp @@ -0,0 +1,26 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace miracle { +class Game { + public: + explicit Game(gsl::not_null 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 m_services; + + le::drawable::Triangle m_triangle{}; + le::drawable::Circle m_circle{}; + + glm::vec2 m_cursor_pos{}; +}; +} // namespace miracle