Use smart ptrs for Minimap's member vars

This commit is contained in:
Desour 2023-09-15 00:00:00 +02:00 committed by sfan5
parent 9827f9df1b
commit 24efd7dc91
2 changed files with 25 additions and 26 deletions

@ -201,7 +201,7 @@ Minimap::Minimap(Client *client)
addMode(MINIMAP_TYPE_RADAR, 128); addMode(MINIMAP_TYPE_RADAR, 128);
// Initialize minimap data // Initialize minimap data
data = new MinimapData; data = std::make_unique<MinimapData>();
data->map_invalidated = true; data->map_invalidated = true;
data->minimap_shape_round = g_settings->getBool("minimap_shape_round"); data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
@ -209,11 +209,11 @@ Minimap::Minimap(Client *client)
setModeIndex(0); setModeIndex(0);
// Create mesh buffer for minimap // Create mesh buffer for minimap
m_meshbuffer = getMinimapMeshBuffer(); m_meshbuffer = createMinimapMeshBuffer();
// Initialize and start thread // Initialize and start thread
m_minimap_update_thread = new MinimapUpdateThread(); m_minimap_update_thread = std::make_unique<MinimapUpdateThread>();
m_minimap_update_thread->data = data; m_minimap_update_thread->data = data.get();
m_minimap_update_thread->start(); m_minimap_update_thread->start();
} }
@ -222,7 +222,7 @@ Minimap::~Minimap()
m_minimap_update_thread->stop(); m_minimap_update_thread->stop();
m_minimap_update_thread->wait(); m_minimap_update_thread->wait();
m_meshbuffer->drop(); m_meshbuffer.reset();
if (data->minimap_mask_round) if (data->minimap_mask_round)
data->minimap_mask_round->drop(); data->minimap_mask_round->drop();
@ -232,12 +232,10 @@ Minimap::~Minimap()
driver->removeTexture(data->texture); driver->removeTexture(data->texture);
driver->removeTexture(data->heightmap_texture); driver->removeTexture(data->heightmap_texture);
for (MinimapMarker *m : m_markers)
delete m;
m_markers.clear(); m_markers.clear();
delete data; data.reset();
delete m_minimap_update_thread; m_minimap_update_thread.reset();
} }
void Minimap::addBlock(v3s16 pos, MinimapMapblock *data) void Minimap::addBlock(v3s16 pos, MinimapMapblock *data)
@ -552,9 +550,9 @@ v3f Minimap::getYawVec()
return v3f(1.0, 0.0, 1.0); return v3f(1.0, 0.0, 1.0);
} }
scene::SMeshBuffer *Minimap::getMinimapMeshBuffer() irr_ptr<scene::SMeshBuffer> Minimap::createMinimapMeshBuffer()
{ {
scene::SMeshBuffer *buf = new scene::SMeshBuffer(); auto buf = make_irr<scene::SMeshBuffer>();
auto &vertices = buf->Vertices->Data; auto &vertices = buf->Vertices->Data;
auto &indices = buf->Indices->Data; auto &indices = buf->Indices->Data;
vertices.resize(4); vertices.resize(4);
@ -628,7 +626,7 @@ void Minimap::drawMinimap(core::rect<s32> rect)
// Draw minimap // Draw minimap
driver->setTransform(video::ETS_WORLD, matrix); driver->setTransform(video::ETS_WORLD, matrix);
driver->setMaterial(material); driver->setMaterial(material);
driver->drawMeshBuffer(m_meshbuffer); driver->drawMeshBuffer(m_meshbuffer.get());
// Draw overlay // Draw overlay
video::ITexture *minimap_overlay = data->minimap_shape_round ? video::ITexture *minimap_overlay = data->minimap_shape_round ?
@ -636,7 +634,7 @@ void Minimap::drawMinimap(core::rect<s32> rect)
material.TextureLayers[0].Texture = minimap_overlay; material.TextureLayers[0].Texture = minimap_overlay;
material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
driver->setMaterial(material); driver->setMaterial(material);
driver->drawMeshBuffer(m_meshbuffer); driver->drawMeshBuffer(m_meshbuffer.get());
// Draw player marker on minimap // Draw player marker on minimap
if (data->minimap_shape_round) { if (data->minimap_shape_round) {
@ -648,7 +646,7 @@ void Minimap::drawMinimap(core::rect<s32> rect)
material.TextureLayers[0].Texture = data->player_marker; material.TextureLayers[0].Texture = data->player_marker;
driver->setTransform(video::ETS_WORLD, matrix); driver->setTransform(video::ETS_WORLD, matrix);
driver->setMaterial(material); driver->setMaterial(material);
driver->drawMeshBuffer(m_meshbuffer); driver->drawMeshBuffer(m_meshbuffer.get());
// Reset transformations // Reset transformations
driver->setTransform(video::ETS_VIEW, oldViewMat); driver->setTransform(video::ETS_VIEW, oldViewMat);
@ -686,17 +684,17 @@ void Minimap::drawMinimap(core::rect<s32> rect)
} }
} }
MinimapMarker* Minimap::addMarker(scene::ISceneNode *parent_node) MinimapMarker *Minimap::addMarker(scene::ISceneNode *parent_node)
{ {
MinimapMarker *m = new MinimapMarker(parent_node); auto m = std::make_unique<MinimapMarker>(parent_node);
m_markers.push_back(m); auto ret = m.get();
return m; m_markers.push_back(std::move(m));
return ret;
} }
void Minimap::removeMarker(MinimapMarker **m) void Minimap::removeMarker(MinimapMarker **m)
{ {
m_markers.remove(*m); m_markers.remove_if([ptr = *m](const auto &up) { return up.get() == ptr; });
delete *m;
*m = nullptr; *m = nullptr;
} }
@ -710,7 +708,7 @@ void Minimap::updateActiveMarkers()
data->mode.scan_height / 2, data->mode.scan_height / 2,
data->mode.map_size / 2); data->mode.map_size / 2);
for (MinimapMarker *marker : m_markers) { for (auto &&marker : m_markers) {
v3s16 pos = floatToInt(marker->parent_node->getAbsolutePosition() + v3s16 pos = floatToInt(marker->parent_node->getAbsolutePosition() +
cam_offset, BS) - pos_offset; cam_offset, BS) - pos_offset;
if (pos.X < 0 || pos.X > data->mode.map_size || if (pos.X < 0 || pos.X > data->mode.map_size ||

@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "../hud.h" #include "../hud.h"
#include "irrlichttypes_extrabloated.h" #include "irrlichttypes_extrabloated.h"
#include "irr_ptr.h"
#include "util/thread.h" #include "util/thread.h"
#include "voxel.h" #include "voxel.h"
#include <map> #include <map>
@ -148,7 +149,7 @@ public:
void blitMinimapPixelsToImageSurface(video::IImage *map_image, void blitMinimapPixelsToImageSurface(video::IImage *map_image,
video::IImage *heightmap_image); video::IImage *heightmap_image);
scene::SMeshBuffer *getMinimapMeshBuffer(); irr_ptr<scene::SMeshBuffer> createMinimapMeshBuffer();
MinimapMarker* addMarker(scene::ISceneNode *parent_node); MinimapMarker* addMarker(scene::ISceneNode *parent_node);
void removeMarker(MinimapMarker **marker); void removeMarker(MinimapMarker **marker);
@ -158,20 +159,20 @@ public:
video::IVideoDriver *driver; video::IVideoDriver *driver;
Client* client; Client* client;
MinimapData *data; std::unique_ptr<MinimapData> data;
private: private:
ITextureSource *m_tsrc; ITextureSource *m_tsrc;
IShaderSource *m_shdrsrc; IShaderSource *m_shdrsrc;
const NodeDefManager *m_ndef; const NodeDefManager *m_ndef;
MinimapUpdateThread *m_minimap_update_thread = nullptr; std::unique_ptr<MinimapUpdateThread> m_minimap_update_thread;
scene::SMeshBuffer *m_meshbuffer; irr_ptr<scene::SMeshBuffer> m_meshbuffer;
bool m_enable_shaders; bool m_enable_shaders;
std::vector<MinimapModeDef> m_modes; std::vector<MinimapModeDef> m_modes;
size_t m_current_mode_index; size_t m_current_mode_index;
u16 m_surface_mode_scan_height; u16 m_surface_mode_scan_height;
f32 m_angle; f32 m_angle;
std::mutex m_mutex; std::mutex m_mutex;
std::list<MinimapMarker*> m_markers; std::list<std::unique_ptr<MinimapMarker>> m_markers;
std::list<v2f> m_active_markers; std::list<v2f> m_active_markers;
}; };