Mercurial > hg > dmlib
annotate dmdrawline.h @ 234:a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
functions to dmlib.h.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 08 Oct 2012 15:21:59 +0300 |
parents | 79dac918c81e |
children | 51ba74f7668c |
rev | line source |
---|---|
232
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
1 /* |
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
2 * DMLib |
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
3 * -- Simple Bresenham's style line drawing |
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
4 * Programmed and designed by Matti 'ccr' Hamalainen |
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
5 * (C) Copyright 2011-2012 Tecnic Software productions (TNSP) |
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
6 */ |
0 | 7 |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
8 int DM_DRAWLINE_NAME (SDL_Surface *screen, int x0, int y0, int x1, int y1, const Uint32 col |
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
9 #ifdef DM_DRAWLINE_ARGS |
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
10 DM_DRAWLINE_ARGS |
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
11 #endif |
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
12 ) |
232
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
13 #ifdef DM_HEADER |
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
14 ; |
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
15 #else |
0 | 16 { |
17 int dx, dy, xstep, ystep; | |
18 const int qpitch = screen->pitch / DM_DRAWLINE_DST_BYTES; | |
19 | |
20 // Clipping | |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
21 if (dmClipLineCoordsInt(screen, &x0, &y0, &x1, &y1) < 0) |
0 | 22 return -1; |
23 | |
24 // Compute initial deltas | |
25 dx = (x1 - x0) * 2; | |
26 dy = (y1 - y0) * 2; | |
27 | |
133
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
28 |
0 | 29 if (dx < 0) |
30 { | |
31 dx = -dx; | |
32 xstep = -1; | |
33 } | |
34 else | |
35 xstep = 1; | |
36 | |
37 if (dy < 0) | |
38 { | |
39 dy = -dy; | |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
40 ystep = -1; |
0 | 41 } |
42 else | |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
43 ystep = 1; |
0 | 44 |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
45 #ifndef DM_DRAWLINE_SPEC |
0 | 46 // Compute offsets |
47 y0 *= qpitch; | |
48 y1 *= qpitch; | |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
49 ystep *= qpitch; |
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
50 #endif |
0 | 51 |
133
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
52 #ifdef DM_DRAWLINE_INIT |
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
53 DM_DRAWLINE_INIT |
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
54 #endif |
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
55 |
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
56 DM_DRAWLINE_DST_TYPE *pix = (DM_DRAWLINE_DST_TYPE *) screen->pixels; |
0 | 57 |
58 // Continue based on which delta is larger | |
59 if (dx > dy) | |
60 { | |
61 int afrac = dy - (dx / 2); | |
62 while (x0 != x1) | |
63 { | |
64 if (afrac >= 0) | |
65 { | |
66 y0 += ystep; | |
67 afrac -= dx; | |
68 } | |
69 | |
70 x0 += xstep; | |
71 afrac += dy; | |
72 | |
73 DM_DRAWLINE_INNER | |
74 } | |
75 } | |
76 else | |
77 { | |
78 int afrac = dx - (dy / 2); | |
79 while (y0 != y1) | |
80 { | |
81 if (afrac >= 0) | |
82 { | |
83 x0 += xstep; | |
84 afrac -= dy; | |
85 } | |
86 | |
87 y0 += ystep; | |
88 afrac += dx; | |
89 | |
90 DM_DRAWLINE_INNER | |
91 } | |
92 } | |
93 | |
94 return 0; | |
95 } | |
232
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
96 #endif |
0 | 97 |
98 #undef DM_DRAWLINE_NAME | |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
99 #undef DM_DRAWLINE_ARGS |
0 | 100 #undef DM_DRAWLINE_DST_BYTES |
101 #undef DM_DRAWLINE_DST_TYPE | |
133
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
102 #undef DM_DRAWLINE_INIT |
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
103 #undef DM_DRAWLINE_INNER |