mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 22:23:43 +00:00
Code clean-up
This commit is contained in:
parent
ba8dca8abc
commit
476d24e930
@ -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);
|
||||||
};
|
};
|
||||||
|
|||||||
37
src/Map.cpp
37
src/Map.cpp
@ -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);
|
||||||
|
|
||||||
@ -126,11 +125,15 @@ void Map::loadMapTmx(const char* path)
|
|||||||
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()
|
||||||
if (idx < tiles.size() && tiles[idx].ID >= tileSets.at(i).getFirstGID()
|
&& tiles[idx].ID < (tileSets.at(i).getFirstGID() + tileSets.at(i).getTileCount()))) {
|
||||||
&& tiles[idx].ID < (tileSets.at(i).getFirstGID() + tileSets.at(i).getTileCount())) {
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto x = idx % mapSize.x;
|
||||||
|
const auto y = idx / mapSize.x;
|
||||||
|
|
||||||
auto idIndex = (tiles[idx].ID - tileSets.at(i).getFirstGID());
|
auto idIndex = (tiles[idx].ID - tileSets.at(i).getFirstGID());
|
||||||
|
|
||||||
@ -147,21 +150,21 @@ void Map::loadMapTmx(const char* path)
|
|||||||
const float tilePosX = static_cast<float>(x) * mapTileSize.x;
|
const float tilePosX = static_cast<float>(x) * mapTileSize.x;
|
||||||
const float tilePosY = (static_cast<float>(y) * mapTileSize.y);
|
const float tilePosY = (static_cast<float>(y) * mapTileSize.y);
|
||||||
|
|
||||||
auto& tile(VEGO_Game().manager.addEntity());
|
Map::addTile(tilePosX, tilePosY, mapTileSize, u, v, zIndex, texturePaths.at(i).c_str());
|
||||||
|
|
||||||
tile.addComponent<TransformComponent>(tilePosX, tilePosY, mapTileSize.x, mapTileSize.y, 1);
|
|
||||||
tile.addComponent<SpriteComponent>(texturePaths.at(i).c_str(), v, u, zIndex); // why does uv need to be reversed?
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (layer->getType() == tmx::Layer::Type::Object) {
|
if (layer->getType() == tmx::Layer::Type::Object) {
|
||||||
// spawn objects
|
// spawn objects
|
||||||
continue;
|
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?
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user