comparison src/video/SDL_drawline.c @ 2898:e40448bc7727

Share code between fill and line drawing Added general RGB surface format fallbacks to drawing code Fixed issues with destination surface alpha channel
author Sam Lantinga <slouken@libsdl.org>
date Sun, 21 Dec 2008 08:28:25 +0000
parents 1ef2f1e75ff7
children 3a9636c83849
comparison
equal deleted inserted replaced
2897:8be863ef68ee 2898:e40448bc7727
19 Sam Lantinga 19 Sam Lantinga
20 slouken@libsdl.org 20 slouken@libsdl.org
21 */ 21 */
22 #include "SDL_config.h" 22 #include "SDL_config.h"
23 23
24 #include "SDL_video.h" 24 #include "SDL_draw.h"
25 #include "SDL_blit.h"
26
27 #define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
28
29 #define SWAP(_x, _y) do { int tmp; tmp = _x; _x = _y; _y = tmp; } while (0)
30
31 #define BRESENHAM(x0, y0, x1, y1, op, color) \
32 { \
33 int deltax, deltay, steep, error, xstep, ystep, x, y; \
34 \
35 deltax = ABS(x1 - x0); \
36 deltay = ABS(y1 - y0); \
37 steep = (deltay > deltax); \
38 if (steep) { \
39 SWAP(x0, y0); \
40 SWAP(x1, y1); \
41 SWAP(deltax, deltay); \
42 } \
43 error = (x1 - x0) / 2; \
44 y = y0; \
45 if (x0 > x1) { \
46 xstep = -1; \
47 } else { \
48 xstep = 1; \
49 } \
50 if (y0 < y1) { \
51 ystep = 1; \
52 } else { \
53 ystep = -1; \
54 } \
55 if (!steep) { \
56 for (x = x0; x != x1; x += xstep) { \
57 op(x, y, color); \
58 error -= deltay; \
59 if (error < 0) { \
60 y += ystep; \
61 error += deltax; \
62 } \
63 } \
64 } else { \
65 for (x = x0; x != x1; x += xstep) { \
66 op(y, x, color); \
67 error -= deltay; \
68 if (error < 0) { \
69 y += ystep; \
70 error += deltax; \
71 } \
72 } \
73 } \
74 }
75 25
76 #define SETPIXEL(x, y, type, bpp, color) \ 26 #define SETPIXEL(x, y, type, bpp, color) \
77 *(type *)(dst->pixels + y * dst->pitch + x * bpp) = (type) color 27 *(type *)(dst->pixels + y * dst->pitch + x * bpp) = (type) color
78 28
79 #define SETPIXEL1(x, y, color) SETPIXEL(x, y, Uint8, 1, color); 29 #define SETPIXEL1(x, y) SETPIXEL(x, y, Uint8, 1, color);
80 #define SETPIXEL2(x, y, color) SETPIXEL(x, y, Uint16, 2, color); 30 #define SETPIXEL2(x, y) SETPIXEL(x, y, Uint16, 2, color);
81 #define SETPIXEL4(x, y, color) SETPIXEL(x, y, Uint32, 4, color); 31 #define SETPIXEL4(x, y) SETPIXEL(x, y, Uint32, 4, color);
82 32
83 SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color) 33 SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
84 { 34 {
85 /* This function doesn't work on surfaces < 8 bpp */ 35 /* This function doesn't work on surfaces < 8 bpp */
86 if (dst->format->BitsPerPixel < 8) { 36 if (dst->format->BitsPerPixel < 8) {
95 } 45 }
96 */ 46 */
97 47
98 switch (dst->format->BytesPerPixel) { 48 switch (dst->format->BytesPerPixel) {
99 case 1: 49 case 1:
100 BRESENHAM(x1, y1, x2, y2, SETPIXEL1, color); 50 BRESENHAM(x1, y1, x2, y2, SETPIXEL1);
101 break; 51 break;
102 case 2: 52 case 2:
103 BRESENHAM(x1, y1, x2, y2, SETPIXEL2, color); 53 BRESENHAM(x1, y1, x2, y2, SETPIXEL2);
104 break; 54 break;
105 case 3: 55 case 3:
106 SDL_Unsupported(); 56 SDL_Unsupported();
107 return -1; 57 return -1;
108 case 4: 58 case 4:
109 BRESENHAM(x1, y1, x2, y2, SETPIXEL4, color); 59 BRESENHAM(x1, y1, x2, y2, SETPIXEL4);
110 break; 60 break;
111 } 61 }
112 return 0; 62 return 0;
113 } 63 }
114 64