Mercurial > hg > dmlib
annotate dmdrawline.h @ 570:a26636faa6b7
Update copyright.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 05 Jan 2013 19:58:23 +0200 |
parents | 51ba74f7668c |
children | cf7612c8de1f |
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 |
265
51ba74f7668c
Make line clipping floating point only.
Matti Hamalainen <ccr@tnsp.org>
parents:
234
diff
changeset
|
8 int DM_DRAWLINE_NAME (SDL_Surface *screen, DMFloat fx0, DMFloat fy0, DMFloat fx1, DMFloat fy1, const Uint32 col |
234
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 | |
265
51ba74f7668c
Make line clipping floating point only.
Matti Hamalainen <ccr@tnsp.org>
parents:
234
diff
changeset
|
21 if (dmClipLineCoordsFloat(screen, &fx0, &fy0, &fx1, &fy1) < 0) |
0 | 22 return -1; |
23 | |
265
51ba74f7668c
Make line clipping floating point only.
Matti Hamalainen <ccr@tnsp.org>
parents:
234
diff
changeset
|
24 int x0 = fx0, y0 = fy0, x1 = fx1, y1 = fy1; |
51ba74f7668c
Make line clipping floating point only.
Matti Hamalainen <ccr@tnsp.org>
parents:
234
diff
changeset
|
25 |
0 | 26 // Compute initial deltas |
27 dx = (x1 - x0) * 2; | |
28 dy = (y1 - y0) * 2; | |
29 | |
133
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
30 |
0 | 31 if (dx < 0) |
32 { | |
33 dx = -dx; | |
34 xstep = -1; | |
35 } | |
36 else | |
37 xstep = 1; | |
38 | |
39 if (dy < 0) | |
40 { | |
41 dy = -dy; | |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
42 ystep = -1; |
0 | 43 } |
44 else | |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
45 ystep = 1; |
0 | 46 |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
47 #ifndef DM_DRAWLINE_SPEC |
0 | 48 // Compute offsets |
49 y0 *= qpitch; | |
50 y1 *= qpitch; | |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
51 ystep *= qpitch; |
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
52 #endif |
0 | 53 |
133
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
54 #ifdef DM_DRAWLINE_INIT |
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
55 DM_DRAWLINE_INIT |
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
56 #endif |
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
57 |
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
58 DM_DRAWLINE_DST_TYPE *pix = (DM_DRAWLINE_DST_TYPE *) screen->pixels; |
0 | 59 |
60 // Continue based on which delta is larger | |
61 if (dx > dy) | |
62 { | |
265
51ba74f7668c
Make line clipping floating point only.
Matti Hamalainen <ccr@tnsp.org>
parents:
234
diff
changeset
|
63 int afrac = dy - (dx / 2.0); |
0 | 64 while (x0 != x1) |
65 { | |
66 if (afrac >= 0) | |
67 { | |
68 y0 += ystep; | |
69 afrac -= dx; | |
70 } | |
71 | |
72 x0 += xstep; | |
73 afrac += dy; | |
74 | |
75 DM_DRAWLINE_INNER | |
76 } | |
77 } | |
78 else | |
79 { | |
265
51ba74f7668c
Make line clipping floating point only.
Matti Hamalainen <ccr@tnsp.org>
parents:
234
diff
changeset
|
80 int afrac = dx - (dy / 2.0); |
0 | 81 while (y0 != y1) |
82 { | |
83 if (afrac >= 0) | |
84 { | |
85 x0 += xstep; | |
86 afrac -= dy; | |
87 } | |
88 | |
89 y0 += ystep; | |
90 afrac += dx; | |
91 | |
92 DM_DRAWLINE_INNER | |
93 } | |
94 } | |
95 | |
96 return 0; | |
97 } | |
232
79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
Matti Hamalainen <ccr@tnsp.org>
parents:
133
diff
changeset
|
98 #endif |
0 | 99 |
100 #undef DM_DRAWLINE_NAME | |
234
a2abd0b991b6
Modularize line drawing related templates and functions, add clipping
Matti Hamalainen <ccr@tnsp.org>
parents:
232
diff
changeset
|
101 #undef DM_DRAWLINE_ARGS |
0 | 102 #undef DM_DRAWLINE_DST_BYTES |
103 #undef DM_DRAWLINE_DST_TYPE | |
133
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
104 #undef DM_DRAWLINE_INIT |
92cc5e1fa180
Some work on line drawing routines.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
105 #undef DM_DRAWLINE_INNER |