From 16ee776dc021c8fa20bc6d344d7f585d371e9553 Mon Sep 17 00:00:00 2001 From: epoch Date: Sun, 11 Oct 2020 01:39:06 -0500 Subject: moved the bounding-box checking-code from driver-specific files into math.c, moved the check and hilighting to driver-agnostic place. --- src/graphics_c2.c | 15 +++++++++++++++ src/graphics_c3.c | 2 +- src/graphics_cs_x11.c | 43 ------------------------------------------- src/math.c | 37 +++++++++++++++++++++++++++++-------- 4 files changed, 45 insertions(+), 52 deletions(-) diff --git a/src/graphics_c2.c b/src/graphics_c2.c index d78f87b..65d139f 100644 --- a/src/graphics_c2.c +++ b/src/graphics_c2.c @@ -1,7 +1,11 @@ +#include "common.h" +#include "math.h" #include "graphics_cs.h" #include "graphics_c3.h" +#include "mouse.h" extern struct gra_global gra_global; +extern struct hvr_global global; int c2sX(real x) { return (gra_global.width/(gra_global.split_screen / (gra_global.red_and_blue ? gra_global.split_screen: 1))) * ((x + RIGHT) / (RIGHT *2)) + gra_global.xoff; } int s2cX(real x) { return (x/(gra_global.width/(gra_global.split_screen / (gra_global.red_and_blue?gra_global.split_screen :1))))*(RIGHT*2)-RIGHT; } @@ -29,6 +33,16 @@ void draw_c2_shape(c2_s_t s) { ss.p[i]=c2_to_cs(s.p[i]); } draw_cs_shape(ss); +// if(cn_PnPoly(gra_global.mouse,s.p,s.len+(s.len==1))) {//if the mouse is inside the shape, we're going to draw a different outline. + if(epoch_PnPoly(gra_global.mouse,s.p,s.len+(s.len==1))) {//if the mouse is inside the shape, we're going to draw a different outline. + set_ansi_color(7); + if(gra_global.mousemap[MOUSE_PRIMARY]==1) { + printf("%s action %s\n",global.user,s.id); + gra_global.mousemap[MOUSE_PRIMARY]=0; + } + //I need to debounce probably + draw_cs_shape(ss);//draw over. dunno. + } } void draw_c2_filled_shape(c2_s_t s) { @@ -40,6 +54,7 @@ void draw_c2_filled_shape(c2_s_t s) { ss.p[i]=c2_to_cs(s.p[i]); } draw_cs_filled_shape(ss); + draw_cs_shape(ss);//redraw outline last } void draw_c2_text(c2_t p,char *text) { diff --git a/src/graphics_c3.c b/src/graphics_c3.c index 72a03e3..e31233c 100644 --- a/src/graphics_c3.c +++ b/src/graphics_c3.c @@ -457,7 +457,7 @@ void draw_screen() { //} } //we check here to see if the mouse button is still down - if(gra_global.mousemap[MOUSE_PRIMARY]==-1) {//0 in x11 + if(gra_global.mousemap[MOUSE_PRIMARY]==-1) { printf("%s action %f %f\n",global.user,gra_global.mouse.x,gra_global.mouse.y); gra_global.mousemap[MOUSE_PRIMARY]=0; } diff --git a/src/graphics_cs_x11.c b/src/graphics_cs_x11.c index 0395a38..1f9774a 100644 --- a/src/graphics_cs_x11.c +++ b/src/graphics_cs_x11.c @@ -85,13 +85,6 @@ void draw_cs_text(cs_t p,char *text) { } void draw_cs_shape(cs_s_t s) {//this is implemented as draw_cs_line... hrm. it could be moved up to graphics.c? probl no. - //test in here whether a mouse click is within this shape's... bounding box? sure. - //cs_s_t bb;//bounding box - cs_t smouse=c2_to_cs(gra_global.mouse); - int minx=s.p[0].x; - int miny=s.p[0].y; - int maxx=s.p[0].x; - int maxy=s.p[0].y; int h; int i;//all cs shapes can have 1, 2, or 3+ points. guess I gotta do that logic here too. switch(s.len) { @@ -99,47 +92,11 @@ void draw_cs_shape(cs_s_t s) {//this is implemented as draw_cs_line... hrm. it c //circle h=max(s.p[0].x,s.p[1].x)-min(s.p[0].x,s.p[1].x); XDrawArc(x11_global.dpy,x11_global.backbuffer,x11_global.backgc,s.p[0].x-h,s.p[0].y-h,h*2,h*2,0,360*64); - if(distance2((c2_t){s.p[0].x,s.p[1].y},(c2_t){smouse.x,smouse.y} ) < h) { - if(gra_global.mousemap[0]==-1) { - gra_global.mousemap[0]=0; - printf("%s action %s\n",global.user,s.id); - } - XDrawArc(x11_global.dpy,x11_global.backbuffer,x11_global.backgc,s.p[0].x-h-2,s.p[0].y-h-2,h*2+4,h*2+4,0,360*64); - } break; default: for(i=0;imaxx)?s.p[i].x:maxx; - maxy=(s.p[i].y>maxy)?s.p[i].y:maxy; draw_cs_line(s.p[i],s.p[(i+1)%(s.len+(s.len==1))]); } - //fix the comparison here and also fix the hilighting shape - if(smouse.x >= minx && //gra_global is a c2_t and these are in cs_t - smouse.y >= miny && - smouse.x <= maxx && - smouse.y <= maxy) { -// if(wn_PnPoly(smouse,s.p,s.len) != 0 && s.len > 2) { - if(gra_global.mousemap[0] == -1) {//if we're inside the bounding box let's make SOMETHING happen. - gra_global.mousemap[0]=0; - printf("%s action %s\n",global.user,s.id); -// } -/* bb.id=strdup("boundingbox"); - bb.len=4; - bb.p[0].x=minx; - bb.p[0].y=miny; - bb.p[1].x=minx; - bb.p[1].y=maxy; - bb.p[2].x=maxx; - bb.p[2].y=maxy; - bb.p[3].x=maxx; - bb.p[3].y=miny; - draw_cs_filled_shape(bb); - free(bb.id); -*/ - } - } break; } } diff --git a/src/math.c b/src/math.c index 0f48dd3..948ded8 100644 --- a/src/math.c +++ b/src/math.c @@ -19,7 +19,8 @@ c3_group_rel_t *get_group_relative(char *id) { if(gr) return gr; } //if we got here, we need to make a new one. - assert(gr=malloc(sizeof(c3_group_rel_t)));//just exit on malloc error? sure... + gr=malloc(sizeof(c3_group_rel_t));//just exit on malloc error? sure... + assert(gr && "failed to malloc something I don't want to figure out how to work around if it doesn't malloc."); memset(gr,0,sizeof(c3_group_rel_t)); gr->s=(c3_t){1,1,1};//scale needs to be all 1s, since it is multiplied against the coordinates. gr->id=strdup(id);//don't forget to free this when it gets deleted. @@ -245,7 +246,7 @@ real distance3(c3_t p1,c3_t p2) { // <0 for P2 right of the line // See: Algorithm 1 "Area of Triangles and Polygons" inline int -isLeft( cs_t P0, cs_t P1, cs_t P2 ) +isLeft( c2_t P0, c2_t P1, c2_t P2 ) { return ( (P1.x - P0.x) * (P2.y - P0.y) - (P2.x - P0.x) * (P1.y - P0.y) ); @@ -256,20 +257,23 @@ isLeft( cs_t P0, cs_t P1, cs_t P2 ) // cn_PnPoly(): crossing number test for a point in a polygon // Input: P = a point, // V[] = vertex points of a polygon V[n+1] with V[n]=V[0] +// hacked on by epoch to make the previous statement automatic. i+1 changed to i+1%n // Return: 0 = outside, 1 = inside // This code is patterned after [Franklin, 2000] + +//pretending like this will work fine with a real instead of an int... probably fine. int -cn_PnPoly( cs_t P, cs_t *V, int n ) +cn_PnPoly( c2_t P, c2_t *V, int n ) { int cn = 0; // the crossing number counter // loop through all edges of the polygon for (int i=0; i P.y)) // an upward crossing - || ((V[i].y > P.y) && (V[i+1].y <= P.y))) { // a downward crossing + if (((V[i].y <= P.y) && (V[i+1%n].y > P.y)) // an upward crossing + || ((V[i].y > P.y) && (V[i+1%n].y <= P.y))) { // a downward crossing // compute the actual edge-ray intersect x-coordinate - float vt = (float)(P.y - V[i].y) / (V[i+1].y - V[i].y); - if (P.x < V[i].x + vt * (V[i+1].x - V[i].x)) // P.x < intersect + float vt = (float)(P.y - V[i].y) / (V[i+1%n].y - V[i].y); + if (P.x < V[i].x + vt * (V[i+1%n].x - V[i].x)) // P.x < intersect ++cn; // a valid crossing of y=P.y right of P.x } } @@ -283,8 +287,9 @@ cn_PnPoly( cs_t P, cs_t *V, int n ) // Input: P = a point, // V[] = vertex points of a polygon V[n+1] with V[n]=V[0] // Return: wn = the winding number (=0 only when P is outside) + int -wn_PnPoly( cs_t P, cs_t *V, int n ) +wn_PnPoly( c2_t P, c2_t *V, int n ) { int wn = 0; // the winding number counter @@ -304,3 +309,19 @@ wn_PnPoly( cs_t P, cs_t *V, int n ) return wn; } //=================================================================== + +int epoch_PnPoly( c2_t P, c2_t *V, int n ) { + cs_t min,max; + int i; + min.x=V[0].x; + min.y=V[0].y; + max.x=V[0].x; + max.y=V[0].y; + for(i=0;i= min.x && P.y >= min.y && P.y <= max.y); +} -- cgit v1.2.3