0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

Code clean-up

This commit is contained in:
Benedikt Galbavy 2024-11-05 15:59:25 +01:00
parent ba8dca8abc
commit 476d24e930
2 changed files with 72 additions and 61 deletions

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <tmxlite/Types.hpp>
#include <map> #include <map>
#include <string> #include <string>
@ -10,8 +11,6 @@ public:
Map() = default; Map() = default;
~Map() = default; ~Map() = default;
// code comment below is a test for doxygen - do not remove
/*! /*!
* *
* \brief * \brief
@ -21,8 +20,17 @@ public:
* \return Boolean for success * \return Boolean for success
* *
*/ */
[[deprecated("ID based text files are not supported anymore, use .txm maps instead")]]
static void loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict /* backreference */); static void loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict /* backreference */);
[[deprecated]]
static void addTile(unsigned long id, int x, int y, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict); static void addTile(unsigned long id, int x, int y, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict);
/*!
* \brief Loads a .tmx map
* \param path Path to the `.tmx` map file
*
*/
static void loadMapTmx(const char* path); static void loadMapTmx(const char* path);
private:
static void addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, const char* texturePath);
}; };

View File

@ -2,6 +2,7 @@
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <cstdio>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <type_traits> #include <type_traits>
@ -72,8 +73,6 @@ void Map::loadMap(const char* path, int sizeX, int sizeY, GameInternal* game, co
void Map::addTile(unsigned long id, int x, int y, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict) // tile entity void Map::addTile(unsigned long id, int x, int y, GameInternal* game, const std::map<int, std::pair<std::string, bool>>* textureDict) // tile entity
{ {
printf("X: %d, Y: %d", x, y);
auto& tile(game->manager.addEntity()); auto& tile(game->manager.addEntity());
tile.addComponent<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id, textureDict); tile.addComponent<TileComponent>(x, y, TILE_SIZE, TILE_SIZE, id, textureDict);
@ -83,85 +82,89 @@ void Map::addTile(unsigned long id, int x, int y, GameInternal* game, const std:
void Map::loadMapTmx(const char* path) void Map::loadMapTmx(const char* path)
{ {
tmx::Map map; tmx::Map map;
if (!map.load(path)) { if (!map.load(path)) {
// TODO: log to console // TODO: log to console
} }
const std::vector<tmx::Tileset>& tileSets = map.getTilesets(); const std::vector<tmx::Tileset>& tileSets = map.getTilesets();
const std::vector<tmx::Layer::Ptr>& mapLayers = map.getLayers(); const std::vector<tmx::Layer::Ptr>& mapLayers = map.getLayers();
const auto mapSize = map.getTileCount(); const auto mapSize = map.getTileCount();
const auto mapTileSize = map.getTileSize(); const auto mapTileSize = map.getTileSize();
std::vector<std::string> texturePaths = {}; std::vector<std::string> texturePaths = {};
for (auto tileSet : tileSets) { for (auto tileSet : tileSets) {
texturePaths.emplace_back(tileSet.getImagePath()); texturePaths.emplace_back(tileSet.getImagePath());
} }
for (auto& layer : mapLayers) { for (auto& layer : mapLayers) {
if (layer->getType() == tmx::Layer::Type::Tile) { if (layer->getType() == tmx::Layer::Type::Tile) {
auto& tileLayer = layer->getLayerAs<tmx::TileLayer>(); auto& tileLayer = layer->getLayerAs<tmx::TileLayer>();
int zIndex = 0; int zIndex = 0;
const std::vector<tmx::Property>& properties = layer->getProperties(); const std::vector<tmx::Property>& properties = layer->getProperties();
auto zIndexIterator = std::find_if(properties.begin(), properties.end(), [](const tmx::Property& property) { auto zIndexIterator = std::find_if(properties.begin(), properties.end(), [](const tmx::Property& property) {
return property.getName() == "zIndex"; return property.getName() == "zIndex";
}); });
if (zIndexIterator != properties.end() && std::is_same<decltype(zIndexIterator->getType()), int>::value) { if (zIndexIterator != properties.end() && std::is_same<decltype(zIndexIterator->getType()), int>::value) {
zIndex = zIndexIterator->getIntValue(); zIndex = zIndexIterator->getIntValue();
} }
const auto& tiles = tileLayer.getTiles(); const auto& tiles = tileLayer.getTiles();
for (auto i = 0u; i < tileSets.size(); i++) { for (auto i = 0u; i < tileSets.size(); i++) {
auto tilesetTexture = VEGO_Game().textureManager->loadTexture(texturePaths.at(i).c_str()); auto tilesetTexture = VEGO_Game().textureManager->loadTexture(texturePaths.at(i).c_str());
int texX, texY; int texX, texY;
SDL_QueryTexture(tilesetTexture, nullptr, nullptr, &texX, &texY); SDL_QueryTexture(tilesetTexture, nullptr, nullptr, &texX, &texY);
const auto tileCountX = texX / mapTileSize.x; const auto tileCountX = texX / mapTileSize.x;
const auto tileCountY = texY / mapTileSize.y; const auto tileCountY = texY / mapTileSize.y;
for (auto y = 0u; y < mapSize.y; ++y) { for (auto idx = 0ul; idx < mapSize.x * mapSize.y; idx++) {
for (auto x = 0u; x < mapSize.x; ++x) {
const auto idx = y * mapSize.x + x;
if (idx < tiles.size() && tiles[idx].ID >= tileSets.at(i).getFirstGID()
&& tiles[idx].ID < (tileSets.at(i).getFirstGID() + tileSets.at(i).getTileCount())) {
auto idIndex = (tiles[idx].ID - tileSets.at(i).getFirstGID()); if (!(idx < tiles.size() && tiles[idx].ID >= tileSets.at(i).getFirstGID()
&& tiles[idx].ID < (tileSets.at(i).getFirstGID() + tileSets.at(i).getTileCount()))) {
continue;
}
int u = idIndex % tileCountX; const auto x = idx % mapSize.x;
int v = idIndex / tileCountY; const auto y = idx / mapSize.x;
u *= mapTileSize.x; //TODO we should be using the tile set size, as this may be different from the map's grid size
v *= mapTileSize.y;
//normalise the UV auto idIndex = (tiles[idx].ID - tileSets.at(i).getFirstGID());
u /= texX;
v /= texY;
//vert pos int u = idIndex % tileCountX;
const float tilePosX = static_cast<float>(x) * mapTileSize.x; int v = idIndex / tileCountY;
const float tilePosY = (static_cast<float>(y) * mapTileSize.y); u *= mapTileSize.x; //TODO we should be using the tile set size, as this may be different from the map's grid size
v *= mapTileSize.y;
auto& tile(VEGO_Game().manager.addEntity()); //normalise the UV
u /= texX;
v /= texY;
tile.addComponent<TransformComponent>(tilePosX, tilePosY, mapTileSize.x, mapTileSize.y, 1); //vert pos
tile.addComponent<SpriteComponent>(texturePaths.at(i).c_str(), v, u, zIndex); // why does uv need to be reversed? const float tilePosX = static_cast<float>(x) * mapTileSize.x;
const float tilePosY = (static_cast<float>(y) * mapTileSize.y);
} Map::addTile(tilePosX, tilePosY, mapTileSize, u, v, zIndex, texturePaths.at(i).c_str());
} }
} }
} if (layer->getType() == tmx::Layer::Type::Object) {
// spawn objects
continue; continue;
} }
if (layer->getType() == tmx::Layer::Type::Object) { }
// spawn objects }
continue; }
}
} void Map::addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, const char* texturePath)
{
auto& tile(VEGO_Game().manager.addEntity());
tile.addComponent<TransformComponent>(x, y, mapTileSize.x, mapTileSize.y, 1);
tile.addComponent<SpriteComponent>(texturePath, v, u, zIndex); // why does uv need to be reversed?
} }