| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any damages |
| 7 | arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any purpose, |
| 10 | including commercial applications, and to alter it and redistribute it |
| 11 | freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must not |
| 14 | claim that you wrote the original software. If you use this software |
| 15 | in a product, an acknowledgment in the product documentation would be |
| 16 | appreciated but is not required. |
| 17 | 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | misrepresented as being the original software. |
| 19 | 3. This notice may not be removed or altered from any source distribution. |
| 20 | */ |
| 21 | #include "SDL_internal.h" |
| 22 | |
| 23 | #ifdef SDL_VIDEO_RENDER_SW |
| 24 | |
| 25 | #include "SDL_draw.h" |
| 26 | #include "SDL_drawline.h" |
| 27 | #include "SDL_drawpoint.h" |
| 28 | |
| 29 | static void SDL_DrawLine1(SDL_Surface *dst, int x1, int y1, int x2, int y2, Uint32 color, |
| 30 | bool draw_end) |
| 31 | { |
| 32 | if (y1 == y2) { |
| 33 | int length; |
| 34 | int pitch = (dst->pitch / dst->fmt->bytes_per_pixel); |
| 35 | Uint8 *pixel; |
| 36 | if (x1 <= x2) { |
| 37 | pixel = (Uint8 *)dst->pixels + y1 * pitch + x1; |
| 38 | length = draw_end ? (x2 - x1 + 1) : (x2 - x1); |
| 39 | } else { |
| 40 | pixel = (Uint8 *)dst->pixels + y1 * pitch + x2; |
| 41 | if (!draw_end) { |
| 42 | ++pixel; |
| 43 | } |
| 44 | length = draw_end ? (x1 - x2 + 1) : (x1 - x2); |
| 45 | } |
| 46 | SDL_memset(pixel, color, length); |
| 47 | } else if (x1 == x2) { |
| 48 | VLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end); |
| 49 | } else if (ABS(x1 - x2) == ABS(y1 - y2)) { |
| 50 | DLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end); |
| 51 | } else { |
| 52 | BLINE(x1, y1, x2, y2, DRAW_FASTSETPIXELXY1, draw_end); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static void SDL_DrawLine2(SDL_Surface *dst, int x1, int y1, int x2, int y2, Uint32 color, |
| 57 | bool draw_end) |
| 58 | { |
| 59 | if (y1 == y2) { |
| 60 | HLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end); |
| 61 | } else if (x1 == x2) { |
| 62 | VLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end); |
| 63 | } else if (ABS(x1 - x2) == ABS(y1 - y2)) { |
| 64 | DLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end); |
| 65 | } else { |
| 66 | Uint8 _r, _g, _b, _a; |
| 67 | const SDL_PixelFormatDetails *fmt = dst->fmt; |
| 68 | SDL_GetRGBA(color, fmt, dst->palette, &_r, &_g, &_b, &_a); |
| 69 | if (fmt->Rmask == 0x7C00) { |
| 70 | AALINE(x1, y1, x2, y2, |
| 71 | DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB555, |
| 72 | draw_end); |
| 73 | } else if (fmt->Rmask == 0xF800) { |
| 74 | AALINE(x1, y1, x2, y2, |
| 75 | DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB565, |
| 76 | draw_end); |
| 77 | } else { |
| 78 | AALINE(x1, y1, x2, y2, |
| 79 | DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY2_BLEND_RGB, |
| 80 | draw_end); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static void SDL_DrawLine4(SDL_Surface *dst, int x1, int y1, int x2, int y2, Uint32 color, |
| 86 | bool draw_end) |
| 87 | { |
| 88 | if (y1 == y2) { |
| 89 | HLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end); |
| 90 | } else if (x1 == x2) { |
| 91 | VLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end); |
| 92 | } else if (ABS(x1 - x2) == ABS(y1 - y2)) { |
| 93 | DLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end); |
| 94 | } else { |
| 95 | Uint8 _r, _g, _b, _a; |
| 96 | const SDL_PixelFormatDetails *fmt = dst->fmt; |
| 97 | SDL_GetRGBA(color, fmt, dst->palette, &_r, &_g, &_b, &_a); |
| 98 | if (fmt->Rmask == 0x00FF0000) { |
| 99 | if (!fmt->Amask) { |
| 100 | AALINE(x1, y1, x2, y2, |
| 101 | DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_XRGB8888, |
| 102 | draw_end); |
| 103 | } else { |
| 104 | AALINE(x1, y1, x2, y2, |
| 105 | DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_ARGB8888, |
| 106 | draw_end); |
| 107 | } |
| 108 | } else { |
| 109 | AALINE(x1, y1, x2, y2, |
| 110 | DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY4_BLEND_RGB, |
| 111 | draw_end); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | typedef void (*DrawLineFunc)(SDL_Surface *dst, |
| 117 | int x1, int y1, int x2, int y2, |
| 118 | Uint32 color, bool draw_end); |
| 119 | |
| 120 | static DrawLineFunc SDL_CalculateDrawLineFunc(const SDL_PixelFormatDetails *fmt) |
| 121 | { |
| 122 | switch (fmt->bytes_per_pixel) { |
| 123 | case 1: |
| 124 | if (fmt->bits_per_pixel < 8) { |
| 125 | break; |
| 126 | } |
| 127 | return SDL_DrawLine1; |
| 128 | case 2: |
| 129 | return SDL_DrawLine2; |
| 130 | case 4: |
| 131 | return SDL_DrawLine4; |
| 132 | } |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | bool SDL_DrawLine(SDL_Surface *dst, int x1, int y1, int x2, int y2, Uint32 color) |
| 137 | { |
| 138 | DrawLineFunc func; |
| 139 | |
| 140 | if (!SDL_SurfaceValid(dst)) { |
| 141 | return SDL_InvalidParamError("SDL_DrawLine(): dst" ); |
| 142 | } |
| 143 | |
| 144 | func = SDL_CalculateDrawLineFunc(dst->fmt); |
| 145 | if (!func) { |
| 146 | return SDL_SetError("SDL_DrawLine(): Unsupported surface format" ); |
| 147 | } |
| 148 | |
| 149 | // Perform clipping |
| 150 | // FIXME: We don't actually want to clip, as it may change line slope |
| 151 | if (!SDL_GetRectAndLineIntersection(&dst->clip_rect, &x1, &y1, &x2, &y2)) { |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | func(dst, x1, y1, x2, y2, color, true); |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | bool SDL_DrawLines(SDL_Surface *dst, const SDL_Point *points, int count, Uint32 color) |
| 160 | { |
| 161 | int i; |
| 162 | int x1, y1; |
| 163 | int x2, y2; |
| 164 | bool draw_end; |
| 165 | DrawLineFunc func; |
| 166 | |
| 167 | if (!SDL_SurfaceValid(dst)) { |
| 168 | return SDL_InvalidParamError("SDL_DrawLines(): dst" ); |
| 169 | } |
| 170 | |
| 171 | func = SDL_CalculateDrawLineFunc(dst->fmt); |
| 172 | if (!func) { |
| 173 | return SDL_SetError("SDL_DrawLines(): Unsupported surface format" ); |
| 174 | } |
| 175 | |
| 176 | for (i = 1; i < count; ++i) { |
| 177 | x1 = points[i - 1].x; |
| 178 | y1 = points[i - 1].y; |
| 179 | x2 = points[i].x; |
| 180 | y2 = points[i].y; |
| 181 | |
| 182 | // Perform clipping |
| 183 | // FIXME: We don't actually want to clip, as it may change line slope |
| 184 | if (!SDL_GetRectAndLineIntersection(&dst->clip_rect, &x1, &y1, &x2, &y2)) { |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | // Draw the end if the whole line is a single point or it was clipped |
| 189 | draw_end = ((x1 == x2) && (y1 == y2)) || (x2 != points[i].x || y2 != points[i].y); |
| 190 | |
| 191 | func(dst, x1, y1, x2, y2, color, draw_end); |
| 192 | } |
| 193 | if (points[0].x != points[count - 1].x || points[0].y != points[count - 1].y) { |
| 194 | SDL_DrawPoint(dst, points[count - 1].x, points[count - 1].y, color); |
| 195 | } |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | #endif // SDL_VIDEO_RENDER_SW |
| 200 | |