Load compressed map data.

This commit is contained in:
Steve 2018-02-08 08:07:04 +00:00
parent 14f4ef57ad
commit dc5aead758
5 changed files with 41 additions and 3 deletions

View File

@ -24,7 +24,7 @@ CXXFLAGS += -Wall -Wempty-body -ansi -pedantic -Werror -Wstrict-prototypes -Werr
CXXFLAGS += -g -lefence
CXXFLAGS += -fms-extensions -std=gnu11
LDFLAGS += `sdl2-config --libs` -lSDL2_mixer -lSDL2_image -lSDL2_ttf -lm
LDFLAGS += `sdl2-config --libs` -lSDL2_mixer -lSDL2_image -lSDL2_ttf -lm -lz
SHARED_FILES = CHANGELOG LICENSE README.md data gfx manual music sound icons
DIST_FILES = $(SHARED_FILES) locale $(PROG)

View File

@ -74,6 +74,42 @@ char *readFile(const char *filename)
return buffer;
}
char *readCompressedFile(const char *filename)
{
unsigned char *buffer, *cBuffer;
unsigned long length, cLength;
FILE *file = fopen(getFileLocation(filename), "rb");
buffer = 0;
cBuffer = 0;
if (file)
{
fread(&length, sizeof(unsigned long), 1, file);
fread(&cLength, sizeof(unsigned long), 1, file);
buffer = malloc(length + 1);
memset(buffer, 0, length + 1);
cBuffer = malloc(cLength + 1);
memset(cBuffer, 0, cLength + 1);
fread(cBuffer, 1, cLength, file);
uncompress(buffer, &length, cBuffer, cLength);
fclose(file);
buffer[length] = '\0';
free(cBuffer);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Decompressed '%s' %ld -> %ld", filename, cLength, length);
}
return (char*) buffer;
}
int writeFile(const char *filename, const char *data)
{
FILE *file = fopen(filename, "wb");

View File

@ -22,5 +22,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "sys/stat.h"
#include "dirent.h"
#include <zlib.h>
extern App app;

View File

@ -245,11 +245,11 @@ static void loadMapData(void)
char *data, *p;
int i, x, y;
sprintf(filename, "data/maps/raw/%s.raw", world.id);
sprintf(filename, "data/maps/raw/%s.raw.z", world.id);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);
data = readFile(filename);
data = readCompressedFile(filename);
p = data;

View File

@ -26,6 +26,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
extern float limit(float i, float a, float b);
extern int rrnd(int low, int high);
extern char *readFile(const char *filename);
extern char *readCompressedFile(const char *filename);
extern Atlas *getImageFromAtlas(char *filename);
extern Texture *getTexture(const char *filename);
extern void blitRect(SDL_Texture *texture, int x, int y, SDL_Rect *srcRect, int center);