remove unnecessary 'new' operators

This commit is contained in:
MihailRis
2024-07-01 06:00:33 +03:00
parent 26e2068164
commit fab124a2e2
9 changed files with 36 additions and 42 deletions
+2 -13
View File
@@ -233,7 +233,6 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
std::unique_ptr<ImageData> _png_load(const char* file){ std::unique_ptr<ImageData> _png_load(const char* file){
int r = 0; int r = 0;
FILE *png = nullptr; FILE *png = nullptr;
char *pngbuf = nullptr;
spng_ctx *ctx = nullptr; spng_ctx *ctx = nullptr;
png = fopen(file, "rb"); png = fopen(file, "rb");
@@ -250,30 +249,26 @@ std::unique_ptr<ImageData> _png_load(const char* file){
logger.error() << "could not to read file " << file; logger.error() << "could not to read file " << file;
return nullptr; return nullptr;
} }
pngbuf = new char[siz_pngbuf]; auto pngbuf = std::make_unique<char[]>(siz_pngbuf);
if(fread(pngbuf, siz_pngbuf, 1, png) != 1){ //check of read elements count if(fread(pngbuf.get(), siz_pngbuf, 1, png) != 1){ //check of read elements count
fclose(png); fclose(png);
delete[] pngbuf;
logger.error() << "fread() failed: " << file; logger.error() << "fread() failed: " << file;
return nullptr; return nullptr;
} }
fclose(png); // <- finally closing file fclose(png); // <- finally closing file
ctx = spng_ctx_new(0); ctx = spng_ctx_new(0);
if (ctx == nullptr){ if (ctx == nullptr){
delete[] pngbuf;
logger.error() << "spng_ctx_new() failed"; logger.error() << "spng_ctx_new() failed";
return nullptr; return nullptr;
} }
r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE); r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE);
if (r != SPNG_SUCCESS){ if (r != SPNG_SUCCESS){
delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
logger.error() << "spng_set_crc_action(): " << spng_strerror(r); logger.error() << "spng_set_crc_action(): " << spng_strerror(r);
return nullptr; return nullptr;
} }
r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf); r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf);
if (r != SPNG_SUCCESS){ if (r != SPNG_SUCCESS){
delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
logger.error() << "spng_set_png_buffer(): " << spng_strerror(r); logger.error() << "spng_set_png_buffer(): " << spng_strerror(r);
return nullptr; return nullptr;
@@ -282,7 +277,6 @@ std::unique_ptr<ImageData> _png_load(const char* file){
spng_ihdr ihdr; spng_ihdr ihdr;
r = spng_get_ihdr(ctx, &ihdr); r = spng_get_ihdr(ctx, &ihdr);
if (r != SPNG_SUCCESS){ if (r != SPNG_SUCCESS){
delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
logger.error() << "spng_get_ihdr(): " << spng_strerror(r); logger.error() << "spng_get_ihdr(): " << spng_strerror(r);
return nullptr; return nullptr;
@@ -291,7 +285,6 @@ std::unique_ptr<ImageData> _png_load(const char* file){
size_t out_size; size_t out_size;
r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size); r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size);
if (r != SPNG_SUCCESS){ if (r != SPNG_SUCCESS){
delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
logger.error() << "spng_decoded_image_size(): " << spng_strerror(r); logger.error() << "spng_decoded_image_size(): " << spng_strerror(r);
return nullptr; return nullptr;
@@ -299,7 +292,6 @@ std::unique_ptr<ImageData> _png_load(const char* file){
auto out = std::make_unique<ubyte[]>(out_size); auto out = std::make_unique<ubyte[]>(out_size);
r = spng_decode_image(ctx, out.get(), out_size, SPNG_FMT_RGBA8, 0); r = spng_decode_image(ctx, out.get(), out_size, SPNG_FMT_RGBA8, 0);
if (r != SPNG_SUCCESS){ if (r != SPNG_SUCCESS){
delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
logger.error() << "spng_decode_image(): " << spng_strerror(r); logger.error() << "spng_decode_image(): " << spng_strerror(r);
return nullptr; return nullptr;
@@ -314,10 +306,7 @@ std::unique_ptr<ImageData> _png_load(const char* file){
} }
auto image = std::make_unique<ImageData>(ImageFormat::rgba8888, ihdr.width, ihdr.height, std::move(flipped)); auto image = std::make_unique<ImageData>(ImageFormat::rgba8888, ihdr.width, ihdr.height, std::move(flipped));
delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
return image; return image;
} }
#endif #endif
+1 -1
View File
@@ -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) { static void load_configs(const fs::path& root) {
+3 -1
View File
@@ -3,7 +3,9 @@
#include "../../graphics/core/Batch2D.hpp" #include "../../graphics/core/Batch2D.hpp"
#include "../../engine.hpp" #include "../../engine.hpp"
Screen::Screen(Engine* engine) : engine(engine), batch(new Batch2D(1024)) { Screen::Screen(Engine* engine)
: engine(engine),
batch(std::make_unique<Batch2D>(1024)) {
} }
Screen::~Screen() { Screen::~Screen() {
+4 -2
View File
@@ -254,7 +254,7 @@ void ImageData::fixAlphaColor() {
} }
} }
ImageData* add_atlas_margins(ImageData* image, int grid_size) { std::unique_ptr<ImageData> add_atlas_margins(ImageData* image, int grid_size) {
// RGBA is only supported // RGBA is only supported
assert(image->getFormat() == ImageFormat::rgba8888); assert(image->getFormat() == ImageFormat::rgba8888);
assert(image->getWidth() == image->getHeight()); 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<ImageData>(
image->getFormat(), dstwidth, dstheight, std::move(dstdata)
);
} }
+1 -1
View File
@@ -47,6 +47,6 @@ public:
} }
}; };
extern ImageData* add_atlas_margins(ImageData* image, int grid_size); std::unique_ptr<ImageData> add_atlas_margins(ImageData* image, int grid_size);
#endif // GRAPHICS_CORE_IMAGE_DATA_HPP_ #endif // GRAPHICS_CORE_IMAGE_DATA_HPP_
+3 -4
View File
@@ -7,13 +7,12 @@ inline constexpr uint LB_VERTEX_SIZE = (3+4);
LineBatch::LineBatch(size_t capacity) : capacity(capacity) { LineBatch::LineBatch(size_t capacity) : capacity(capacity) {
const vattr attrs[] = { {3},{4}, {0} }; const vattr attrs[] = { {3},{4}, {0} };
buffer = new float[capacity * LB_VERTEX_SIZE * 2]; buffer = std::make_unique<float[]>(capacity * LB_VERTEX_SIZE * 2);
mesh = std::make_unique<Mesh>(buffer, 0, attrs); mesh = std::make_unique<Mesh>(buffer.get(), 0, attrs);
index = 0; index = 0;
} }
LineBatch::~LineBatch(){ LineBatch::~LineBatch(){
delete[] buffer;
} }
void LineBatch::line( 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(){ void LineBatch::render(){
if (index == 0) if (index == 0)
return; return;
mesh->reload(buffer, index / LB_VERTEX_SIZE); mesh->reload(buffer.get(), index / LB_VERTEX_SIZE);
mesh->draw(GL_LINES); mesh->draw(GL_LINES);
index = 0; index = 0;
} }
+1 -1
View File
@@ -9,7 +9,7 @@ class Mesh;
class LineBatch { class LineBatch {
std::unique_ptr<Mesh> mesh; std::unique_ptr<Mesh> mesh;
float* buffer; std::unique_ptr<float[]> buffer;
size_t index; size_t index;
size_t capacity; size_t capacity;
public: public:
+12 -15
View File
@@ -2,7 +2,7 @@
#include <algorithm> #include <algorithm>
inline int getPackerScore(rectangle& rect) { static int get_packer_score(const rectangle& rect) {
if (rect.width * rect.height > 100) if (rect.width * rect.height > 100)
return rect.height * rect.height * 1000; return rect.height * rect.height * 1000;
return (rect.width * rect.height * rect.height); return (rect.width * rect.height * rect.height);
@@ -14,7 +14,7 @@ LMPacker::LMPacker(const uint32_t sizes[], size_t length) {
rects.push_back(rect); rects.push_back(rect);
} }
sort(rects.begin(), rects.end(), [](rectangle a, rectangle b) { 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() { void LMPacker::cleanup() {
if (matrix) { matrix.reset();
for (unsigned int y = 0; y < (height >> mbit); y++) {
delete[] matrix[y];
}
delete[] matrix;
}
placed.clear(); placed.clear();
} }
@@ -43,9 +38,9 @@ bool LMPacker::build(uint32_t width, uint32_t height,
const unsigned int mwidth = width >> mbit; const unsigned int mwidth = width >> mbit;
const unsigned int mheight = height >> mbit; const unsigned int mheight = height >> mbit;
matrix = new rectangle**[mheight]; matrix = std::make_unique<matrix_row[]>(mheight);
for (unsigned int y = 0; y < mheight; y++) { for (unsigned int y = 0; y < mheight; y++) {
matrix[y] = new rectangle*[mwidth]; matrix[y] = std::make_unique<rectangle*[]>(mwidth);
for (unsigned int x = 0; x < mwidth; x++) { for (unsigned int x = 0; x < mwidth; x++) {
matrix[y][x] = nullptr; matrix[y][x] = nullptr;
} }
@@ -85,7 +80,9 @@ bool LMPacker::build(uint32_t width, uint32_t height,
return built; 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 row = y; row < y+h; row++) {
for (int col = x; col < x+w; col++) { for (int col = x; col < x+w; col++) {
rectangle* rect = matrix[row][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; 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 row = y; row < y+h; row++) {
for (int col = x; col < x+w; col++) { for (int col = x; col < x+w; col++) {
matrix[row][col] = rect; 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 mwidth = width >> mbit;
const unsigned int mheight = height >> mbit; const unsigned int mheight = height >> mbit;
for (unsigned int y = 0; y + rh < mheight; y += vstep) { for (unsigned int y = 0; y + rh < mheight; y += vstep) {
rectangle** line = matrix[y]; auto& line = matrix[y];
bool skiplines = true; bool skiplines = true;
rectangle** lower = matrix[y + rh - 1]; auto& lower = matrix[y + rh - 1];
for (unsigned int x = 0; x + rw < mwidth; x++) { for (unsigned int x = 0; x + rw < mwidth; x++) {
rectangle* prect = line[x]; rectangle* prect = line[x];
if (prect) { if (prect) {
@@ -131,7 +128,7 @@ bool LMPacker::place(rectangle* rectptr, uint32_t vstep) {
if (lfree >= rw) if (lfree >= rw)
skiplines = false; skiplines = false;
} }
prect = findCollision(matrix, x, y, rw, rh); prect = find_collision(matrix, x, y, rw, rh);
if (prect) { if (prect) {
x = (prect->x >> mbit) + (prect->width >> mbit) - 1; x = (prect->x >> mbit) + (prect->width >> mbit) - 1;
continue; continue;
+6 -1
View File
@@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
#include <memory>
struct rectangle { struct rectangle {
unsigned int idx; unsigned int idx;
@@ -22,11 +23,15 @@ struct rectangle {
}; };
class LMPacker { class LMPacker {
public:
using matrix_row = std::unique_ptr<rectangle*[]>;
using matrix_ptr = std::unique_ptr<matrix_row[]>;
private:
std::vector<rectangle> rects; std::vector<rectangle> rects;
std::vector<rectangle*> placed; std::vector<rectangle*> placed;
uint32_t width = 0; uint32_t width = 0;
uint32_t height = 0; uint32_t height = 0;
rectangle*** matrix = nullptr; matrix_ptr matrix = nullptr;
uint32_t mbit = 0; uint32_t mbit = 0;
void cleanup(); void cleanup();