save edited level

This commit is contained in:
KikooDX 2022-01-12 22:36:10 +01:00
parent b525fd7e89
commit 27a5a69a5d
7 changed files with 88 additions and 33 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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 <gint/display.h>
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);
}

View File

@ -1,4 +1,5 @@
#include "filedialog.h"
#include "input.h"
#include <dirent.h>
#include <gint/display.h>
#include <gint/gint.h>
@ -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;
}

View File

@ -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)
{

View File

@ -7,8 +7,11 @@
#include "util.h"
#include "vec.h"
#include "visual_data.h"
#include <fcntl.h>
#include <gint/display.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
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)
{

View File

@ -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 <gint/cpu.h>
#include <gint/display.h>
#include <gint/gint.h>
#include <gint/keyboard.h>
#include <gint/timer.h>
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 (;;) {