5 #include <unordered_map>
18 using std::unordered_map;
26 SimpleTextureAtlas::SimpleTextureAtlas(
const string&
id): atlasTextureId(id) {
31 atlasTextureEntity->releaseReference();
55 if (textureIdx > 255) {
111 auto atlasTextureByteBuffer =
ByteBuffer(atlasTextureWidth * atlasTextureHeight * 4);
112 array<Texture*, 256> atlasTextureIdxToTextureMappingVector {
nullptr };
113 array<ByteBuffer, 256> atlasTextureIdxToTextureTextureDataVector;
115 atlasTextureIdxToTextureMappingVector[atlasTextureIdx] = atlasTextureEntity;
116 atlasTextureIdxToTextureTextureDataVector[atlasTextureIdx] = atlasTextureEntity->getRGBTextureData();
118 vector<ByteBuffer> atlasTextureByteBufferMapping;
119 for (
auto y = 0; y < atlasTextureHeight; y++)
120 for (
auto x = 0; x < atlasTextureWidth; x++) {
125 auto textureXFloat =
static_cast<float>(textureX) /
static_cast<float>(
ATLAS_TEXTURE_SIZE);
126 auto textureYFloat =
static_cast<float>(textureY) /
static_cast<float>(
ATLAS_TEXTURE_SIZE);
127 auto atlasTextureIdx = atlasTextureIdxY * textureAtlasSize + atlasTextureIdxX;
128 auto texture = atlasTextureIdxToTextureMappingVector[atlasTextureIdx];
129 if (texture !=
nullptr) {
130 auto textureTextureData = &atlasTextureIdxToTextureTextureDataVector[atlasTextureIdx];
131 auto textureWidth = texture->getTextureWidth();
132 auto textureHeight = texture->getTextureHeight();
133 auto textureBytesPerPixel = texture->getRGBDepthBitsPerPixel() / 8;
134 auto textureXInt =
static_cast<int>(textureXFloat *
static_cast<float>(textureWidth));
135 auto textureYInt =
static_cast<int>(textureYFloat *
static_cast<float>(textureHeight));
138 textureXInt =
static_cast<int>((
static_cast<float>(textureXInt) -
static_cast<float>(
ATLAS_TEXTURE_BORDER)) * (
static_cast<float>(textureWidth) +
static_cast<float>(
ATLAS_TEXTURE_BORDER) * 2.0f) /
static_cast<float>(textureWidth));
141 textureYInt =
static_cast<int>((
static_cast<float>(textureYInt) -
static_cast<float>(
ATLAS_TEXTURE_BORDER)) * (
static_cast<float>(textureHeight) +
static_cast<float>(
ATLAS_TEXTURE_BORDER) * 2.0f) /
static_cast<float>(textureHeight));
142 auto texturePixelOffset =
143 textureYInt * textureWidth * textureBytesPerPixel +
144 textureXInt * textureBytesPerPixel;
145 auto r = textureTextureData->get(texturePixelOffset + 0);
146 auto g = textureTextureData->get(texturePixelOffset + 1);
147 auto b = textureTextureData->get(texturePixelOffset + 2);
148 auto a = textureBytesPerPixel == 4?textureTextureData->get(texturePixelOffset + 3):0xff;
149 atlasTextureByteBuffer.put(r);
150 atlasTextureByteBuffer.put(g);
151 atlasTextureByteBuffer.put(b);
152 atlasTextureByteBuffer.put(a);
158 atlasTextureByteBuffer.put(r);
159 atlasTextureByteBuffer.put(g);
160 atlasTextureByteBuffer.put(b);
161 atlasTextureByteBuffer.put(a);
166 Texture::TEXTUREDEPTH_RGBA,
167 Texture::TEXTUREFORMAT_RGBA,
168 atlasTextureWidth, atlasTextureHeight,
169 atlasTextureWidth, atlasTextureHeight,
170 Texture::TEXTUREFORMAT_RGBA,
171 atlasTextureByteBuffer
const string & getId() const
void setUseMipMap(bool useMipMap)
Set if to use mip map.
void setAtlasSize(uint16_t atlasSize)
Set atlas size.
static void println()
Print new line to console.
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
virtual void acquireReference()
Acquires a reference, incrementing the counter.
void removeTexture(Texture *texture)
Remove texture.
unordered_map< Texture *, int > textureReferenceCounter
vector< int > freeTextureIds
~SimpleTextureAtlas()
Public destructor.
unordered_map< Texture *, int > textureToAtlasTextureIdxMapping
int addTexture(Texture *texture)
Add texture.
static constexpr int ATLAS_TEXTURE_SIZE
int getTextureIdx(Texture *texture)
Returns specific atlas texture index within atlas.
static constexpr int TEXTURE_IDX_NONE
void update()
Update texture atlas.
static constexpr int ATLAS_TEXTURE_BORDER
unordered_map< int, Texture * > atlasTextureIdxToTextureMapping