diff --git a/CMakeLists.txt b/CMakeLists.txt index 62dde38..7720b22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,13 +8,15 @@ include_directories(include) set(SOURCES src/main.c - src/level.c) + src/level.c + src/draw.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) + -Os) add_executable(${PROJECT_NAME} ${SOURCES} ${SOURCES_RAYLIB}) diff --git a/README b/README index 6f2868d..156f95b 100644 --- a/README +++ b/README @@ -15,6 +15,12 @@ $ mkdir sle/build && cd sle/build $ cmake .. $ make +RUN +=== +$ +Example: +$ build/sle assets/tileset.png sample.kble + LICENSE ======= Copyright (C) 2021 KikooDX diff --git a/assets/tileset.png b/assets/tileset.png new file mode 100644 index 0000000..6a05fda Binary files /dev/null and b/assets/tileset.png differ diff --git a/include/conf.h b/include/conf.h index 12e9634..e8631fb 100644 --- a/include/conf.h +++ b/include/conf.h @@ -1,8 +1,11 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ /* Copyright (C) 2021 KikooDX */ - #pragma once static const int window_width = 396; static const int window_height = 224; +static const int draw_offset_x = -2; +static const int draw_offset_y = -8; +static const int tile_width = 16; +static const int tile_height = 16; static const int target_fps = 60; diff --git a/include/draw.h b/include/draw.h new file mode 100644 index 0000000..0fadd68 --- /dev/null +++ b/include/draw.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ +#pragma once + +#include "level.h" +#include + +void level_draw(struct Level level, Texture2D tileset); diff --git a/include/level.h b/include/level.h index 970cdb9..28a9476 100644 --- a/include/level.h +++ b/include/level.h @@ -1,6 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ /* Copyright (C) 2021 KikooDX */ - #pragma once typedef unsigned int tile_t; diff --git a/sample.kble b/sample.kble index 9096459..3c18d3b 100644 Binary files a/sample.kble and b/sample.kble differ diff --git a/src/draw.c b/src/draw.c new file mode 100644 index 0000000..e818fe3 --- /dev/null +++ b/src/draw.c @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ + +#include "conf.h" +#include "level.h" +#include +#include + +void level_draw(struct Level level, Texture2D tileset) +{ + int x; + int y; + for (x = 0; x < level.width; x += 1) { + for (y = 0; y < level.height; y += 1) { + const int tile = + level.data[x + y * level.width]; + Rectangle tile_rect = { + (int)(tile % tileset.width) * tile_width, + (int)(tile / tileset.width) * tile_width, + tile_width, tile_height}; + Vector2 tile_pos = { + x * tile_width + draw_offset_x, + y * tile_height + draw_offset_y}; + if (tile) + DrawTextureRec(tileset, tile_rect, + tile_pos, WHITE); + } + } +} diff --git a/src/level.c b/src/level.c index ea3538f..98c6c6b 100644 --- a/src/level.c +++ b/src/level.c @@ -1,12 +1,11 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ /* Copyright (C) 2021 KikooDX */ +#include "level.h" #include #include #include -#include "level.h" - static const int kble_fmt_version = 0; static char read_byte(FILE *file); diff --git a/src/main.c b/src/main.c index 8555b77..e89a4e3 100644 --- a/src/main.c +++ b/src/main.c @@ -1,23 +1,19 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ /* Copyright (C) 2021 KikooDX */ +#include "conf.h" +#include "draw.h" +#include "level.h" #include #include #include #include -#include "conf.h" -#include "level.h" - 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 arguments, got %d\n", @@ -25,8 +21,25 @@ int main(int argc, char **argv) return EXIT_FAILURE; }; + /* initialize raylib */ + InitWindow(window_width, window_height, "SLE"); + SetTargetFPS(target_fps); + /* load textures */ + const Texture2D tileset = LoadTexture(argv[1]); + assert(tileset.width > 0); + /* load level */ - level_read(&level, argv[1]); + level_read(&level, argv[2]); + + while (!WindowShouldClose()) { + /* draw */ + BeginDrawing(); + + ClearBackground(BLACK); + level_draw(level, tileset); + + EndDrawing(); + } /* save level */ level_write(level, argv[2]); @@ -34,8 +47,6 @@ int main(int argc, char **argv) /* deinit */ level_free(&level); - for (;;) {} - CloseWindow(); return EXIT_SUCCESS;