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);
// Initialize minimap data
data = new MinimapData;
data = std::make_unique<MinimapData>();
data->map_invalidated = true;
data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
@ -209,11 +209,11 @@ Minimap::Minimap(Client *client)
setModeIndex(0);
// Create mesh buffer for minimap
m_meshbuffer = getMinimapMeshBuffer();
m_meshbuffer = createMinimapMeshBuffer();
// Initialize and start thread
m_minimap_update_thread = new MinimapUpdateThread();
m_minimap_update_thread->data = data;
m_minimap_update_thread = std::make_unique<MinimapUpdateThread>();
m_minimap_update_thread->data = data.get();
m_minimap_update_thread->start();
}
@ -222,7 +222,7 @@ Minimap::~Minimap()
m_minimap_update_thread->stop();
m_minimap_update_thread->wait();
m_meshbuffer->drop();
m_meshbuffer.reset();
if (data->minimap_mask_round)
data->minimap_mask_round->drop();
@ -232,12 +232,10 @@ Minimap::~Minimap()
driver->removeTexture(data->texture);
driver->removeTexture(data->heightmap_texture);
for (MinimapMarker *m : m_markers)
delete m;
m_markers.clear();
delete data;
delete m_minimap_update_thread;
data.reset();
m_minimap_update_thread.reset();
}
void Minimap::addBlock(v3s16 pos, MinimapMapblock *data)
@ -552,9 +550,9 @@ v3f Minimap::getYawVec()
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 &indices = buf->Indices->Data;
vertices.resize(4);
@ -628,7 +626,7 @@ void Minimap::drawMinimap(core::rect<s32> rect)
// Draw minimap
driver->setTransform(video::ETS_WORLD, matrix);
driver->setMaterial(material);
driver->drawMeshBuffer(m_meshbuffer);
driver->drawMeshBuffer(m_meshbuffer.get());
// Draw overlay
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.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
driver->setMaterial(material);
driver->drawMeshBuffer(m_meshbuffer);
driver->drawMeshBuffer(m_meshbuffer.get());
// Draw player marker on minimap
if (data->minimap_shape_round) {
@ -648,7 +646,7 @@ void Minimap::drawMinimap(core::rect<s32> rect)
material.TextureLayers[0].Texture = data->player_marker;
driver->setTransform(video::ETS_WORLD, matrix);
driver->setMaterial(material);
driver->drawMeshBuffer(m_meshbuffer);
driver->drawMeshBuffer(m_meshbuffer.get());
// Reset transformations
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);
m_markers.push_back(m);
return m;
auto m = std::make_unique<MinimapMarker>(parent_node);
auto ret = m.get();
m_markers.push_back(std::move(m));
return ret;
}
void Minimap::removeMarker(MinimapMarker **m)
{
m_markers.remove(*m);
delete *m;
m_markers.remove_if([ptr = *m](const auto &up) { return up.get() == ptr; });
*m = nullptr;
}
@ -710,7 +708,7 @@ void Minimap::updateActiveMarkers()
data->mode.scan_height / 2,
data->mode.map_size / 2);
for (MinimapMarker *marker : m_markers) {
for (auto &&marker : m_markers) {
v3s16 pos = floatToInt(marker->parent_node->getAbsolutePosition() +
cam_offset, BS) - pos_offset;
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 "irrlichttypes_extrabloated.h"
#include "irr_ptr.h"
#include "util/thread.h"
#include "voxel.h"
#include <map>
@ -148,7 +149,7 @@ public:
void blitMinimapPixelsToImageSurface(video::IImage *map_image,
video::IImage *heightmap_image);
scene::SMeshBuffer *getMinimapMeshBuffer();
irr_ptr<scene::SMeshBuffer> createMinimapMeshBuffer();
MinimapMarker* addMarker(scene::ISceneNode *parent_node);
void removeMarker(MinimapMarker **marker);
@ -158,20 +159,20 @@ public:
video::IVideoDriver *driver;
Client* client;
MinimapData *data;
std::unique_ptr<MinimapData> data;
private:
ITextureSource *m_tsrc;
IShaderSource *m_shdrsrc;
const NodeDefManager *m_ndef;
MinimapUpdateThread *m_minimap_update_thread = nullptr;
scene::SMeshBuffer *m_meshbuffer;
std::unique_ptr<MinimapUpdateThread> m_minimap_update_thread;
irr_ptr<scene::SMeshBuffer> m_meshbuffer;
bool m_enable_shaders;
std::vector<MinimapModeDef> m_modes;
size_t m_current_mode_index;
u16 m_surface_mode_scan_height;
f32 m_angle;
std::mutex m_mutex;
std::list<MinimapMarker*> m_markers;
std::list<std::unique_ptr<MinimapMarker>> m_markers;
std::list<v2f> m_active_markers;
};