jtmm2/src/editor.c

128 lines
2.5 KiB
C

#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>
#include <gint/gint.h>
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);
static void editor_save(void);
static void editor_load(void);
void
editor(void)
{
extern volatile int has_ticked;
level_regen_visual_data(1);
do {
editor_draw();
editor_update();
} while (!input_pressed(K_EDITOR));
editor_save();
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);
}
/* load level */
if (input_down(K_PLAYBACK)) editor_load();
/* save level */
if (input_down(K_SAVE_REPLAY)) editor_save();
}
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);
}
static void
editor_save(void)
{
char path[256];
if (filedialog_save(path, sizeof(path))) return;
gint_world_switch(GINT_CALL(level_save_kble, path));
}
static void
editor_load(void)
{
char path[256];
if (filedialog_open(path, sizeof(path))) return;
gint_world_switch(GINT_CALL(level_load_kble, path));
level_load_kble_followup();
}