diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..8574fb9 --- /dev/null +++ b/.clang-format @@ -0,0 +1,7 @@ +BasedOnStyle: LLVM +IndentWidth: 8 +UseTab: AlignWithSpaces +BreakBeforeBraces: Linux +AllowShortIfStatementsOnASingleLine: false +IndentCaseLabels: false +ColumnLimit: 72 diff --git a/.gitignore b/.gitignore index 3f0c725..f70a192 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build/ # KBLE (Zig) files backup_*.kble +sample_*.kble diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ea65bf..62dde38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/README b/README index 2c39b8e..6f2868d 100644 --- a/README +++ b/README @@ -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 diff --git a/include/conf.h b/include/conf.h index f6d476e..12e9634 100644 --- a/include/conf.h +++ b/include/conf.h @@ -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; diff --git a/include/level.h b/include/level.h index c5abc00..970cdb9 100644 --- a/include/level.h +++ b/include/level.h @@ -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); diff --git a/include/renderer/init.h b/include/renderer/init.h deleted file mode 100644 index b368bcf..0000000 --- a/include/renderer/init.h +++ /dev/null @@ -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); diff --git a/src/level.c b/src/level.c index 5a42756..ea3538f 100644 --- a/src/level.c +++ b/src/level.c @@ -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) { diff --git a/src/main.c b/src/main.c index 33bb4d5..8555b77 100644 --- a/src/main.c +++ b/src/main.c @@ -1,27 +1,42 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ /* Copyright (C) 2021 KikooDX */ +#include +#include #include #include +#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; } diff --git a/src/ncurses/init.c b/src/ncurses/init.c deleted file mode 100644 index 3904e57..0000000 --- a/src/ncurses/init.c +++ /dev/null @@ -1,24 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* Copyright (C) 2021 KikooDX */ - -#include -#include -#include - -#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(); -} diff --git a/src/raylib/init.c b/src/raylib/init.c deleted file mode 100644 index e26f454..0000000 --- a/src/raylib/init.c +++ /dev/null @@ -1,17 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* Copyright (C) 2021 KikooDX */ - -#include - -#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(); -}