Mercurial > sdl-ios-xcode
comparison src/video/SDL_draw.h @ 2903:e426c4fc9cf7
Working Bresenham line drawing algorithm. We can optimize later, if needed.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 21 Dec 2008 20:16:21 +0000 |
parents | 133601e3b255 |
children | bd518fc76f28 |
comparison
equal
deleted
inserted
replaced
2902:83c3a4b0e421 | 2903:e426c4fc9cf7 |
---|---|
281 * Define line drawing macro | 281 * Define line drawing macro |
282 */ | 282 */ |
283 | 283 |
284 #define ABS(_x) ((_x) < 0 ? -(_x) : (_x)) | 284 #define ABS(_x) ((_x) < 0 ? -(_x) : (_x)) |
285 | 285 |
286 #define SWAP(_x, _y) do { int tmp; tmp = _x; _x = _y; _y = tmp; } while (0) | 286 #define BRESENHAM(x1, y1, x2, y2, op) \ |
287 | |
288 #define BRESENHAM(x0, y0, x1, y1, op) \ | |
289 { \ | 287 { \ |
290 int deltax, deltay, steep, error, xstep, ystep, x, y; \ | 288 int i, deltax, deltay, numpixels; \ |
291 \ | 289 int d, dinc1, dinc2; \ |
292 deltax = ABS(x1 - x0); \ | 290 int x, xinc1, xinc2; \ |
293 deltay = ABS(y1 - y0); \ | 291 int y, yinc1, yinc2; \ |
294 steep = (deltay > deltax); \ | 292 \ |
295 if (steep) { \ | 293 deltax = ABS(x2 - x1); \ |
296 SWAP(x0, y0); \ | 294 deltay = ABS(y2 - y1); \ |
297 SWAP(x1, y1); \ | 295 \ |
298 SWAP(deltax, deltay); \ | 296 if (deltax >= deltay) { \ |
299 } \ | 297 numpixels = deltax + 1; \ |
300 error = (x1 - x0) / 2; \ | 298 d = (2 * deltay) - deltax; \ |
301 y = y0; \ | 299 dinc1 = deltay * 2; \ |
302 if (x0 > x1) { \ | 300 dinc2 = (deltay - deltax) * 2; \ |
303 xstep = -1; \ | 301 xinc1 = 1; \ |
302 xinc2 = 1; \ | |
303 yinc1 = 0; \ | |
304 yinc2 = 1; \ | |
304 } else { \ | 305 } else { \ |
305 xstep = 1; \ | 306 numpixels = deltay + 1; \ |
306 } \ | 307 d = (2 * deltax) - deltay; \ |
307 if (y0 < y1) { \ | 308 dinc1 = deltax * 2; \ |
308 ystep = 1; \ | 309 dinc2 = (deltax - deltay) * 2; \ |
309 } else { \ | 310 xinc1 = 0; \ |
310 ystep = -1; \ | 311 xinc2 = 1; \ |
311 } \ | 312 yinc1 = 1; \ |
312 if (!steep) { \ | 313 yinc2 = 1; \ |
313 for (x = x0; x != x1; x += xstep) { \ | 314 } \ |
314 op(x, y); \ | 315 \ |
315 error -= deltay; \ | 316 if (x1 > x2) { \ |
316 if (error < 0) { \ | 317 xinc1 = -xinc1; \ |
317 y += ystep; \ | 318 xinc2 = -xinc2; \ |
318 error += deltax; \ | 319 } \ |
319 } \ | 320 if (y1 > y2) { \ |
320 } \ | 321 yinc1 = -yinc1; \ |
321 } else { \ | 322 yinc2 = -yinc2; \ |
322 for (x = x0; x != x1; x += xstep) { \ | 323 } \ |
323 op(y, x); \ | 324 \ |
324 error -= deltay; \ | 325 x = x1; \ |
325 if (error < 0) { \ | 326 y = y1; \ |
326 y += ystep; \ | 327 \ |
327 error += deltax; \ | 328 for (i = 1; i < numpixels; ++i) { \ |
328 } \ | 329 op(x, y); \ |
330 if (d < 0) { \ | |
331 d += dinc1; \ | |
332 x += xinc1; \ | |
333 y += yinc1; \ | |
334 } else { \ | |
335 d += dinc2; \ | |
336 x += xinc2; \ | |
337 y += yinc2; \ | |
329 } \ | 338 } \ |
330 } \ | 339 } \ |
331 } | 340 } |
332 #define DRAWLINE(x0, y0, x1, y1, op) BRESENHAM(x0, y0, x1, y1, op) | 341 #define DRAWLINE(x0, y0, x1, y1, op) BRESENHAM(x0, y0, x1, y1, op) |
333 | 342 |