From bc78ff27ed4baff971f2ce6f124902659719c927 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Wed, 1 May 2013 17:41:59 +0000 Subject: [PATCH] Classic: clean up mirror drawing code. No functional changes, but a (commented out) debug line for an oob read of the frame buffer when the mirror covers the whole screen is inserted. git-svn-id: https://svn.eduke32.com/eduke32@3719 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/include/build.h | 4 +- polymer/eduke32/build/src/engine.c | 67 +++++++++++++++++---------- polymer/eduke32/build/src/polymer.c | 4 +- polymer/eduke32/source/game.c | 4 +- 4 files changed, 49 insertions(+), 30 deletions(-) diff --git a/polymer/eduke32/build/include/build.h b/polymer/eduke32/build/include/build.h index d8a554fbc..e84f482c9 100644 --- a/polymer/eduke32/build/include/build.h +++ b/polymer/eduke32/build/include/build.h @@ -971,8 +971,8 @@ void plotpixel(int32_t x, int32_t y, char col); char getpixel(int32_t x, int32_t y); void setviewtotile(int16_t tilenume, int32_t xsiz, int32_t ysiz); void setviewback(void); -void preparemirror(int32_t dax, int32_t day, int32_t daz, int16_t daang, int32_t dahoriz, - int16_t dawall, int16_t dasector, int32_t *tposx, int32_t *tposy, int16_t *tang); +void preparemirror(int32_t dax, int32_t day, int16_t daang, int16_t dawall, + int32_t *tposx, int32_t *tposy, int16_t *tang); void completemirror(void); int32_t drawrooms(int32_t daposx, int32_t daposy, int32_t daposz, diff --git a/polymer/eduke32/build/src/engine.c b/polymer/eduke32/build/src/engine.c index 604e4aa62..600a97ff9 100644 --- a/polymer/eduke32/build/src/engine.c +++ b/polymer/eduke32/build/src/engine.c @@ -14580,21 +14580,23 @@ void squarerotatetile(int16_t tilenume) // // preparemirror // -void preparemirror(int32_t dax, int32_t day, int32_t daz, int16_t daang, int32_t dahoriz, int16_t dawall, int16_t dasector, int32_t *tposx, int32_t *tposy, int16_t *tang) +void preparemirror(int32_t dax, int32_t day, int16_t daang, int16_t dawall, + int32_t *tposx, int32_t *tposy, int16_t *tang) { - int32_t i, j, x, y, dx, dy; + int32_t i; - UNREFERENCED_PARAMETER(daz); - UNREFERENCED_PARAMETER(dahoriz); - UNREFERENCED_PARAMETER(dasector); + const int32_t x = wall[dawall].x, dx = wall[wall[dawall].point2].x-x; + const int32_t y = wall[dawall].y, dy = wall[wall[dawall].point2].y-y; + + const int32_t j = dx*dx + dy*dy; + if (j == 0) + return; + + i = ((dax-x)*dx + (day-y)*dy)<<1; - x = wall[dawall].x; dx = wall[wall[dawall].point2].x-x; - y = wall[dawall].y; dy = wall[wall[dawall].point2].y-y; - j = dx*dx + dy*dy; if (j == 0) return; - i = (((dax-x)*dx + (day-y)*dy)<<1); *tposx = (x<<1) + scale(dx,i,j) - dax; *tposy = (y<<1) + scale(dy,i,j) - day; - *tang = (((getangle(dx,dy)<<1)-daang)&2047); + *tang = ((getangle(dx,dy)<<1)-daang)&2047; inpreparemirror = 1; } @@ -14605,29 +14607,46 @@ void preparemirror(int32_t dax, int32_t day, int32_t daz, int16_t daang, int32_t // void completemirror(void) { - int32_t i, dy; - intptr_t p; - #ifdef USE_OPENGL if (rendmode) return; #endif //Can't reverse with uninitialized data if (inpreparemirror) { inpreparemirror = 0; return; } - if (mirrorsx1 > 0) mirrorsx1--; - if (mirrorsx2 < windowx2-windowx1-1) mirrorsx2++; - if (mirrorsx2 < mirrorsx1) return; + + if (mirrorsx1 > 0) + mirrorsx1--; + if (mirrorsx2 < windowx2-windowx1-1) + mirrorsx2++; + + if (mirrorsx1 > mirrorsx2) + return; begindrawing(); - p = frameplace + ylookup[windowy1+mirrorsy1] + windowx1+mirrorsx1; - i = windowx2-windowx1-mirrorsx2-mirrorsx1; mirrorsx2 -= mirrorsx1; - for (dy=mirrorsy2-mirrorsy1-1; dy>=0; dy--) { - copybufbyte((void *)(p+1),tempbuf,mirrorsx2+1); - tempbuf[mirrorsx2] = tempbuf[mirrorsx2-1]; // FIXME: with GEKKO, array subscripts are below array bounds - copybufreverse(&tempbuf[mirrorsx2],(void *)(p+i),mirrorsx2+1); - p += ylookup[1]; - faketimerhandler(); + // Address of the mirror's top left corner: + intptr_t p = frameplace + ylookup[windowy1+mirrorsy1] + windowx1+mirrorsx1; + + const int32_t mirrofs = windowx2-windowx1-mirrorsx2-mirrorsx1; + // Width in pixels (screen x's are inclusive on both sides): + const int32_t width = mirrorsx2-mirrorsx1+1; + // Height in pixels (screen y's are half-open because they come from umost/dmost): + const int32_t height = mirrorsy2-mirrorsy1; + int32_t y; + + for (y=0; y= bytesperline*ydim) + printf("oob read: mirrorsx1=%d, mirrorsx2=%d\n", mirrorsx1, mirrorsx2); +#endif + copybufbyte((void *)(p+1), tempbuf, width); + tempbuf[width-1] = tempbuf[width-2]; // FIXME: with GEKKO, array subscripts are below array bounds + copybufreverse(&tempbuf[width-1], (void *)(p+mirrofs), width); + + p += ylookup[1]; + faketimerhandler(); + } } enddrawing(); } diff --git a/polymer/eduke32/build/src/polymer.c b/polymer/eduke32/build/src/polymer.c index 9570f940b..875503da4 100644 --- a/polymer/eduke32/build/src/polymer.c +++ b/polymer/eduke32/build/src/polymer.c @@ -1885,8 +1885,8 @@ static void polymer_displayrooms(int16_t dacursectnum) //bglEnable(GL_CLIP_PLANE0); if (mirrorlist[i].wallnum >= 0) - preparemirror(globalposx, globalposy, 0, globalang, 0, - mirrorlist[i].wallnum, 0, &gx, &gy, &viewangle); + preparemirror(globalposx, globalposy, globalang, + mirrorlist[i].wallnum, &gx, &gy, &viewangle); gx = globalposx; gy = globalposy; diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c index 2fffd5bb9..66ab53482 100644 --- a/polymer/eduke32/source/game.c +++ b/polymer/eduke32/source/game.c @@ -3480,10 +3480,10 @@ void G_HandleMirror(int32_t x, int32_t y, int32_t z, int32_t a, int32_t horiz, i if (wall[g_mirrorWall[i]].overpicnum == MIRROR) { - int32_t tposx,tposy; + int32_t tposx, tposy; int16_t tang; - preparemirror(x,y,z,a,horiz,g_mirrorWall[i],g_mirrorSector[i],&tposx,&tposy,&tang); + preparemirror(x, y, a, g_mirrorWall[i], &tposx, &tposy, &tang); j = g_visibility; g_visibility = (j>>1) + (j>>2);