From ae9d075ed77116dfcebe46c461507e9d323b247d Mon Sep 17 00:00:00 2001 From: KikooDX Date: Sun, 19 Dec 2021 00:30:09 +0100 Subject: [PATCH] half of a level editor --- CMakeLists.txt | 2 + inc/editor.h | 3 ++ inc/input.h | 14 +++++- inc/level.h | 5 ++- inc/tile.h | 1 + res/editor_cursor.png | Bin 0 -> 4994 bytes src/editor.c | 100 ++++++++++++++++++++++++++++++++++++++++++ src/input.c | 3 +- src/level.c | 23 ++++++++-- src/main.c | 6 ++- src/polarity.c | 2 +- 11 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 inc/editor.h create mode 100644 res/editor_cursor.png create mode 100644 src/editor.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 02646a0..9b72d22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,12 +15,14 @@ set(SOURCES src/draw.c src/input.c src/level.c + src/editor.c src/player.c src/polarity.c ) set(ASSETS res/tileset.png + res/editor_cursor.png res/test.kble res/burn.kble res/bounce.kble diff --git a/inc/editor.h b/inc/editor.h new file mode 100644 index 0000000..a37cfa5 --- /dev/null +++ b/inc/editor.h @@ -0,0 +1,3 @@ +#pragma once + +void editor(void); diff --git a/inc/input.h b/inc/input.h index 9cf7c81..e4676e7 100644 --- a/inc/input.h +++ b/inc/input.h @@ -1,6 +1,18 @@ #pragma once -enum Key { K_LEFT, K_RIGHT, K_UP, K_DOWN, K_JUMP, K_POLARITY, K_EXIT, K_COUNT }; +enum Key { + K_LEFT, + K_RIGHT, + K_UP, + K_DOWN, + K_JUMP, + K_POLARITY, + K_EXIT, + K_EDITOR, + K_SCROLL_UP, + K_SCROLL_DOWN, + K_COUNT +}; enum KeyState { KS_UP, KS_DOWN, KS_PRESS }; struct Input { diff --git a/inc/level.h b/inc/level.h index 8c1790d..1777a64 100644 --- a/inc/level.h +++ b/inc/level.h @@ -28,11 +28,14 @@ void level_deinit(void); void level_load(int id); void level_reload(void); -void level_regen_visual_data(void); +void level_regen_visual_data(int editing); void level_next(void); void level_draw(void); +void level_draw_name(void); int level_get(int x, int y); int level_get_px(int x, int y); +void level_set(int x, int y, int v); struct Vec level_find(enum Tile); +struct Vec level_dim(void); diff --git a/inc/tile.h b/inc/tile.h index 74862a6..82c77fa 100644 --- a/inc/tile.h +++ b/inc/tile.h @@ -13,5 +13,6 @@ enum Tile { TILE_GRAV_U, TILE_GRAV_R, TILE_GRAV_L, + TILE_COUNT, TILE_OOB, }; diff --git a/res/editor_cursor.png b/res/editor_cursor.png new file mode 100644 index 0000000000000000000000000000000000000000..6ce82c7f9c0afd4d0bd9ce1b5319be1df02f3ddc GIT binary patch literal 4994 zcmeHKX;c$u7Y@p9Sk$5vL5Fccgd{WBNFtFffkX@-s|yt;Gn0@)7Lq^$U!{m*QK+_R zwIHn(#G@s~cVgNK+wM>zU1Nux24&X+G zj6^d2d@W(+j-L7CVYy3e1_l0S3k^|?5VNGku>RY8gH%|ST z^we?4$-d*%x%c+M&73_C?=q&7^((@cuPA45*M}q{pt2+0eRz8(P)aM@Z zam`x?eo~j0wdzcEN2Svy_E~9u>Q%NdGiFW$wSC`bUp5y@JN5p-_fmdQdHwijTBnhJ zGP~HzSZQ@>@~_l6Q&&uT%OaoOyLa{4o$<;t+tWea;2z1F?%KZ38!^Dm`llonuc~^> z%@x1TynMew`Js(hb7Rx?h4xO9s;}HA!z-`kx846VK2DW2aEy;KJ-r;t5KnFSh)B2wE z77xhQl8UXI2js=#hB&v9IpI^S${5MhMVZaP1(*GQP*v^w`l{{9qR(2tz0r4j)f$8x zZdEEaBw0+a;m&{xxhYi+h3%J0+@{(5avOzLS&^+DRF{i3pmz?GS|9zzvf7z)$+f+b zE84HLzYc!vNLoi8ux+S@ahONn$zhCQ_ z@j(xL;?x~Uc2_ZP;Oyg^hvHR}L*|MGJlDHMw|@|_ampGpeEN-h(Z1rX2JgKeN1xvw zvvg@?P}#h=A6C%iC(7T{wtA}!=Rx^7kaIrmH!~)yA?HU?G4aj*&c5C6l_*!{j^T$toqNp z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iHbD4|%82cS*sW;K zx2rMEOf>$B$5=1^#RvrS>maYh?`yhV)AdRWypr+j>3U7qD>3j&#;>RA|3;Vniw_=L zMf?jg5T7%o{t^Z8DP%1T4-=4fkq(mTu0TR5A+ghlmg`8QNmEUy1*y2il@QwK#S)=S zA9>0&&Lqmpn=C?fSS$#NU3T)`&Tz*Z$W}0Ea3o=7Lh(GSyzIbj)$G=k6nV0Di*U2L zs&weaE!#`3`FHO~FK#8j&+qP<{mqu3D;pP=#9LH2^Sc{K4uZ;uwmEvAio0$QXsMp- zHmhRfZ9#rg)|VU)@RZOi(bn^kO_Om^{H6feT9z?%U13e7XWQaKUE6z?@0ShEu*;oz zYL+KQl$a-~>watZ#-7@ZH+M3S+p#5&s5x`zHadUBtQFMyo$g3#_Bu8a10)1x{Y_Lr N5(^^)Uj?tu{Vxk#bpikY literal 0 HcmV?d00001 diff --git a/src/editor.c b/src/editor.c new file mode 100644 index 0000000..598ec4b --- /dev/null +++ b/src/editor.c @@ -0,0 +1,100 @@ +#include "editor.h" +#include "conf.h" +#include "input.h" +#include "level.h" +#include "tile.h" +#include "vec.h" +#include + +static int cx, cy; +static int ctile = 1; + +static void editor_update(void); +static void editor_draw(void); +static void draw_grid(void); +static void draw_cursor(void); + +void +editor(void) +{ + extern volatile int has_ticked; + level_regen_visual_data(1); + do { + editor_draw(); + editor_update(); + } while (!input_pressed(K_EDITOR)); + has_ticked = 0; + level_regen_visual_data(0); +} + +static void +editor_update(void) +{ + const struct Vec dim = level_dim(); + + input_update(); + + /* move cursor */ + cx += input_pressed(K_RIGHT) - input_pressed(K_LEFT); + cy += input_pressed(K_DOWN) - input_pressed(K_UP); + cx %= dim.x; + cx += dim.x * (cx < 0); + cy %= dim.y; + cy += dim.y * (cy < 0); + + /* select tile */ + ctile += input_pressed(K_SCROLL_UP) - input_pressed(K_SCROLL_DOWN); + ctile += (TILE_COUNT - 1) * (ctile < 1); + ctile %= TILE_COUNT; + ctile += !ctile; + + /* change the world */ + if (input_down(K_JUMP)) { + level_set(cx, cy, ctile); + level_regen_visual_data(1); + } + if (input_down(K_POLARITY)) { + level_set(cx, cy, 0); + level_regen_visual_data(1); + } +} + +static void +editor_draw(void) +{ + dclear(C_BLACK); + level_draw(); + draw_cursor(); + draw_grid(); + dupdate(); +} + +static void +draw_grid(void) +{ + int i; + + /* hlines */ + i = 0; + while (i += TILE_SIZE, i < DHEIGHT) + dhline(i, C_BLUE); + + /* vlines */ + i = DRAW_OFF_X; + while (i += TILE_SIZE, i < DWIDTH) + dvline(i, C_BLUE); +} + +static void +draw_cursor(void) +{ + const int dx = cx * TILE_SIZE + DRAW_OFF_X; + const int dy = cy * TILE_SIZE; + extern bopti_image_t bimg_editor_cursor, bimg_tileset; + const int tileset_width = bimg_tileset.width / TILE_SIZE; + + dimage(dx - TILE_SIZE / 2, dy - TILE_SIZE / 2, &bimg_editor_cursor); + dsubimage(dx, dy, &bimg_tileset, ctile % tileset_width * TILE_SIZE, + (int)(ctile / tileset_width) * TILE_SIZE, TILE_SIZE, + TILE_SIZE, 0); +} diff --git a/src/input.c b/src/input.c index 72f05e6..1846a8c 100644 --- a/src/input.c +++ b/src/input.c @@ -3,7 +3,8 @@ static struct Input input; static const int default_map[K_COUNT] = { - KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_SHIFT, KEY_ALPHA, KEY_EXIT}; + KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_SHIFT, + KEY_ALPHA, KEY_EXIT, KEY_F3, KEY_F2, KEY_F1}; void input_init(void) diff --git a/src/level.c b/src/level.c index 2bc538c..866c3be 100644 --- a/src/level.c +++ b/src/level.c @@ -53,7 +53,7 @@ level_load(int id) while (i-- > 0) level.data[i] = b->data[i]; polarity_reset(); - level_regen_visual_data(); + level_regen_visual_data(0); player_spawn(level.player); } @@ -73,7 +73,7 @@ level_next(void) } void -level_regen_visual_data(void) +level_regen_visual_data(int editing) { const int tileset_width = bimg_tileset.width / TILE_SIZE; int y = level.height; @@ -88,6 +88,7 @@ level_regen_visual_data(void) vd->img_x = tile % tileset_width * TILE_SIZE; vd->img_y = (int)(tile / tileset_width) * TILE_SIZE; vd->visible = tile != 0; + if (editing) continue; switch (tile) { case TILE_RED: vd->img_y += polarity() ? TILE_SIZE : 0; @@ -114,8 +115,11 @@ level_draw(void) 0); } } +} - /* level name */ +void +level_draw_name(void) +{ dprint_opt(DWIDTH - 4, DHEIGHT - 2, C_BLACK, C_NONE, DTEXT_RIGHT, DTEXT_BOTTOM, "%s", levels[level.id].name); } @@ -134,6 +138,13 @@ level_get_px(int x, int y) return level_get(x / TILE_SIZE, y / TILE_SIZE); } +void +level_set(int x, int y, int v) +{ + if (x < 0 || y < 0 || x >= level.width || y >= level.height) return; + level.data[x + y * level.width] = v; +} + struct Vec level_find(enum Tile t) { @@ -143,6 +154,12 @@ level_find(enum Tile t) return (struct Vec){i % level.width, i / level.width}; } +struct Vec +level_dim(void) +{ + return (struct Vec){level.width, level.height}; +} + static void level_free(void) { diff --git a/src/main.c b/src/main.c index 1532b01..81f7588 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,5 @@ #include "conf.h" +#include "editor.h" #include "input.h" #include "level.h" #include "player.h" @@ -8,7 +9,7 @@ static struct Player player; static int timer; -static volatile int has_ticked; +volatile int has_ticked; static void init(void); static void deinit(void); @@ -68,6 +69,8 @@ update(void) { input_update(); player_update(&player); + /* enter editor */ + if (input_pressed(K_EDITOR)) editor(); } static void @@ -76,6 +79,7 @@ draw(void) dclear(C_BLACK); level_draw(); player_draw(&player); + level_draw_name(); dupdate(); } diff --git a/src/polarity.c b/src/polarity.c index f24905e..37a2eaf 100644 --- a/src/polarity.c +++ b/src/polarity.c @@ -13,7 +13,7 @@ void polarity_invert(void) { pol = !pol; - level_regen_visual_data(); + level_regen_visual_data(0); } void