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

Fixed templates and const char* for texture

This commit is contained in:
Benedikt Galbavy 2024-11-17 13:07:33 +01:00
parent 70260c01bb
commit 92dfbacd9b
2 changed files with 30 additions and 28 deletions

View File

@ -33,7 +33,7 @@ private:
}; };
struct TileSetData { struct TileSetData {
const char* texturePath{}; std::string texturePath{};
tmx::Vector2i textureSize; tmx::Vector2i textureSize;
uint32_t tileCount{}; uint32_t tileCount{};
tmx::Vector2u tileCount2D; tmx::Vector2u tileCount2D;
@ -45,32 +45,8 @@ private:
std::vector<std::function<void()>> tileConstructors; std::vector<std::function<void()>> tileConstructors;
void loadTileLayer(const tmx::TileLayer& layer); void loadTileLayer(const tmx::TileLayer& layer);
static void addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, const char* texturePath, bool hasCollision); static void addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, std::string texturePath, bool hasCollision);
template<typename T> template<typename T>
static std::optional<T> getLayerProperty(const std::vector<tmx::Property>& properties, std::string propertyName) { return std::nullopt; }; static std::optional<T> getLayerProperty(const std::vector<tmx::Property>& properties, std::string propertyName) { return std::nullopt; };
template<> std::optional<bool> getLayerProperty(const std::vector<tmx::Property>& properties, std::string propertyName) {
auto zIndexIterator = std::ranges::find_if(properties, [propertyName](const tmx::Property& property) {
return property.getName().compare(propertyName) == 0;
});
if (zIndexIterator != properties.end() && zIndexIterator->getType() == tmx::Property::Type::Boolean) {
return zIndexIterator->getBoolValue();
}
return std::nullopt;
}
template<> std::optional<int> getLayerProperty(const std::vector<tmx::Property>& properties, std::string propertyName)
{
auto zIndexIterator = std::ranges::find_if(properties, [propertyName](const tmx::Property& property) {
return property.getName().compare(propertyName) == 0;
});
if (zIndexIterator != properties.end() && zIndexIterator->getType() == tmx::Property::Type::Int) {
return zIndexIterator->getIntValue();
}
return std::nullopt;
}
}; };

View File

@ -26,6 +26,32 @@
#include "TileComponent.h" #include "TileComponent.h"
#include "VEGO.h" #include "VEGO.h"
template<> std::optional<bool> Map::getLayerProperty(const std::vector<tmx::Property>& properties, std::string propertyName) {
auto zIndexIterator = std::ranges::find_if(properties, [propertyName](const tmx::Property& property) {
return property.getName().compare(propertyName) == 0;
});
if (zIndexIterator != properties.end() && zIndexIterator->getType() == tmx::Property::Type::Boolean) {
return zIndexIterator->getBoolValue();
}
return std::nullopt;
}
template<> std::optional<int> Map::getLayerProperty(const std::vector<tmx::Property>& properties, std::string propertyName)
{
auto zIndexIterator = std::ranges::find_if(properties, [propertyName](const tmx::Property& property) {
return property.getName().compare(propertyName) == 0;
});
if (zIndexIterator != properties.end() && zIndexIterator->getType() == tmx::Property::Type::Int) {
return zIndexIterator->getIntValue();
}
return std::nullopt;
}
Map::Map(const char* path) Map::Map(const char* path)
{ {
if (!this->map.load(path)) { if (!this->map.load(path)) {
@ -138,12 +164,12 @@ void Map::loadTileLayer(const tmx::TileLayer& layer)
this->tileConstructors.insert(this->tileConstructors.end(), tileConstructorRange.begin(), tileConstructorRange.end()); this->tileConstructors.insert(this->tileConstructors.end(), tileConstructorRange.begin(), tileConstructorRange.end());
} }
void Map::addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, const char* texturePath, bool hasCollision) void Map::addTile(float x, float y, const tmx::Vector2u& mapTileSize, int u, int v, int zIndex, std::string texturePath, bool hasCollision)
{ {
auto& tile(VEGO_Game().manager.addEntity()); auto& tile(VEGO_Game().manager.addEntity());
tile.addComponent<TransformComponent>(x, y, mapTileSize.x, mapTileSize.y, 1); tile.addComponent<TransformComponent>(x, y, mapTileSize.x, mapTileSize.y, 1);
tile.addComponent<SpriteComponent>("assets/grassy-river-tiles.png" /*texturePath*/, v, u, zIndex); // why does uv need to be reversed? tile.addComponent<SpriteComponent>(texturePath.c_str(), v, u, zIndex); // why does uv need to be reversed?
if (hasCollision) { if (hasCollision) {
// tag currently does not have a clear purposes, TODO: figure out appropriate tag name // tag currently does not have a clear purposes, TODO: figure out appropriate tag name