From 76c3689b01d18e5949e91d0658deb278714f882b Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Thu, 16 Sep 2021 22:10:14 +0300 Subject: [PATCH] fixed dunger build, added current cell coordinates readout --- .gitignore | 1 + src/level.c | 15 +++++++++++++++ tools/dunger/Makefile | 7 +++++-- tools/dunger/src/lview.c | 16 ++++++++++++++++ tools/dunger/src/main.c | 22 +++++++++++++++++----- 5 files changed, 54 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 7122e49..f21f44f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ tools/dunger/dunger data/ datasrc/ game +tools/dunger/src/level.c diff --git a/src/level.c b/src/level.c index 6f265c3..566b1c3 100644 --- a/src/level.c +++ b/src/level.c @@ -10,6 +10,8 @@ static int load_tileset(struct level *lvl, struct ts_node *tsn); int init_level(struct level *lvl, int xsz, int ysz) { + memset(lvl, 0, sizeof *lvl); + if(!(lvl->cells = calloc(xsz * ysz, sizeof *lvl->cells))) { free(lvl); return -1; @@ -37,7 +39,9 @@ int load_level(struct level *lvl, const char *fname) lvl->fname = strdup(fname); if((lvl->dirname = malloc(strlen(fname) + 1))) { +#ifndef LEVEL_EDITOR path_dir(lvl->fname, lvl->dirname); +#endif } if(!(ts = ts_load(fname))) { @@ -192,6 +196,8 @@ err: return -1; } +#ifndef LEVEL_EDITOR + static int load_tileset(struct level *lvl, struct ts_node *tsn) { static const char *tile_types[] = {"empty", "straight", "corner", "door", 0}; @@ -332,3 +338,12 @@ int gen_level_geom(struct level *lvl) } return 0; } + +#else + +static int load_tileset(struct level *lvl, struct ts_node *tsn) +{ + return 0; /* in the level editor we don't need tileset loading */ +} + +#endif /* !LEVEL_EDITOR */ diff --git a/tools/dunger/Makefile b/tools/dunger/Makefile index c2d2333..81ff599 100644 --- a/tools/dunger/Makefile +++ b/tools/dunger/Makefile @@ -1,14 +1,17 @@ -src = $(wildcard src/*.c) ../../src/level.c +src = src/main.c src/lview.c src/level.c obj = $(src:.c=.o) dep = $(src:.c=.d) bin = dunger -CFLAGS = -pedantic -Wall -g +CFLAGS = -pedantic -Wall -g -DLEVEL_EDITOR -I../../src LDFLAGS = -lGL -lGLU -lglut -lutk -ldrawtext -ltreestore $(bin): $(obj) $(CC) -o $@ $(obj) $(LDFLAGS) +src/level.c: ../../src/level.c + cp $< $@ + -include $(dep) .PHONY: clean diff --git a/tools/dunger/src/lview.c b/tools/dunger/src/lview.c index 8e0ee27..e0001fe 100644 --- a/tools/dunger/src/lview.c +++ b/tools/dunger/src/lview.c @@ -1,4 +1,5 @@ #include +#include #include "lview.h" static struct level *lvl; @@ -129,6 +130,21 @@ void draw_lview(void) } glEnd(); + if(sel) { + int cidx = sel - lvl->cells; + int row = cidx / lvl->width; + int col = cidx % lvl->width; + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glTranslatef(10, 10, 0); + + glColor3f(1, 1, 1); + dtx_printf("(%d, %d)", col, row); + dtx_flush(); + + glPopMatrix(); + } } void cell_to_pos(int cx, int cy, float *px, float *py) diff --git a/tools/dunger/src/main.c b/tools/dunger/src/main.c index 5a23d71..ec5a827 100644 --- a/tools/dunger/src/main.c +++ b/tools/dunger/src/main.c @@ -57,6 +57,8 @@ static utk_widget *cbox_newsz; static struct level lvl; +static const char *opt_fname; + int main(int argc, char **argv) { @@ -141,9 +143,16 @@ static int init(void) utk_set_size(uiwin_new, utk_get_width(vbox) + pad * 2.0f, utk_get_height(vbox) + pad * 2.0f); - if(init_level(&lvl, 32, 32) == -1) { - fprintf(stderr, "failed to create level\n"); - return -1; + if(opt_fname) { + if(load_level(&lvl, opt_fname) == -1) { + fprintf(stderr, "failed to load level: %s\n", opt_fname); + return -1; + } + } else { + if(init_level(&lvl, 32, 32) == -1) { + fprintf(stderr, "failed to create level\n"); + return -1; + } } if(init_lview(&lvl) == -1) { return -1; @@ -420,8 +429,11 @@ static int parse_args(int argc, char **argv) return -1; } } else { - fprintf(stderr, "unexpected argument: %s\n", argv[i]); - return -1; + if(opt_fname) { + fprintf(stderr, "unexpected argument: %s\n", argv[i]); + return -1; + } + opt_fname = argv[i]; } } return 0; -- 1.7.10.4