Forking KBLE C -> SLE

Will be a simple editor with mouse control only.
This commit is contained in:
KikooDX 2021-03-23 17:18:06 +01:00
parent 779b4e94f0
commit 246287b466
11 changed files with 89 additions and 107 deletions

7
.clang-format Normal file
View File

@ -0,0 +1,7 @@
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: AlignWithSpaces
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 72

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ build/
# KBLE (Zig) files
backup_*.kble
sample_*.kble

View File

@ -1,34 +1,23 @@
cmake_minimum_required(VERSION 3.18)
# Add render targets here.
project(kble C)
set(CMAKE_C_COMPILER tcc)
project(sle C)
include_directories(include)
set(SOURCES
src/main.c
src/level.c
)
set(SOURCES_NCURSES
src/ncurses/init.c
)
set(SOURCES_RAYLIB
src/raylib/init.c
)
src/level.c)
set(FLAGS
-Wall -Wextra -Wshadow -Wswitch-default -Wswitch-enum
-Wunreachable-code -Wstrict-prototypes -Wmissing-prototypes
-Wold-style-definition -Werror-implicit-function-declaration
-g -Os
)
-g -Os)
add_executable(kble-ncurses ${SOURCES} ${SOURCES_NCURSES})
add_executable(kble-raylib ${SOURCES} ${SOURCES_RAYLIB})
add_executable(${PROJECT_NAME} ${SOURCES} ${SOURCES_RAYLIB})
target_compile_options(kble-ncurses PRIVATE ${FLAGS} -DNCURSES)
target_compile_options(kble-raylib PRIVATE ${FLAGS} -DRAYLIB)
add_compile_options(PRIVATE ${FLAGS})
target_link_libraries(kble-ncurses ncurses)
target_link_libraries(kble-raylib raylib)
target_link_libraries(${PROJECT_NAME} raylib)

18
README
View File

@ -1,8 +1,20 @@
KBLE C
======
Work in progress C rewrite of KBLE.
SLE
===
Work in progress!
A simple level editor using the KBLE format.
=> https://git.sr.ht/~kikoodx/kble
COMPILATION
===========
Dependencies: raylib
The default compiler is TCC, edit CMakeLists.txt to choose another
compiler.
$ git clone https://git.sr.ht/~kikoodx/sle
$ mkdir sle/build && cd sle/build
$ cmake ..
$ make
LICENSE
=======
Copyright (C) 2021 KikooDX <kikoodx@paranoici.org>

View File

@ -3,8 +3,6 @@
#pragma once
#if defined(RAYLIB)
# define WINDOW_WIDTH 640
# define WINDOW_HEIGHT 480
# define TARGET_FPS 60
#endif
static const int window_width = 396;
static const int window_height = 224;
static const int target_fps = 60;

View File

@ -10,6 +10,6 @@ struct Level {
tile_t *data;
};
void level_read(struct Level*, char *path);
void level_free(struct Level*);
void level_read(struct Level *, char *path);
void level_free(struct Level *);
void level_write(struct Level level, char *path);

View File

@ -1,9 +0,0 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
#pragma once
/* Initialize renderer. */
void renderer_init(void);
/* Deinitialize renderer. */
void renderer_deinit(void);

View File

