dungeon drawing
[vrlugburz] / tools / dunger / src / main.c
index 52074ba..83d7fd0 100644 (file)
@@ -1,9 +1,11 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <GL/glut.h>
 #include <utk/cubertk.h>
 #include <drawtext.h>
 #include "level.h"
+#include "lview.h"
 
 static int init(void);
 static void cleanup(void);
@@ -23,16 +25,19 @@ static void utext(int x, int y, const char *txt, int sz);
 static int utextspacing(void);
 static int utextwidth(const char *txt, int sz);
 
+static int parse_args(int argc, char **argv);
+
+
 int win_width, win_height;
 int view_width, view_height;
 float view_panx, view_pany, view_zoom = 1.0f;
 
 static int bnstate[8];
-static int mousex, mousey, clickx, clicky;
+int mousex, mousey, clickx, clicky;
 
 static float uiscale = 1.0f;
 #define UISPLIT        150
-static int splitx;
+int splitx;
 
 #define FONTSZ 16
 static struct dtx_font *uifont;
@@ -44,8 +49,13 @@ static struct level *lvl;
 int main(int argc, char **argv)
 {
        glutInit(&argc, argv);
+
+       if(parse_args(argc, argv) == -1) {
+               return 1;
+       }
+
        glutInitWindowSize(1280, 800);
-       glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
+       glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE);
        glutCreateWindow("dunger");
 
        win_width = glutGet(GLUT_WINDOW_WIDTH);
@@ -73,6 +83,8 @@ static int init(void)
 {
        utk_widget *win;
 
+       glEnable(GL_MULTISAMPLE);
+
        glClearColor(0.15, 0.15, 0.15, 1);
 
        if(!(uifont = dtx_open_font("uifont.ttf", 0))) {
@@ -105,6 +117,9 @@ static int init(void)
                fprintf(stderr, "failed to create level\n");
                return -1;
        }
+       if(init_lview(lvl) == -1) {
+               return -1;
+       }
 
        splitx = UISPLIT * uiscale;
        view_width = win_width - splitx;
@@ -115,6 +130,7 @@ static int init(void)
 
 static void cleanup(void)
 {
+       destroy_lview();
        free_level(lvl);
        dtx_close_font(uifont);
        utk_close(uiroot);
@@ -158,7 +174,7 @@ static void display(void)
        glVertex2f(0, view_height);
        glEnd();
 
-       draw_level(lvl);
+       draw_lview();
 
        glutSwapBuffers();
 }
@@ -171,6 +187,8 @@ static void reshape(int x, int y)
        if(uiroot) {
                utk_set_size(uiroot, x / uiscale, y / uiscale);
        }
+
+       lview_viewport(splitx, 0, x - splitx, y);
 }
 
 static void keyb(unsigned char key, int x, int y)
@@ -208,6 +226,8 @@ static void mouse(int bn, int st, int x, int y)
                if(press) view_zoom -= 0.1;
        }
 
+       lview_mbutton(bidx, press, x, y);
+
        utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
        glutPostRedisplay();
 }
@@ -227,10 +247,43 @@ static void motion(int x, int y)
                }
        }
 
+       lview_mouse(x, y);
+
        utk_mmotion_event(x / uiscale, y / uiscale);
        glutPostRedisplay();
 }
 
+static int parse_args(int argc, char **argv)
+{
+       int i;
+
+       for(i=1; i<argc; i++) {
+               if(argv[i][0] == '-') {
+                       if(strcmp(argv[i], "-uiscale") == 0) {
+                               if(!argv[++i] || !(uiscale = atoi(argv[i]))) {
+                                       fprintf(stderr, "-uiscale should be followed by a positive number\n");
+                                       return -1;
+                               }
+                       } else if(strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "-h") == 0) {
+                               printf("Usage: %s [options]\n", argv[0]);
+                               printf("Options:\n");
+                               printf(" -uiscale <scale>: UI scale factor (default: 1)\n");
+                               printf(" -h,-help: print usage and exit\n");
+                               exit(0);
+                       } else {
+                               fprintf(stderr, "unknown option: %s\n", argv[i]);
+                               return -1;
+                       }
+               } else {
+                       fprintf(stderr, "unexpected argument: %s\n", argv[i]);
+                       return -1;
+               }
+       }
+       return 0;
+}
+
+/* --- ubertk callbacks --- */
+
 static void ucolor(int r, int g, int b, int a)
 {
        glColor4ub(r, g, b, a);