Merge pull request #65 from A-lex-Ra/main

Refactoring and checks
This commit is contained in:
MihailRis 2023-12-18 18:17:24 +03:00 committed by GitHub
commit 9657f04ae8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 68 additions and 41 deletions

View File

@ -63,7 +63,7 @@ bool assetload::atlas(Assets* assets,
if (builder.has(name)) { if (builder.has(name)) {
continue; // skip duplicates continue; // skip duplicates
} }
std::unique_ptr<ImageData> image (png::load_image(file.string())); std::unique_ptr<ImageData> image (png::load_image(file.string()));//but what if load_image return nullptr?
image->fixAlphaColor(); image->fixAlphaColor();
builder.add(name, image.release()); builder.add(name, image.release());
} }

View File

@ -176,6 +176,7 @@ bool load_wav_file_header(std::ifstream& file,
return true; return true;
} }
// after that user must free returned memory by himself!
char* load_wav(const std::string& filename, char* load_wav(const std::string& filename,
std::uint8_t& channels, std::uint8_t& channels,
std::int32_t& sampleRate, std::int32_t& sampleRate,
@ -192,8 +193,13 @@ char* load_wav(const std::string& filename,
} }
char* data = new char[size]; char* data = new char[size];
try {
in.read(data, size); in.read(data, size);
return data; return data;
}
catch (const std::exception&) {
delete[] data;
std::cerr << "ERROR: Could not load wav data of \"" << filename << "\"" << std::endl;
return nullptr;
}
} }

View File

