X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=tools%2Fdunger%2Fsrc%2Fmain.c;h=5a23d71bae879301eec304096250941fd33d329f;hb=f242719e98650720b5d3ed43aca62a4f1fe3ec10;hp=e6f6cb2010e06efa8007eaecfe4dd5297d31cf80;hpb=5b980d5096befa9b3b0ab4732de2f319729cd563;p=vrlugburz diff --git a/tools/dunger/src/main.c b/tools/dunger/src/main.c index e6f6cb2..5a23d71 100644 --- a/tools/dunger/src/main.c +++ b/tools/dunger/src/main.c @@ -17,11 +17,13 @@ static void mouse(int bn, int st, int x, int y); static void motion(int x, int y); static void cb_new(utk_event *ev, void *data); +static void cb_new_ok(utk_event *ev, void *data); static void cb_open(utk_event *ev, void *data); +static void cb_open_ok(utk_event *ev, void *data); static void cb_save(utk_event *ev, void *data); +static void cb_save_ok(utk_event *ev, void *data); static void cb_cancel(utk_event *ev, void *data); -static void cb_new_ok(utk_event *ev, void *data); static int parse_args(int argc, char **argv); @@ -45,7 +47,7 @@ int mousex, mousey, clickx, clicky; static float uiscale = 1.0f; #define UISPLIT 150 -int splitx; +static int splitx; #define FONTSZ 16 static struct dtx_font *uifont; @@ -53,7 +55,7 @@ static utk_widget *uiroot, *uiwin_new; static utk_widget *uigrab; static utk_widget *cbox_newsz; -static struct level *lvl; +static struct level lvl; int main(int argc, char **argv) @@ -139,11 +141,11 @@ static int init(void) utk_set_size(uiwin_new, utk_get_width(vbox) + pad * 2.0f, utk_get_height(vbox) + pad * 2.0f); - if(!(lvl = create_level(32, 32))) { + if(init_level(&lvl, 32, 32) == -1) { fprintf(stderr, "failed to create level\n"); return -1; } - if(init_lview(lvl) == -1) { + if(init_lview(&lvl) == -1) { return -1; } @@ -157,7 +159,7 @@ static int init(void) static void cleanup(void) { destroy_lview(); - free_level(lvl); + destroy_level(&lvl); dtx_close_font(uifont); utk_close(uiroot); } @@ -200,7 +202,11 @@ static void display(void) glVertex2f(splitx, win_height); glVertex2f(0, win_height); glEnd(); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); utk_draw(uiroot); + glDisable(GL_BLEND); glutSwapBuffers(); @@ -223,7 +229,11 @@ static void keyb(unsigned char key, int x, int y) switch(key) { case 27: if(uigrab) { - utk_hide(uigrab); + if(utk_is_dialog(uigrab)) { + utk_destroy_window(uigrab); + } else { + utk_hide(uigrab); + } uigrab = 0; } else { exit(0); @@ -306,21 +316,21 @@ static void cb_new_ok(utk_event *ev, void *data) { static int levsz[] = {16, 24, 32}; int sz; - struct level *newlvl; + struct level newlvl; sz = levsz[utk_get_selected(cbox_newsz)]; - if(!(newlvl = create_level(sz, sz))) { - fprintf(stderr, "failed to create new %dx%d level\n", sz, sz); - /* TODO: messagebox */ + if(init_level(&newlvl, sz, sz) == -1) { + utk_message_dialog("failed to create new level", UTK_MSG_TYPE_ERROR, + UTK_MSG_BN_OK, cb_cancel, 0); return; } - free_level(lvl); + destroy_level(&lvl); destroy_lview(); lvl = newlvl; - init_lview(newlvl); + init_lview(&lvl); utk_hide(uiwin_new); uigrab = 0; @@ -328,15 +338,63 @@ static void cb_new_ok(utk_event *ev, void *data) static void cb_open(utk_event *ev, void *data) { + uigrab = utk_file_dialog(UTK_FILE_DIALOG_OPEN, 0, "Level file (*.lvl) [.lvl]", 0, cb_open_ok, 0); +} + +static void cb_open_ok(utk_event *ev, void *data) +{ + char *errmsg = 0; + const char *path; + + utk_widget *dlg = utk_event_widget(ev); + path = utk_file_dialog_path(dlg); + printf("selected: %s\n", path); + if(load_level(&lvl, path) == -1) { + errmsg = alloca(strlen(path) + 32); + sprintf(errmsg, "Failed to load level: %s", path); + } + utk_destroy_window(dlg); + if(uigrab == dlg) uigrab = 0; + + if(errmsg) { + uigrab = utk_message_dialog(errmsg, UTK_MSG_TYPE_ERROR, UTK_MSG_BN_OK, cb_cancel, 0); + } } static void cb_save(utk_event *ev, void *data) { + uigrab = utk_file_dialog(UTK_FILE_DIALOG_SAVE, 0, "Level file (*.lvl) [.lvl]", 0, cb_save_ok, 0); +} + +static void cb_save_ok(utk_event *ev, void *data) +{ + char *errmsg = 0; + const char *path; + + utk_widget *dlg = utk_event_widget(ev); + path = utk_file_dialog_path(dlg); + printf("selected: %s\n", path); + if(save_level(&lvl, path) == -1) { + errmsg = alloca(strlen(path) + 32); + sprintf(errmsg, "Failed to save level file: %s", path); + } + utk_destroy_window(dlg); + if(uigrab == dlg) uigrab = 0; + + if(errmsg) { + uigrab = utk_message_dialog(errmsg, UTK_MSG_TYPE_ERROR, UTK_MSG_BN_OK, cb_cancel, 0); + } } static void cb_cancel(utk_event *ev, void *data) { - utk_hide(data); + if(!data) data = utk_get_window(utk_event_widget(ev)); + + if(utk_is_dialog(data)) { + utk_destroy_window(data); + } else { + utk_hide(data); + } uigrab = 0; }