@ -14,7 +14,8 @@ 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);
void level_read(struct Level *level, char *path) {
void level_read(struct Level *level, char *path)
{
FILE *file = NULL;
char byte = 0;
unsigned int tile_size = 0;
@ -25,15 +26,18 @@ void level_read(struct Level *level, char *path) {
/* open file in read mode */
file = fopen(path, "rb");
if (file == NULL) {
fprintf(stderr, "ERROR: cannot open input file %s\n", path);
fprintf(stderr, "ERROR: cannot open input file %s\n",
path);
exit(EXIT_FAILURE);
}
/* check KBLE format version */
byte = read_byte(file);
if (byte != kble_fmt_version) {
fprintf(stderr, "ERROR: KBLE format version doesn't match ; "
"expected %d, got %d\n", kble_fmt_version, byte);
fprintf(stderr,
"ERROR: KBLE format version doesn't match ; "
"expected %d, got %d\n",
kble_fmt_version, byte);
exit(EXIT_FAILURE);
}
@ -41,9 +45,11 @@ void level_read(struct Level *level, char *path) {
tile_size = read_byte(file);
/* check than tile size is in boundaries */
if (tile_size > sizeof(tile_t) / sizeof(char)) {
fprintf(stderr, "ERROR: tile size is too big ; "
"maximum is %ld, found %d", sizeof(tile_t), tile_size);
exit(EXIT_FAILURE);
fprintf(stderr,
"ERROR: tile size is too big ; "
"maximum is %ld, found %d",
sizeof(tile_t), tile_size);
exit(EXIT_FAILURE);
}
/* get width */
level->width = read_byte_group(file, 2);
@ -55,7 +61,8 @@ void level_read(struct Level *level, char *path) {
if (level->data == NULL) {
level->data = malloc(level_size * sizeof(tile_t));
} else {
level->data = realloc(level->data, level_size * sizeof(tile_t));
level->data =
realloc(level->data, level_size * sizeof(tile_t));
}
/* check for allocation failure */
if (level->data == NULL) {
@ -73,11 +80,10 @@ void level_read(struct Level *level, char *path) {
fclose(file);
}
void level_free(struct Level *level) {
free(level->data);
}
void level_free(struct Level *level) { free(level->data); }
void level_write(struct Level level, char *path) {
void level_write(struct Level level, char *path)
{
FILE *file = NULL;
int tile = 0;
int tile_size = 0;
@ -88,7 +94,8 @@ void level_write(struct Level level, char *path) {
/* open file in write mode */
file = fopen(path, "wb");
if (file == NULL) {
fprintf(stderr, "ERROR: cannot open output file %s\n", path);
fprintf(stderr, "ERROR: cannot open output file %s\n",
path);
exit(EXIT_FAILURE);
}
@ -121,7 +128,8 @@ void level_write(struct Level level, char *path) {
}
/* Read a single byte safely (handle EOF). */
static char read_byte(FILE *file) {
static char read_byte(FILE *file)
{
const int byte = fgetc(file);
if (byte == EOF) {
fprintf(stderr, "ERROR: unexpected EOF\n");
@ -131,20 +139,21 @@ static char read_byte(FILE *file) {
}
/* Read multiple bytes and "merge" them into one integer. */
static tile_t read_byte_group(FILE *file, int size) {
static tile_t read_byte_group(FILE *file, int size)
{
int group = 0;
char *byte_ptr = (char*)&group;
char *byte_ptr = (char *)&group;
int i = 0;
byte_ptr += size;
for (i = 0; i < size; i += 1) {
byte_ptr -= 1,
*byte_ptr = read_byte(file);
byte_ptr -= 1, *byte_ptr = read_byte(file);
}
return group;
}
/* Write a single byte safely (handle EOF). */
static void write_byte(FILE *file, char byte) {
static void write_byte(FILE *file, char byte)
{
const int result = fputc(byte, file);
if (result == EOF) {
fprintf(stderr, "ERROR: file write error\n");
@ -153,8 +162,9 @@ static void write_byte(FILE *file, char byte) {
}
/* Write an integer as multiple bytes. */
static void write_byte_group(FILE *file, tile_t value, int size) {
char *value_ptr = (char*)&value;
static void write_byte_group(FILE *file, tile_t value, int size)
{
char *value_ptr = (char *)&value;
value_ptr += size;
int i = 0;
for (i = 0; i < size; i += 1) {

View File

@ -1,27 +1,42 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
#include <assert.h>
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include "conf.h"
#include "level.h"
#include "renderer/init.h"
int main(int argc, char **argv) {
int main(int argc, char **argv)
{
struct Level level;
level.data = NULL;
/* initialize raylib */
InitWindow(window_width, window_height, "SLE");
SetTargetFPS(target_fps);
/* check for argument count */
if (argc != 3) {
fprintf(stderr, "ERROR: expected 2 argument, got %d\n", argc-1);
fprintf(stderr, "ERROR: expected 2 arguments, got %d\n",
argc - 1);
return EXIT_FAILURE;
}
};
/* load level */
level_read(&level, argv[1]);
renderer_init();
/* save level */
level_write(level, argv[2]);
/* deinit */
level_free(&level);
renderer_deinit();
for (;;) {}
CloseWindow();
return EXIT_SUCCESS;
}

View File

@ -1,24 +0,0 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
#include <locale.h>
#include <ncurses.h>
#include <unistd.h>
#include "renderer/init.h"
void renderer_init(void) {
setlocale(LC_ALL, "");
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
curs_set(TRUE);
}
void renderer_deinit(void) {
endwin();
}

View File

@ -1,17 +0,0 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
#include <raylib.h>
#include "conf.h"
#include "renderer/init.h"
void renderer_init(void) {
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "KBLE");
SetTargetFPS(TARGET_FPS);
}
void renderer_deinit(void) {
CloseWindow();
}