@@ -195,7 +195,7 @@ MCSurfaceManager::MCSurfaceManager()
195
195
{
196
196
}
197
197
198
- MCSurface & MCSurfaceManager::createSurfaceFromImage (const MCSurfaceMetaData & data, QImage image)
198
+ std::shared_ptr< MCSurface> MCSurfaceManager::createSurfaceFromImage (const MCSurfaceMetaData & data, QImage image)
199
199
{
200
200
if (data.handle .size () == 0 )
201
201
{
@@ -212,35 +212,32 @@ MCSurface & MCSurfaceManager::createSurfaceFromImage(const MCSurfaceMetaData & d
212
212
// that are initialized before this surface.
213
213
MCGLMaterialPtr material (new MCGLMaterial);
214
214
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 );
217
217
218
218
if (data.specularCoeff .second )
219
219
{
220
220
material->setSpecularCoeff (data.specularCoeff .first );
221
221
}
222
222
223
223
// 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 );
226
225
227
226
// Maybe better place for this could be in the material?
228
227
surface->setColor (data.color );
229
228
230
- assert (surface);
231
- createSurfaceCommon (*surface, data);
229
+ createSurfaceCommon (surface, data);
232
230
233
- return * surface;
231
+ return surface;
234
232
}
235
233
236
- void MCSurfaceManager::createSurfaceCommon (MCSurface & surface, const MCSurfaceMetaData & data)
234
+ void MCSurfaceManager::createSurfaceCommon (std::shared_ptr< MCSurface> surface, const MCSurfaceMetaData & data)
237
235
{
238
236
// 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 );
241
238
242
239
// Store MCSurface to map
243
- m_surfaceMap[data.handle ] = & surface;
240
+ m_surfaceMap[data.handle ] = surface;
244
241
}
245
242
#ifdef __MC_GLES__
246
243
static bool isPowerOfTwo (unsigned int x)
@@ -425,25 +422,21 @@ void MCSurfaceManager::applyAlphaClamp(QImage & textureImage, unsigned int a) co
425
422
MCSurfaceManager::~MCSurfaceManager ()
426
423
{
427
424
// Delete OpenGL textures and Textures
428
- auto iter (m_surfaceMap.begin ());
429
- while (iter != m_surfaceMap.end ())
425
+ for (auto && iter : m_surfaceMap)
430
426
{
431
- if (iter-> second )
427
+ if (iter. second )
432
428
{
433
- MCSurface * p = iter-> second ;
429
+ const auto surface = iter. second ;
434
430
for (unsigned int i = 0 ; i < MCGLMaterial::MAX_TEXTURES; i++)
435
431
{
436
- GLuint dummyHandle1 = p ->material ()->texture (i);
432
+ GLuint dummyHandle1 = surface ->material ()->texture (i);
437
433
glDeleteTextures (1 , &dummyHandle1);
438
434
}
439
- delete p;
440
435
}
441
- iter++;
442
436
}
443
437
}
444
438
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)
447
440
{
448
441
MCSurfaceConfigLoader loader;
449
442
@@ -479,7 +472,7 @@ void MCSurfaceManager::load(
479
472
}
480
473
}
481
474
482
- MCSurface & MCSurfaceManager::surface (const std::string & id) const
475
+ std::shared_ptr< MCSurface> MCSurfaceManager::surface (const std::string & id) const
483
476
{
484
477
// Try to find existing texture for the surface
485
478
if (m_surfaceMap.count (id) == 0 )
@@ -488,7 +481,5 @@ MCSurface & MCSurfaceManager::surface(const std::string & id) const
488
481
}
489
482
490
483
// 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 ;
494
485
}
0 commit comments