~kikoodx/kble-c

84a8594f48366dd129cf46711b9bddef1ec8dcc8 — KikooDX 3 years ago c559481
Use pointers instead of bitshift tricks.
5 files changed, 27 insertions(+), 14 deletions(-)

M include/conf.h
M include/renderer/init.h
M src/level.c
M src/ncurses/init.c
M src/raylib/init.c
M include/conf.h => include/conf.h +3 -0
@@ 1,3 1,6 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */

#pragma once

#if defined(RAYLIB)

M include/renderer/init.h => include/renderer/init.h +3 -0
@@ 1,3 1,6 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */

#pragma once

/* Initialize renderer. */

M src/level.c => src/level.c +15 -14
@@ 1,6 1,7 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


@@ 126,16 127,17 @@ static int read_byte(FILE *file) {

/* Read multiple bytes and "merge" them into one integer. */
static int read_byte_group(FILE *file, int size) {
	int byte = 0;
	int merged = 0;
	int group = 0;
	uint8_t *byte_ptr = (uint8_t*)&group;
	int i = 0;
	int shift = size * 8;
	byte_ptr += size;
	for (i = 0; i < size; i += 1) {
		shift -= 8;
		byte = read_byte(file);
		merged |= byte << shift;
		byte_ptr -= 1;
		*byte_ptr = read_byte(file);
		printf("%d ", *byte_ptr);
	}
	return merged;
	printf("%d\n", group);
	return group;
}

/* Write a single byte safely (handle EOF). */


@@ 149,14 151,13 @@ static void write_byte(FILE *file, unsigned char byte) {

/* Write an integer as multiple bytes. */
static void write_byte_group(FILE *file, int value, int size) {
	int byte = 0;
	int shift = size * 8;
	uint8_t *value_ptr = (uint8_t*)&value;
	value_ptr += size;
	int i = 0;
	const int mask = (1 << 8) - 1;
	for (i = 0; i < size; i += 1) {
		shift -= 8;
		byte = value >> shift;
		byte &= mask;
		write_byte(file, byte);
		value_ptr -= 1;
    		printf("%d ", *value_ptr);
		write_byte(file, *value_ptr);
	}
	printf("\n");
}

M src/ncurses/init.c => src/ncurses/init.c +3 -0
@@ 1,3 1,6 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */

#include <locale.h>
#include <ncurses.h>
#include <unistd.h>

M src/raylib/init.c => src/raylib/init.c +3 -0
@@ 1,3 1,6 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */

#include <raylib.h>

#include "conf.h"