Formatting + renamed typedef tile_t->Tile

This commit is contained in:
KikooDX 2021-03-25 17:06:18 +01:00
parent 80c6531492
commit d97f609245
3 changed files with 25 additions and 19 deletions

View File

@ -2,11 +2,11 @@
/* Copyright (C) 2021 KikooDX */
#pragma once
typedef unsigned int tile_t;
typedef unsigned int Tile;
struct Level {
int width;
int height;
tile_t *data;
Tile *data;
};
/* return -1 if error */

View File

@ -9,9 +9,9 @@
static const int kble_fmt_version = 0;
static int read_byte(FILE *file);
static tile_t read_byte_group(FILE *file, int size);
static Tile read_byte_group(FILE *file, int size);
static int write_byte(FILE *file, char byte);
static int write_byte_group(FILE *file, tile_t value, int size);
static int write_byte_group(FILE *file, Tile value, int size);
int level_read(struct Level *level, char *path)
{
@ -20,7 +20,7 @@ int level_read(struct Level *level, char *path)
unsigned int tile_size = 0;
int level_size = 0;
int i = 0;
tile_t tile = 0;
Tile tile = 0;
/* open file in read mode */
file = fopen(path, "rb");
@ -42,29 +42,32 @@ int level_read(struct Level *level, char *path)
/* get tile size (in bytes) */
tile_size = read_byte(file);
if (tile_size < 0) return -1;
if (tile_size < 0)
return -1;
/* check than tile size is in boundaries */
if (tile_size > sizeof(tile_t) / sizeof(char)) {
if (tile_size > sizeof(Tile) / sizeof(char)) {
fprintf(stderr,
"ERROR: tile size is too big ; "
"maximum is %ld, found %d",
sizeof(tile_t), tile_size);
sizeof(Tile), tile_size);
return -1;
}
/* get width */
level->width = read_byte_group(file, 2);
if (level->width == (tile_t)-1) return -1;
if (level->width == (Tile)-1)
return -1;
/* get height */
level->height = read_byte_group(file, 2);
if (level->height == (tile_t)-1) return -1;
if (level->height == (Tile)-1)
return -1;
/* allocate memory for data */
level_size = level->width * level->height;
if (level->data == NULL) {
level->data = malloc(level_size * sizeof(tile_t));
level->data = malloc(level_size * sizeof(Tile));
} else {
level->data =
realloc(level->data, level_size * sizeof(tile_t));
realloc(level->data, level_size * sizeof(Tile));
}
/* check for allocation failure */
if (level->data == NULL) {
@ -144,8 +147,9 @@ static int read_byte(FILE *file)
return (char)byte;
}
/* Read multiple bytes and "merge" them into one integer. Return -1 if error. */
static tile_t read_byte_group(FILE *file, int size)
/* Read multiple bytes and "merge" them into one integer. Return -1 if
* error. */
static Tile read_byte_group(FILE *file, int size)
{
int group = 0;
char *byte_ptr = (char *)&group;
@ -173,7 +177,7 @@ static int write_byte(FILE *file, char byte)
}
/* Write an integer as multiple bytes. Return -1 if error. */
static int write_byte_group(FILE *file, tile_t value, int size)
static int write_byte_group(FILE *file, Tile value, int size)
{
char *value_ptr = (char *)&value;
value_ptr += size;
@ -182,7 +186,8 @@ static int write_byte_group(FILE *file, tile_t value, int size)
for (i = 0; i < size; i += 1) {
value_ptr -= 1;
error = write_byte(file, *value_ptr);
if (error < 0) return -1;
if (error < 0)
return -1;
}
return 0;
}

View File

@ -5,9 +5,9 @@
#include "mouse.h"
#include "shared_data.h"
#include "tile_picker/draw.h"
#include <stdio.h>
#include <stdbool.h>
#include <raylib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
static void init_mouse(void);
@ -75,5 +75,6 @@ static void update_mouse(int *mouse_x, int *mouse_y,
/* set left button tile */
if (left_click)
shared_data->selected_tile = *mouse_x + *mouse_y * TILESET_WIDTH;
shared_data->selected_tile =
*mouse_x + *mouse_y * TILESET_WIDTH;
}