📄 sdl_gfxprimitives.c
字号:
h = (ix + 32) >> 6; i = (iy + 32) >> 6; j = (h * ry) / rx; k = (i * ry) / rx; if (((ok != k) && (oj != k)) || ((oj != j) && (ok != j)) || (k != j)) { xph = x + h; xmh = x - h; if (k > 0) { ypk = y + k; ymk = y - k; result |= pixelColorNolock (dst, xmh, ypk, color); result |= pixelColorNolock (dst, xph, ypk, color); result |= pixelColorNolock (dst, xmh, ymk, color); result |= pixelColorNolock (dst, xph, ymk, color); } else { result |= pixelColorNolock (dst, xmh, y, color); result |= pixelColorNolock (dst, xph, y, color); } ok = k; xpi = x + i; xmi = x - i; if (j > 0) { ypj = y + j; ymj = y - j; result |= pixelColorNolock (dst, xmi, ypj, color); result |= pixelColorNolock (dst, xpi, ypj, color); result |= pixelColorNolock (dst, xmi, ymj, color); result |= pixelColor(dst, xpi, ymj, color); } else { result |= pixelColorNolock (dst, xmi, y, color); result |= pixelColorNolock (dst, xpi, y, color); } oj = j; } ix = ix + iy / rx; iy = iy - ix / rx; } while (i > h); } else { ix = 0; iy = ry * 64; do { h = (ix + 32) >> 6; i = (iy + 32) >> 6; j = (h * rx) / ry; k = (i * rx) / ry; if (((oi != i) && (oh != i)) || ((oh != h) && (oi != h) && (i != h))) { xmj = x - j; xpj = x + j; if (i > 0) { ypi = y + i; ymi = y - i; result |= pixelColorNolock (dst, xmj, ypi, color); result |= pixelColorNolock (dst, xpj, ypi, color); result |= pixelColorNolock (dst, xmj, ymi, color); result |= pixelColorNolock (dst, xpj, ymi, color); } else { result |= pixelColorNolock (dst, xmj, y, color); result |= pixelColorNolock (dst, xpj, y, color); } oi = i; xmk = x - k; xpk = x + k; if (h > 0) { yph = y + h; ymh = y - h; result |= pixelColorNolock (dst, xmk, yph, color); result |= pixelColorNolock (dst, xpk, yph, color); result |= pixelColorNolock (dst, xmk, ymh, color); result |= pixelColorNolock (dst, xpk, ymh, color); } else { result |= pixelColorNolock (dst, xmk, y, color); result |= pixelColorNolock (dst, xpk, y, color); } oh = h; } ix = ix + iy / ry; iy = iy - ix / ry; } while (i > h); } } /* Alpha check */ /* Unlock surface */ if (SDL_MUSTLOCK(dst)) { SDL_UnlockSurface(dst); } return (result);}int ellipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a){ /* * Draw */ return (ellipseColor(dst, x, y, rx, ry, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a));}/* ----- AA Ellipse *//* Based on code from Anders Lindstroem, based on code from SGE, based on code from TwinLib */int aaellipseColor(SDL_Surface * dst, Sint16 xc, Sint16 yc, Sint16 rx, Sint16 ry, Uint32 color){ Sint16 left, right, top, bottom; Sint16 x1,y1,x2,y2; int i; int a2, b2, ds, dt, dxt, t, s, d; Sint16 x, y, xs, ys, dyt, xx, yy, xc2, yc2; float cp; Uint8 weight, iweight; int result; /* * Sanity check radii */ if ((rx < 0) || (ry < 0)) { return (-1); } /* * Special case for rx=0 - draw a vline */ if (rx == 0) { return (vlineColor(dst, xc, yc - ry, yc + ry, color)); } /* * Special case for ry=0 - draw a hline */ if (ry == 0) { return (hlineColor(dst, xc - rx, xc + rx, yc, color)); } /* * Get clipping boundary */ left = dst->clip_rect.x; right = dst->clip_rect.x + dst->clip_rect.w - 1; top = dst->clip_rect.y; bottom = dst->clip_rect.y + dst->clip_rect.h - 1; /* * Test if bounding box of ellipse is visible */ x1 = xc - rx; x2 = xc + rx; y1 = yc - ry; y2 = yc + ry; if ((x1<left) && (x2<left)) { return(0); } if ((x1>right) && (x2>right)) { return(0); } if ((y1<top) && (y2<top)) { return(0); } if ((y1>bottom) && (y2>bottom)) { return(0); } /* Variable setup */ a2 = rx * rx; b2 = ry * ry; ds = 2 * a2; dt = 2 * b2; xc2 = 2 * xc; yc2 = 2 * yc; dxt = (int) (a2 / sqrt(a2 + b2)); t = 0; s = -2 * a2 * ry; d = 0; x = xc; y = yc - ry; /* Draw */ result = 0; /* Lock surface */ if (SDL_MUSTLOCK(dst)) { if (SDL_LockSurface(dst) < 0) { return (-1); } } /* "End points" */ result |= pixelColorNolock(dst, x, y, color); result |= pixelColorNolock(dst, xc2 - x, y, color); result |= pixelColorNolock(dst, x, yc2 - y, color); result |= pixelColorNolock(dst, xc2 - x, yc2 - y, color); for (i = 1; i <= dxt; i++) { x--; d += t - b2; if (d >= 0) ys = y - 1; else if ((d - s - a2) > 0) { if ((2 * d - s - a2) >= 0) ys = y + 1; else { ys = y; y++; d -= s + a2; s += ds; } } else { y++; ys = y + 1; d -= s + a2; s += ds; } t -= dt; /* Calculate alpha */ if (s != 0.0) { cp = (float) abs(d) / (float) abs(s); if (cp > 1.0) { cp = 1.0; } } else { cp = 1.0; } /* Calculate weights */ weight = (Uint8) (cp * 255); iweight = 255 - weight; /* Upper half */ xx = xc2 - x; result |= pixelColorWeightNolock(dst, x, y, color, iweight); result |= pixelColorWeightNolock(dst, xx, y, color, iweight); result |= pixelColorWeightNolock(dst, x, ys, color, weight); result |= pixelColorWeightNolock(dst, xx, ys, color, weight); /* Lower half */ yy = yc2 - y; result |= pixelColorWeightNolock(dst, x, yy, color, iweight); result |= pixelColorWeightNolock(dst, xx, yy, color, iweight); yy = yc2 - ys; result |= pixelColorWeightNolock(dst, x, yy, color, weight); result |= pixelColorWeightNolock(dst, xx, yy, color, weight); } dyt = abs(y - yc); for (i = 1; i <= dyt; i++) { y++; d -= s + a2; if (d <= 0) xs = x + 1; else if ((d + t - b2) < 0) { if ((2 * d + t - b2) <= 0) xs = x - 1; else { xs = x; x--; d += t - b2; t -= dt; } } else { x--; xs = x - 1; d += t - b2; t -= dt; } s += ds; /* Calculate alpha */ if (t != 0.0) { cp = (float) abs(d) / (float) abs(t); if (cp > 1.0) { cp = 1.0; } } else { cp = 1.0; } /* Calculate weight */ weight = (Uint8) (cp * 255); iweight = 255 - weight; /* Left half */ xx = xc2 - x; yy = yc2 - y; result |= pixelColorWeightNolock(dst, x, y, color, iweight); result |= pixelColorWeightNolock(dst, xx, y, color, iweight); result |= pixelColorWeightNolock(dst, x, yy, color, iweight); result |= pixelColorWeightNolock(dst, xx, yy, color, iweight); /* Right half */ xx = 2 * xc - xs; result |= pixelColorWeightNolock(dst, xs, y, color, weight); result |= pixelColorWeightNolock(dst, xx, y, color, weight); result |= pixelColorWeightNolock(dst, xs, yy, color, weight); result |= pixelColorWeightNolock(dst, xx, yy, color, weight); } /* Unlock surface */ if (SDL_MUSTLOCK(dst)) { SDL_UnlockSurface(dst); } return (result);}int aaellipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a){ /* * Draw */ return (aaellipseColor (dst, x, y, rx, ry, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a));}/* ---- Filled Ellipse *//* Note: *//* Based on algorithm from sge library with multiple-hline draw removal *//* and other speedup changes. */int filledEllipseColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint32 color){ Sint16 left, right, top, bottom; int result; Sint16 x1, y1, x2, y2; int ix, iy; int h, i, j, k; int oh, oi, oj, ok; int xmh, xph; int xmi, xpi; int xmj, xpj; int xmk, xpk; /* * Sanity check radii */ if ((rx < 0) || (ry < 0)) { return (-1); } /* * Special case for rx=0 - draw a vline */ if (rx == 0) { return (vlineColor(dst, x, y - ry, y + ry, color)); } /* * Special case for ry=0 - draw a hline */ if (ry == 0) { return (hlineColor(dst, x - rx, x + rx, y, color)); } /* * Get clipping boundary */ left = dst->clip_rect.x; right = dst->clip_rect.x + dst->clip_rect.w - 1; top = dst->clip_rect.y; bottom = dst->clip_rect.y + dst->clip_rect.h - 1; /* * Test if bounding box of ellipse is visible */ x1 = x - rx; x2 = x + rx; y1 = y - ry; y2 = y + ry; if ((x1<left) && (x2<left)) { return(0); } if ((x1>right) && (x2>right)) { return(0); } if ((y1<top) && (y2<top)) { return(0); } if ((y1>bottom) && (y2>bottom)) { return(0); } /* * Init vars */ oh = oi = oj = ok = 0xFFFF; /* * Draw */ result = 0; if (rx > ry) { ix = 0; iy = rx * 64; do { h = (ix + 32) >> 6; i = (iy + 32) >> 6; j = (h * ry) / rx; k = (i * ry) / rx; if ((ok != k) && (oj != k)) { xph = x + h; xmh = x - h; if (k > 0) { result |= hlineColor(dst, xmh, xph, y + k, color); result |= hlineColor(dst, xmh, xph, y - k, color); } else { result |= hlineColor(dst, xmh, xph, y, color); } ok = k; } if ((oj != j) && (ok != j) && (k != j)) { xmi = x - i; xpi = x + i; if (j > 0) { result |= hlineColor(dst, xmi, xpi, y + j, color); result |= hlineColor(dst, xmi, xpi, y - j, color); } else { result |= hlineColor(dst, xmi, xpi, y, color); } oj = j; } ix = ix + iy / rx; iy = iy - ix / rx; } while (i > h); } else { ix = 0; iy = ry * 64; do { h = (ix + 32) >> 6; i = (iy + 32) >> 6; j = (h * rx) / ry; k = (i * rx) / ry; if ((oi != i) && (oh != i)) { xmj = x - j; xpj = x + j; if (i > 0) { result |= hlineColor(dst, xmj, xpj, y + i, color); result |= hlineColor(dst, xmj, xpj, y - i, color); } else { result |= hlineColor(dst, xmj, xpj, y, color); } oi = i; } if ((oh != h) && (oi != h) && (i != h)) { xmk = x - k; xpk = x + k; if (h > 0) { result |= hlineColor(dst, xmk, xpk, y + h, color); result |= hlineColor(dst, xmk, xpk, y - h, color); } else { result |= hlineColor(dst, xmk, xpk, y, color); } oh = h; } ix = ix + iy / ry; iy = iy - ix / ry; } while (i > h); } return (result);}int filledEllipseRGBA(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rx, Sint16 ry, Uint8 r, Uint8 g, Uint8 b, Uint8 a){ /* * Draw */ return (filledEllipseColor (dst, x, y, rx, ry, ((Uint32) r << 24) | ((Uint32) g << 16) | ((Uint32) b << 8) | (Uint32) a));}/* ----- filled pie *//* Low-speed float pie-calc implementation by drawing polygons/lines. */int doPieColor(SDL_Surface * dst, Sint16 x, Sint16 y, Sint16 rad, Sint16 start, Sint16 end, Uint32 color, Uint8 filled){ Sint16 left, right, top, bottom; Sint16 x1, y1, x2, y2; int result; double angle, start_angle, end_angle; double deltaAngle; double dr; int posX, posY; int numpoints, i; Sint16 *vx, *vy; /* * Sanity check radii */ if (rad < 0) { return (-1); } /* * Fixup angles */ start = start % 360; end = end % 360; /* * Special case for rad=0 - draw a point */ if (rad == 0) { return (pixelColor(dst, x, y, color)); } /* * Get clipping boundary */ left = dst->clip_rect.x; right = dst->clip_rect.x + dst->clip_rect.w - 1; top = dst->clip_rect.y; bottom = dst->clip_rect.y + dst->clip_rect.h - 1; /* * Test if bounding box of pie's circle is visible */ x1 = x - rad; x2 = x + rad; y1 = y - rad; y2 = y + rad; if ((x1<left) && (x2<left)) { return(0); } if ((x1>right) && (x2>right)) { return(0); } if ((y1<top) && (y2<top)) { return(0); } if ((y1>bottom) && (y2>bottom)) { return(0); } /* * Variable setup */ dr = (double) rad; deltaAngle = 3.0 / dr; start_angle = (double) start *(2.0 * M_PI / 360.0); end_angle = (double) end *(2.0 * M_PI / 360.0); if (start > end) { end_angle += (2.0 * M_PI); } /* Count points (rather than calculate it) */ numpoints = 1; angle = start_angle; while (angle <= end_angle) { angle += deltaAngle; numpoints++; } /* Check size of array */ if (numpoints == 1) { return (pixelColor(dst, x, y, color)); } else if (numpoints == 2) { posX = x + (int) (dr * cos(start_angle)); posY = y + (int) (dr * sin(start_angle)); return (lineColor(dst, x, y, posX, posY, color)); } /* Allocate vertex array */ vx = vy = (Uint16 *) malloc(2 * sizeof(Uint16) * numpoints); if (vx == NULL) { return (-1); } vy += numpoints; /* Center */ vx[0] = x; vy[0] = y; /* Calculate and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -