2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "../../SDL_internal.h"
23 #if !SDL_RENDER_DISABLED
26 #include "SDL_drawline.h"
27 #include "SDL_drawpoint.h"
31 SDL_DrawLine1(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
36 int pitch = (dst->pitch / dst->format->BytesPerPixel);
39 pixel = (Uint8 *)dst->pixels + y1 * pitch + x1;
40 length = draw_end ? (x2-x1+1) : (x2-x1);
42 pixel = (Uint8 *)dst->pixels + y1 * pitch + x2;
46 length = draw_end ? (x1-x2+1) : (x1-x2);
48 SDL_memset(pixel, color, length);
49 } else if (x1 == x2) {
50 VLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
51 } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
52 DLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
54 BLINE(x1, y1, x2, y2, DRAW_FASTSETPIXELXY1, draw_end);
59 SDL_DrawLine2(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
63 HLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
64 } else if (x1 == x2) {
65 VLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
66 } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
67 DLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
70 const SDL_PixelFormat * fmt = dst->format;
71 SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
72 if (fmt->Rmask == 0x7C00) {
73 AALINE(x1, y1, x2, y2,
74 DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB555,
76 } else if (fmt->Rmask == 0xF800) {
77 AALINE(x1, y1, x2, y2,
78 DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB565,
81 AALINE(x1, y1, x2, y2,
82 DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY2_BLEND_RGB,
89 SDL_DrawLine4(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
93 HLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
94 } else if (x1 == x2) {
95 VLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
96 } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
97 DLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
100 const SDL_PixelFormat * fmt = dst->format;
101 SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
102 if (fmt->Rmask == 0x00FF0000) {
104 AALINE(x1, y1, x2, y2,
105 DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_RGB888,
108 AALINE(x1, y1, x2, y2,
109 DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_ARGB8888,
113 AALINE(x1, y1, x2, y2,
114 DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY4_BLEND_RGB,
120 typedef void (*DrawLineFunc) (SDL_Surface * dst,
121 int x1, int y1, int x2, int y2,
122 Uint32 color, SDL_bool draw_end);
125 SDL_CalculateDrawLineFunc(const SDL_PixelFormat * fmt)
127 switch (fmt->BytesPerPixel) {
129 if (fmt->BitsPerPixel < 8) {
132 return SDL_DrawLine1;
134 return SDL_DrawLine2;
136 return SDL_DrawLine4;
142 SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
147 return SDL_SetError("SDL_DrawLine(): Passed NULL destination surface");
150 func = SDL_CalculateDrawLineFunc(dst->format);
152 return SDL_SetError("SDL_DrawLine(): Unsupported surface format");
155 /* Perform clipping */
156 /* FIXME: We don't actually want to clip, as it may change line slope */
157 if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
161 func(dst, x1, y1, x2, y2, color, SDL_TRUE);
166 SDL_DrawLines(SDL_Surface * dst, const SDL_Point * points, int count,
176 return SDL_SetError("SDL_DrawLines(): Passed NULL destination surface");
179 func = SDL_CalculateDrawLineFunc(dst->format);
181 return SDL_SetError("SDL_DrawLines(): Unsupported surface format");
184 for (i = 1; i < count; ++i) {
190 /* Perform clipping */
191 /* FIXME: We don't actually want to clip, as it may change line slope */
192 if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
196 /* Draw the end if it was clipped */
197 draw_end = (x2 != points[i].x || y2 != points[i].y);
199 func(dst, x1, y1, x2, y2, color, draw_end);
201 if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
202 SDL_DrawPoint(dst, points[count-1].x, points[count-1].y, color);
207 #endif /* !SDL_RENDER_DISABLED */
209 /* vi: set ts=4 sw=4 expandtab: */