From 27a5a69a5d91aaea2a2d3179c8af23c221cd3c22 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 12 Jan 2022 22:36:10 +0100 Subject: [PATCH] save edited level --- inc/input.h | 4 +++ inc/level.h | 1 + src/editor.c | 12 ++++++++- src/filedialog.c | 4 +++ src/input.c | 18 +++++++++++++ src/level.c | 68 +++++++++++++++++++++++++++++++++++------------- src/main.c | 14 ---------- 7 files changed, 88 insertions(+), 33 deletions(-) diff --git a/inc/input.h b/inc/input.h index d2536e1..abda200 100644 --- a/inc/input.h +++ b/inc/input.h @@ -34,6 +34,10 @@ void input_update(void); void input_dump_replay(void); void input_load_replay(void); +void input_set_down(enum Key); +void input_set_pressed(enum Key); +void input_set_up(enum Key); + int input_down(enum Key); int input_pressed(enum Key); int input_up(enum Key); diff --git a/inc/level.h b/inc/level.h index 90f3ee5..a80be78 100644 --- a/inc/level.h +++ b/inc/level.h @@ -27,6 +27,7 @@ void level_init(struct Player *); void level_deinit(void); void level_load(int id); +void level_save_kble(const char *path); void level_reload(void); void level_regen_visual_data(int editing); void level_next(void); diff --git a/src/editor.c b/src/editor.c index 598ec4b..c118e62 100644 --- a/src/editor.c +++ b/src/editor.c @@ -1,11 +1,11 @@ #include "editor.h" #include "conf.h" +#include "filedialog.h" #include "input.h" #include "level.h" #include "tile.h" #include "vec.h" #include - static int cx, cy; static int ctile = 1; @@ -13,6 +13,7 @@ static void editor_update(void); static void editor_draw(void); static void draw_grid(void); static void draw_cursor(void); +static void editor_save(void); void editor(void) @@ -23,6 +24,7 @@ editor(void) editor_draw(); editor_update(); } while (!input_pressed(K_EDITOR)); + editor_save(); has_ticked = 0; level_regen_visual_data(0); } @@ -98,3 +100,11 @@ draw_cursor(void) (int)(ctile / tileset_width) * TILE_SIZE, TILE_SIZE, TILE_SIZE, 0); } + +static void +editor_save(void) +{ + char path[256]; + if (filedialog_save(path, sizeof(path))) return; + level_save_kble(path); +} diff --git a/src/filedialog.c b/src/filedialog.c index 26bdfe2..414c120 100644 --- a/src/filedialog.c +++ b/src/filedialog.c @@ -1,4 +1,5 @@ #include "filedialog.h" +#include "input.h" #include #include #include @@ -69,6 +70,7 @@ letter_for_key(int key, int alpha) static int do_dialog(char *buf, int n, int allow_new_file) { + extern volatile int has_ticked; char *path = strdup("/"), *entry_name, *selected_name; DIR *dp = NULL; struct dirent *ent, *selected_ent; @@ -173,6 +175,8 @@ do_dialog(char *buf, int n, int allow_new_file) end: free(path); closedir(dp); + has_ticked = 0; + input_set_down(K_EXIT); return rc; } diff --git a/src/input.c b/src/input.c index e345aec..215a30c 100644 --- a/src/input.c +++ b/src/input.c @@ -80,6 +80,24 @@ input_load_replay(void) } } +void +input_set_down(enum Key k) +{ + input.states[k] = KS_DOWN; +} + +void +input_set_pressed(enum Key k) +{ + input.states[k] = KS_PRESS; +} + +void +input_set_up(enum Key k) +{ + input.states[k] = KS_UP; +} + int input_down(enum Key k) { diff --git a/src/level.c b/src/level.c index b0e3a04..0b238f4 100644 --- a/src/level.c +++ b/src/level.c @@ -7,8 +7,11 @@ #include "util.h" #include "vec.h" #include "visual_data.h" +#include #include #include +#include +#include static struct Level level; extern bopti_image_t bimg_tileset; @@ -33,6 +36,7 @@ static const struct LevelBinNamed levels[] = { {NULL, NULL}}; static void level_free(void); +static void load(const struct LevelBin *); void level_init(struct Player *p) @@ -52,28 +56,30 @@ void level_load(int id) { const struct LevelBin *const b = levels[id].bin; - int i = b->width * b->height; - level_free(); - level.width = b->width; - level.height = b->height; - level.size = i; + load(b); level.id = id; - level.data = malloc(i); - level.visual_data = malloc(i * sizeof(struct VisualData)); - while (i-- > 0) - level.data[i] = b->data[i]; - polarity_reset(); - level_regen_visual_data(0); - player_spawn(level.player); +} + +void +level_save_kble(const char *path) +{ + const int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC); + unsigned char data[356] = {0}; + int i; + + data[0] = 0; + data[1] = 1; + data[3] = level.width; + data[5] = level.height; - /* missiles */ - missile_manager_init(); i = level.size; while (i-- > 0) - if (level.data[i] == TILE_MISSILE_LAUNCHER) - missile_new(i % level.width * TILE_SIZE + TILE_SIZE / 2, - (int)(i / level.width) * TILE_SIZE + - TILE_SIZE / 2); + data[i + 6] = level.data[i]; + + if (fd != -1) { + write(fd, data, sizeof(data)); + close(fd); + } } void @@ -191,6 +197,32 @@ level_count(enum Tile t) return c; } +static void +load(const struct LevelBin *b) +{ + int i = b->width * b->height; + level_free(); + level.width = b->width; + level.height = b->height; + level.size = i; + level.data = malloc(i); + level.visual_data = malloc(i * sizeof(struct VisualData)); + while (i-- > 0) + level.data[i] = b->data[i]; + polarity_reset(); + level_regen_visual_data(0); + player_spawn(level.player); + + /* missiles */ + missile_manager_init(); + i = level.size; + while (i-- > 0) + if (level.data[i] == TILE_MISSILE_LAUNCHER) + missile_new(i % level.width * TILE_SIZE + TILE_SIZE / 2, + (int)(i / level.width) * TILE_SIZE + + TILE_SIZE / 2); +} + struct Vec level_dim(void) { diff --git a/src/main.c b/src/main.c index 4cadc72..47bb856 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,5 @@ #include "conf.h" #include "editor.h" -#include "filedialog.h" #include "input.h" #include "level.h" #include "missile.h" @@ -10,7 +9,6 @@ #include #include #include -#include #include static struct Player player; @@ -31,18 +29,6 @@ main(void) init(); - //- - char path[128]; - int rc = filedialog_save(path, 128); - has_ticked = 0; - dclear(C_BLACK); - dprint(1, 1, C_WHITE, "%s (%d)", *path ? path : "(null)", rc); - dupdate(); - getkey(); - input_update(); - input_update(); - //- - level_load(0); for (;;) {