rubber band in platform-specific code
authorJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 3 Jul 2023 02:42:16 +0000 (05:42 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 3 Jul 2023 02:42:16 +0000 (05:42 +0300)
src/dos/main.c
src/modern/main.c
src/rtk.c
src/rtk.h
src/scr_mod.c

index 50c876f..e6e2346 100644 (file)
@@ -44,7 +44,7 @@ static uint32_t *vmem;
 static int quit, disp_pending, dirty_valid;
 static rtk_rect dirty;
 static int mx, my;
-static rtk_rect rband;
+static rtk_rect rband, prev_rband;
 
 
 int main(int argc, char **argv)
@@ -200,20 +200,28 @@ void app_swap_buffers(void)
        if(opt.vsync) {
                vid_vsync();
        }
-       if(!dirty_valid) return;
-       if(dirty.width < win_width || dirty.height < win_height) {
-               uint32_t *src = framebuf + dirty.y * win_width + dirty.x;
-               vid_blit32(dirty.x, dirty.y, dirty.width, dirty.height, src, 0);
+       if(dirty_valid) {
+               if(dirty.width < win_width || dirty.height < win_height) {
+                       uint32_t *src = framebuf + dirty.y * win_width + dirty.x;
+                       vid_blit32(dirty.x, dirty.y, dirty.width, dirty.height, src, 0);
 
-               if(mx >= dirty.x && my >= dirty.y && mx < dirty.x + dirty.width && my < dirty.y + dirty.height) {
+                       if(mx >= dirty.x && my >= dirty.y && mx < dirty.x + dirty.width && my < dirty.y + dirty.height) {
+                               draw_cursor(mx, my);
+                       }
+               } else {
+                       vid_blitfb32(framebuf, 0);
                        draw_cursor(mx, my);
                }
-       } else {
-               vid_blitfb32(framebuf, 0);
-               draw_cursor(mx, my);
+               dirty_valid = 0;
        }
 
-       dirty_valid = 0;
+       if(rband.width) {
+               if(prev_rband.width) {
+                       draw_rband(&prev_rband);
+               }
+               draw_rband(&rband);
+               prev_rband = rband;
+       }
 }
 
 void app_quit(void)
@@ -237,6 +245,7 @@ void app_rband(int x, int y, int w, int h)
 {
        if(!(w | h)) {
                w = h = 0;
+               prev_rband.width = 0;
        }
 
        rband.x = x;
@@ -259,16 +268,16 @@ static void draw_cursor(int x, int y)
        }
 }
 
-static void draw_rband(void)
+static void draw_rband(rtk_rect *r)
 {
        int i;
        rtk_rect rect;
        uint32_t *fbptr, *bptr;
 
-       rect = rband;
-       fix_rect(&rect);
+       rect = *r;
+       rtk_fix_rect(&rect);
 
-       fbptr = framebuf + rect.y * win_width + rect.x;
+       fbptr = vmem + rect.y * win_width + rect.x;
        bptr = fbptr + win_width * (rect.height - 1);
 
        for(i=0; i<rect.width; i++) {
@@ -281,5 +290,4 @@ static void draw_rband(void)
                fbptr[rect.width - 1] ^= 0xffffff;
                fbptr += win_width;
        }
-       app_redisplay(rect.x, rect.y, rect.width, rect.height);
 }
index bc002ac..5c90787 100644 (file)
@@ -20,6 +20,7 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #include <assert.h>
 #include "miniglut.h"
 #include "app.h"
+#include "rtk.h"
 #include "logger.h"
 
 static void display(void);
@@ -45,6 +46,7 @@ static void (*glx_swap_interval_sgi)();
 static PROC wgl_swap_interval_ext;
 #endif
 
+static rtk_rect rband;
 
 
 int main(int argc, char **argv)
@@ -101,17 +103,17 @@ long app_getmsec(void)
 
 void app_redisplay(int x, int y, int w, int h)
 {
-       dbgmsg("fakeupd: %d,%d (%dx%d)\n", x, y, w, h);
+       /*dbgmsg("fakeupd: %d,%d (%dx%d)\n", x, y, w, h);*/
        glutPostRedisplay();
 }
 
 void app_swap_buffers(void)
 {
+       glMatrixMode(GL_MODELVIEW);
+       glLoadIdentity();
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
-       glMatrixMode(GL_MODELVIEW);
-       glLoadIdentity();
 
        glRasterPos2i(-1, 1);
        glPixelZoom(1, -1);
@@ -120,7 +122,27 @@ void app_swap_buffers(void)
        glDrawPixels(win_width, win_height, GL_BGRA, GL_UNSIGNED_BYTE, framebuf);
        glDisable(GL_ALPHA_TEST);
 
-       glMatrixMode(GL_PROJECTION);
+       if(rband.width | rband.height) {
+               glOrtho(0, win_width, win_height, 0, -1, 1);
+
+               glPushAttrib(GL_ENABLE_BIT);
+               glDisable(GL_DEPTH_TEST);
+               glDisable(GL_LIGHTING);
+
+               glEnable(GL_COLOR_LOGIC_OP);
+               glLogicOp(GL_XOR);
+
+               glBegin(GL_LINE_LOOP);
+               glColor3f(1, 1, 1);
+               glVertex2f(rband.x, rband.y);
+               glVertex2f(rband.x + rband.width, rband.y);
+               glVertex2f(rband.x + rband.width, rband.y + rband.height);
+               glVertex2f(rband.x, rband.y + rband.height);
+               glEnd();
+
+               glPopAttrib();
+       }
+
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
 
@@ -180,6 +202,16 @@ void app_vsync(int vsync)
 }
 #endif
 
+void app_rband(int x, int y, int w, int h)
+{
+       rband.x = x;
+       rband.y = y;
+       rband.width = w;
+       rband.height = h;
+
+       glutPostRedisplay();
+}
+
 
 static void display(void)
 {
index 4bb93c8..2ed29e9 100644 (file)
--- a/src/rtk.c
+++ b/src/rtk.c
@@ -818,6 +818,31 @@ int rtk_input_mmotion(rtk_widget *w, int x, int y)
        return 0;
 }
 
+void rtk_fix_rect(rtk_rect *rect)
+{
+       int x, y, w, h;
+
+       x = rect->x;
+       y = rect->y;
+
+       if(rect->width < 0) {
+               w = -rect->width;
+               x += rect->width;
+       } else {
+               w = rect->width;
+       }
+       if(rect->height < 0) {
+               h = -rect->height;
+               y += rect->height;
+       } else {
+               h = rect->height;
+       }
+
+       rect->x = x;
+       rect->y = y;
+       rect->width = w;
+       rect->height = h;
+}
 
 void rtk_rect_union(rtk_rect *a, const rtk_rect *b)
 {
index aad342e..2ebdef2 100644 (file)
--- a/src/rtk.h
+++ b/src/rtk.h
@@ -105,6 +105,7 @@ int rtk_input_mbutton(rtk_widget *w, int bn, int press, int x, int y);
 int rtk_input_mmotion(rtk_widget *w, int x, int y);
 
 /* misc */
+void rtk_fix_rect(rtk_rect *r);
 void rtk_rect_union(rtk_rect *a, const rtk_rect *b);
 
 #endif /* RTK_H_ */
index 5a33e45..ae6de9b 100644 (file)
@@ -90,7 +90,6 @@ static void act_settool(int tidx);
 static void act_addobj(void);
 static void act_rmobj(void);
 
-static void fix_rect(rtk_rect *rect);
 static void draw_rband(void);
 static void moveobj(struct object *obj, int px0, int py0, int px1, int py1);
 
@@ -259,10 +258,6 @@ static void mdl_display(void)
 
        /* GUI */
        rtk_draw_widget(toolbar);
-
-       if(rband_valid) {
-               draw_rband();
-       }
 }
 
 static void draw_object(struct object *obj)
@@ -391,6 +386,7 @@ static void mdl_mouse(int bn, int press, int x, int y)
 
                if(rband_valid) {
                        rband_valid = 0;
+                       app_rband(0, 0, 0, 0);
 
                        if(cur_tool == TOOL_REND_AREA) {
                                if(prev_tool >= 0) {
@@ -398,7 +394,7 @@ static void mdl_mouse(int bn, int press, int x, int y)
                                }
                                rendering = 1;
                                rend_size(win_width, win_height);
-                               fix_rect(&rband);
+                               rtk_fix_rect(&rband);
                                rendrect = rband;
                                rend_begin(rband.x, rband.y, rband.width, rband.height);
                        }
@@ -457,6 +453,7 @@ static void mdl_motion(int x, int y)
                                        rband.width = x - rband.x;
                                        rband.height = y - rband.y;
                                        rband_valid = 1;
+                                       app_rband(rband.x, rband.y, rband.width, rband.height);
                                }
                                break;
 
@@ -557,32 +554,6 @@ static void act_rmobj(void)
        }
 }
 
-static void fix_rect(rtk_rect *rect)
-{
-       int x, y, w, h;
-
-       x = rband.x;
-       y = rband.y;
-
-       if(rband.width < 0) {
-               w = -rband.width;
-               x += rband.width;
-       } else {
-               w = rband.width;
-       }
-       if(rband.height < 0) {
-               h = -rband.height;
-               y += rband.height;
-       } else {
-               h = rband.height;
-       }
-
-       rect->x = x;
-       rect->y = y;
-       rect->width = w;
-       rect->height = h;
-}
-
 
 void primray(cgm_ray *ray, int x, int y)
 {