Graphics: Add TileMap class
Former-commit-id: ecc9f3f1e786da4017ef24322a2f2510eab33a6c [formerly 2d18b57f6ef6c8480f83226082cfcceff3779093] Former-commit-id: 531e4724efe7fa76f64c1e252665be31d1754e7b
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include <Nazara/Graphics/SkinningManager.hpp>
|
||||
#include <Nazara/Graphics/SkyboxBackground.hpp>
|
||||
#include <Nazara/Graphics/Sprite.hpp>
|
||||
#include <Nazara/Graphics/TileMap.hpp>
|
||||
#include <Nazara/Graphics/Formats/MeshLoader.hpp>
|
||||
#include <Nazara/Graphics/Formats/TextureLoader.hpp>
|
||||
#include <Nazara/Renderer/Renderer.hpp>
|
||||
@@ -110,6 +111,12 @@ namespace Nz
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TileMap::Initialize())
|
||||
{
|
||||
NazaraError("Failed to initialize tilemaps");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Generic loaders
|
||||
Loaders::RegisterMesh();
|
||||
Loaders::RegisterTexture();
|
||||
@@ -217,6 +224,7 @@ namespace Nz
|
||||
Material::Uninitialize();
|
||||
SkyboxBackground::Uninitialize();
|
||||
Sprite::Uninitialize();
|
||||
TileMap::Uninitialize();
|
||||
|
||||
NazaraNotice("Uninitialized: Graphics module");
|
||||
|
||||
|
||||
110
src/Nazara/Graphics/TileMap.cpp
Normal file
110
src/Nazara/Graphics/TileMap.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Graphics/TileMap.hpp>
|
||||
#include <Nazara/Graphics/AbstractRenderQueue.hpp>
|
||||
#include <Nazara/Graphics/AbstractViewer.hpp>
|
||||
#include <Nazara/Math/Rect.hpp>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
/*!
|
||||
* \ingroup graphics
|
||||
* \class Nz::TileMap
|
||||
* \brief Graphics class that represent several tiles of the same size assembled into a grid
|
||||
* This class is far more efficient than using a sprite for every tile
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Adds the TileMap to the rendering queue
|
||||
*
|
||||
* \param renderQueue Queue to be added
|
||||
* \param instanceData Data for the instance
|
||||
*/
|
||||
void TileMap::AddToRenderQueue(AbstractRenderQueue* renderQueue, const InstanceData& instanceData) const
|
||||
{
|
||||
const VertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<const VertexStruct_XYZ_Color_UV*>(instanceData.data.data());
|
||||
|
||||
std::size_t spriteCount = 0;
|
||||
for (const Layer& layer : m_layers)
|
||||
{
|
||||
if (layer.material)
|
||||
renderQueue->AddSprites(instanceData.renderOrder, layer.material, &vertices[spriteCount], layer.tiles.size());
|
||||
|
||||
spriteCount += layer.tiles.size();
|
||||
}
|
||||
}
|
||||
|
||||
void TileMap::MakeBoundingVolume() const
|
||||
{
|
||||
Nz::Vector2f size = GetSize();
|
||||
m_boundingVolume.Set(Vector3f(0.f), size.x*Vector3f::Right() + size.y*Vector3f::Down());
|
||||
}
|
||||
|
||||
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[spriteCount].color, sizeof(VertexStruct_XYZ_Color_UV));
|
||||
SparsePtr<Vector3f> posPtr(&vertices[spriteCount].position, sizeof(VertexStruct_XYZ_Color_UV));
|
||||
SparsePtr<Vector2f> texCoordPtr(&vertices[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(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();
|
||||
}
|
||||
}
|
||||
|
||||
bool TileMap::Initialize()
|
||||
{
|
||||
if (!TileMapLibrary::Initialize())
|
||||
{
|
||||
NazaraError("Failed to initialise library");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TileMap::Uninitialize()
|
||||
{
|
||||
TileMapLibrary::Uninitialize();
|
||||
}
|
||||
|
||||
TileMapLibrary::LibraryMap TileMap::s_library;
|
||||
}
|
||||
Reference in New Issue
Block a user