implemented TileMap, still need Shader for it with billboarding and all that jizz XD

This commit is contained in:
2026-02-02 20:52:28 +01:00
parent 2f3fdf83ae
commit ea2dc4f6b5
7 changed files with 130 additions and 81 deletions

View File

@@ -0,0 +1,54 @@
#include "TileMap.hpp"
TSE::TileMapChunk::TileMapChunk(int _chunksize, const Vector2 &_pos, SortingOrder _order)
{
chunksize = _chunksize;
pos = _pos;
order = _order;
}
void TSE::TileMapChunk::SetTile(const Vector2& p, const Vector2& Spriteindex, TileSet* set)
{
Sprite s;
set->GetSpriteAt(Spriteindex, s);
sprites[p] = s;
}
void TSE::TileMapChunk::RemoveTile(Vector2 p)
{
sprites.erase(p);
}
void TSE::TileMapChunk::SetOrdering(SortingOrder _order)
{
order = _order;
}
void TSE::TileMap::RemoveTile(Vector2 p)
{
Vector2 chunkInnerPos = LocalToChunkPos(p);
Vector2 chunkIndex = p - chunkInnerPos;
if(chunks.contains(chunkIndex))
chunks[chunkIndex].RemoveTile(chunkInnerPos);
}
void TSE::TileMap::SetTile(Vector2 p, Vector2 Spriteindex)
{
Vector2 chunkInnerPos = LocalToChunkPos(p);
Vector2 chunkIndex = p - chunkInnerPos;
if(!chunks.contains(chunkIndex))
{
chunks[chunkIndex] = TileMapChunk(chunkSize, chunkIndex, order);
}
chunks[chunkIndex].SetTile(chunkInnerPos, Spriteindex, set);
}
TSE::Vector2 TSE::TileMap::LocalToChunkPos(const Vector2 &v)
{
return Vector2((int)v.x % chunkSize, (int)v.y % chunkSize);
}
TSE::Vector2 TSE::TileMap::ChunkToLocalPos(const Vector2 &v, const TileMapChunk &chunk)
{
return v + chunk.pos * chunkSize;
}

View File

@@ -0,0 +1,56 @@
#pragma once
#define TILE_MAP typeid(TSE::TileMap).name()
#include <unordered_map>
#include "elements/BehaviourScript.hpp"
#include "Vector2.hpp"
#include "elements/Sprite.hpp"
#include "elements/TileSet.hpp"
namespace TSE
{
enum SortingOrder
{
TopRight,
TopLeft,
BottomRight,
BottomLeft,
};
struct TileMapChunk
{
private:
SortingOrder order;
int chunksize;
std::unordered_map<Vector2, Sprite> sprites;
public:
Vector2 pos;
TileMapChunk(int _chunksize, const Vector2& _pos, SortingOrder _order);
void SetTile(const Vector2& p, const Vector2& Spriteindex, TileSet* set);
void RemoveTile(Vector2 p);
void SetOrdering(SortingOrder _order);
};
class TileMap : public BehaviourScript
{
public:
int chunkSize = 16;
SortingOrder order = TopRight;
TileSet* set;
std::unordered_map<Vector2, TileMapChunk> chunks;
void RemoveTile(Vector2 p);
void SetTile(Vector2 p, Vector2 Spriteindex);
inline const char* GetName() override
{
return "Tile Map";
}
private:
Vector2 LocalToChunkPos(const Vector2& v);
Vector2 ChunkToLocalPos(const Vector2& v, const TileMapChunk& chunk);
};
} // namespace TSE

View File

