2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2011 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 #if !SDL_RENDER_DISABLED
27 #include "SDL_drawline.h"
28 #include "SDL_drawpoint.h"
32 SDL_DrawLine1(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
36 //HLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
38 int pitch = (dst->pitch / dst->format->BytesPerPixel);
41 pixel = (Uint8 *)dst->pixels + y1 * pitch + x1;
42 length = draw_end ? (x2-x1+1) : (x2-x1);
44 pixel = (Uint8 *)dst->pixels + y1 * pitch + x2;
48 length = draw_end ? (x1-x2+1) : (x1-x2);
50 SDL_memset(pixel, color, length);
51 } else if (x1 == x2) {
52 VLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
53 } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
54 DLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
56 BLINE(x1, y1, x2, y2, DRAW_FASTSETPIXELXY1, draw_end);
61 SDL_DrawLine2(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
65 HLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
66 } else if (x1 == x2) {
67 VLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
68 } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
69 DLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
72 const SDL_PixelFormat * fmt = dst->format;
73 SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
74 if (fmt->Rmask == 0x7C00) {
75 AALINE(x1, y1, x2, y2,
76 DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB555,
78 } else if (fmt->Rmask == 0xF800) {
79 AALINE(x1, y1, x2, y2,
80 DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB565,
83 AALINE(x1, y1, x2, y2,
84 DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY2_BLEND_RGB,
91 SDL_DrawLine4(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
95 HLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
96 } else if (x1 == x2) {
97 VLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
98 } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
99 DLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
101 Uint8 _r, _g, _b, _a;
102 const SDL_PixelFormat * fmt = dst->format;
103 SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
104 if (fmt->Rmask == 0x00FF0000) {
106 AALINE(x1, y1, x2, y2,
107 DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_RGB888,
110 AALINE(x1, y1, x2, y2,
111 DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_ARGB8888,
115 AALINE(x1, y1, x2, y2,
116 DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY4_BLEND_RGB,
122 typedef void (*DrawLineFunc) (SDL_Surface * dst,
123 int x1, int y1, int x2, int y2,
124 Uint32 color, SDL_bool draw_end);
127 SDL_CalculateDrawLineFunc(const SDL_PixelFormat * fmt)
129 switch (fmt->BytesPerPixel) {
131 if (fmt->BitsPerPixel < 8) {
134 return SDL_DrawLine1;
136 return SDL_DrawLine2;
138 return SDL_DrawLine4;
144 SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
149 SDL_SetError("SDL_DrawLine(): Passed NULL destination surface");
153 func = SDL_CalculateDrawLineFunc(dst->format);
155 SDL_SetError("SDL_DrawLine(): Unsupported surface format");
159 /* Perform clipping */
160 /* FIXME: We don't actually want to clip, as it may change line slope */
161 if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
165 func(dst, x1, y1, x2, y2, color, SDL_TRUE);
170 SDL_DrawLines(SDL_Surface * dst, const SDL_Point * points, int count,
180 SDL_SetError("SDL_DrawLines(): Passed NULL destination surface");
184 func = SDL_CalculateDrawLineFunc(dst->format);
186 SDL_SetError("SDL_DrawLines(): Unsupported surface format");
190 for (i = 1; i < count; ++i) {
196 /* Perform clipping */
197 /* FIXME: We don't actually want to clip, as it may change line slope */
198 if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
202 /* Draw the end if it was clipped */
203 draw_end = (x2 != points[i].x || y2 != points[i].y);
205 func(dst, x1, y1, x2, y2, color, draw_end);
207 if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
208 SDL_DrawPoint(dst, points[count-1].x, points[count-1].y, color);
213 #endif /* !SDL_RENDER_DISABLED */
215 /* vi: set ts=4 sw=4 expandtab: */