From 1373658abe0981513a8d9bb351fb21191d36d6f6 Mon Sep 17 00:00:00 2001 From: Joris <joris.dauphin@gmail.com> Date: Tue, 21 Aug 2012 16:08:46 +0200 Subject: [PATCH] Some clean up: - use std::swap --- src/video/linedraw.cpp | 282 +++++++++++++---------------------------- 1 file changed, 90 insertions(+), 192 deletions(-) diff --git a/src/video/linedraw.cpp b/src/video/linedraw.cpp index 8a1d046c4..b989fc423 100644 --- a/src/video/linedraw.cpp +++ b/src/video/linedraw.cpp @@ -104,15 +104,12 @@ void VideoDrawPixel32(Uint32 color, int x, int y) */ static void VideoDoDrawTransPixel16(Uint32 color, int x, int y, unsigned char alpha) { - Uint16 *p; - unsigned long dp; - // Loses precision for speed alpha = (255 - alpha) >> 3; - p = &((Uint16 *)TheScreen->pixels)[x + y * Video.Width]; + Uint16 *p = &((Uint16 *)TheScreen->pixels)[x + y * Video.Width]; color = (((color << 16) | color) & 0x07E0F81F); - dp = *p; + unsigned long dp = *p; dp = ((dp << 16) | dp) & 0x07E0F81F; dp = ((((dp - color) * alpha) >> 5) + color) & 0x07E0F81F; *p = (Uint16)((dp >> 16) | dp); @@ -133,20 +130,15 @@ void VideoDrawTransPixel16(Uint32 color, int x, int y, unsigned char alpha) */ static void VideoDoDrawTransPixel32(Uint32 color, int x, int y, unsigned char alpha) { - unsigned long sp2; - unsigned long dp1; - unsigned long dp2; - Uint32 *p; - alpha = 255 - alpha; - p = &((Uint32 *)TheScreen->pixels)[x + y * Video.Width]; + Uint32 *p = &((Uint32 *)TheScreen->pixels)[x + y * Video.Width]; - sp2 = (color & 0xFF00FF00) >> 8; + const unsigned long sp2 = (color & 0xFF00FF00) >> 8; color &= 0x00FF00FF; - dp1 = *p; - dp2 = (dp1 & 0xFF00FF00) >> 8; + unsigned long dp1 = *p; + unsigned long dp2 = (dp1 & 0xFF00FF00) >> 8; dp1 &= 0x00FF00FF; dp1 = ((((dp1 - color) * alpha) >> 8) + color) & 0x00FF00FF; @@ -305,12 +297,6 @@ void DrawTransHLineClip(Uint32 color, int x, int y, */ void DrawLine(Uint32 color, int sx, int sy, int dx, int dy) { - int x; - int y; - int xlen; - int ylen; - int incr; - if (sx == dx) { if (sy < dy) { DrawVLine(color, sx, sy, dy - sy + 1); @@ -331,15 +317,13 @@ void DrawLine(Uint32 color, int sx, int sy, int dx, int dy) // exchange coordinates if (sy > dy) { - int t; - t = dx; - dx = sx; - sx = t; - t = dy; - dy = sy; - sy = t; + std::swap(dx, sx); + std::swap(dy, sy); } - ylen = dy - sy; + int xlen; + int incr; + + int ylen = dy - sy; if (sx > dx) { xlen = sx - dx; @@ -349,17 +333,14 @@ void DrawLine(Uint32 color, int sx, int sy, int dx, int dy) incr = 1; } - y = sy; - x = sx; + int y = sy; + int x = sx; if (xlen > ylen) { int p; if (sx > dx) { - int t; - t = sx; - sx = dx; - dx = t; + std::swap(sx, dx); y = dy; } @@ -380,9 +361,7 @@ void DrawLine(Uint32 color, int sx, int sy, int dx, int dy) } if (ylen > xlen) { - int p; - - p = (xlen << 1) - ylen; + int p = (xlen << 1) - ylen; Video.LockScreen(); for (y = sy; y < dy; ++y) { @@ -415,12 +394,6 @@ void DrawLine(Uint32 color, int sx, int sy, int dx, int dy) */ void DrawLineClip(Uint32 color, int sx, int sy, int dx, int dy) { - int x; - int y; - int xlen; - int ylen; - int incr; - if (sx == dx) { if (sy < dy) { DrawVLineClip(color, sx, sy, dy - sy + 1); @@ -441,15 +414,12 @@ void DrawLineClip(Uint32 color, int sx, int sy, int dx, int dy) // exchange coordinates if (sy > dy) { - int t; - t = dx; - dx = sx; - sx = t; - t = dy; - dy = sy; - sy = t; + std::swap(dx, sx); + std::swap(dy, sy); } - ylen = dy - sy; + int ylen = dy - sy; + int xlen; + int incr; if (sx > dx) { xlen = sx - dx; @@ -459,21 +429,16 @@ void DrawLineClip(Uint32 color, int sx, int sy, int dx, int dy) incr = 1; } - y = sy; - x = sx; + int y = sy; + int x = sx; if (xlen > ylen) { - int p; - if (sx > dx) { - int t; - t = sx; - sx = dx; - dx = t; + std::swap(dx, sx); y = dy; } - p = (ylen << 1) - xlen; + int p = (ylen << 1) - xlen; Video.LockScreen(); for (x = sx; x < dx; ++x) { @@ -490,9 +455,7 @@ void DrawLineClip(Uint32 color, int sx, int sy, int dx, int dy) } if (ylen > xlen) { - int p; - - p = (xlen << 1) - ylen; + int p = (xlen << 1) - ylen; Video.LockScreen(); for (y = sy; y < dy; ++y) { @@ -660,15 +623,11 @@ void FillTransRectangleClip(Uint32 color, int x, int y, */ void DrawCircle(Uint32 color, int x, int y, int r) { - int p; - int px; - int py; - - p = 1 - r; - py = r; + int p = 1 - r; + int py = r; Video.LockScreen(); - for (px = 0; px <= py + 1; ++px) { + for (int px = 0; px <= py + 1; ++px) { VideoDoDrawPixel(color, x + px, y + py); VideoDoDrawPixel(color, x + px, y - py); VideoDoDrawPixel(color, x - px, y + py); @@ -695,15 +654,11 @@ void DrawCircle(Uint32 color, int x, int y, int r) void DrawTransCircle(Uint32 color, int x, int y, int r, unsigned char alpha) { - int p; - int px; - int py; - - p = 1 - r; - py = r; + int p = 1 - r; + int py = r; Video.LockScreen(); - for (px = 0; px <= py + 1; ++px) { + for (int px = 0; px <= py + 1; ++px) { VideoDoDrawTransPixel(color, x + px, y + py, alpha); VideoDoDrawTransPixel(color, x + px, y - py, alpha); VideoDoDrawTransPixel(color, x - px, y + py, alpha); @@ -729,15 +684,11 @@ void DrawTransCircle(Uint32 color, int x, int y, */ void DrawCircleClip(Uint32 color, int x, int y, int r) { - int p; - int px; - int py; - - p = 1 - r; - py = r; + int p = 1 - r; + int py = r; Video.LockScreen(); - for (px = 0; px <= py + 1; ++px) { + for (int px = 0; px <= py + 1; ++px) { VideoDoDrawPixelClip(color, x + px, y + py); VideoDoDrawPixelClip(color, x + px, y - py); VideoDoDrawPixelClip(color, x - px, y + py); @@ -764,15 +715,11 @@ void DrawCircleClip(Uint32 color, int x, int y, int r) void DrawTransCircleClip(Uint32 color, int x, int y, int r, unsigned char alpha) { - int p; - int px; - int py; - - p = 1 - r; - py = r; + int p = 1 - r; + int py = r; Video.LockScreen(); - for (px = 0; px <= py + 1; ++px) { + for (int px = 0; px <= py + 1; ++px) { VideoDoDrawTransPixelClip(color, x + px, y + py, alpha); VideoDoDrawTransPixelClip(color, x + px, y - py, alpha); VideoDoDrawTransPixelClip(color, x - px, y + py, alpha); @@ -798,14 +745,10 @@ void DrawTransCircleClip(Uint32 color, int x, int y, */ void FillCircle(Uint32 color, int x, int y, int r) { - int p; - int px; - int py; + int p = 1 - r; + int py = r; - p = 1 - r; - py = r; - - for (px = 0; px <= py; ++px) { + for (int px = 0; px <= py; ++px) { // Fill up the middle half of the circle DrawVLine(color, x + px, y, py + 1); @@ -837,14 +780,10 @@ void FillCircle(Uint32 color, int x, int y, int r) void FillTransCircle(Uint32 color, int x, int y, int r, unsigned char alpha) { - int p; - int px; - int py; + int p = 1 - r; + int py = r; - p = 1 - r; - py = r; - - for (px = 0; px <= py; ++px) { + for (int px = 0; px <= py; ++px) { // Fill up the middle half of the circle DrawTransVLine(color, x + px, y, py + 1, alpha); @@ -875,14 +814,10 @@ void FillTransCircle(Uint32 color, int x, int y, */ void FillCircleClip(Uint32 color, int x, int y, int r) { - int p; - int px; - int py; + int p = 1 - r; + int py = r; - p = 1 - r; - py = r; - - for (px = 0; px <= py; ++px) { + for (int px = 0; px <= py; ++px) { // Fill up the middle half of the circle DrawVLineClip(color, x + px, y, py + 1); @@ -914,14 +849,10 @@ void FillCircleClip(Uint32 color, int x, int y, int r) void FillTransCircleClip(Uint32 color, int x, int y, int r, unsigned char alpha) { - int p; - int px; - int py; + int p = 1 - r; + int py = r; - p = 1 - r; - py = r; - - for (px = 0; px <= py; ++px) { + for (int px = 0; px <= py; ++px) { // Fill up the middle half of the circle DrawTransVLineClip(color, x + px, y, py + 1, alpha); @@ -1012,8 +943,7 @@ void DrawPixel(Uint32 color, int x, int y) ** @param y y coordinate on the screen ** @param alpha alpha value of pixel. */ -void DrawTransPixel(Uint32 color, int x, int y, - unsigned char alpha) +void DrawTransPixel(Uint32 color, int x, int y, unsigned char alpha) { GLubyte r, g, b; @@ -1045,8 +975,7 @@ void DrawPixelClip(Uint32 color, int x, int y) ** @param y y coordinate on the screen ** @param alpha alpha value of pixel. */ -void DrawTransPixelClip(Uint32 color, int x, int y, - unsigned char alpha) +void DrawTransPixelClip(Uint32 color, int x, int y, unsigned char alpha) { GLubyte r, g, b; @@ -1100,8 +1029,7 @@ void DrawHLine(Uint32 color, int x, int y, int width) ** @param width width of line (0=don't draw). ** @param alpha alpha value of pixels. */ -void DrawTransHLine(Uint32 color, int x, int y, int width, - unsigned char alpha) +void DrawTransHLine(Uint32 color, int x, int y, int width, unsigned char alpha) { GLubyte r, g, b; @@ -1149,8 +1077,7 @@ void DrawHLineClip(Uint32 color, int x, int y, int width) ** @param width Width of line (0=don't draw) ** @param alpha Alpha value of pixels */ -void DrawTransHLineClip(Uint32 color, int x, int y, int width, - unsigned char alpha) +void DrawTransHLineClip(Uint32 color, int x, int y, int width, unsigned char alpha) { GLubyte r, g, b; @@ -1204,8 +1131,7 @@ void DrawVLine(Uint32 color, int x, int y, int height) ** @param height height of line (0=don't draw). ** @param alpha alpha value of pixels. */ -void DrawTransVLine(Uint32 color, int x, int y, int height, - unsigned char alpha) +void DrawTransVLine(Uint32 color, int x, int y, int height, unsigned char alpha) { GLubyte r, g, b; @@ -1253,8 +1179,7 @@ void DrawVLineClip(Uint32 color, int x, int y, int height) ** @param height height of line (0=don't draw). ** @param alpha alpha value of pixels. */ -void DrawTransVLineClip(Uint32 color, int x, int y, - int height, unsigned char alpha) +void DrawTransVLineClip(Uint32 color, int x, int y, int height, unsigned char alpha) { GLubyte r, g, b; @@ -1274,13 +1199,12 @@ void DrawTransVLineClip(Uint32 color, int x, int y, */ void DrawLine(Uint32 color, int x1, int y1, int x2, int y2) { - float xx1, yy1, xx2, yy2; GLubyte r, g, b, a; - xx1 = (float)x1; - xx2 = (float)x2; - yy1 = (float)y1; - yy2 = (float)y2; + float xx1 = (float)x1; + float xx2 = (float)x2; + float yy1 = (float)y1; + float yy2 = (float)y2; if (xx1 <= xx2) { xx2 += 0.5f; @@ -1385,7 +1309,6 @@ void DrawLineClip(Uint32 color, int x1, int y1, int x2, int y2) { int code1; int code2; - int temp; // Make sure coordinates or on/in clipped rectangle while (code1 = ClipCodeLine(x1, y1), code2 = ClipCodeLine(x2, y2), @@ -1395,25 +1318,25 @@ void DrawLineClip(Uint32 color, int x1, int y1, int x2, int y2) } if (!code1) { - temp = x1; x1 = x2; x2 = temp; - temp = y1; y1 = y2; y2 = temp; + std::swap(x1, x2); + std::swap(y1, y2); code1 = code2; } if (code1 & ClipCodeAbove) { - temp = ClipY1; + int temp = ClipY1; x1 += (int)(((long)(temp - y1) * (x2 - x1)) / (y2 - y1)); y1 = temp; } else if (code1 & ClipCodeBelow) { - temp = ClipY2; + int temp = ClipY2; x1 += (int)(((long)(temp - y1) * (x2 - x1)) / (y2 - y1)); y1 = temp; } else if (code1 & ClipCodeLeft) { - temp = ClipX1; + int temp = ClipX1; y1 += (int)(((long)(temp - x1) * (y2 - y1)) / (x2 - x1)); x1 = temp; } else { /* code1 & ClipCodeRight */ - temp = ClipX2; + int temp = ClipX2; y1 += (int)(((long)(temp - x1) * (y2 - y1)) / (x2 - x1)); x1 = temp; } @@ -1533,12 +1456,6 @@ void DrawTransRectangle(Uint32 color, int x, int y, void DrawRectangleClip(Uint32 color, int x, int y, int w, int h) { - int f; - int left; - int right; - int top; - int bottom; - // Ensure non-empty rectangle if (!w || !h) { // rectangle is `void' @@ -1546,10 +1463,13 @@ void DrawRectangleClip(Uint32 color, int x, int y, } // Clip rectangle boundary - left = right = top = bottom = 1; + int left = 1; + int right = 1; + int top = 1; + int bottom = 1; if (x < ClipX1) { // no left side - f = ClipX1 - x; + int f = ClipX1 - x; if (w <= f) { return; // entire rectangle left --> not visible } @@ -1565,7 +1485,7 @@ void DrawRectangleClip(Uint32 color, int x, int y, right = 0; } if (y < ClipY1) { // no top - f = ClipY1 - y; + int f = ClipY1 - y; if (h <= f) { return; // entire rectangle above --> not visible } @@ -1734,17 +1654,11 @@ void FillTransRectangleClip(Uint32 color, int x, int y, */ void DrawCircle(Uint32 color, int x, int y, int radius) { - int cx; - int cy; - int df; - int d_e; - int d_se; - - cx = 0; - cy = radius; - df = 1 - radius; - d_e = 3; - d_se = -2 * radius + 5; + int cx = 0; + int cy = radius; + int df = 1 - radius; + int d_e = 3; + int d_se = -2 * radius + 5; // FIXME: could be much improved :) do { @@ -1793,17 +1707,11 @@ void DrawCircle(Uint32 color, int x, int y, int radius) */ void DrawCircleClip(Uint32 color, int x, int y, int radius) { - int cx; - int cy; - int df; - int d_e; - int d_se; - - cx = 0; - cy = radius; - df = 1 - radius; - d_e = 3; - d_se = -2 * radius + 5; + int cx = 0; + int cy = radius; + int df = 1 - radius; + int d_e = 3; + int d_se = -2 * radius + 5; // FIXME: could be much improved :) do { @@ -1890,14 +1798,10 @@ void DrawTransCircleClip(Uint32 color, int x, int y, int radius, */ void FillCircle(Uint32 color, int x, int y, int radius) { - int p; - int px; - int py; + int p = 1 - radius; + int py = radius; - p = 1 - radius; - py = radius; - - for (px = 0; px <= py; ++px) { + for (int px = 0; px <= py; ++px) { // Fill up the middle half of the circle DrawVLine(color, x + px, y, py + 1); DrawVLine(color, x + px, y - py, py); @@ -1951,17 +1855,11 @@ void FillTransCircle(Uint32 color, int x, int y, */ void FillCircleClip(Uint32 color, int x, int y, int radius) { - int cx; - int cy; - int df; - int d_e; - int d_se; - - cx = 0; - cy = radius; - df = 1 - radius; - d_e = 3; - d_se = -2 * radius + 5; + int cx = 0; + int cy = radius; + int df = 1 - radius; + int d_e = 3; + int d_se = -2 * radius + 5; // FIXME: could be much improved :) do {