Skip to content

Commit

Permalink
add compression for terrain files
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgrc0 committed Mar 21, 2024
1 parent 28d4d0d commit f183f85
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#define WISPY_LINUX

#include <pthread.h>
#include <sys/stat.h>
#include <unistd.h>

#elif defined(__ANDROID__)
Expand Down
77 changes: 58 additions & 19 deletions src/terrain/terrain_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ char *get_terrain_path_folder() {
path[0] = 0;

#if defined(WISPY_ANDROID)
strcat(path, GetAndroidApp()->activity->internalDataPath);
strcat(path, "/");
strcat(path, GetAndroidApp()->activity->internalDataPath);
strcat(path, "/");
#else
strcat(path, GetApplicationDirectory());
strcat(path, GetApplicationDirectory());
#endif
strcat(path, TERRAIN_FOLDER);
return path;
Expand All @@ -32,7 +32,7 @@ char *get_terrain_path_chunk(unsigned int position) {
return NULL;
}

char position_str[12]; // log10(2^32) + 1 + 1
char position_str[12]; // log10(2^32) + 1 (path) + 1 (end-of-string)
sprintf(position_str, "/%u", position);
strcat(path, position_str);

Expand All @@ -49,22 +49,36 @@ bool read_chunk_file(unsigned int position, w_block *blocks) {
return false;
}

int size = 0;
char *data = LoadFileData(chunk_path, &size);

if (size != CHUNK_W * CHUNK_H) {
UnloadFileData(data);
int in_size = 0;
char *in_buffer = LoadFileData(chunk_path, &in_size);
if (in_buffer == NULL) {
free(chunk_path);
return false;
}
size_t out_size = CHUNK_W * CHUNK_H;
char *out_buffer = malloc(out_size);
if (out_buffer == NULL) {
UnloadFileData(in_buffer);
free(chunk_path);
return false;
}
if (uncompress((Bytef *)out_buffer, (uLongf *)&out_size,
(const Bytef *)in_buffer, (uLong)in_size) != Z_OK) {
UnloadFileData(in_buffer);
free(out_buffer);
free(chunk_path);
return false;
}

for (unsigned int i = 0; i < CHUNK_W * CHUNK_H; i++) {
blocks[i] = (w_block){
.is_background = BLOCK_IS_BACKGROUND(data[i]),
.type = BLOCK_TYPE(data[i]),
.is_background = BLOCK_IS_BACKGROUND(out_buffer[i]),
.type = BLOCK_TYPE(out_buffer[i]),
};
}

UnloadFileData(data);
UnloadFileData(in_buffer);
free(out_buffer);
free(chunk_path);
return true;
}
Expand All @@ -75,23 +89,48 @@ bool write_chunk_file(unsigned int position, w_block *blocks) {
return false;
}

char *data = malloc(CHUNK_W * CHUNK_H);
if (data == NULL) {
size_t in_size = CHUNK_W * CHUNK_H;
char *in_buffer = malloc(in_size);
if (in_buffer == NULL) {
free(chunk_path);
return false;
}

for (unsigned int i = 0; i < CHUNK_W * CHUNK_H; i++) {
size_t out_size = in_size;
char *out_buffer = malloc(out_size);
if (out_buffer == NULL) {
free(chunk_path);
free(in_buffer);
return false;
}

for (unsigned int i = 0; i < in_size; i++) {

if (blocks[i].is_background) {
data[i] = BLOCK_SET_BACKGROUND(data[i]);
in_buffer[i] = BLOCK_SET_BACKGROUND(in_buffer[i]);
} else {
data[i] = blocks[i].type;
in_buffer[i] = blocks[i].type;
}
}

SaveFileData(chunk_path, data, CHUNK_W * CHUNK_H);
if (compress((Bytef *)out_buffer, (uLongf *)&out_size,
(const Bytef *)in_buffer, (uLong)in_size) != Z_OK) {
free(chunk_path);
free(in_buffer);
return false;
}

void *tmp = realloc(out_buffer, out_size);
if (tmp == NULL) {
free(chunk_path);
free(in_buffer);
free(out_buffer);
return false;
}
out_buffer = tmp;

SaveFileData(chunk_path, out_buffer, (int)out_size);
free(chunk_path);
free(data);
free(in_buffer);
return true;
}
1 change: 1 addition & 0 deletions src/unpack/unpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../stdafx.h"

#define MAX_UNCOMPRESSE_TRY 4

#if defined(WISPY_WINDOWS)
#define RESOURCE_NAME "ASSETS_PACK"
#elif defined(WISPY_LINUX)
Expand Down

0 comments on commit f183f85

Please sign in to comment.