mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 15:53:42 +00:00
Compare commits
2 Commits
ba8dca8abc
...
d7f11e7ebd
| Author | SHA1 | Date | |
|---|---|---|---|
| d7f11e7ebd | |||
| 476d24e930 |
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <tmxlite/Types.hpp>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
@ -10,8 +11,6 @@ public:
|
||||
Map() = default;
|
||||
~Map() = default;
|
||||
|
||||
// code comment below is a test for doxygen - do not remove
|
||||
|
||||
/*!
|
||||
*
|
||||
* \brief
|
||||
@ -21,8 +20,17 @@ public:
|
||||
* \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 */);
|
||||
[[deprecated]]
|
||||
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);
|
||||
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 <cctype>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#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
|
||||
{
|
||||
printf("X: %d, Y: %d", x, y);
|
||||
|
||||
auto& tile(game->manager.addEntity());
|
||||
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 tileCountY = texY / mapTileSize.y;
|
||||
|
||||
for (auto y = 0u; y < mapSize.y; ++y) {
|
||||
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())) {
|
||||
for (auto idx = 0ul; idx < mapSize.x * mapSize.y; idx++) {
|
||||
|
||||
if (!(idx < tiles.size() && tiles[idx].ID >= tileSets.at(i).getFirstGID()
|
||||
&& 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());
|
||||
|
||||
@ -147,21 +150,21 @@ void Map::loadMapTmx(const char* path)
|
||||
const float tilePosX = static_cast<float>(x) * mapTileSize.x;
|
||||
const float tilePosY = (static_cast<float>(y) * mapTileSize.y);
|
||||
|
||||
auto& tile(VEGO_Game().manager.addEntity());
|
||||
|
||||
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?
|
||||
|
||||
Map::addTile(tilePosX, tilePosY, mapTileSize, u, v, zIndex, texturePaths.at(i).c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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?
|
||||
}
|
||||
@ -64,6 +64,13 @@ void SpriteComponent::init()
|
||||
|
||||
void SpriteComponent::update()
|
||||
{
|
||||
// This code is not compatible for animated tiles
|
||||
if (animated) {
|
||||
srcRect.x = srcRect.w * static_cast<int>((SDL_GetTicks() / speed) % frames);
|
||||
|
||||
srcRect.y = animationIndex * transform->height;
|
||||
}
|
||||
|
||||
this->destRect.x = this->transform->position.x;
|
||||
this->destRect.y = this->transform->position.y;
|
||||
this->destRect.w = transform->width * transform->scale;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user