Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MIN(a,b) (((a) < (b)) ? (a) : (b))
- #define MAX(a,b) (((a) > (b)) ? (a) : (b))
- #define SIGN(x) (((x) < 0) ? -1 : (((x) > 0) ? 1 : 0))
- #define ABS(x) (((x) < 0) ? -(x) : (x))
- #define DONT_INTERSECT 0
- #define DO_INTERSECT 1
- #define OUTCODE_INSIDE 0
- #define OUTCODE_LEFT 0x1
- #define OUTCODE_RIGHT 0x2
- #define OUTCODE_TOP 0x4
- #define OUTCODE_BOTTOM 0x8
- void drawRectFill(int x, int y, int w, int h, unsigned char color)
- {
- unsigned char far* pix;
- if (x < SCREEN_WIDTH && y < SCREEN_HEIGHT)
- {
- if (y < 0)
- {
- h += y;
- y = 0;
- }
- if (h > 0)
- {
- if (x < 0)
- {
- w += x;
- x = 0;
- }
- if (w > 0)
- {
- if (x+w > SCREEN_WIDTH)
- w = SCREEN_WIDTH - x;
- if (y+h > SCREEN_HEIGHT)
- h = SCREEN_HEIGHT - y;
- pix = off_screen + Y_OFFSET(y) + x;
- for (; (h--); pix += SCREEN_WIDTH)
- _fmemset(pix, color, w);
- }
- }
- }
- }
- void setPixel(int x, int y, unsigned char color)
- {
- *(off_screen+x + Y_OFFSET(y)) = color;
- }
- unsigned char pointInScreen(int x, int y)
- {
- unsigned char outcode = OUTCODE_INSIDE;
- if (x < 0) outcode |= OUTCODE_LEFT;
- else if (x >= SCREEN_WIDTH) outcode |= OUTCODE_RIGHT;
- if (y < 0) outcode |= OUTCODE_TOP;
- else if (y >= SCREEN_HEIGHT) outcode |= OUTCODE_BOTTOM;
- return outcode;
- }
- int clipLineToScreen(struct Vec2* p0, struct Vec2* p1)
- {
- int x, y, dx, dy;
- int ax = p0->x;
- int ay = p0->y;
- int bx = p1->x;
- int by = p1->y;
- unsigned char outcode;
- unsigned char outcode0 = pointInScreen(ax, ay);
- unsigned char outcode1 = pointInScreen(bx, by);
- while (1)
- {
- if ((outcode0 | outcode1) == OUTCODE_INSIDE)
- return DO_INTERSECT;
- else if ((outcode0 & outcode1) != OUTCODE_INSIDE)
- return DONT_INTERSECT;
- else
- {
- outcode = MAX(outcode0, outcode1);
- dx = bx-ax;
- dy = by-ay;
- if (outcode & OUTCODE_LEFT)
- {
- y = ay + (-ax*dy + dx/2) / dx;
- x = 0;
- }
- else if (outcode & OUTCODE_RIGHT)
- {
- y = ay + ((SCREEN_WIDTH-ax)*dy + dx/2) / dx;
- x = SCREEN_WIDTH-1;
- }
- else if (outcode & OUTCODE_TOP)
- {
- x = ax + (-ay*dx + dy/2) / dy;
- y = 0;
- }
- else if (outcode & OUTCODE_BOTTOM)
- {
- x = ax + ((SCREEN_HEIGHT-ay)*dx + dy/2) / dy;
- y = SCREEN_HEIGHT-1;
- }
- if (outcode == outcode0)
- {
- p0->x = ax = x;
- p0->y = ay = y;
- outcode0 = pointInScreen(ax, ay);
- }
- else
- {
- p1->x = bx = x;
- p1->y = by = y;
- outcode1 = pointInScreen(bx, by);
- }
- }
- }
- }
- void drawLineSlope(int ax, int ay, int bx, int by, unsigned char color)
- {
- int i, x, y, px, py;
- const int dx = bx - ax;
- const int dy = by - ay;
- const int dx_abs = ABS(dx);
- const int dy_abs = ABS(dy);
- const int x_sign = SIGN(dx);
- const int y_sign = SIGN(dy);
- x = dy_abs >> 1;
- y = dx_abs >> 1;
- // Starting point
- px = ax;
- py = ay;
- if (dx_abs > dy_abs)
- {
- for (i = 0; i <= dx_abs; i++)
- {
- setPixel(px, py, color);
- y += dy_abs;
- if (y >= dx_abs)
- {
- y -= dx_abs;
- py += y_sign;
- }
- px += x_sign;
- }
- }
- else
- {
- for (i = 0; i <= dy_abs; i++)
- {
- setPixel(px, py, color);
- x += dx_abs;
- if (x >= dy_abs)
- {
- x -= dy_abs;
- px += x_sign;
- }
- py += y_sign;
- }
- }
- }
- void drawLine(struct Vec2 p0, struct Vec2 p1, unsigned char color)
- {
- if (clipLineToScreen(&p0, &p1) == DO_INTERSECT)
- {
- drawLineSlope(p0.x, p0.y, p1.x, p1.y, color);
- }
- }
Advertisement
Advertisement