From fab124a2e2f0d278d2e26c1f595cf47b62d9adfa Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 1 Jul 2024 06:00:33 +0300 Subject: [PATCH] remove unnecessary 'new' operators --- src/coders/png.cpp | 15 ++------------- src/engine.cpp | 2 +- src/frontend/screens/Screen.cpp | 4 +++- src/graphics/core/ImageData.cpp | 6 ++++-- src/graphics/core/ImageData.hpp | 2 +- src/graphics/core/LineBatch.cpp | 7 +++---- src/graphics/core/LineBatch.hpp | 8 ++++---- src/maths/LMPacker.cpp | 27 ++++++++++++--------------- src/maths/LMPacker.hpp | 7 ++++++- 9 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/coders/png.cpp b/src/coders/png.cpp index 08adb5c9..c6bc76ae 100644 --- a/src/coders/png.cpp +++ b/src/coders/png.cpp @@ -233,7 +233,6 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data, std::unique_ptr _png_load(const char* file){ int r = 0; FILE *png = nullptr; - char *pngbuf = nullptr; spng_ctx *ctx = nullptr; png = fopen(file, "rb"); @@ -250,30 +249,26 @@ std::unique_ptr _png_load(const char* file){ logger.error() << "could not to read file " << file; return nullptr; } - pngbuf = new char[siz_pngbuf]; - if(fread(pngbuf, siz_pngbuf, 1, png) != 1){ //check of read elements count + auto pngbuf = std::make_unique(siz_pngbuf); + if(fread(pngbuf.get(), siz_pngbuf, 1, png) != 1){ //check of read elements count fclose(png); - delete[] pngbuf; logger.error() << "fread() failed: " << file; return nullptr; } fclose(png); // <- finally closing file ctx = spng_ctx_new(0); if (ctx == nullptr){ - delete[] pngbuf; logger.error() << "spng_ctx_new() failed"; return nullptr; } r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE); if (r != SPNG_SUCCESS){ - delete[] pngbuf; spng_ctx_free(ctx); logger.error() << "spng_set_crc_action(): " << spng_strerror(r); return nullptr; } r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf); if (r != SPNG_SUCCESS){ - delete[] pngbuf; spng_ctx_free(ctx); logger.error() << "spng_set_png_buffer(): " << spng_strerror(r); return nullptr; @@ -282,7 +277,6 @@ std::unique_ptr _png_load(const char* file){ spng_ihdr ihdr; r = spng_get_ihdr(ctx, &ihdr); if (r != SPNG_SUCCESS){ - delete[] pngbuf; spng_ctx_free(ctx); logger.error() << "spng_get_ihdr(): " << spng_strerror(r); return nullptr; @@ -291,7 +285,6 @@ std::unique_ptr _png_load(const char* file){ size_t out_size; r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size); if (r != SPNG_SUCCESS){ - delete[] pngbuf; spng_ctx_free(ctx); logger.error() << "spng_decoded_image_size(): " << spng_strerror(r); return nullptr; @@ -299,7 +292,6 @@ std::unique_ptr _png_load(const char* file){ auto out = std::make_unique(out_size); r = spng_decode_image(ctx, out.get(), out_size, SPNG_FMT_RGBA8, 0); if (r != SPNG_SUCCESS){ - delete[] pngbuf; spng_ctx_free(ctx); logger.error() << "spng_decode_image(): " << spng_strerror(r); return nullptr; @@ -314,10 +306,7 @@ std::unique_ptr _png_load(const char* file){ } auto image = std::make_unique(ImageFormat::rgba8888, ihdr.width, ihdr.height, std::move(flipped)); - - delete[] pngbuf; spng_ctx_free(ctx); - return image; } #endif diff --git a/src/engine.cpp b/src/engine.cpp index b7396ff3..32c05513 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -258,7 +258,7 @@ void Engine::loadAssets() { } } } - assets.reset(new_assets.release()); + assets = std::move(new_assets); } static void load_configs(const fs::path& root) { diff --git a/src/frontend/screens/Screen.cpp b/src/frontend/screens/Screen.cpp index 2f68f4d2..477baf24 100644 --- a/src/frontend/screens/Screen.cpp +++ b/src/frontend/screens/Screen.cpp @@ -3,7 +3,9 @@ #include "../../graphics/core/Batch2D.hpp" #include "../../engine.hpp" -Screen::Screen(Engine* engine) : engine(engine), batch(new Batch2D(1024)) { +Screen::Screen(Engine* engine) + : engine(engine), + batch(std::make_unique(1024)) { } Screen::~Screen() { diff --git a/src/graphics/core/ImageData.cpp b/src/graphics/core/ImageData.cpp index f03a841d..6499afb4 100644 --- a/src/graphics/core/ImageData.cpp +++ b/src/graphics/core/ImageData.cpp @@ -254,7 +254,7 @@ void ImageData::fixAlphaColor() { } } -ImageData* add_atlas_margins(ImageData* image, int grid_size) { +std::unique_ptr add_atlas_margins(ImageData* image, int grid_size) { // RGBA is only supported assert(image->getFormat() == ImageFormat::rgba8888); assert(image->getWidth() == image->getHeight()); @@ -300,5 +300,7 @@ ImageData* add_atlas_margins(ImageData* image, int grid_size) { } } } - return new ImageData(image->getFormat(), dstwidth, dstheight, std::move(dstdata)); + return std::make_unique( + image->getFormat(), dstwidth, dstheight, std::move(dstdata) + ); } diff --git a/src/graphics/core/ImageData.hpp b/src/graphics/core/ImageData.hpp index 0647730c..0a0872f9 100644 --- a/src/graphics/core/ImageData.hpp +++ b/src/graphics/core/ImageData.hpp @@ -47,6 +47,6 @@ public: } }; -extern ImageData* add_atlas_margins(ImageData* image, int grid_size); +std::unique_ptr add_atlas_margins(ImageData* image, int grid_size); #endif // GRAPHICS_CORE_IMAGE_DATA_HPP_ diff --git a/src/graphics/core/LineBatch.cpp b/src/graphics/core/LineBatch.cpp index 33d72def..254a25a6 100644 --- a/src/graphics/core/LineBatch.cpp +++ b/src/graphics/core/LineBatch.cpp @@ -7,13 +7,12 @@ inline constexpr uint LB_VERTEX_SIZE = (3+4); LineBatch::LineBatch(size_t capacity) : capacity(capacity) { const vattr attrs[] = { {3},{4}, {0} }; - buffer = new float[capacity * LB_VERTEX_SIZE * 2]; - mesh = std::make_unique(buffer, 0, attrs); + buffer = std::make_unique(capacity * LB_VERTEX_SIZE * 2); + mesh = std::make_unique(buffer.get(), 0, attrs); index = 0; } LineBatch::~LineBatch(){ - delete[] buffer; } void LineBatch::line( @@ -69,7 +68,7 @@ void LineBatch::box(float x, float y, float z, float w, float h, float d, void LineBatch::render(){ if (index == 0) return; - mesh->reload(buffer, index / LB_VERTEX_SIZE); + mesh->reload(buffer.get(), index / LB_VERTEX_SIZE); mesh->draw(GL_LINES); index = 0; } diff --git a/src/graphics/core/LineBatch.hpp b/src/graphics/core/LineBatch.hpp index 0cd8065a..58303dc9 100644 --- a/src/graphics/core/LineBatch.hpp +++ b/src/graphics/core/LineBatch.hpp @@ -9,7 +9,7 @@ class Mesh; class LineBatch { std::unique_ptr mesh; - float* buffer; + std::unique_ptr buffer; size_t index; size_t capacity; public: @@ -20,13 +20,13 @@ public: line(a.x, a.y, a.z, b.x, b.y, b.z, color.r, color.g, color.b, color.a); } void line(float x1, float y1, float z1, float x2, float y2, float z2, - float r, float g, float b, float a); + float r, float g, float b, float a); void box(float x, float y, float z, float w, float h, float d, - float r, float g, float b, float a); + float r, float g, float b, float a); inline void box(glm::vec3 xyz, glm::vec3 whd, glm::vec4 rgba) { box(xyz.x, xyz.y, xyz.z, whd.x, whd.y, whd.z, - rgba.r, rgba.g, rgba.b, rgba.a); + rgba.r, rgba.g, rgba.b, rgba.a); } void render(); diff --git a/src/maths/LMPacker.cpp b/src/maths/LMPacker.cpp index 31cd50a7..c0878b8b 100644 --- a/src/maths/LMPacker.cpp +++ b/src/maths/LMPacker.cpp @@ -2,7 +2,7 @@ #include -inline int getPackerScore(rectangle& rect) { +static int get_packer_score(const rectangle& rect) { if (rect.width * rect.height > 100) return rect.height * rect.height * 1000; return (rect.width * rect.height * rect.height); @@ -14,7 +14,7 @@ LMPacker::LMPacker(const uint32_t sizes[], size_t length) { rects.push_back(rect); } sort(rects.begin(), rects.end(), [](rectangle a, rectangle b) { - return -getPackerScore(a) < -getPackerScore(b); + return -get_packer_score(a) < -get_packer_score(b); }); } @@ -23,12 +23,7 @@ LMPacker::~LMPacker() { } void LMPacker::cleanup() { - if (matrix) { - for (unsigned int y = 0; y < (height >> mbit); y++) { - delete[] matrix[y]; - } - delete[] matrix; - } + matrix.reset(); placed.clear(); } @@ -43,9 +38,9 @@ bool LMPacker::build(uint32_t width, uint32_t height, const unsigned int mwidth = width >> mbit; const unsigned int mheight = height >> mbit; - matrix = new rectangle**[mheight]; + matrix = std::make_unique(mheight); for (unsigned int y = 0; y < mheight; y++) { - matrix[y] = new rectangle*[mwidth]; + matrix[y] = std::make_unique(mwidth); for (unsigned int x = 0; x < mwidth; x++) { matrix[y][x] = nullptr; } @@ -85,7 +80,9 @@ bool LMPacker::build(uint32_t width, uint32_t height, return built; } -inline rectangle* findCollision(rectangle*** matrix, int x, int y, int w, int h) { +inline rectangle* find_collision( + const LMPacker::matrix_ptr& matrix, int x, int y, int w, int h +) { for (int row = y; row < y+h; row++) { for (int col = x; col < x+w; col++) { rectangle* rect = matrix[row][col]; @@ -97,7 +94,7 @@ inline rectangle* findCollision(rectangle*** matrix, int x, int y, int w, int h) return nullptr; } -inline void fill(rectangle*** matrix, rectangle* rect, int x, int y, int w, int h) { +inline void fill(LMPacker::matrix_ptr& matrix, rectangle* rect, int x, int y, int w, int h) { for (int row = y; row < y+h; row++) { for (int col = x; col < x+w; col++) { matrix[row][col] = rect; @@ -115,9 +112,9 @@ bool LMPacker::place(rectangle* rectptr, uint32_t vstep) { const unsigned int mwidth = width >> mbit; const unsigned int mheight = height >> mbit; for (unsigned int y = 0; y + rh < mheight; y += vstep) { - rectangle** line = matrix[y]; + auto& line = matrix[y]; bool skiplines = true; - rectangle** lower = matrix[y + rh - 1]; + auto& lower = matrix[y + rh - 1]; for (unsigned int x = 0; x + rw < mwidth; x++) { rectangle* prect = line[x]; if (prect) { @@ -131,7 +128,7 @@ bool LMPacker::place(rectangle* rectptr, uint32_t vstep) { if (lfree >= rw) skiplines = false; } - prect = findCollision(matrix, x, y, rw, rh); + prect = find_collision(matrix, x, y, rw, rh); if (prect) { x = (prect->x >> mbit) + (prect->width >> mbit) - 1; continue; diff --git a/src/maths/LMPacker.hpp b/src/maths/LMPacker.hpp index c3a73b6c..4bba767b 100644 --- a/src/maths/LMPacker.hpp +++ b/src/maths/LMPacker.hpp @@ -6,6 +6,7 @@ #include #include #include +#include struct rectangle { unsigned int idx; @@ -22,11 +23,15 @@ struct rectangle { }; class LMPacker { +public: + using matrix_row = std::unique_ptr; + using matrix_ptr = std::unique_ptr; +private: std::vector rects; std::vector placed; uint32_t width = 0; uint32_t height = 0; - rectangle*** matrix = nullptr; + matrix_ptr matrix = nullptr; uint32_t mbit = 0; void cleanup();