@ -179,6 +179,8 @@ ImageData* _png_load(const char* file){
printf( "Color type %d not supported!\n", printf( "Color type %d not supported!\n",
png_get_color_type( png_ptr, info_ptr ) ); png_get_color_type( png_ptr, info_ptr ) );
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ); png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
delete[] image_data;
fclose(f);
return nullptr; return nullptr;
} }
ImageData* image = new ImageData(format, t_width, t_height, (void*)image_data); ImageData* image = new ImageData(format, t_width, t_height, (void*)image_data);
@ -241,67 +243,83 @@ 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; std::cerr << "could not to open file " << file << std::endl;
return 0; return nullptr;
} }
fseek(png, 0, SEEK_END); fseek(png, 0, SEEK_END);
long siz_pngbuf = ftell(png); long siz_pngbuf = ftell(png);
rewind(png); rewind(png);
if(siz_pngbuf < 1) { if(siz_pngbuf < 1) {
fclose(png);
std::cerr << "could not to read file " << file << std::endl; std::cerr << "could not to read file " << file << std::endl;
return 0; return nullptr;
} }
pngbuf = new char[siz_pngbuf]; pngbuf = new char[siz_pngbuf];
if(fread(pngbuf, siz_pngbuf, 1, png) != 1){ if(fread(pngbuf, siz_pngbuf, 1, png) != 1){
fclose(png);
delete[] pngbuf;
std::cerr << "fread() failed" << std::endl; std::cerr << "fread() failed" << std::endl;
return 0; return nullptr;
} }
fclose(png); // <- finally closing file
ctx = spng_ctx_new(0); ctx = spng_ctx_new(0);
if (ctx == nullptr){ if (ctx == nullptr){
delete[] pngbuf;
std::cerr << "spng_ctx_new() failed" << std::endl; std::cerr << "spng_ctx_new() failed" << std::endl;
return 0; 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){ if (r){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_set_crc_action() error: " << spng_strerror(r) << std::endl; std::cerr << "spng_set_crc_action() error: " << spng_strerror(r) << std::endl;
return 0; return nullptr;
} }
r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf); r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf);
if (r){ if (r){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_set_png_buffer() error: " << spng_strerror(r) << std::endl; std::cerr << "spng_set_png_buffer() error: " << spng_strerror(r) << std::endl;
return 0; return nullptr;
} }
spng_ihdr ihdr; spng_ihdr ihdr;
r = spng_get_ihdr(ctx, &ihdr); r = spng_get_ihdr(ctx, &ihdr);
if (r){ if (r){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_get_ihdr() error: " << spng_strerror(r) << std::endl; std::cerr << "spng_get_ihdr() error: " << spng_strerror(r) << std::endl;
return 0; return nullptr;
} }
//// Unused "something"
const char *clr_type_str; //const char *clr_type_str;
if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE) //if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE)
clr_type_str = "grayscale"; // clr_type_str = "grayscale";
else if(ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR) //else if(ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR)
clr_type_str = "truecolor"; // clr_type_str = "truecolor";
else if(ihdr.color_type == SPNG_COLOR_TYPE_INDEXED) //else if(ihdr.color_type == SPNG_COLOR_TYPE_INDEXED)
clr_type_str = "indexed color"; // clr_type_str = "indexed color";
else if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA) //else if(ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA)
clr_type_str = "grayscale with alpha"; // clr_type_str = "grayscale with alpha";
else //else
clr_type_str = "truecolor with alpha"; // 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){ if (r){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_decoded_image_size() error: " << spng_strerror(r) << std::endl; std::cerr << "spng_decoded_image_size() error: " << spng_strerror(r) << std::endl;
return 0; return nullptr;
} }
out = new unsigned char[out_size]; out = new unsigned char[out_size];
r = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0); r = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0);
if (r){ if (r){
delete[] out;
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_decode_image() error: " << spng_strerror(r) << std::endl; std::cerr << "spng_decode_image() error: " << spng_strerror(r) << std::endl;
return 0; return nullptr;
} }
unsigned char* flipped = new unsigned char[out_size]; unsigned char* flipped = new unsigned char[out_size];
@ -312,27 +330,30 @@ ImageData* _png_load(const char* file){
flipped[(ihdr.height-i-1)*rowsize+j] = out[i*rowsize+j]; flipped[(ihdr.height-i-1)*rowsize+j] = out[i*rowsize+j];
} }
} }
delete[] out; delete[] out; // <- finally delete out
unsigned int texture;
ImageData* image = new ImageData(ImageFormat::rgba8888, ihdr.width, ihdr.height, (void*)flipped); ImageData* image = new ImageData(ImageFormat::rgba8888, ihdr.width, ihdr.height, (void*)flipped);
spng_ctx_free(ctx);
delete[] pngbuf; delete[] pngbuf;
spng_ctx_free(ctx);
return image; return image;
} }
#endif #endif
ImageData* png::load_image(std::string filename) { ImageData* png::load_image(std::string filename) {
return _png_load(filename.c_str()); ImageData* image (_png_load(filename.c_str()));
if (image == nullptr) {
std::cerr << "Could not load image " << filename << std::endl;
return nullptr;
}
return image;
} }
Texture* png::load_texture(std::string filename) { Texture* png::load_texture(std::string filename) {
unique_ptr<ImageData> image (_png_load(filename.c_str())); unique_ptr<ImageData> image (_png_load(filename.c_str()));
if (image == nullptr){ if (image == nullptr){
std::cerr << "Could not load image " << filename << std::endl; std::cerr << "Could not load texture " << filename << std::endl;
return nullptr; return nullptr;
} }
return Texture::from(image.get()); return Texture::from(image.get());

View File

@ -62,6 +62,7 @@ Content::Content(ContentIndices* indices, DrawGroups* drawGroups,
Content::~Content() { Content::~Content() {
delete indices; delete indices;
delete drawGroups;
} }
Block* Content::findBlock(string id) const { Block* Content::findBlock(string id) const {

View File

@ -11,7 +11,7 @@
using glm::vec3; using glm::vec3;
// All in-game definitions (blocks, items, etc..) // All in-game definitions (blocks, items, etc..)
void setup_definitions(ContentBuilder* builder) { void setup_definitions(ContentBuilder* builder) { // Strange function, need to REDO ?
Block* block = new Block("core:air", "air"); Block* block = new Block("core:air", "air");
block->replaceable = true; block->replaceable = true;
block->drawGroup = 1; block->drawGroup = 1;

View File

@ -238,22 +238,18 @@ ubyte* WorldFiles::getData(unordered_map<ivec2, WorldRegion*>& regions,
int localX = x - (regionX * REGION_SIZE); int localX = x - (regionX * REGION_SIZE);
int localZ = z - (regionZ * REGION_SIZE); int localZ = z - (regionZ * REGION_SIZE);
WorldRegion* region = getRegion(regions, regionX, regionZ); WorldRegion* region = getOrCreateRegion(regions, regionX, regionZ);
if (region == nullptr) {
region = new WorldRegion();
regions[ivec2(regionX, regionZ)] = region;
}
ubyte* data = region->get(localX, localZ); ubyte* data = region->get(localX, localZ);
if (data == nullptr) { if (data == nullptr) {
uint32_t size; uint32_t size;
data = readChunkData(x, z, size, data = readChunkData(x, z, size,
folder/getRegionFilename(regionX, regionZ)); folder/getRegionFilename(regionX, regionZ));
if (data) { if (data != nullptr) {
region->put(localX, localZ, data, size); region->put(localX, localZ, data, size);
} }
} }
if (data) { if (data != nullptr) {
return decompress(data, region->getSize(localX, localZ), CHUNK_DATA_LEN); return decompress(data, region->getSize(localX, localZ), CHUNK_DATA_LEN);
} }
return nullptr; return nullptr;
@ -291,6 +287,9 @@ ubyte* WorldFiles::readChunkData(int x, int z, uint32_t& length, path filename){
ubyte* data = new ubyte[length]; ubyte* data = new ubyte[length];
input.read((char*)data, length); input.read((char*)data, length);
input.close(); input.close();
if (data == nullptr) {
std::cerr << "ERROR: failed to read data of chunk x("<< x <<"), z("<< z <<")" << std::endl;
}
return data; return data;
} }

View File

@ -50,7 +50,7 @@ char* files::read_bytes(path filename, size_t& length) {
length = input.tellg(); length = input.tellg();
input.seekg(0, std::ios_base::beg); input.seekg(0, std::ios_base::beg);
unique_ptr<char> data {new char[length]}; unique_ptr<char> data(new char[length]);
input.read(data.get(), length); input.read(data.get(), length);
input.close(); input.close();
return data.release(); return data.release();