Mercurial > hg > dmlib
view dmdrawline.h @ 232:79dac918c81e
Modularize line clipping etc. a lot, and export all line drawing and
blitting functions.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 08 Oct 2012 09:01:05 +0300 |
parents | 92cc5e1fa180 |
children | a2abd0b991b6 |
line wrap: on
line source
/* * DMLib * -- Simple Bresenham's style line drawing * Programmed and designed by Matti 'ccr' Hamalainen * (C) Copyright 2011-2012 Tecnic Software productions (TNSP) */ int DM_DRAWLINE_NAME (SDL_Surface *screen, int x0, int y0, int x1, int y1, const Uint32 col) #ifdef DM_HEADER ; #else { int dx, dy, xstep, ystep; const int qpitch = screen->pitch / DM_DRAWLINE_DST_BYTES; // Clipping if (dmClipLineCoordsInt(&x0, &y0, &x1, &y1, screen) < 0) return -1; // Compute initial deltas dx = (x1 - x0) * 2; dy = (y1 - y0) * 2; if (dx < 0) { dx = -dx; xstep = -1; } else xstep = 1; if (dy < 0) { dy = -dy; ystep = -qpitch; } else ystep = qpitch; // Compute offsets y0 *= qpitch; y1 *= qpitch; #ifdef DM_DRAWLINE_INIT DM_DRAWLINE_INIT #endif DM_DRAWLINE_DST_TYPE *pix = (DM_DRAWLINE_DST_TYPE *) screen->pixels; // Continue based on which delta is larger if (dx > dy) { int afrac = dy - (dx / 2); while (x0 != x1) { if (afrac >= 0) { y0 += ystep; afrac -= dx; } x0 += xstep; afrac += dy; DM_DRAWLINE_INNER } } else { int afrac = dx - (dy / 2); while (y0 != y1) { if (afrac >= 0) { x0 += xstep; afrac -= dy; } y0 += ystep; afrac += dx; DM_DRAWLINE_INNER } } return 0; } #endif #undef DM_DRAWLINE_NAME #undef DM_DRAWLINE_DST_BYTES #undef DM_DRAWLINE_DST_TYPE #undef DM_DRAWLINE_INIT #undef DM_DRAWLINE_INNER