cleaned up the level view code
[vrlugburz] / tools / dunger / src / main.c
index 4c0fe7f..fbb24c4 100644 (file)
@@ -1,8 +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);
@@ -22,19 +25,37 @@ 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 win_width, win_height;
+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];
+int mousex, mousey, clickx, clicky;
+
 static float uiscale = 1.0f;
+#define UISPLIT        150
+int splitx;
 
 #define FONTSZ 16
 static struct dtx_font *uifont;
 static utk_widget *uiroot;
 
+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);
@@ -62,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))) {
@@ -84,14 +107,31 @@ static int init(void)
        utk_set_text_spacing_func(utextspacing);
        utk_set_text_width_func(utextwidth);
 
-       win = utk_window(uiroot, 20, 20, 100, 100, "window");
-       utk_show(win);
+       win = utk_vbox(uiroot, 0, UTK_DEF_SPACING);
+       utk_set_pos(win, 15, 15);
+       utk_button(win, "hello", 0, 0, 0, 0);
+       utk_button(win, "button 2", 0, 0, 0, 0);
+       utk_button(win, "button 3", 0, 0, 0, 0);
+
+       if(!(lvl = create_level(32, 32))) {
+               fprintf(stderr, "failed to create level\n");
+               return -1;
+       }
+       if(init_lview(lvl) == -1) {
+               return -1;
+       }
+
+       splitx = UISPLIT * uiscale;
+       view_width = win_width - splitx;
+       view_height = win_height;
 
        return 0;
 }
 
 static void cleanup(void)
 {
+       destroy_lview();
+       free_level(lvl);
        dtx_close_font(uifont);
        utk_close(uiroot);
 }
@@ -103,8 +143,39 @@ static void display(void)
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
 
+       /* draw UI */
+
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity();
+       glOrtho(0, splitx, win_height, 0, -1, 1);
+       glViewport(0, 0, splitx, win_height);
+
+       glBegin(GL_QUADS);
+       glColor3f(0.25, 0.25, 0.25);
+       glVertex2f(0, 0);
+       glVertex2f(splitx, 0);
+       glVertex2f(splitx, win_height);
+       glVertex2f(0, win_height);
+       glEnd();
        utk_draw(uiroot);
 
+       /* draw view */
+
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity();
+       glOrtho(0, view_width, 0, view_height, -1, 1);
+       glViewport(splitx, 0, view_width, view_height);
+
+       glBegin(GL_QUADS);
+       glColor3f(0.1, 0.1, 0.1);
+       glVertex2f(0, 0);
+       glVertex2f(view_width, 0);
+       glVertex2f(view_width, view_height);
+       glVertex2f(0, view_height);
+       glEnd();
+
+       draw_lview();
+
        glutSwapBuffers();
 }
 
@@ -113,14 +184,11 @@ static void reshape(int x, int y)
        win_width = x;
        win_height = y;
 
-       glViewport(0, 0, x, y);
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       glOrtho(0, x, y, 0, -1, 1);
-
        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)
@@ -141,16 +209,79 @@ static void mouse(int bn, int st, int x, int y)
        int bidx = bn - GLUT_LEFT_BUTTON;
        int press = st == GLUT_DOWN;
 
+       bnstate[bidx] = press;
+       mousex = x;
+       mousey = y;
+
+       if(bn <= 2) {
+               if(press) {
+                       clickx = x;
+                       clicky = y;
+               } else {
+                       clickx = clicky = -1;
+               }
+       } else if(bn == 3) {
+               if(press) view_zoom += 0.1;
+       } else if(bn == 4) {
+               if(press) view_zoom -= 0.1;
+       }
+
        utk_mbutton_event(bidx, press, x / uiscale, y / uiscale);
        glutPostRedisplay();
 }
 
 static void motion(int x, int y)
 {
+       int dx, dy;
+       dx = x - mousex;
+       dy = y - mousey;
+       mousex = x;
+       mousey = y;
+
+       if(clickx >= splitx) {
+               if(bnstate[1]) {
+                       view_panx -= dx;
+                       view_pany += dy;
+               }
+       }
+
+       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);