comparison src/video/SDL_drawline.c @ 2890:1863c8b59658

Placeholder for line drawing algorithm (current code doesn't work)
author Sam Lantinga <slouken@libsdl.org>
date Sat, 20 Dec 2008 13:54:19 +0000
parents 32e8bbba1e94
children 1ef2f1e75ff7
comparison
equal deleted inserted replaced
2889:67f84eb26ea1 2890:1863c8b59658
22 #include "SDL_config.h" 22 #include "SDL_config.h"
23 23
24 #include "SDL_video.h" 24 #include "SDL_video.h"
25 #include "SDL_blit.h" 25 #include "SDL_blit.h"
26 26
27 #define ABS(x) (x < 0 ? -x : x)
27 28
28 int 29 #define SWAP(x, y) (x ^= y ^= x ^= y)
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 error = deltax / 2; \
39 if (steep) { \
40 SWAP(x0, y0); \
41 SWAP(x1, y1); \
42 } \
43 y = y0; \
44 if (x0 > x1) { \
45 xstep = -1; \
46 deltax = -deltax; \
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 = 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 = y + ystep; \
70 error += deltax; \
71 } \
72 } \
73 } \
74 }
75
76 #define SETPIXEL(x, y, type, bpp, color) \
77 *(type *)(dst->pixels + y * dst->pitch + x * bpp) = (type) color
78
79 #define SETPIXEL1(x, y, color) SETPIXEL(x, y, Uint8, 1, color);
80 #define SETPIXEL2(x, y, color) SETPIXEL(x, y, Uint16, 2, color);
81 #define SETPIXEL4(x, y, color) SETPIXEL(x, y, Uint32, 4, color);
82
29 SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color) 83 SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
30 { 84 {
31 /* This function doesn't work on surfaces < 8 bpp */ 85 /* This function doesn't work on surfaces < 8 bpp */
32 if (dst->format->BitsPerPixel < 8) { 86 if (dst->format->BitsPerPixel < 8) {
33 SDL_SetError("SDL_DrawLine(): Unsupported surface format"); 87 SDL_SetError("SDL_DrawLine(): Unsupported surface format");
39 if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) { 93 if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) {
40 return (0); 94 return (0);
41 } 95 }
42 */ 96 */
43 97
44 SDL_Unsupported(); 98 switch (dst->format->BytesPerPixel) {
45 return -1; 99 case 1:
100 BRESENHAM(x1, y1, x2, y2, SETPIXEL1, color);
101 break;
102 case 2:
103 BRESENHAM(x1, y1, x2, y2, SETPIXEL2, color);
104 break;
105 case 3:
106 SDL_Unsupported();
107 return -1;
108 case 4:
109 BRESENHAM(x1, y1, x2, y2, SETPIXEL4, color);
110 break;
111 }
112 return 0;
46 } 113 }
47 114
48 /* vi: set ts=4 sw=4 expandtab: */ 115 /* vi: set ts=4 sw=4 expandtab: */