annotate src/video/SDL_drawline.c @ 2893:e67f3f3bf221

Turn on line antialiasing (requires blending to be enabled)
author Sam Lantinga <slouken@libsdl.org>
date Sat, 20 Dec 2008 18:40:30 +0000
parents 1863c8b59658
children 1ef2f1e75ff7
rev   line source
2888
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 Copyright (C) 1997-2009 Sam Lantinga
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 Lesser General Public License for more details.
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 Sam Lantinga
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 slouken@libsdl.org
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21 */
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 #include "SDL_config.h"
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 #include "SDL_video.h"
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 #include "SDL_blit.h"
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26
2890
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
27 #define ABS(x) (x < 0 ? -x : x)
2888
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28
2890
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
29 #define SWAP(x, y) (x ^= y ^= x ^= y)
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
30
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
31 #define BRESENHAM(x0, y0, x1, y1, op, color) \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
32 { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
33 int deltax, deltay, steep, error, xstep, ystep, x, y; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
34 \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
35 deltax = ABS(x1 - x0); \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
36 deltay = ABS(y1 - y0); \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
37 steep = deltay > deltax; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
38 error = deltax / 2; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
39 if (steep) { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
40 SWAP(x0, y0); \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
41 SWAP(x1, y1); \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
42 } \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
43 y = y0; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
44 if (x0 > x1) { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
45 xstep = -1; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
46 deltax = -deltax; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
47 } else { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
48 xstep = 1; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
49 } \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
50 if (y0 < y1) { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
51 ystep = 1; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
52 } else { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
53 ystep = -1; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
54 } \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
55 if (!steep) { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
56 for (x = x0; x != x1; x += xstep) { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
57 op(x, y, color); \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
58 error -= deltay; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
59 if (error < 0) { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
60 y = y + ystep; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
61 error += deltax; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
62 } \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
63 } \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
64 } else { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
65 for (x = x0; x != x1; x += xstep) { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
66 op(y, x, color); \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
67 error -= deltay; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
68 if (error < 0) { \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
69 y = y + ystep; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
70 error += deltax; \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
71 } \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
72 } \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
73 } \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
74 }
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
75
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
76 #define SETPIXEL(x, y, type, bpp, color) \
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
77 *(type *)(dst->pixels + y * dst->pitch + x * bpp) = (type) color
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
78
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
79 #define SETPIXEL1(x, y, color) SETPIXEL(x, y, Uint8, 1, color);
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
80 #define SETPIXEL2(x, y, color) SETPIXEL(x, y, Uint16, 2, color);
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
81 #define SETPIXEL4(x, y, color) SETPIXEL(x, y, Uint32, 4, color);
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
82
2888
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 {
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 /* This function doesn't work on surfaces < 8 bpp */
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 if (dst->format->BitsPerPixel < 8) {
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 SDL_SetError("SDL_DrawLine(): Unsupported surface format");
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 return (-1);
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 }
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 /* Perform clipping */
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 /* FIXME
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93 if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) {
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 return (0);
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 }
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 */
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97
2890
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
98 switch (dst->format->BytesPerPixel) {
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
99 case 1:
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
100 BRESENHAM(x1, y1, x2, y2, SETPIXEL1, color);
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
101 break;
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
102 case 2:
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
103 BRESENHAM(x1, y1, x2, y2, SETPIXEL2, color);
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
104 break;
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
105 case 3:
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
106 SDL_Unsupported();
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
107 return -1;
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
108 case 4:
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
109 BRESENHAM(x1, y1, x2, y2, SETPIXEL4, color);
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
110 break;
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
111 }
1863c8b59658 Placeholder for line drawing algorithm (current code doesn't work)
Sam Lantinga <slouken@libsdl.org>
parents: 2888
diff changeset
112 return 0;
2888
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 }
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114
32e8bbba1e94 Added stubs for software implementations of blending fills and line drawing
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 /* vi: set ts=4 sw=4 expandtab: */