@@ -199,84 +199,6 @@ TSE::Texture TSE::Texture::CutOut(const Vector2 &pos, Vector2 &size) const
return out; return out;
} }
TSE::Texture TSE::Texture::Upscale(const Vector2 &size) const
{
Texture result = Texture(0,0);
if(bmp == nullptr)
{
TSE_ERROR("Failed to upscale texture. Source bitmap was nullptr.");
Texture::makeError(result);
return result;
}
const int width = static_cast<int>(size.x);
const int height = static_cast<int>(size.y);
if(width <= 0 || height <= 0)
{
TSE_ERROR("Failed to upscale texture. Size must be greater than 0.");
Texture::makeError(result);
return result;
}
FIBITMAP* scaled = FreeImage_Rescale(bmp, width, height, FILTER_NEAREST);
if(scaled == nullptr)
{
TSE_ERROR("Failed to upscale texture. FreeImage_Rescale returned nullptr.");
Texture::makeError(result);
return result;
}
Texture out = Texture(width, height, Bpp);
if(out.bmp != nullptr)
FreeImage_Unload(out.bmp);
out.bmp = scaled;
out.imagePtr = FreeImage_GetBits(scaled);
out.Bpp = Bpp;
out.chanels = chanels;
out.Size = Vector2(width, height);
out.Apply();
return out;
}
TSE::Texture TSE::Texture::Downscale(const Vector2 &size) const
{
Texture result = Texture(0,0);
if(bmp == nullptr)
{
TSE_ERROR("Failed to downscale texture. Source bitmap was nullptr.");
Texture::makeError(result);
return result;
}
const int width = static_cast<int>(size.x);
const int height = static_cast<int>(size.y);
if(width <= 0 || height <= 0)
{
TSE_ERROR("Failed to downscale texture. Size must be greater than 0.");
Texture::makeError(result);
return result;
}
FIBITMAP* scaled = FreeImage_Rescale(bmp, width, height, FILTER_NEAREST);
if(scaled == nullptr)
{
TSE_ERROR("Failed to downscale texture. FreeImage_Rescale returned nullptr.");
Texture::makeError(result);
return result;
}
Texture out = Texture(width, height, Bpp);
if(out.bmp != nullptr)
FreeImage_Unload(out.bmp);
out.bmp = scaled;
out.imagePtr = FreeImage_GetBits(scaled);
out.Bpp = Bpp;
out.chanels = chanels;
out.Size = Vector2(width, height);
out.Apply();
return out;
}
void TSE::Texture::PasteIn(const Vector2 &pos, Texture &t) void TSE::Texture::PasteIn(const Vector2 &pos, Texture &t)
{ {
if(bmp == nullptr) if(bmp == nullptr)

View File

@@ -39,8 +39,6 @@ namespace TSE
void SetPixel(const Vector2& pos, const Color& c); void SetPixel(const Vector2& pos, const Color& c);
void GetPixel(const Vector2& pos, Color& c) const; void GetPixel(const Vector2& pos, Color& c) const;
Texture CutOut(const Vector2& pos, Vector2& size) const; Texture CutOut(const Vector2& pos, Vector2& size) const;
Texture Upscale(const Vector2& size) const;
Texture Downscale(const Vector2& size) const;
void PasteIn(const Vector2& pos, Texture& t); void PasteIn(const Vector2& pos, Texture& t);
void SetPixelNoApply(const Vector2& pos, const Color& c); void SetPixelNoApply(const Vector2& pos, const Color& c);
void ToSprite(Sprite& s); void ToSprite(Sprite& s);

View File

@@ -23,7 +23,7 @@ namespace TSE
{ {
SetCount(v.x, v.y); SetCount(v.x, v.y);
}; };
inline void GetSpriteAt(Vector2& v, Sprite& s) inline void GetSpriteAt(const Vector2& v, Sprite& s)
{ {
GetSpriteAt(v.x, v.y, s); GetSpriteAt(v.x, v.y, s);
}; };

View File

@@ -11,6 +11,9 @@ namespace TSE
/// @brief the epsilon used as min float value for comparisons in the engine /// @brief the epsilon used as min float value for comparisons in the engine
constexpr float TSE_EPSILON = 1e-6f; constexpr float TSE_EPSILON = 1e-6f;
/// @brief 32-bit golden ratio constant used for hash mixing
constexpr uint TSE_HASH_GOLDEN_RATIO_32 = 0x9e3779b9u;
/// @brief a simple degrees to radiant conversion function /// @brief a simple degrees to radiant conversion function
/// @param deg the degrees value /// @param deg the degrees value
/// @return the radiant value /// @return the radiant value

View File

@@ -1,6 +1,8 @@
#pragma once #pragma once
#include "Types.hpp" #include "Types.hpp"
#include "MathF.hpp"
#include <functional>
namespace TSE namespace TSE
{ {
@@ -135,3 +137,17 @@ namespace TSE
#pragma endregion methods #pragma endregion methods
}; };
} // namespace TSE } // namespace TSE
namespace std
{
template<>
struct hash<TSE::Vector2>
{
size_t operator()(const TSE::Vector2& v) const noexcept
{
size_t h1 = std::hash<float>{}(v.x);
size_t h2 = std::hash<float>{}(v.y);
return h1 ^ (h2 + TSE::TSE_HASH_GOLDEN_RATIO_32 + (h1 << 6) + (h1 >> 2));
}
};
}