From 44dae5686ed2cef199911872dc660f78ceb864a7 Mon Sep 17 00:00:00 2001 From: ketmar Date: Sun, 18 Mar 2012 16:43:54 +0200 Subject: [PATCH] polymod still broken --- src/game.c | 109 ++++----- src/polymod.c | 230 +++++++++++++++--- src/polymod.h | 5 +- src/sincostab.c | 724 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 967 insertions(+), 101 deletions(-) create mode 100644 src/sincostab.c diff --git a/src/game.c b/src/game.c index a234b2d..9263cbf 100644 --- a/src/game.c +++ b/src/game.c @@ -876,70 +876,53 @@ static void frmGameDraw (SDL_Surface *frame) { SDL_FillRect(frame, &rc, palette[0]); } // -#if 0 - polymodStart(); +#if 1 + static angle = 0; + // + if (!fkeys[9]) { + angle = (angle+1)%360; + //fprintf(stderr, "a=%d\n", angle); + } + polymodStart(320/2, 200/2, 1*POLYFIX_BASE, angle); // - /* - polymodAddPoint(10, 200-10); - polymodAddPoint(320-10, 200-10); - polymodAddPoint(10, 10); - polymodAddPoint(320-10, 10); - */ - /* - polymodAddPoint(10, 10); - polymodAddPoint(320-10, 10); - polymodAddPoint(10, 200-10); - */ - /* - polymodAddPoint(202, 100); - polymodAddPoint(200, 110); - polymodAddPoint(196, 120); - polymodAddPoint(189, 129); - polymodAddPoint(181, 136); - polymodAddPoint(170, 140); - polymodAddPoint(160, 142); - polymodAddPoint(150, 140); - polymodAddPoint(140, 136); - polymodAddPoint(131, 129); - polymodAddPoint(124, 120); - polymodAddPoint(120, 110); - polymodAddPoint(118, 100); - polymodAddPoint(120, 90); - polymodAddPoint(124, 79); - polymodAddPoint(131, 71); - polymodAddPoint(139, 64); - polymodAddPoint(150, 60); - polymodAddPoint(160, 58); - polymodAddPoint(170, 60); - polymodAddPoint(181, 64); - polymodAddPoint(189, 71); - polymodAddPoint(196, 79); - polymodAddPoint(200, 90); - */ - polymodAddPoint(196, 100); - polymodAddPoint(206, 112); - polymodAddPoint(191, 117); - polymodAddPoint(193, 133); - polymodAddPoint(178, 131); - polymodAddPoint(172, 146); - polymodAddPoint(160, 136); - polymodAddPoint(148, 146); - polymodAddPoint(143, 131); - polymodAddPoint(127, 133); - polymodAddPoint(129, 117); - polymodAddPoint(114, 112); - polymodAddPoint(124, 100); - polymodAddPoint(114, 88); - polymodAddPoint(129, 82); - polymodAddPoint(127, 67); - polymodAddPoint(142, 69); - polymodAddPoint(148, 54); - polymodAddPoint(160, 64); - polymodAddPoint(172, 54); - polymodAddPoint(178, 69); - polymodAddPoint(193, 67); - polymodAddPoint(191, 82); - polymodAddPoint(206, 88); +#if 0 + polymodAddPoint(-20, -20); + polymodAddPoint(20, -20); + polymodAddPoint(20, 20); + polymodAddPoint(-20, 20); +#endif +#if 0 + polymodAddPoint(-10, -10); + polymodAddPoint(20, -10); + polymodAddPoint(-10, 20); + polymodAddPoint(20, 20); +#endif +#if 1 + polymodAddPoint(36, 0); + polymodAddPoint(46, 12); + polymodAddPoint(31, 17); + polymodAddPoint(33, 33); + polymodAddPoint(18, 31); + polymodAddPoint(12, 46); + polymodAddPoint(0, 36); + polymodAddPoint(-12, 46); + polymodAddPoint(-17, 31); + polymodAddPoint(-33, 33); + polymodAddPoint(-31, 17); + polymodAddPoint(-46, 12); + polymodAddPoint(-36, 0); + polymodAddPoint(-46, -12); + polymodAddPoint(-31, -18); + polymodAddPoint(-33, -33); + polymodAddPoint(-18, -31); + polymodAddPoint(-12, -46); + polymodAddPoint(0, -36); + polymodAddPoint(12, -46); + polymodAddPoint(18, -31); + polymodAddPoint(33, -33); + polymodAddPoint(31, -18); + polymodAddPoint(46, -12); +#endif // polymodEnd(); polymodFill(frame, 3, 50); diff --git a/src/polymod.c b/src/polymod.c index bcba98e..2f49d07 100644 --- a/src/polymod.c +++ b/src/polymod.c @@ -14,8 +14,26 @@ */ #include "polymod.h" -#include "video.h" -#include "gameglobals.h" +#include + +#ifdef TEST +# include +# include +# include +#else +# include "video.h" +# include "gameglobals.h" +#endif + +#include "sincostab.c" + + +#ifdef TEST +static void fatal (const char *msg) { + printf("FATAL: %s\n", msg); + abort(); +} +#endif typedef struct PolyPoint { @@ -31,6 +49,11 @@ static int polyScans[200]; // index of the first point of each scan; -1: none static int firstX, firstY; static int lastX, lastY; static int firstPoint; +static int lastDY; +static int minY, maxY; +static int pofsx, pofsy; +static int pscale; +static int pangle; void polymodInitialize (void) { @@ -70,95 +93,228 @@ static inline void insertPoint (int x, int y) { if (y >= 0 && y < 200) { int n; // - //fprintf(stderr, "pt: (%d, %d)\n", x, y); if ((n = polyScans[y]) != -1) { int p; // for (p = -1; n != -1 && x > polyPointPool[n].x; p = n, n = polyPointPool[n].next) ; - if (n != -1 && x == polyPointPool[n].x) return; // duplicate point if (p == -1) { // this is new first point - //fprintf(stderr, " first\n"); polyScans[y] = allocPolyPoint(x, n); } else { // insert new point after p; n is the next point - //fprintf(stderr, " mid\n"); polyPointPool[p].next = allocPolyPoint(x, n); } } else { // first - //fprintf(stderr, " first\n"); polyScans[y] = allocPolyPoint(x, -1); } } } +static inline void rotate (int *x, int *y) { + int64_t tx = (int64_t)(*x)*pscale/POLYFIX_BASE; + int64_t ty = (int64_t)(*y)*pscale/POLYFIX_BASE; + *x = (tx*costab[pangle]-ty*sintab[pangle])/POLYFIX_BASE+pofsx; + *y = (tx*sintab[pangle]+ty*costab[pangle])/POLYFIX_BASE+pofsy; +} + + static void lineDDA (int x0, int y0, int x1, int y1) { - //fprintf(stderr, "*(%d,%d)-(%d,%d)\n", x0, y0, x1, y1); - if (x0 == x1 && y0 == y1) return; - // - if (x0 == x1) { - // straight vertical line - if (y0 < y1) for (int y = y0; y < y1; ++y) insertPoint(x0, y); - else for (int y = y0; y > y1; --y) insertPoint(x0, y); - return; - } + int dy; + double x, dx; // if (y0 == y1) { - // straight horizontal line - insertPoint(x0, y0); + // horizontal line + if (y0 >= 0 && y0 <= 199 && lastDY != 0) insertPoint(x0, y0); + lastDY = 0; return; } - // draw vertical - if (y0 < y1) { - double x = x0, dx = (double)(x1-x0)/(double)(y1-y0); - // - for (int y = y0; y < y1; ++y) { insertPoint(x+0.5, y); x += dx; } + // + rotate(&x0, &y0); + rotate(&x1, &y1); + // + //fprintf(stderr, "*(%d,%d)-(%d,%d)\n", x0, y0, x1, y1); + if ((y0 < 0 && y1 < 0) || (y0 > 199 && y1 > 199)) return; // out-of-screen lines aren't interesting + // + if (x0 == x1) { + // straight vertical line + if (y0 < y1) { + if (y0 < 0) y0 = 0; + if (y1 > 199) y1 = 199; + if (y0 < minY) minY = y0; + if (y1 > maxY) maxY = y1; + if (lastDY == -11) insertPoint(x0, y0); + lastDY = 1; + for (; y0 < y1; ++y0) insertPoint(x0, y0); + } else { + if (y1 < 0) y1 = 0; + if (y0 > 199) y0 = 199; + if (y1 < minY) minY = y1; + if (y0 > maxY) maxY = y0; + if (lastDY == 1) insertPoint(x0, y0); + lastDY = -1; + for (; y0 > y1; --y0) insertPoint(x0, y0); + } } else { - double x = x0, dx = (double)(x1-x0)/(double)(y0-y1); - // - for (int y = y0; y > y1; --y) { insertPoint(x+0.5, y); x += dx; } + // draw sloped + x = x0; + if (y0 < y1) { + dx = (double)(x1-x0)/(double)(y1-y0); + dy = 1; + if (y0 < 0) { x += dx*(0-y0); y0 = 0; } + if (y1 > 199) y1 = 199; + if (y0 < minY) minY = y0; + if (y1 > maxY) maxY = y1; + } else { + dx = (double)(x1-x0)/(double)(y0-y1); + dy = -1; + if (y1 < 0) y1 = 0; + if (y0 > 199) { x += dy*(y0-199); y0 = 199; } + if (y1 < minY) minY = y1; + if (y0 > maxY) maxY = y0; + } + if (lastDY == -dy) insertPoint(x0, y0); + lastDY = dy; + for (; y0 != y1; y0 += dy) { insertPoint(x+0.5, y0); x += dx; } } } -void polymodStart (void) { + + +void polymodStart (int ofsx, int ofsy, int scale, int angle) { for (int f = 0; f < 200; ++f) polyScans[f] = -1; polyPointPoolPos = 0; + lastDY = 0; firstPoint = 1; + minY = 200; + maxY = -1; + pofsx = ofsx; + pofsy = ofsy; + pscale = scale; + while (angle < 0) angle += 360; // hehe + pangle = angle%360; } void polymodAddPoint (int x, int y) { - if (!firstPoint) lineDDA(lastX, lastY, x, y); else { firstX = x; firstY = y; firstPoint = 0; } + if (!firstPoint) { + lineDDA(lastX, lastY, x, y); + } else { + firstPoint = 0; + firstX = x; + firstY = y; + } lastX = x; lastY = y; } void polymodEnd (void) { - if (!firstPoint) lineDDA(lastX, lastY, firstX, firstY); + if (!firstPoint) { + lineDDA(lastX, lastY, firstX, firstY); + insertPoint(firstX, firstY); + } } void polymodFill (SDL_Surface *frame, Uint8 color, Uint8 alpha) { - for (int y = 0; y < 200; ++y) { + for (int y = minY; y <= maxY; ++y) { int p = polyScans[y]; // if (p >= 0) { - while (p >= 0 && polyPointPool[p].x < 320) { - int n = polyPointPool[p].next/*, ex*/; +#ifdef TEST + printf("y: %d\n", y); + while (p >= 0) { + printf(" x: %d\n", polyPointPool[p].x); + p = polyPointPool[p].next; + } +#else + for (;;) { + int n = polyPointPool[p].next; // - if (n < 0) { - //putPixelA2x(frame, polyPointPool[p].x, y, color, alpha); - putPixel2x(frame, polyPointPool[p].x, y, color); - break; - } + if (n < 0) break; //fprintf(stderr, "y: %d; x0: %d; x1: %d\n", y, polyPointPool[p].x, polyPointPool[n].x); //ex = polyPointPool[n].x; //for (int x = polyPointPool[p].x; x <= ex; ++x) putPixelA2x(frame, x, y, color, alpha); hline2x(frame, polyPointPool[p].x, y, polyPointPool[n].x-polyPointPool[p].x+1, color); p = polyPointPool[n].next; + if (p < 0) break; } +#endif } } } + + +#ifdef TEST +int main () { + polymodStart(); + // + /* + polymodAddPoint(50, 50); + polymodAddPoint(90, 50); + polymodAddPoint(90, 90); + polymodAddPoint(50, 90); + */ + /* + polymodAddPoint(10, 200-10); + polymodAddPoint(320-10, 200-10); + polymodAddPoint(10, 10); + polymodAddPoint(320-10, 10); + */ + /* + polymodAddPoint(202, 100); + polymodAddPoint(200, 110); + polymodAddPoint(196, 120); + polymodAddPoint(189, 129); + polymodAddPoint(181, 136); + polymodAddPoint(170, 140); + polymodAddPoint(160, 142); + polymodAddPoint(150, 140); + polymodAddPoint(140, 136); + polymodAddPoint(131, 129); + polymodAddPoint(124, 120); + polymodAddPoint(120, 110); + polymodAddPoint(118, 100); + polymodAddPoint(120, 90); + polymodAddPoint(124, 79); + polymodAddPoint(131, 71); + polymodAddPoint(139, 64); + polymodAddPoint(150, 60); + polymodAddPoint(160, 58); + polymodAddPoint(170, 60); + polymodAddPoint(181, 64); + polymodAddPoint(189, 71); + polymodAddPoint(196, 79); + polymodAddPoint(200, 90); + */ + polymodAddPoint(196, 100); + polymodAddPoint(206, 112); + polymodAddPoint(191, 117); + polymodAddPoint(193, 133); + polymodAddPoint(178, 131); + polymodAddPoint(172, 146); + polymodAddPoint(160, 136); + polymodAddPoint(148, 146); + polymodAddPoint(143, 131); + polymodAddPoint(127, 133); + polymodAddPoint(129, 117); + polymodAddPoint(114, 112); + polymodAddPoint(124, 100); + polymodAddPoint(114, 88); + polymodAddPoint(129, 82); + polymodAddPoint(127, 67); + polymodAddPoint(142, 69); + polymodAddPoint(148, 54); + polymodAddPoint(160, 64); + polymodAddPoint(172, 54); + polymodAddPoint(178, 69); + polymodAddPoint(193, 67); + polymodAddPoint(191, 82); + polymodAddPoint(206, 88); + // + polymodEnd(); + polymodFill(NULL, 3, 50); + return 0; +} +#endif diff --git a/src/polymod.h b/src/polymod.h index fb9146b..25d8cc5 100644 --- a/src/polymod.h +++ b/src/polymod.h @@ -18,10 +18,13 @@ #include "SDL.h" +#define POLYFIX_BASE (0x4000) + + extern void polymodInitialize (void); extern void polymodDeinitialize (void); -extern void polymodStart (void); +extern void polymodStart (int ofsx, int ofsy, int scale, int angle); extern void polymodAddPoint (int x, int y); extern void polymodEnd (void); diff --git a/src/sincostab.c b/src/sincostab.c new file mode 100644 index 0000000..56b97d2 --- /dev/null +++ b/src/sincostab.c @@ -0,0 +1,724 @@ +static const int costab[360] = { +16384, +16381, +16374, +16361, +16344, +16321, +16294, +16261, +16224, +16182, +16135, +16082, +16025, +15964, +15897, +15825, +15749, +15668, +15582, +15491, +15395, +15295, +15190, +15081, +14967, +14848, +14725, +14598, +14466, +14329, +14188, +14043, +13894, +13740, +13582, +13420, +13254, +13084, +12910, +12732, +12550, +12365, +12175, +11982, +11785, +11585, +11381, +11173, +10963, +10748, +10531, +10310, +10086, +9860, +9630, +9397, +9161, +8923, +8682, +8438, +8192, +7943, +7691, +7438, +7182, +6924, +6663, +6401, +6137, +5871, +5603, +5334, +5062, +4790, +4516, +4240, +3963, +3685, +3406, +3126, +2845, +2563, +2280, +1996, +1712, +1427, +1142, +857, +571, +285, +0, +-285, +-571, +-857, +-1142, +-1427, +-1712, +-1996, +-2280, +-2563, +-2845, +-3126, +-3406, +-3685, +-3963, +-4240, +-4516, +-4790, +-5062, +-5334, +-5603, +-5871, +-6137, +-6401, +-6663, +-6924, +-7182, +-7438, +-7691, +-7943, +-8191, +-8438, +-8682, +-8923, +-9161, +-9397, +-9630, +-9860, +-10086, +-10310, +-10531, +-10748, +-10963, +-11173, +-11381, +-11585, +-11785, +-11982, +-12175, +-12365, +-12550, +-12732, +-12910, +-13084, +-13254, +-13420, +-13582, +-13740, +-13894, +-14043, +-14188, +-14329, +-14466, +-14598, +-14725, +-14848, +-14967, +-15081, +-15190, +-15295, +-15395, +-15491, +-15582, +-15668, +-15749, +-15825, +-15897, +-15964, +-16025, +-16082, +-16135, +-16182, +-16224, +-16261, +-16294, +-16321, +-16344, +-16361, +-16374, +-16381, +-16384, +-16381, +-16374, +-16361, +-16344, +-16321, +-16294, +-16261, +-16224, +-16182, +-16135, +-16082, +-16025, +-15964, +-15897, +-15825, +-15749, +-15668, +-15582, +-15491, +-15395, +-15295, +-15190, +-15081, +-14967, +-14848, +-14725, +-14598, +-14466, +-14329, +-14188, +-14043, +-13894, +-13740, +-13582, +-13420, +-13254, +-13084, +-12910, +-12732, +-12550, +-12365, +-12175, +-11982, +-11785, +-11585, +-11381, +-11173, +-10963, +-10748, +-10531, +-10310, +-10086, +-9860, +-9630, +-9397, +-9161, +-8923, +-8682, +-8438, +-8192, +-7943, +-7691, +-7438, +-7182, +-6924, +-6663, +-6401, +-6137, +-5871, +-5603, +-5334, +-5062, +-4790, +-4516, +-4240, +-3963, +-3685, +-3406, +-3126, +-2845, +-2563, +-2280, +-1996, +-1712, +-1427, +-1142, +-857, +-571, +-285, +0, +285, +571, +857, +1142, +1427, +1712, +1996, +2280, +2563, +2845, +3126, +3406, +3685, +3963, +4240, +4516, +4790, +5062, +5334, +5603, +5871, +6137, +6401, +6663, +6924, +7182, +7438, +7691, +7943, +8192, +8438, +8682, +8923, +9161, +9397, +9630, +9860, +10086, +10310, +10531, +10748, +10963, +11173, +11381, +11585, +11785, +11982, +12175, +12365, +12550, +12732, +12910, +13084, +13254, +13420, +13582, +13740, +13894, +14043, +14188, +14329, +14466, +14598, +14725, +14848, +14967, +15081, +15190, +15295, +15395, +15491, +15582, +15668, +15749, +15825, +15897, +15964, +16025, +16082, +16135, +16182, +16224, +16261, +16294, +16321, +16344, +16361, +16374, +16381, +}; +static const int sintab[360] = { +0, +285, +571, +857, +1142, +1427, +1712, +1996, +2280, +2563, +2845, +3126, +3406, +3685, +3963, +4240, +4516, +4790, +5062, +5334, +5603, +5871, +6137, +6401, +6663, +6924, +7182, +7438, +7691, +7943, +8191, +8438, +8682, +8923, +9161, +9397, +9630, +9860, +10086, +10310, +10531, +10748, +10963, +11173, +11381, +11585, +11785, +11982, +12175, +12365, +12550, +12732, +12910, +13084, +13254, +13420, +13582, +13740, +13894, +14043, +14188, +14329, +14466, +14598, +14725, +14848, +14967, +15081, +15190, +15295, +15395, +15491, +15582, +15668, +15749, +15825, +15897, +15964, +16025, +16082, +16135, +16182, +16224, +16261, +16294, +16321, +16344, +16361, +16374, +16381, +16384, +16381, +16374, +16361, +16344, +16321, +16294, +16261, +16224, +16182, +16135, +16082, +16025, +15964, +15897, +15825, +15749, +15668, +15582, +15491, +15395, +15295, +15190, +15081, +14967, +14848, +14725, +14598, +14466, +14329, +14188, +14043, +13894, +13740, +13582, +13420, +13254, +13084, +12910, +12732, +12550, +12365, +12175, +11982, +11785, +11585, +11381, +11173, +10963, +10748, +10531, +10310, +10086, +9860, +9630, +9397, +9161, +8923, +8682, +8438, +8191, +7943, +7691, +7438, +7182, +6924, +6663, +6401, +6137, +5871, +5603, +5334, +5062, +4790, +4516, +4240, +3963, +3685, +3406, +3126, +2845, +2563, +2280, +1996, +1712, +1427, +1142, +857, +571, +285, +0, +-285, +-571, +-857, +-1142, +-1427, +-1712, +-1996, +-2280, +-2563, +-2845, +-3126, +-3406, +-3685, +-3963, +-4240, +-4516, +-4790, +-5062, +-5334, +-5603, +-5871, +-6137, +-6401, +-6663, +-6924, +-7182, +-7438, +-7691, +-7943, +-8192, +-8438, +-8682, +-8923, +-9161, +-9397, +-9630, +-9860, +-10086, +-10310, +-10531, +-10748, +-10963, +-11173, +-11381, +-11585, +-11785, +-11982, +-12175, +-12365, +-12550, +-12732, +-12910, +-13084, +-13254, +-13420, +-13582, +-13740, +-13894, +-14043, +-14188, +-14329, +-14466, +-14598, +-14725, +-14848, +-14967, +-15081, +-15190, +-15295, +-15395, +-15491, +-15582, +-15668, +-15749, +-15825, +-15897, +-15964, +-16025, +-16082, +-16135, +-16182, +-16224, +-16261, +-16294, +-16321, +-16344, +-16361, +-16374, +-16381, +-16384, +-16381, +-16374, +-16361, +-16344, +-16321, +-16294, +-16261, +-16224, +-16182, +-16135, +-16082, +-16025, +-15964, +-15897, +-15825, +-15749, +-15668, +-15582, +-15491, +-15395, +-15295, +-15190, +-15081, +-14967, +-14848, +-14725, +-14598, +-14466, +-14329, +-14188, +-14043, +-13894, +-13740, +-13582, +-13420, +-13254, +-13084, +-12910, +-12732, +-12550, +-12365, +-12175, +-11982, +-11785, +-11585, +-11381, +-11173, +-10963, +-10748, +-10531, +-10310, +-10086, +-9860, +-9630, +-9397, +-9161, +-8923, +-8682, +-8438, +-8192, +-7943, +-7691, +-7438, +-7182, +-6924, +-6663, +-6401, +-6137, +-5871, +-5603, +-5334, +-5062, +-4790, +-4516, +-4240, +-3963, +-3685, +-3406, +-3126, +-2845, +-2563, +-2280, +-1996, +-1712, +-1427, +-1142, +-857, +-571, +-285, +}; -- 2.11.4.GIT