remove unnecessary 'new' operators
This commit is contained in:
parent
26e2068164
commit
fab124a2e2
@ -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){
|
||||
int r = 0;
|
||||
FILE *png = nullptr;
|
||||
char *pngbuf = nullptr;
|
||||
spng_ctx *ctx = nullptr;
|
||||
|
||||
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;
|
||||
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<char[]>(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<ImageData> _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<ImageData> _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<ImageData> _png_load(const char* file){
|
||||
auto out = std::make_unique<ubyte[]>(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<ImageData> _png_load(const char* file){
|
||||
}
|
||||
|
||||
auto image = std::make_unique<ImageData>(ImageFormat::rgba8888, ihdr.width, ihdr.height, std::move(flipped));
|
||||
|
||||
delete[] pngbuf;
|
||||
spng_ctx_free(ctx);
|
||||
|
||||
return image;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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<Batch2D>(1024)) {
|
||||
}
|
||||
|
||||
Screen::~Screen() {
|
||||
|
||||
@ -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
|
||||
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<ImageData>(
|
||||
image->getFormat(), dstwidth, dstheight, std::move(dstdata)
|
||||
);
|
||||
}
|
||||
|
||||
@ -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_
|
||||
|
||||
@ -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<Mesh>(buffer, 0, attrs);
|
||||
buffer = std::make_unique<float[]>(capacity * LB_VERTEX_SIZE * 2);
|
||||
mesh = std::make_unique<Mesh>(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;
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ class Mesh;
|
||||
|
||||
class LineBatch {
|
||||
std::unique_ptr<Mesh> mesh;
|
||||
float* buffer;
|
||||
std::unique_ptr<float[]> 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();
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
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<matrix_row[]>(mheight);
|
||||
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++) {
|
||||
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;
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
struct rectangle {
|
||||
unsigned int idx;
|
||||
@ -22,11 +23,15 @@ struct rectangle {
|
||||
};
|
||||
|
||||
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*> placed;
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
rectangle*** matrix = nullptr;
|
||||
matrix_ptr matrix = nullptr;
|
||||
uint32_t mbit = 0;
|
||||
|
||||
void cleanup();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user