Skip to content

Commit 4697d8c

Browse files
committed
Fix GitHub Issue #86: Deliver MCSurface as a shared_ptr
- Also modernizes renderers
1 parent 61f7c1e commit 4697d8c

File tree

81 files changed

+933
-927
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+933
-927
lines changed

src/game/MiniCore/src/Asset/mcmeshconfigloader.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bool MCMeshConfigLoader::load(const std::string & filePath)
5151
auto && node = root.firstChild();
5252
while (!node.isNull() && node.nodeName() == "mesh")
5353
{
54-
MeshDataPtr newData(new MCMeshMetaData);
54+
const auto newData = std::make_shared<MCMeshMetaData>();
5555
const auto && element = node.toElement();
5656
if (!element.isNull())
5757
{
@@ -68,7 +68,7 @@ bool MCMeshConfigLoader::load(const std::string & filePath)
6868
return true;
6969
}
7070

71-
void MCMeshConfigLoader::parseAttributes(const QDomElement & element, MeshDataPtr newData, const std::string & baseModelPath)
71+
void MCMeshConfigLoader::parseAttributes(const QDomElement & element, MCMeshDataPtr newData, const std::string & baseModelPath)
7272
{
7373
const std::string model = element.attribute("model", "").toStdString();
7474

@@ -84,7 +84,7 @@ void MCMeshConfigLoader::parseAttributes(const QDomElement & element, MeshDataPt
8484
}
8585
}
8686

87-
void MCMeshConfigLoader::parseChildNodes(const QDomNode & node, MeshDataPtr newData)
87+
void MCMeshConfigLoader::parseChildNodes(const QDomNode & node, MCMeshDataPtr newData)
8888
{
8989
auto && childNode = node.firstChild();
9090
while (!childNode.isNull())
@@ -120,14 +120,12 @@ void MCMeshConfigLoader::parseChildNodes(const QDomNode & node, MeshDataPtr newD
120120
}
121121
}
122122

123-
unsigned int MCMeshConfigLoader::meshCount() const
123+
size_t MCMeshConfigLoader::meshCount() const
124124
{
125-
return static_cast<unsigned int>(m_meshes.size());
125+
return m_meshes.size();
126126
}
127127

128-
const MCMeshMetaData & MCMeshConfigLoader::mesh(unsigned int index) const
128+
MCMeshDataPtr MCMeshConfigLoader::mesh(size_t index) const
129129
{
130-
assert(index < static_cast<unsigned int>(m_meshes.size()));
131-
assert(m_meshes.at(index));
132-
return *m_meshes.at(index);
130+
return m_meshes.at(index);
133131
}

src/game/MiniCore/src/Asset/mcmeshconfigloader.hh

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,17 @@ public:
3737
bool load(const std::string & filePath);
3838

3939
//! Get mesh count.
40-
unsigned int meshCount() const;
40+
size_t meshCount() const;
4141

4242
//! Get mesh data of given index.
43-
const MCMeshMetaData & mesh(unsigned int index) const;
43+
MCMeshDataPtr mesh(size_t index) const;
4444

4545
private:
46-
typedef std::shared_ptr<MCMeshMetaData> MeshDataPtr;
46+
void parseAttributes(const QDomElement & element, MCMeshDataPtr newData, const std::string & baseModelPath);
4747

48-
void parseAttributes(const QDomElement & element, MeshDataPtr newData, const std::string & baseModelPath);
48+
void parseChildNodes(const QDomNode & node, MCMeshDataPtr newData);
4949

50-
void parseChildNodes(const QDomNode & node, MeshDataPtr newData);
51-
52-
std::vector<MeshDataPtr> m_meshes;
50+
std::vector<MCMeshDataPtr> m_meshes;
5351
};
5452

5553
#endif // MCMESHCONFIGLOADER_HH

src/game/MiniCore/src/Asset/mcmeshmanager.cc

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,21 @@ MCMeshManager::MCMeshManager()
3535
{
3636
}
3737

