leaks fix (valgrind full leak check) in world

This commit is contained in:
MihailRis 2024-04-30 03:17:24 +03:00
parent f8289a5d78
commit 3b4fc2dfea
10 changed files with 215 additions and 222 deletions

View File

@ -218,7 +218,7 @@ bool AssetsLoader::loadExternalTexture(
if (fs::exists(path)) { if (fs::exists(path)) {
try { try {
auto image = imageio::read(path.string()); auto image = imageio::read(path.string());
assets->store(Texture::from(image.get()), name); assets->store(Texture::from(image.get()).release(), name);
return true; return true;
} catch (const std::exception& err) { } catch (const std::exception& err) {
logger.error() << "error while loading external " logger.error() << "error while loading external "

View File

@ -41,7 +41,7 @@ assetload::postfunc assetload::texture(
imageio::read(paths->find(filename+".png").u8string()).release() imageio::read(paths->find(filename+".png").u8string()).release()
); );
return [name, image](auto assets) { return [name, image](auto assets) {
assets->store(Texture::from(image.get()), name); assets->store(Texture::from(image.get()).release(), name);
}; };
} }

View File

@ -1,6 +1,6 @@
#include "imageio.hpp" #include "imageio.hpp"
#include "png.h" #include "png.hpp"
#include "../graphics/core/ImageData.hpp" #include "../graphics/core/ImageData.hpp"
#include <filesystem> #include <filesystem>
@ -9,7 +9,7 @@
namespace fs = std::filesystem; namespace fs = std::filesystem;
using image_reader = std::function<ImageData*(const std::string&)>; using image_reader = std::function<std::unique_ptr<ImageData>(const std::string&)>;
using image_writer = std::function<void(const std::string&, const ImageData*)>; using image_writer = std::function<void(const std::string&, const ImageData*)>;
static std::unordered_map<std::string, image_reader> readers { static std::unordered_map<std::string, image_reader> readers {

View File

@ -1,12 +1,14 @@
#include "png.h" #include "png.hpp"
#include <iostream>
#include <memory>
#include <GL/glew.h>
#include "../graphics/core/ImageData.hpp" #include "../graphics/core/ImageData.hpp"
#include "../graphics/core/Texture.hpp" #include "../graphics/core/Texture.hpp"
#include "../files/files.h" #include "../files/files.h"
#include "../debug/Logger.hpp"
#include <iostream>
#include <GL/glew.h>
static debug::Logger logger("png-coder");
#ifndef _WIN32 #ifndef _WIN32
#define LIBPNG #define LIBPNG
@ -22,7 +24,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
// Open file for writing (binary mode) // Open file for writing (binary mode)
FILE* fp = fopen(filename, "wb"); FILE* fp = fopen(filename, "wb");
if (fp == nullptr) { if (fp == nullptr) {
fprintf(stderr, "Could not open file %s for writing\n", filename); logger.error() << "could not open file " << filename << " for writing";
fclose(fp); fclose(fp);
return 1; return 1;
} }
@ -30,16 +32,15 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
// Initialize write structure // Initialize write structure
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (png_ptr == nullptr) { if (png_ptr == nullptr) {
fprintf(stderr, "Could not allocate write struct\n"); logger.error() << "could not allocate write struct";
fclose(fp); fclose(fp);
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
return 1; return 1;
} }
// Initialize info structure // Initialize info structure
png_infop info_ptr = png_create_info_struct(png_ptr); png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == nullptr) { if (info_ptr == nullptr) {
fprintf(stderr, "Could not allocate info struct\n"); logger.error() << "could not allocate info struct";
fclose(fp); fclose(fp);
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
@ -49,10 +50,10 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
// Setup Exception handling // Setup Exception handling
if (setjmp(png_jmpbuf(png_ptr))) { if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stderr, "Error during png creation\n"); logger.error() << "error during png creation";
fclose(fp); fclose(fp);
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); png_destroy_write_struct(&png_ptr, &info_ptr);
return 1; return 1;
} }
@ -69,8 +70,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
png_write_info(png_ptr, info_ptr); png_write_info(png_ptr, info_ptr);
std::unique_ptr<png_byte[]> row(new png_byte[pixsize * width]); auto row = std::make_unique<png_byte[]>(pixsize * width);
// Write image data // Write image data
for (uint y = 0; y < height; y++) { for (uint y = 0; y < height; y++) {
for (uint x = 0; x < width; x++) { for (uint x = 0; x < width; x++) {
@ -86,11 +86,11 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
fclose(fp); fclose(fp);
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr); png_destroy_write_struct(&png_ptr, &info_ptr);
return 0; return 0;
} }
ImageData* _png_load(const char* file){ std::unique_ptr<ImageData> _png_load(const char* file){
FILE* fp = nullptr; FILE* fp = nullptr;
if ((fp = fopen(file, "rb")) == nullptr) { if ((fp = fopen(file, "rb")) == nullptr) {
return nullptr; return nullptr;
@ -175,12 +175,12 @@ ImageData* _png_load(const char* file){
format = ImageFormat::rgb888; format = ImageFormat::rgb888;
break; break;
default: default:
printf("Color type %d not supported!\n", color_type); logger.error() << "color type " << color_type << " is not supported!";
png_destroy_read_struct(&png, &info, &end_info); png_destroy_read_struct(&png, &info, &end_info);
fclose(fp); fclose(fp);
return nullptr; return nullptr;
} }
ImageData* image = new ImageData(format, width, height, (void*)image_data.release()); auto image = std::make_unique<ImageData>(format, width, height, (void*)image_data.release());
png_destroy_read_struct(&png, &info, &end_info); png_destroy_read_struct(&png, &info, &end_info);
fclose(fp); fclose(fp);
return image; return image;
@ -212,8 +212,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
fmt = SPNG_FMT_PNG; fmt = SPNG_FMT_PNG;
ret = spng_encode_image(ctx, data, (size_t)width * (size_t)height * pixsize , fmt, SPNG_ENCODE_FINALIZE); ret = spng_encode_image(ctx, data, (size_t)width * (size_t)height * pixsize , fmt, SPNG_ENCODE_FINALIZE);
if (ret != SPNG_SUCCESS) { if (ret != SPNG_SUCCESS) {
printf("spng_encode_image() error: %s\n", spng_strerror(ret)); logger.error() << "spng_encode_image() error: " << spng_strerror(ret);
fflush(stdout);
spng_ctx_free(ctx); spng_ctx_free(ctx);
return ret; return ret;
} }
@ -222,17 +221,16 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
void* png_buf = spng_get_png_buffer(ctx, &png_size, &ret); void* png_buf = spng_get_png_buffer(ctx, &png_size, &ret);
if (png_buf == nullptr) { if (png_buf == nullptr) {
printf("spng_get_png_buffer() error: %s\n", spng_strerror(ret)); logger.error() << "spng_get_png_buffer() error: " << spng_strerror(ret);
} }
else { else {
files::write_bytes(filename, (const unsigned char*)png_buf, png_size); files::write_bytes(filename, (const unsigned char*)png_buf, png_size);
} }
fflush(stdout);
spng_ctx_free(ctx); spng_ctx_free(ctx);
return ret; return ret;
} }
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; char *pngbuf = nullptr;
@ -241,7 +239,7 @@ ImageData* _png_load(const char* file){
png = fopen(file, "rb"); png = fopen(file, "rb");
if (png == nullptr){ if (png == nullptr){
std::cerr << "could not to open file " << file << std::endl; logger.error() << "could not to open file " << file;
return nullptr; return nullptr;
} }
@ -250,35 +248,35 @@ ImageData* _png_load(const char* file){
rewind(png); rewind(png);
if(siz_pngbuf < 1) { if(siz_pngbuf < 1) {
fclose(png); fclose(png);
std::cerr << "could not to read file " << file << std::endl; logger.error() << "could not to read file " << file;
return nullptr; return nullptr;
} }
pngbuf = new char[siz_pngbuf]; pngbuf = new char[siz_pngbuf];
if(fread(pngbuf, siz_pngbuf, 1, png) != 1){ //check of read elements count if(fread(pngbuf, siz_pngbuf, 1, png) != 1){ //check of read elements count
fclose(png); fclose(png);
delete[] pngbuf; delete[] pngbuf;
std::cerr << "fread() failed" << std::endl; 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; delete[] pngbuf;
std::cerr << "spng_ctx_new() failed" << std::endl; 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; delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
std::cerr << "spng_set_crc_action() error: " << spng_strerror(r) << std::endl; 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; delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
std::cerr << "spng_set_png_buffer() error: " << spng_strerror(r) << std::endl; logger.error() << "spng_set_png_buffer(): " << spng_strerror(r);
return nullptr; return nullptr;
} }
@ -287,28 +285,16 @@ ImageData* _png_load(const char* file){
if (r != SPNG_SUCCESS){ if (r != SPNG_SUCCESS){
delete[] pngbuf; delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
std::cerr << "spng_get_ihdr() error: " << spng_strerror(r) << std::endl; logger.error() << "spng_get_ihdr(): " << spng_strerror(r);
return nullptr; return nullptr;
} }
//// Unused "something"
//const char *clr_type_str;
//if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE)
// clr_type_str = "grayscale";
//else if(ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR)
// clr_type_str = "truecolor";
//else if(ihdr.color_type == SPNG_COLOR_TYPE_INDEXED)
// clr_type_str = "indexed color";
//else if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA)
// clr_type_str = "grayscale with alpha";
//else
// clr_type_str = "truecolor with alpha";
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; delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
std::cerr << "spng_decoded_image_size() error: " << spng_strerror(r) << std::endl; logger.error() << "spng_decoded_image_size(): " << spng_strerror(r);
return nullptr; return nullptr;
} }
out = new unsigned char[out_size]; out = new unsigned char[out_size];
@ -317,7 +303,7 @@ ImageData* _png_load(const char* file){
delete[] out; delete[] out;
delete[] pngbuf; delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
std::cerr << "spng_decode_image() error: " << spng_strerror(r) << std::endl; logger.error() << "spng_decode_image(): " << spng_strerror(r);
return nullptr; return nullptr;
} }
@ -331,7 +317,7 @@ ImageData* _png_load(const char* file){
} }
delete[] out; // <- finally delete out // no, delete spng usage delete[] out; // <- finally delete out // no, delete spng usage
ImageData* image = new ImageData(ImageFormat::rgba8888, ihdr.width, ihdr.height, (void*)flipped); auto image = std::make_unique<ImageData>(ImageFormat::rgba8888, ihdr.width, ihdr.height, (void*)flipped);
delete[] pngbuf; delete[] pngbuf;
spng_ctx_free(ctx); spng_ctx_free(ctx);
@ -340,21 +326,27 @@ ImageData* _png_load(const char* file){
} }
#endif #endif
ImageData* png::load_image(const std::string& filename) { std::unique_ptr<ImageData> png::load_image(const std::string& filename) {
ImageData* image (_png_load(filename.c_str())); auto image = _png_load(filename.c_str());
if (image == nullptr) { if (image == nullptr) {
throw std::runtime_error("could not load image "+filename); throw std::runtime_error("could not load image "+filename);
} }
return image; return image;
} }
Texture* png::load_texture(const std::string& filename) { std::unique_ptr<Texture> png::load_texture(const std::string& filename) {
std::unique_ptr<ImageData> image (load_image(filename)); auto image = load_image(filename);
auto texture = Texture::from(image.get()); auto texture = Texture::from(image.get());
texture->setNearestFilter(); texture->setNearestFilter();
return texture; return texture;
} }
void png::write_image(const std::string& filename, const ImageData* image) { void png::write_image(const std::string& filename, const ImageData* image) {
_png_write(filename.c_str(), image->getWidth(), image->getHeight(), (const ubyte*)image->getData(), image->getFormat() == ImageFormat::rgba8888); _png_write(
filename.c_str(),
image->getWidth(),
image->getHeight(),
(const ubyte*)image->getData(),
image->getFormat() == ImageFormat::rgba8888
);
} }

View File

@ -1,16 +0,0 @@
#ifndef CODERS_PNG_H_
#define CODERS_PNG_H_
#include <string>
#include "../typedefs.h"
class Texture;
class ImageData;
namespace png {
extern ImageData* load_image(const std::string& filename);
extern void write_image(const std::string& filename, const ImageData* image);
extern Texture* load_texture(const std::string& filename);
}
#endif /* CODERS_PNG_H_ */

16
src/coders/png.hpp Normal file
View File

@ -0,0 +1,16 @@
#ifndef CODERS_PNG_HPP_
#define CODERS_PNG_HPP_
#include <memory>
#include <string>
class Texture;
class ImageData;
namespace png {
std::unique_ptr<ImageData> load_image(const std::string& filename);
void write_image(const std::string& filename, const ImageData* image);
std::unique_ptr<Texture> load_texture(const std::string& filename);
}
#endif // CODERS_PNG_HPP_

View File

@ -98,6 +98,7 @@ std::shared_ptr<UINode> create_debug_panel(
// Coord input // Coord input
auto box = std::make_shared<TextBox>(L""); auto box = std::make_shared<TextBox>(L"");
auto boxRef = box.get();
box->setTextSupplier([=]() { box->setTextSupplier([=]() {
Hitbox* hitbox = player->hitbox.get(); Hitbox* hitbox = player->hitbox.get();
return util::to_wstring(hitbox->position[ax], 2); return util::to_wstring(hitbox->position[ax], 2);
@ -113,7 +114,7 @@ std::shared_ptr<UINode> create_debug_panel(
}); });
box->setOnEditStart([=](){ box->setOnEditStart([=](){
Hitbox* hitbox = player->hitbox.get(); Hitbox* hitbox = player->hitbox.get();
box->setText(std::to_wstring(int(hitbox->position[ax]))); boxRef->setText(std::to_wstring(int(hitbox->position[ax])));
}); });
box->setSize(glm::vec2(230, 27)); box->setSize(glm::vec2(230, 27));

View File

@ -22,7 +22,7 @@ Atlas::~Atlas() {
} }
void Atlas::prepare() { void Atlas::prepare() {
texture.reset(Texture::from(image.get())); texture = Texture::from(image.get());
} }
bool Atlas::has(const std::string& name) const { bool Atlas::has(const std::string& name) const {

View File

@ -64,11 +64,11 @@ void Texture::setNearestFilter() {
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
Texture* Texture::from(const ImageData* image) { std::unique_ptr<Texture> Texture::from(const ImageData* image) {
uint width = image->getWidth(); uint width = image->getWidth();
uint height = image->getHeight(); uint height = image->getHeight();
const void* data = image->getData(); const void* data = image->getData();
return new Texture((ubyte*)data, width, height, image->getFormat()); return std::make_unique<Texture>((ubyte*)data, width, height, image->getFormat());
} }
uint Texture::getWidth() const { uint Texture::getWidth() const {

View File

@ -31,7 +31,7 @@ public:
virtual uint getId() const; virtual uint getId() const;
static Texture* from(const ImageData* image); static std::unique_ptr<Texture> from(const ImageData* image);
}; };
#endif // GRAPHICS_CORE_TEXTURE_HPP_ #endif // GRAPHICS_CORE_TEXTURE_HPP_