Compare commits

..

No commits in common. "master" and "feature/pan_zoom" have entirely different histories.

2 changed files with 47 additions and 45 deletions

View File

@ -1,27 +1,45 @@
#include <memory>
#include <thread> #include <thread>
#include <memory>
#include "gameloop.hpp" #include "gameloop.hpp"
#include "log.hpp"
#include "math.hpp"
#include "pathfinder/base.hpp"
#include "pathfindingdemo.hpp" #include "pathfindingdemo.hpp"
#include "user_input.hpp"
#include "window.hpp" #include "window.hpp"
#include "user_input.hpp"
#include "log.hpp"
#include "pathfinder/base.hpp"
#include "math.hpp"
void GameLoop::Run() {
LOG_INFO("Running the game");
while (!m_Game->IsExitRequested()) {
m_Game->HandleActions(m_UserInput->GetActions());
m_Game->UpdatePlayerVelocity();
m_Window->ClearWindow();
// TODO wrap all of the drawing in some function
// TODO rethink coupling and dependencies here
void GameLoop::Draw() {
// 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();
for (size_t row = 0; row < tiles.size(); row++) { for (size_t row = 0; row < tiles.size(); row++) {
for (size_t col = 0; col < tiles[row].size(); col++) { for (size_t col = 0; col < tiles[row].size(); col++) {
const auto& camera = m_Game->GetCamera(); const auto& camera = m_Game->GetCamera();
const auto &position = camera.WorldToWindow(map.TileEdgeToWorld( const auto& position = camera.WorldToWindow(
TilePos{static_cast<int32_t>(row), static_cast<int32_t>(col)})); map.TileEdgeToWorld(
const auto &size = camera.WorldToWindowSize(map.GetTileSize()); TilePos{static_cast<int32_t>(row), static_cast<int32_t>(col)}
)
);
const auto& size = camera.WorldToWindowSize(
map.GetTileSize()
);
// LOG_DEBUG("Drawing rect (", row, ", ", col, ")"); // LOG_DEBUG("Drawing rect (", row, ", ", col, ")");
m_Window->DrawRect(position, size, tiles[row][col]->R, tiles[row][col]->G, m_Window->DrawRect(
position,
size,
tiles[row][col]->R, tiles[row][col]->G,
tiles[row][col]->B, tiles[row][col]->A); tiles[row][col]->B, tiles[row][col]->A);
} }
} }
@ -30,32 +48,18 @@ void GameLoop::Draw() {
WorldPos start_pos = m_Game->GetPlayer()->GetPosition(); WorldPos start_pos = m_Game->GetPlayer()->GetPosition();
for (const auto& next_pos: m_Game->GetPath()) { for (const auto& next_pos: m_Game->GetPath()) {
const auto& camera = m_Game->GetCamera(); const auto& camera = m_Game->GetCamera();
m_Window->DrawLine(camera.WorldToWindow(start_pos), m_Window->DrawLine(camera.WorldToWindow(start_pos), camera.WorldToWindow(next_pos));
camera.WorldToWindow(next_pos));
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()) {
const auto& camera = m_Game->GetCamera(); const auto& camera = m_Game->GetCamera();
m_Window->DrawSprite(camera.WorldToWindow(entity->GetPosition()), m_Window->DrawSprite(camera.WorldToWindow(entity->GetPosition()), entity->GetSprite(), camera.GetZoom());
entity->GetSprite(), camera.GetZoom());
}
} }
// TODO rethink coupling and dependencies in the game loop class
void GameLoop::Run() {
LOG_INFO("Running the game");
while (!m_Game->IsExitRequested()) {
m_Game->HandleActions(m_UserInput->GetActions());
m_Game->UpdatePlayerVelocity();
// TODO measure fps, draw only if delay for target fps was reached
m_Window->ClearWindow();
Draw();
m_Window->Flush(); m_Window->Flush();
// TODO measure fps
std::this_thread::sleep_for(std::chrono::milliseconds(30)); std::this_thread::sleep_for(std::chrono::milliseconds(30));
} }
} }

View File

@ -27,8 +27,6 @@ public:
} }
private: private:
void Draw();
std::unique_ptr<PathFindingDemo> m_Game; std::unique_ptr<PathFindingDemo> m_Game;
std::unique_ptr<Window> m_Window; std::unique_ptr<Window> m_Window;
std::unique_ptr<UserInput> m_UserInput; std::unique_ptr<UserInput> m_UserInput;