void TileMap::UpdateData(InstanceData* instanceData) const { std::size_t spriteCount = 0; for (const Layer& layer : m_layers) spriteCount += layer.tiles.size(); instanceData->data.resize(4 * spriteCount * sizeof(VertexStruct_XYZ_Color_UV)); VertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<VertexStruct_XYZ_Color_UV*>(instanceData->data.data()); spriteCount = 0; for (const Layer& layer : m_layers) { SparsePtr<Color> colorPtr(&vertices[4 * spriteCount].color, sizeof(VertexStruct_XYZ_Color_UV)); SparsePtr<Vector3f> posPtr(&vertices[4 * spriteCount].position, sizeof(VertexStruct_XYZ_Color_UV)); SparsePtr<Vector2f> texCoordPtr(&vertices[4 * spriteCount].uv, sizeof(VertexStruct_XYZ_Color_UV)); for (std::size_t tileIndex : layer.tiles) { const Tile& tile = m_tiles[tileIndex]; NazaraAssert(tile.enabled, "Tile specified for rendering is not enabled"); std::size_t x = tileIndex % m_mapSize.x; std::size_t y = tileIndex / m_mapSize.x; Vector3f tileLeftCorner; if (m_isometricModeEnabled) tileLeftCorner.Set(x * m_tileSize.x + m_tileSize.x/2.f * (y % 2), y/2.f * -m_tileSize.y, 0.f); else tileLeftCorner.Set(x * m_tileSize.x, y * -m_tileSize.y, 0.f); *colorPtr++ = tile.color; *posPtr++ = instanceData->transformMatrix.Transform(tileLeftCorner); *texCoordPtr++ = tile.textureCoords.GetCorner(RectCorner_LeftTop); *colorPtr++ = tile.color; *posPtr++ = instanceData->transformMatrix.Transform(tileLeftCorner + m_tileSize.x * Vector3f::Right()); *texCoordPtr++ = tile.textureCoords.GetCorner(RectCorner_RightTop); *colorPtr++ = tile.color; *posPtr++ = instanceData->transformMatrix.Transform(tileLeftCorner + m_tileSize.y * Vector3f::Down()); *texCoordPtr++ = tile.textureCoords.GetCorner(RectCorner_LeftBottom); *colorPtr++ = tile.color; *posPtr++ = instanceData->transformMatrix.Transform(tileLeftCorner + m_tileSize.x * Vector3f::Right() + m_tileSize.y * Vector3f::Down()); *texCoordPtr++ = tile.textureCoords.GetCorner(RectCorner_RightBottom); } spriteCount += layer.tiles.size(); } }
void TextSprite::OnAtlasLayerChange(const AbstractAtlas* atlas, AbstractImage* oldLayer, AbstractImage* newLayer) { NazaraUnused(atlas); #ifdef NAZARA_DEBUG if (m_atlases.find(atlas) == m_atlases.end()) { NazaraInternalError("Not listening to " + String::Pointer(atlas)); return; } #endif // The texture of an atlas have just been recreated (size change) // we have to adjust the coordinates of the texture and the rendering texture Texture* oldTexture = static_cast<Texture*>(oldLayer); Texture* newTexture = static_cast<Texture*>(newLayer); // It is possible that we don't use the texture (the atlas warning us for each of its layers) auto it = m_renderInfos.find(oldTexture); if (it != m_renderInfos.end()) { // We indeed use this texture, we have to update its coordinates RenderIndices indices = std::move(it->second); Vector2ui oldSize(oldTexture->GetSize()); Vector2ui newSize(newTexture->GetSize()); Vector2f scale = Vector2f(oldSize) / Vector2f(newSize); // ratio of the old one to the new one // Now we will iterate through each coordinates of the concerned texture to multiply them by the ratio SparsePtr<Vector2f> texCoordPtr(&m_localVertices[indices.first].uv, sizeof(VertexStruct_XYZ_Color_UV)); for (unsigned int i = 0; i < indices.count; ++i) { for (unsigned int j = 0; j < 4; ++j) m_localVertices[i*4 + j].uv *= scale; } // We get rid off the old texture and we set the new one at the place (same for indices) m_renderInfos.erase(it); m_renderInfos.insert(std::make_pair(newTexture, std::move(indices))); } }
void TextSprite::UpdateData(InstanceData* instanceData) const { instanceData->data.resize(m_localVertices.size() * sizeof(VertexStruct_XYZ_Color_UV)); VertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<VertexStruct_XYZ_Color_UV*>(instanceData->data.data()); SparsePtr<Color> colorPtr(&vertices[0].color, sizeof(VertexStruct_XYZ_Color_UV)); SparsePtr<Vector3f> posPtr(&vertices[0].position, sizeof(VertexStruct_XYZ_Color_UV)); SparsePtr<Vector2f> texCoordPtr(&vertices[0].uv, sizeof(VertexStruct_XYZ_Color_UV)); // We will not initialize the final vertices (those send to the RenderQueue) // With the help of the coordinates axis, the matrix and our color attribute for (auto& pair : m_renderInfos) { RenderIndices& indices = pair.second; if (indices.count == 0) continue; //< Ignore empty render indices SparsePtr<Color> color = colorPtr + indices.first * 4; SparsePtr<Vector3f> pos = posPtr + indices.first * 4; SparsePtr<Vector2f> uv = texCoordPtr + indices.first * 4; VertexStruct_XY_Color_UV* localVertex = &m_localVertices[indices.first * 4]; for (unsigned int i = 0; i < indices.count; ++i) { for (unsigned int j = 0; j < 4; ++j) { Vector3f localPos = localVertex->position.x*Vector3f::Right() + localVertex->position.y*Vector3f::Down(); localPos *= m_scale; *pos++ = instanceData->transformMatrix->Transform(localPos); *color++ = m_color * localVertex->color; *uv++ = localVertex->uv; localVertex++; } } } }