better grid drawing
authorJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 18 Aug 2021 04:05:41 +0000 (07:05 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 18 Aug 2021 04:05:41 +0000 (07:05 +0300)
tools/dunger/src/level.c
tools/dunger/src/level.h
tools/dunger/src/main.c

index f61f3f6..6fb5706 100644 (file)
@@ -5,6 +5,8 @@
 #include "level.h"
 
 extern int view_width, view_height;
+extern float view_panx, view_pany, view_zoom;
+
 
 struct level *create_level(int xsz, int ysz)
 {
@@ -29,15 +31,35 @@ void free_level(struct level *lvl)
        free(lvl);
 }
 
-static void draw_cell(float x, float y, float sz, struct cell *cell)
+#define LTHICK 0.5f
+static void draw_cell(struct level *lvl, struct cell *cell)
 {
+       int cidx, row, col;
+       float x, y, xsz, ysz, sz;
        static const float colors[][3] = {{0, 0, 0}, {0.6, 0.6, 0.6}, {0.4, 0.2, 0.1}};
 
-       if(cell) {
-               glColor3fv(colors[cell->type]);
-       } else {
-               glColor3f(1, 1, 1);
-       }
+       xsz = view_zoom * view_width / lvl->width;
+       ysz = view_zoom * view_height / lvl->height;
+       sz = xsz > ysz ? ysz : xsz;
+
+       cidx = cell - lvl->cells;
+       row = cidx / lvl->width;
+       col = cidx % lvl->width;
+
+       x = col * sz - view_panx;
+       y = row * sz - view_pany;
+
+       glColor3f(1, 1, 1);
+       glVertex2f(x, y);
+       glVertex2f(x + sz, y);
+       glVertex2f(x + sz, y + sz);
+       glVertex2f(x, y + sz);
+
+       x += LTHICK;
+       y += LTHICK;
+       sz -= LTHICK * 2.0f;
+
+       glColor3fv(colors[cell->type]);
        glVertex2f(x, y);
        glVertex2f(x + sz, y);
        glVertex2f(x + sz, y + sz);
@@ -47,37 +69,14 @@ static void draw_cell(float x, float y, float sz, struct cell *cell)
 void draw_level(struct level *lvl)
 {
        int i, j;
-       float x, y, dx, dy, cellsz;
        struct cell *cell;
 
-       dx = view_width / lvl->width;
-       dy = view_height / lvl->height;
-       cellsz = dx > dy ? dy : dx;
-
        glBegin(GL_QUADS);
        cell = lvl->cells;
-       y = 0;
-       for(i=0; i<lvl->height; i++) {
-               x = 0;
-               for(j=0; j<lvl->width; j++) {
-                       draw_cell(x, y, cellsz, cell++);
-                       x += cellsz;
-               }
-               y += cellsz;
-       }
-       glEnd();
-
-       glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
-       glBegin(GL_QUADS);
-       y = 0;
        for(i=0; i<lvl->height; i++) {
-               x = 0;
                for(j=0; j<lvl->width; j++) {
-                       draw_cell(x, y, cellsz, 0);
-                       x += cellsz;
+                       draw_cell(lvl, cell++);
                }
-               y += cellsz;
        }
        glEnd();
-       glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 }
index 93fc08e..2df8b8e 100644 (file)
@@ -18,6 +18,7 @@ struct level {
        struct cell *cells;
 };
 
+
 struct level *create_level(int xsz, int ysz);
 void free_level(struct level *lvl);
 
index 7c022c1..52074ba 100644 (file)
@@ -25,9 +25,14 @@ static int utextwidth(const char *txt, int sz);
 
 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;
 
 static float uiscale = 1.0f;
 #define UISPLIT        150
+static int splitx;
 
 #define FONTSZ 16
 static struct dtx_font *uifont;
@@ -101,6 +106,10 @@ static int init(void)
                return -1;
        }
 
+       splitx = UISPLIT * uiscale;
+       view_width = win_width - splitx;
+       view_height = win_height;
+
        return 0;
 }
 
@@ -113,11 +122,6 @@ static void cleanup(void)
 
 static void display(void)
 {
-       int splitx = UISPLIT * uiscale;
-
-       view_width = win_width - splitx;
-       view_height = win_height;
-
        glClear(GL_COLOR_BUFFER_BIT);
 
        glMatrixMode(GL_MODELVIEW);
@@ -187,12 +191,42 @@ 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;
+               }
+       }
+
        utk_mmotion_event(x / uiscale, y / uiscale);
        glutPostRedisplay();
 }