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)) {
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();
builder.add(name, image.release());
}

View File

@ -176,6 +176,7 @@ bool load_wav_file_header(std::ifstream& file,
return true;
}
// after that user must free returned memory by himself!
char* load_wav(const std::string& filename,
std::uint8_t& channels,
std::int32_t& sampleRate,
@ -192,8 +193,13 @@ char* load_wav(const std::string& filename,
}
char* data = new char[size];
in.read(data, size);
return data;
try {
in.read(data, size);
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",
png_get_color_type( png_ptr, info_ptr ) );
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
delete[] image_data;
fclose(f);
return nullptr;
}
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");
if (png == nullptr){
std::cerr << "could not to open file " << file << std::endl;
return 0;
return nullptr;
}
fseek(png, 0, SEEK_END);
long siz_pngbuf = ftell(png);
rewind(png);
if(siz_pngbuf < 1) {
fclose(png);
std::cerr << "could not to read file " << file << std::endl;
return 0;
return nullptr;
}
pngbuf = new char[siz_pngbuf];
if(fread(pngbuf, siz_pngbuf, 1, png) != 1){
fclose(png);
delete[] pngbuf;
std::cerr << "fread() failed" << std::endl;
return 0;
return nullptr;
}
fclose(png); // <- finally closing file
ctx = spng_ctx_new(0);
if (ctx == nullptr){
delete[] pngbuf;
std::cerr << "spng_ctx_new() failed" << std::endl;
return 0;
return nullptr;
}
r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE);
if (r){
delete[] pngbuf;
spng_ctx_free(ctx);
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);
if (r){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_set_png_buffer() error: " << spng_strerror(r) << std::endl;
return 0;
return nullptr;
}
spng_ihdr ihdr;
r = spng_get_ihdr(ctx, &ihdr);
if (r){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_get_ihdr() error: " << spng_strerror(r) << std::endl;
return 0;
return nullptr;
}
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";
//// 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;
r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size);
if (r){
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_decoded_image_size() error: " << spng_strerror(r) << std::endl;
return 0;
return nullptr;
}
out = new unsigned char[out_size];
r = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0);
if (r){
delete[] out;
delete[] pngbuf;
spng_ctx_free(ctx);
std::cerr << "spng_decode_image() error: " << spng_strerror(r) << std::endl;
return 0;
return nullptr;
}
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];
}
}
delete[] out;
unsigned int texture;
delete[] out; // <- finally delete out
ImageData* image = new ImageData(ImageFormat::rgba8888, ihdr.width, ihdr.height, (void*)flipped);
spng_ctx_free(ctx);
delete[] pngbuf;
spng_ctx_free(ctx);
return image;
}
#endif
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) {
unique_ptr<ImageData> image (_png_load(filename.c_str()));
if (image == nullptr){
std::cerr << "Could not load image " << filename << std::endl;
std::cerr << "Could not load texture " << filename << std::endl;
return nullptr;
}
return Texture::from(image.get());

View File

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

View File

@ -11,7 +11,7 @@
using glm::vec3;
// 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->replaceable = true;
block->drawGroup = 1;

View File

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

View File

@ -50,7 +50,7 @@ char* files::read_bytes(path filename, size_t& length) {
length = input.tellg();
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.close();
return data.release();