2021-03-18 17:27:34 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
|
|
/* Copyright (C) 2021 KikooDX */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "level.h"
|
|
|
|
|
|
|
|
static const int kble_fmt_version = 0;
|
|
|
|
|
2021-03-20 23:14:13 +01:00
|
|
|
static char read_byte(FILE *file);
|
|
|
|
static tile_t read_byte_group(FILE *file, int size);
|
|
|
|
static void write_byte(FILE *file, char byte);
|
|
|
|
static void write_byte_group(FILE *file, tile_t value, int size);
|
2021-03-18 17:27:34 +01:00
|
|
|
|
2021-03-23 17:18:06 +01:00
|
|
|
void level_read(struct Level *level, char *path)
|
|
|
|
{
|
2021-03-18 17:27:34 +01:00
|
|
|
FILE *file = NULL;
|
2021-03-20 23:14:13 +01:00
|
|
|
char byte = 0;
|
|
|
|
unsigned int tile_size = 0;
|
2021-03-18 17:27:34 +01:00
|
|
|
int level_size = 0;
|
|
|
|
int i = 0;
|
2021-03-20 23:14:13 +01:00
|
|
|
tile_t tile = 0;
|
2021-03-18 17:27:34 +01:00
|
|
|
|
|
|
|
/* open file in read mode */
|
|
|
|
file = fopen(path, "rb");
|
|
|
|
if (file == NULL) {
|
2021-03-23 17:18:06 +01:00
|
|
|
fprintf(stderr, "ERROR: cannot open input file %s\n",
|
|
|
|
path);
|
2021-03-18 17:27:34 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check KBLE format version */
|
|
|
|
byte = read_byte(file);
|
|
|
|
if (byte != kble_fmt_version) {
|
2021-03-23 17:18:06 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: KBLE format version doesn't match ; "
|
|
|
|
"expected %d, got %d\n",
|
|
|
|
kble_fmt_version, byte);
|
2021-03-18 17:27:34 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get tile size (in bytes) */
|
|
|
|
tile_size = read_byte(file);
|
2021-03-20 23:14:13 +01:00
|
|
|
/* check than tile size is in boundaries */
|
|
|
|
if (tile_size > sizeof(tile_t) / sizeof(char)) {
|
2021-03-23 17:18:06 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: tile size is too big ; "
|
|
|
|
"maximum is %ld, found %d",
|
|
|
|
sizeof(tile_t), tile_size);
|
|
|
|
exit(EXIT_FAILURE);
|
2021-03-20 23:14:13 +01:00
|
|
|
}
|
2021-03-18 17:27:34 +01:00
|
|
|
/* get width */
|
2021-03-18 18:30:36 +01:00
|
|
|
level->width = read_byte_group(file, 2);
|
2021-03-18 17:27:34 +01:00
|
|
|
/* get height */
|
2021-03-18 18:30:36 +01:00
|
|
|
level->height = read_byte_group(file, 2);
|
2021-03-18 17:27:34 +01:00
|
|
|
|
|
|
|
/* allocate memory for data */
|
2021-03-18 18:30:36 +01:00
|
|
|
level_size = level->width * level->height;
|
2021-03-18 17:27:34 +01:00
|
|
|
if (level->data == NULL) {
|
2021-03-20 23:14:13 +01:00
|
|
|
level->data = malloc(level_size * sizeof(tile_t));
|
2021-03-18 17:27:34 +01:00
|
|
|
} else {
|
2021-03-23 17:18:06 +01:00
|
|
|
level->data =
|
|
|
|
realloc(level->data, level_size * sizeof(tile_t));
|
2021-03-18 17:27:34 +01:00
|
|
|
}
|
|
|
|
/* check for allocation failure */
|
|
|
|
if (level->data == NULL) {
|
|
|
|
fprintf(stderr, "ERROR: memory allocation failure\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read file content */
|
|
|
|
for (i = 0; i < level_size; i += 1) {
|
|
|
|
tile = read_byte_group(file, tile_size);
|
|
|
|
level->data[i] = tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close file */
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
2021-03-23 17:18:06 +01:00
|
|
|
void level_free(struct Level *level) { free(level->data); }
|
2021-03-18 17:27:34 +01:00
|
|
|
|
2021-03-23 17:18:06 +01:00
|
|
|
void level_write(struct Level level, char *path)
|
|
|
|
{
|
2021-03-18 18:30:36 +01:00
|
|
|
FILE *file = NULL;
|
|
|
|
int tile = 0;
|
|
|
|
int tile_size = 0;
|
|
|
|
int max_tile_size = 1;
|
|
|
|
int i = 0;
|
|
|
|
const int level_size = level.width * level.height;
|
|
|
|
|
|
|
|
/* open file in write mode */
|
|
|
|
file = fopen(path, "wb");
|
|
|
|
if (file == NULL) {
|
2021-03-23 17:18:06 +01:00
|
|
|
fprintf(stderr, "ERROR: cannot open output file %s\n",
|
|
|
|
path);
|
2021-03-18 18:30:36 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find longest value in data (in bytes) */
|
|
|
|
for (i = 0; i < level_size; i += 1) {
|
|
|
|
tile = level.data[i];
|
|
|
|
tile_size = 1;
|
|
|
|
while (tile >>= 8)
|
|
|
|
tile_size += 1;
|
|
|
|
if (tile_size > max_tile_size)
|
|
|
|
max_tile_size = tile_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write KBLE format version */
|
|
|
|
write_byte(file, kble_fmt_version);
|
|
|
|
/* write tile size (in bytes) */
|
|
|
|
write_byte(file, max_tile_size);
|
|
|
|
/* write width */
|
|
|
|
write_byte_group(file, level.width, 2);
|
|
|
|
/* write height */
|
|
|
|
write_byte_group(file, level.height, 2);
|
|
|
|
|
|
|
|
/* write level content */
|
|
|
|
for (i = 0; i < level_size; i += 1) {
|
|
|
|
write_byte_group(file, level.data[i], max_tile_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close file */
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read a single byte safely (handle EOF). */
|
2021-03-23 17:18:06 +01:00
|
|
|
static char read_byte(FILE *file)
|
|
|
|
{
|
2021-03-18 18:30:36 +01:00
|
|
|
const int byte = fgetc(file);
|
2021-03-18 17:27:34 +01:00
|
|
|
if (byte == EOF) {
|
|
|
|
fprintf(stderr, "ERROR: unexpected EOF\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-03-20 23:14:13 +01:00
|
|
|
return (char)byte;
|
2021-03-18 17:27:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read multiple bytes and "merge" them into one integer. */
|
2021-03-23 17:18:06 +01:00
|
|
|
static tile_t read_byte_group(FILE *file, int size)
|
|
|
|
{
|
2021-03-20 19:03:26 +01:00
|
|
|
int group = 0;
|
2021-03-23 17:18:06 +01:00
|
|
|
char *byte_ptr = (char *)&group;
|
2021-03-18 17:27:34 +01:00
|
|
|
int i = 0;
|
2021-03-20 19:03:26 +01:00
|
|
|
byte_ptr += size;
|
2021-03-18 18:30:36 +01:00
|
|
|
for (i = 0; i < size; i += 1) {
|
2021-03-23 17:18:06 +01:00
|
|
|
byte_ptr -= 1, *byte_ptr = read_byte(file);
|
2021-03-18 17:27:34 +01:00
|
|
|
}
|
2021-03-20 19:03:26 +01:00
|
|
|
return group;
|
2021-03-18 17:27:34 +01:00
|
|
|
}
|
2021-03-18 18:30:36 +01:00
|
|
|
|
|
|
|
/* Write a single byte safely (handle EOF). */
|
2021-03-23 17:18:06 +01:00
|
|
|
static void write_byte(FILE *file, char byte)
|
|
|
|
{
|
2021-03-18 18:30:36 +01:00
|
|
|
const int result = fputc(byte, file);
|
|
|
|
if (result == EOF) {
|
|
|
|
fprintf(stderr, "ERROR: file write error\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write an integer as multiple bytes. */
|
2021-03-23 17:18:06 +01:00
|
|
|
static void write_byte_group(FILE *file, tile_t value, int size)
|
|
|
|
{
|
|
|
|
char *value_ptr = (char *)&value;
|
2021-03-20 19:03:26 +01:00
|
|
|
value_ptr += size;
|
2021-03-18 18:30:36 +01:00
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < size; i += 1) {
|
2021-03-20 19:03:26 +01:00
|
|
|
value_ptr -= 1;
|
|
|
|
write_byte(file, *value_ptr);
|
2021-03-18 18:30:36 +01:00
|
|
|
}
|
|
|
|
}
|