diff --git a/cpp/src/gameloop.cpp b/cpp/src/gameloop.cpp index 65f1b18..5541b0d 100644 --- a/cpp/src/gameloop.cpp +++ b/cpp/src/gameloop.cpp @@ -7,6 +7,8 @@ #include "window.hpp" #include "user_input.hpp" #include "log.hpp" +#include "pathfinder.hpp" +#include "math.hpp" void GameLoop::Run() { LOG_INFO("Running the game"); @@ -16,6 +18,9 @@ void GameLoop::Run() { m_Window->ClearWindow(); + // TODO wrap all of the drawing in some function + // TODO rethink coupling and dependencies here + // draw the map (terrain tiles) const Map &map = m_Game->GetMap(); const auto &tiles = map.GetMapTiles(); @@ -29,11 +34,18 @@ void GameLoop::Run() { } } + // draw the path, if it exists + WorldPos start_pos = m_Game->GetPlayer()->GetPosition(); + for (const auto& next_pos: m_Game->GetPath()) { + m_Window->DrawLine(start_pos, next_pos); + start_pos = next_pos; + } + // draw all the entities (player etc) for (auto &entity : m_Game->GetEntities()) { m_Window->DrawSprite(entity->GetPosition(), entity->GetSprite()); } - + m_Window->Flush(); // TODO measure fps std::this_thread::sleep_for(std::chrono::milliseconds(30)); diff --git a/cpp/src/pathfindingdemo.hpp b/cpp/src/pathfindingdemo.hpp index a716441..126d1db 100644 --- a/cpp/src/pathfindingdemo.hpp +++ b/cpp/src/pathfindingdemo.hpp @@ -22,8 +22,9 @@ public: PathFindingDemo &operator=(PathFindingDemo &&) = delete; std::shared_ptr GetPlayer() { return m_Player; } - std::vector> &GetEntities() { return m_Entities; } - const Map &GetMap() const { return m_Map; } + std::vector>& GetEntities() { return m_Entities; } + const Map& GetMap() const { return m_Map; } + const pathfinder::Path& GetPath() const { return m_Path; } bool IsExitRequested() const { return m_ExitRequested; } void AddEntity(std::shared_ptr e); diff --git a/cpp/src/window.cpp b/cpp/src/window.cpp index 363b771..a488258 100644 --- a/cpp/src/window.cpp +++ b/cpp/src/window.cpp @@ -109,3 +109,10 @@ void Window::DrawCircle(const WorldPos &position, float radius) { cy + static_cast(std::round(radius * std::sin(a)))); } } + +void Window::DrawLine(const WorldPos &A, const WorldPos &B) +{ + SDL_SetRenderDrawColor(m_Renderer.get(), 255, 0, 0, 255); + SDL_RenderLine(m_Renderer.get(), A.x, A.y, B.x, B.y); +} + diff --git a/cpp/src/window.hpp b/cpp/src/window.hpp index 9a291e6..b582b53 100644 --- a/cpp/src/window.hpp +++ b/cpp/src/window.hpp @@ -28,6 +28,7 @@ public: void ClearWindow(); void Flush(); void DrawCircle(const WorldPos &position, float radius); + void DrawLine(const WorldPos &A, const WorldPos &B); std::shared_ptr m_Renderer = nullptr; SDL_Window *m_Window;