diff --git a/cpp/Makefile b/cpp/Makefile index 03c6397..82782da 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -3,7 +3,7 @@ all: test pathfinding # TODO linter? pathfinding: - 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 + 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 src/map.cpp test: src/test.cpp src/array.hpp g++ -Wall -Wextra -Wpedantic -ggdb3 -std=c++23 -lgtest -o test src/test.cpp diff --git a/cpp/src/main.cpp b/cpp/src/main.cpp index 06da93a..6da7157 100644 --- a/cpp/src/main.cpp +++ b/cpp/src/main.cpp @@ -12,8 +12,7 @@ int main(int argc, char **argv) { * Initialize the input/output system */ - auto window = std::make_unique(640, 480); // the holy resolution - // auto window_init = window->Init(); + auto window = std::make_unique(640, 480); if (auto initialized = window->Init(); !initialized) { LOG_ERROR(initialized.error()); return error; @@ -37,4 +36,5 @@ int main(int argc, char **argv) { game_loop.SetUserInput(std::move(user_input)); game_loop.SetGame(std::move(demo)); game_loop.Run(); + return 0; } diff --git a/cpp/src/map.cpp b/cpp/src/map.cpp index e69de29..cd439df 100644 --- a/cpp/src/map.cpp +++ b/cpp/src/map.cpp @@ -0,0 +1,52 @@ +#include +#include + +#include "log.hpp" +#include "map.hpp" +#include "tile.hpp" + +Map::Map(int rows, int cols) : m_Cols(cols), m_Rows(rows) { + bool sw = true; + LOG_DEBUG("cols = ", cols, " rows = ", rows); + m_Tiles = std::vector>{}; + for (size_t row = 0; row < m_Rows; row++) { + m_Tiles.push_back(std::vector{}); + for (size_t col = 0; col < m_Cols; col++) { + if (sw) + m_Tiles[row].push_back(&tile_types.at("Grass")); + else + m_Tiles[row].push_back(&tile_types.at("Road")); + sw = !sw; + } + sw = !sw; + } +} + +WorldPos Map::TileToWorld(TilePos p) const { + return WorldPos{p.x * TILE_SIZE, p.y * TILE_SIZE}; +} + +TilePos Map::WorldToTile(WorldPos p) const { + return TilePos{p.x / TILE_SIZE, p.y / TILE_SIZE}; +} + +WorldPos Map::GetTileSize() const { return WorldPos{TILE_SIZE, TILE_SIZE}; } + +const Tile *Map::GetTileAt(TilePos p) const { + assert(IsTilePosValid(p)); + size_t row = p.x; + size_t col = p.y; + + return m_Tiles[row][col]; +} + +const Tile *Map::GetTileAt(WorldPos p) const { + return GetTileAt(WorldToTile(p)); +} + +bool Map::IsTilePosValid(TilePos p) const { + size_t row = p.x; + size_t col = p.y; + + return row < m_Tiles.size() && col < m_Tiles[0].size(); +} diff --git a/cpp/src/map.hpp b/cpp/src/map.hpp index 478b26f..b63b17e 100644 --- a/cpp/src/map.hpp +++ b/cpp/src/map.hpp @@ -1,80 +1,36 @@ #pragma once #include -#include #include "math.hpp" #include "tile.hpp" +using TileGrid = std::vector>; + class Map { - - // TODO using = ... for tile vector - public: static constexpr float TILE_SIZE = 100.0f; // tile size in world - Map(int rows, int cols) : m_Cols(cols), m_Rows(rows) { - bool sw = true; - LOG_DEBUG("cols = ", cols, " rows = ", rows); - m_Tiles = std::vector>{}; - for (size_t row = 0; row < m_Rows; row++) { - m_Tiles.push_back(std::vector{}); - for (size_t col = 0; col < m_Cols; col++) { - if (sw) - m_Tiles[row].push_back(&tile_types.at("Grass")); - else - m_Tiles[row].push_back(&tile_types.at("Road")); - sw = !sw; - } - sw = !sw; - } - } + Map(int rows, int cols); Map() : Map(0, 0) {} - const std::vector> &GetMapTiles() const { - return m_Tiles; - } + const TileGrid &GetMapTiles() const { return m_Tiles; } - WorldPos TileToWorld(TilePos p) const { - return WorldPos{p.x * TILE_SIZE, p.y * TILE_SIZE}; - } + WorldPos TileToWorld(TilePos p) const; + TilePos WorldToTile(WorldPos p) const; - TilePos WorldToTile(WorldPos p) const { - return TilePos{p.x / TILE_SIZE, p.y / TILE_SIZE}; - } + WorldPos GetTileSize() const; + const Tile *GetTileAt(TilePos p) const; + const Tile *GetTileAt(WorldPos p) const; - Vec2D GetTileSize() const { - return Vec2D{TILE_SIZE, TILE_SIZE}; - } + bool IsTilePosValid(TilePos p) const; - const Tile* GetTileAt(TilePos p) const { - assert(IsTilePosValid(p)); - size_t row = p.x; - size_t col = p.y; - - return m_Tiles[row][col]; - - } - - const Tile* GetTileAt(WorldPos p) const { - return GetTileAt(WorldToTile(p)); - } - - bool IsTilePosValid(TilePos p) const { - size_t row = p.x; - size_t col = p.y; - - return row < m_Tiles.size() && col < m_Tiles[0].size(); - } - - template - double GetTileVelocityCoeff(T p) const { + template double GetTileVelocityCoeff(T p) const { return 1.0 / GetTileAt(p)->cost; } private: - // std::vector> m_Tiles; - std::vector> m_Tiles; + TileGrid m_Tiles; size_t m_Cols = 0; size_t m_Rows = 0; };