38-
MCMesh & MCMeshManager::createMesh(
39-
const MCMeshMetaData & data, const MCMesh::FaceVector & faces)
38+
MCMeshPtr MCMeshManager::createMesh(const MCMeshMetaData & data, const MCMesh::FaceVector & faces)
4039
{
4140
// Create material
42-
MCGLMaterialPtr material(new MCGLMaterial);
41+
const auto material = std::make_shared<MCGLMaterial>();
4342

4443
material->setTexture(
45-
data.texture1 != "" ? MCAssetManager::surfaceManager().surface(data.texture1).material()->texture(0) : 0,
44+
data.texture1 != "" ? MCAssetManager::surfaceManager().surface(data.texture1)->material()->texture(0) : 0,
4645
0);
4746

4847
material->setTexture(
49-
data.texture2 != "" ? MCAssetManager::surfaceManager().surface(data.texture2).material()->texture(0) : 0,
48+
data.texture2 != "" ? MCAssetManager::surfaceManager().surface(data.texture2)->material()->texture(0) : 0,
5049
1);
5150

5251
// Create a new MCMesh object
53-
MeshPtr mesh(new MCMesh(data.handle, faces, material));
52+
const auto mesh = std::make_shared<MCMesh>(data.handle, faces, material);
5453

5554
// Maybe better place for this could be in the material?
5655
mesh->setColor(data.color);
@@ -62,29 +61,28 @@ MCMesh & MCMeshManager::createMesh(
6261

6362
m_meshMap[data.handle] = mesh;
6463

65-
return *mesh;
64+
return mesh;
6665
}
6766

68-
void MCMeshManager::load(
69-
const std::string & configFilePath, const std::string & baseDataPath)
67+
void MCMeshManager::load(const std::string & configFilePath, const std::string & baseDataPath)
7068
{
7169
MCMeshConfigLoader configLoader;
7270
MCMeshLoader modelLoader;
7371

7472
if (configLoader.load(configFilePath))
7573
{
76-
for (unsigned int i = 0; i < configLoader.meshCount(); i++)
74+
for (size_t i = 0; i < configLoader.meshCount(); i++)
7775
{
78-
const MCMeshMetaData & metaData = configLoader.mesh(i);
76+
auto && metaData = configLoader.mesh(i);
7977

8078
QString modelPath =
81-
QString(baseDataPath.c_str()) + QDir::separator().toLatin1() + metaData.modelPath.c_str();
79+
QString(baseDataPath.c_str()) + QDir::separator().toLatin1() + metaData->modelPath.c_str();
8280
modelPath.replace("./", "");
8381
modelPath.replace("//", "/");
8482

8583
if (modelLoader.load(modelPath))
8684
{
87-
createMesh(metaData, modelLoader.faces());
85+
createMesh(*metaData, modelLoader.faces());
8886
}
8987
else
9088
{
@@ -98,7 +96,7 @@ void MCMeshManager::load(
9896
}
9997
}
10098

101-
MCMesh & MCMeshManager::mesh(const std::string & handle) const
99+
MCMeshPtr MCMeshManager::mesh(const std::string & handle) const
102100
{
103101
// Try to find existing mesh for the handle
104102
if (m_meshMap.count(handle) == 0)
@@ -107,7 +105,5 @@ MCMesh & MCMeshManager::mesh(const std::string & handle) const
107105
}
108106

109107
// Yes: return handle for the mesh
110-
MeshPtr mesh = m_meshMap.find(handle)->second;
111-
assert(mesh);
112-
return *mesh;
108+
return m_meshMap.find(handle)->second;
113109
}

src/game/MiniCore/src/Asset/mcmeshmanager.hh

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,36 +61,34 @@ public:
6161
MCMeshManager();
6262

6363
//! Destructor
64-
virtual ~MCMeshManager() {};
64+
virtual ~MCMeshManager()
65+
{
66+
}
6567

6668
/*! Loads mesh config from strBasePath using the given mapping file strFile.
6769
* \param configFilePath Path to the XML-based input file.
6870
* \param baseDataPath The absolute search path for an mesh is
6971
* baseDataPath + baseModelPath + fileName. baseModelPath and the fileName are
7072
* defined in the input file. */
71-
virtual void load(
72-
const std::string & configFilePath, const std::string & baseDataPath);
73+
virtual void load(const std::string & configFilePath, const std::string & baseDataPath);
7374

7475
/*! Returns a mesh object associated with given strId.
7576
* MCMeshManager will keep the ownership.
7677
* \param handle Handle defined in the mesh config file.
7778
* \return Reference to the corresponding MCMesh.
7879
* \throws std::runtime_error on failure. */
79-
MCMesh & mesh(
80-
const std::string & handle) const;
80+
MCMeshPtr mesh(const std::string & handle) const;
8181

8282
//! Create a mesh from given meta data and face vector.
83-
MCMesh & createMesh(
84-
const MCMeshMetaData & data, const MCMesh::FaceVector & faces);
83+
MCMeshPtr createMesh(const MCMeshMetaData & data, const MCMesh::FaceVector & faces);
8584

8685
private:
87-
//! Map for resulting mesh objects
88-
typedef std::shared_ptr<MCMesh> MeshPtr;
89-
typedef std::unordered_map<std::string, MeshPtr> MeshHash;
86+
typedef std::unordered_map<std::string, MCMeshPtr> MeshHash;
9087
MeshHash m_meshMap;
9188

9289
DISABLE_COPY(MCMeshManager);
9390
DISABLE_ASSI(MCMeshManager);
91+
DISABLE_MOVE(MCMeshManager);
9492
};
9593

9694
#endif // MCMESHMANAGER_HH

src/game/MiniCore/src/Asset/mcmeshmetadata.hh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@
2424
#include <MCVector3d>
2525

2626
#include <map>
27+
#include <memory>
2728
#include <string>
2829

2930
/*! Mesh metadata structure returned by MCMeshConfigLoader.
3031
* MCMeshManager can create MCMesh objects based on this data. */
3132
struct MCMeshMetaData
3233
{
33-
MCMeshMetaData()
34-
{
35-
}
36-
3734
//! Handle of the mesh.
3835
std::string handle;
3936

@@ -53,4 +50,6 @@ struct MCMeshMetaData
5350
std::pair<MCVector3dF, bool> scale;
5451
};
5552

53+
using MCMeshDataPtr = std::shared_ptr<MCMeshMetaData>;
54+
5655
#endif // MCMESHMETADATA_HH

src/game/MiniCore/src/Asset/mcsurfacemanager.cc

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ MCSurfaceManager::MCSurfaceManager()
195195
{
196196
}
197197

198-
MCSurface & MCSurfaceManager::createSurfaceFromImage(const MCSurfaceMetaData & data, QImage image)
198+
std::shared_ptr<MCSurface> MCSurfaceManager::createSurfaceFromImage(const MCSurfaceMetaData & data, QImage image)
199199
{
200200
if (data.handle.size() == 0)
201201
{
@@ -212,35 +212,32 @@ MCSurface & MCSurfaceManager::createSurfaceFromImage(const MCSurfaceMetaData & d
212212
// that are initialized before this surface.
213213
MCGLMaterialPtr material(new MCGLMaterial);
214214
material->setTexture(create2DTextureFromImage(data, image), 0);
215-
material->setTexture(data.handle2.length() ? surface(data.handle2).material()->texture(0) : 0, 1);
216-
material->setTexture(data.handle3.length() ? surface(data.handle3).material()->texture(0) : 0, 2);
215+
material->setTexture(data.handle2.length() ? surface(data.handle2)->material()->texture(0) : 0, 1);
216+
material->setTexture(data.handle3.length() ? surface(data.handle3)->material()->texture(0) : 0, 2);
217217

218218
if (data.specularCoeff.second)
219219
{
220220
material->setSpecularCoeff(data.specularCoeff.first);
221221
}
222222

223223
// Create a new MCSurface object
224-
MCSurface * surface =
225-
new MCSurface(data.handle, material, origW, origH, data.z0, data.z1, data.z2, data.z3);
224+
auto surface = std::make_shared<MCSurface>(data.handle, material, origW, origH, data.z0, data.z1, data.z2, data.z3);
226225

227226
// Maybe better place for this could be in the material?
228227
surface->setColor(data.color);
229228

230-
assert(surface);
231-
createSurfaceCommon(*surface, data);
229+
createSurfaceCommon(surface, data);
232230

233-
return *surface;
231+
return surface;
234232
}
235233

236-
void MCSurfaceManager::createSurfaceCommon(MCSurface & surface, const MCSurfaceMetaData & data)
234+
void MCSurfaceManager::createSurfaceCommon(std::shared_ptr<MCSurface> surface, const MCSurfaceMetaData & data)
237235
{
238236
// Enable alpha blend, if set
239-
surface.material()->setAlphaBlend(
240-
data.alphaBlend.second, data.alphaBlend.first.m_src, data.alphaBlend.first.m_dst);
237+
surface->material()->setAlphaBlend(data.alphaBlend.second, data.alphaBlend.first.m_src, data.alphaBlend.first.m_dst);
241238

242239
// Store MCSurface to map
243-
m_surfaceMap[data.handle] = &surface;
240+
m_surfaceMap[data.handle] = surface;
244241
}
245242
#ifdef __MC_GLES__
246243
static bool isPowerOfTwo(unsigned int x)
@@ -425,25 +422,21 @@ void MCSurfaceManager::applyAlphaClamp(QImage & textureImage, unsigned int a) co
425422
MCSurfaceManager::~MCSurfaceManager()
426423
{
427424
// Delete OpenGL textures and Textures
428-
auto iter(m_surfaceMap.begin());
429-
while (iter != m_surfaceMap.end())
425+
for (auto && iter : m_surfaceMap)
430426
{
431-
if (iter->second)
427+
if (iter.second)
432428
{
433-
MCSurface * p = iter->second;
429+
const auto surface = iter.second;
434430
for (unsigned int i = 0; i < MCGLMaterial::MAX_TEXTURES; i++)
435431
{
436-
GLuint dummyHandle1 = p->material()->texture(i);
432+
GLuint dummyHandle1 = surface->material()->texture(i);
437433
glDeleteTextures(1, &dummyHandle1);
438434
}
439-
delete p;
440435
}
441-
iter++;
442436
}
443437
}
444438

445-
void MCSurfaceManager::load(
446-
const std::string & configFilePath, const std::string & baseDataPath)
439+
void MCSurfaceManager::load(const std::string & configFilePath, const std::string & baseDataPath)
447440
{
448441
MCSurfaceConfigLoader loader;
449442

@@ -479,7 +472,7 @@ void MCSurfaceManager::load(
479472
}
480473
}
481474

482-
MCSurface & MCSurfaceManager::surface(const std::string & id) const
475+
std::shared_ptr<MCSurface> MCSurfaceManager::surface(const std::string & id) const
483476
{
484477
// Try to find existing texture for the surface
485478
if (m_surfaceMap.count(id) == 0)
@@ -488,7 +481,5 @@ MCSurface & MCSurfaceManager::surface(const std::string & id) const
488481
}
489482

490483
// Yes: return handle for the texture
491-
MCSurface * pSurface = m_surfaceMap.find(id)->second;
492-
assert(pSurface);
493-
return *pSurface;
484+
return m_surfaceMap.find(id)->second;
494485
}

src/game/MiniCore/src/Asset/mcsurfacemanager.hh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef MCSURFACEMANAGER_HH
2121
#define MCSURFACEMANAGER_HH
2222

23+
#include <memory>
2324
#include <string>
2425
#include <unordered_map>
2526

@@ -88,16 +89,14 @@ public:
8889
/*! Returns a surface object associated with given strId.
8990
* Corresponding OpenGL texture handle can be obtained
9091
* by calling handle() of the resulting MCSurface.
91-
* MCSurfaceManager will keep the ownership.
9292
* \param handle Handle defined in the textures XML file.
9393
* \return Reference to the corresponding MCSurface.
9494
* \throws std::runtime_error on failure. */
95-
MCSurface & surface(
96-
const std::string & handle) const;
95+
std::shared_ptr<MCSurface> surface(const std::string & handle) const;
9796

9897
/*! Creates an MCSurface containing an OpenGL texture from a QImage + texture meta data.
9998
* MCSurfaceManager keeps the ownership. */
100-
MCSurface & createSurfaceFromImage(const MCSurfaceMetaData & data, QImage image);
99+
std::shared_ptr<MCSurface> createSurfaceFromImage(const MCSurfaceMetaData & data, QImage image);
101100

102101
private:
103102
//! Apply alpha clamp (set alpha values off based on the given limit).
@@ -110,10 +109,10 @@ private:
110109
GLuint create2DTextureFromImage(const MCSurfaceMetaData & data, const QImage & image);
111110

112111
//! Helper to set surface meta data.
113-
void createSurfaceCommon(MCSurface & surface, const MCSurfaceMetaData & data);
112+
void createSurfaceCommon(std::shared_ptr<MCSurface> surface, const MCSurfaceMetaData & data);
114113

115114
//! Map for resulting surface objects
116-
typedef std::unordered_map<std::string, MCSurface *> SurfaceHash;
115+
typedef std::unordered_map<std::string, std::shared_ptr<MCSurface>> SurfaceHash;
117116
SurfaceHash m_surfaceMap;
118117

119118
DISABLE_COPY(MCSurfaceManager);

src/game/MiniCore/src/Asset/mcsurfaceobjectdata.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ void MCSurfaceObjectData::setDefaultCircleShape(bool state)
4141
m_defaultCircleShape = state;
4242
}
4343

44-
bool MCSurfaceObjectData::defaultCirleShape() const
44+
bool MCSurfaceObjectData::defaultCircleShape() const
4545
{
4646
return m_defaultCircleShape;
4747
}
48+
49+
MCSurfaceObjectData::~MCSurfaceObjectData() = default;

src/game/MiniCore/src/Asset/mcsurfaceobjectdata.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public:
3333
explicit MCSurfaceObjectData(const std::string & typeId);
3434

3535
//! Destructor.
36-
virtual ~MCSurfaceObjectData() {};
36+
virtual ~MCSurfaceObjectData();
3737

3838
//! Set the id of the already created MCSurface.
3939
void setSurfaceId(const std::string & id);
@@ -45,7 +45,7 @@ public:
4545
void setDefaultCircleShape(bool state);
4646

4747
//! Returns true if default circle shape is wanted.
48-
bool defaultCirleShape() const;
48+
bool defaultCircleShape() const;
4949

5050
private:
5151
DISABLE_COPY(MCSurfaceObjectData);

0 commit comments

Comments
 (0)