mirror of https://git.sr.ht/~kikoodx/kble-c
Use pointers instead of bitshift tricks.
This commit is contained in:
parent
c559481af9
commit
84a8594f48
|
@ -1,3 +1,6 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/* Copyright (C) 2021 KikooDX */
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined(RAYLIB)
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/* Copyright (C) 2021 KikooDX */
|
||||
|
||||
#pragma once
|
||||
|
||||
/* Initialize renderer. */
|
||||
|
|
29
src/level.c
29
src/level.c
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||
/* Copyright (C) 2021 KikooDX */
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
#include "conf.h"
|
||||
|
|
Loading…
Reference in New Issue