Draw the path

This commit is contained in:
Jan Mrna 2025-09-27 18:22:53 +02:00
parent ea316ab997
commit e6fc4e881c
4 changed files with 24 additions and 3 deletions

View File

@ -7,6 +7,8 @@
#include "window.hpp" #include "window.hpp"
#include "user_input.hpp" #include "user_input.hpp"
#include "log.hpp" #include "log.hpp"
#include "pathfinder.hpp"
#include "math.hpp"
void GameLoop::Run() { void GameLoop::Run() {
LOG_INFO("Running the game"); LOG_INFO("Running the game");
@ -16,6 +18,9 @@ void GameLoop::Run() {
m_Window->ClearWindow(); m_Window->ClearWindow();
// TODO wrap all of the drawing in some function
// TODO rethink coupling and dependencies here
// draw the map (terrain tiles) // draw the map (terrain tiles)
const Map &map = m_Game->GetMap(); const Map &map = m_Game->GetMap();
const auto &tiles = map.GetMapTiles(); 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) // draw all the entities (player etc)
for (auto &entity : m_Game->GetEntities()) { for (auto &entity : m_Game->GetEntities()) {
m_Window->DrawSprite(entity->GetPosition(), entity->GetSprite()); m_Window->DrawSprite(entity->GetPosition(), entity->GetSprite());
} }
m_Window->Flush(); m_Window->Flush();
// TODO measure fps // TODO measure fps
std::this_thread::sleep_for(std::chrono::milliseconds(30)); std::this_thread::sleep_for(std::chrono::milliseconds(30));

View File

@ -22,8 +22,9 @@ public:
PathFindingDemo &operator=(PathFindingDemo &&) = delete; PathFindingDemo &operator=(PathFindingDemo &&) = delete;
std::shared_ptr<Player> GetPlayer() { return m_Player; } std::shared_ptr<Player> GetPlayer() { return m_Player; }
std::vector<std::shared_ptr<Entity>> &GetEntities() { return m_Entities; } std::vector<std::shared_ptr<Entity>>& GetEntities() { return m_Entities; }
const Map &GetMap() const { return m_Map; } const Map& GetMap() const { return m_Map; }
const pathfinder::Path& GetPath() const { return m_Path; }
bool IsExitRequested() const { return m_ExitRequested; } bool IsExitRequested() const { return m_ExitRequested; }
void AddEntity(std::shared_ptr<Entity> e); void AddEntity(std::shared_ptr<Entity> e);

View File

@ -109,3 +109,10 @@ void Window::DrawCircle(const WorldPos &position, float radius) {
cy + static_cast<int>(std::round(radius * std::sin(a)))); cy + static_cast<int>(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);
}

View File

@ -28,6 +28,7 @@ public:
void ClearWindow(); void ClearWindow();
void Flush(); void Flush();
void DrawCircle(const WorldPos &position, float radius); void DrawCircle(const WorldPos &position, float radius);
void DrawLine(const WorldPos &A, const WorldPos &B);
std::shared_ptr<SDL_Renderer> m_Renderer = nullptr; std::shared_ptr<SDL_Renderer> m_Renderer = nullptr;
SDL_Window *m_Window; SDL_Window *m_Window;