Move gameloop implementation to gamelopp.cpp
This commit is contained in:
parent
82eb2e2539
commit
4419e9bac9
@ -2,8 +2,8 @@ all: test pathfinding
|
|||||||
# TODO add extra warnings
|
# TODO add extra warnings
|
||||||
# TODO linter?
|
# TODO linter?
|
||||||
|
|
||||||
pathfinding: src/main.cpp src/array.hpp src/sprite.cpp
|
pathfinding:
|
||||||
g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp src/entities.cpp
|
g++ -Wall -ggdb3 -lSDL3 -lSDL3_image -std=c++23 -lGLEW -lGL -o pathfinding src/main.cpp src/sprite.cpp src/entities.cpp src/gameloop.cpp
|
||||||
|
|
||||||
test: src/test.cpp src/array.hpp
|
test: src/test.cpp src/array.hpp
|
||||||
g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp
|
g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
#include <thread>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "gameloop.hpp"
|
||||||
|
|
||||||
|
#include "pathfindingdemo.hpp"
|
||||||
|
#include "window.hpp"
|
||||||
|
#include "user_input.hpp"
|
||||||
|
#include "log.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();
|
||||||
|
|
||||||
|
// draw the map (terrain tiles)
|
||||||
|
const Map &map = m_Game->GetMap();
|
||||||
|
const auto &tiles = map.GetMapTiles();
|
||||||
|
for (size_t row = 0; row < tiles.size(); row++) {
|
||||||
|
for (size_t col = 0; col < tiles[row].size(); col++) {
|
||||||
|
// LOG_DEBUG("Drawing rect (", row, ", ", col, ")");
|
||||||
|
m_Window->DrawRect(
|
||||||
|
map.TileToWorld(TilePos{row, col}),
|
||||||
|
map.GetTileSize(), tiles[row][col]->R, tiles[row][col]->G,
|
||||||
|
tiles[row][col]->B, tiles[row][col]->A);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
}
|
@ -1,55 +1,28 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
#include "pathfindingdemo.hpp"
|
#include "pathfindingdemo.hpp"
|
||||||
#include "window.hpp"
|
#include "window.hpp"
|
||||||
#include "user_input.hpp"
|
#include "user_input.hpp"
|
||||||
#include "log.hpp"
|
|
||||||
|
|
||||||
class GameLoop {
|
class GameLoop {
|
||||||
public:
|
public:
|
||||||
GameLoop() = default;
|
GameLoop() = default;
|
||||||
|
~GameLoop() = default;
|
||||||
|
|
||||||
void Run() {
|
GameLoop(const GameLoop&) = delete;
|
||||||
LOG_INFO("Running the game");
|
GameLoop(GameLoop&&) = delete;
|
||||||
while (!m_Game->IsExitRequested()) {
|
GameLoop& operator=(const GameLoop&) = delete;
|
||||||
m_Game->HandleActions(m_UserInput->GetActions());
|
GameLoop& operator=(GameLoop&&) = delete;
|
||||||
m_Game->UpdatePlayerVelocity();
|
|
||||||
|
void Run();
|
||||||
|
|
||||||
m_Window->ClearWindow();
|
void SetGame(std::unique_ptr<PathFindingDemo> x) {
|
||||||
|
|
||||||
// draw the map (terrain tiles)
|
|
||||||
const Map &map = m_Game->GetMap();
|
|
||||||
const auto &tiles = map.GetMapTiles();
|
|
||||||
for (size_t row = 0; row < tiles.size(); row++) {
|
|
||||||
for (size_t col = 0; col < tiles[row].size(); col++) {
|
|
||||||
// LOG_DEBUG("Drawing rect (", row, ", ", col, ")");
|
|
||||||
m_Window->DrawRect(
|
|
||||||
map.TileToWorld(TilePos{row, col}),
|
|
||||||
map.GetTileSize(), tiles[row][col]->R, tiles[row][col]->G,
|
|
||||||
tiles[row][col]->B, tiles[row][col]->A);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void SetGame(std::unique_ptr<PathFindingDemo> x) {
|
|
||||||
m_Game = std::move(x);
|
m_Game = std::move(x);
|
||||||
}
|
}
|
||||||
inline void SetWindow(std::unique_ptr<Window> x) { m_Window = std::move(x); }
|
void SetWindow(std::unique_ptr<Window> x) { m_Window = std::move(x); }
|
||||||
inline void SetUserInput(std::unique_ptr<UserInput> x) {
|
void SetUserInput(std::unique_ptr<UserInput> x) {
|
||||||
m_UserInput = std::move(x);
|
m_UserInput = std::move(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user