/*
 * FILE				: vga.c
 * DESCRIPTION		: This file is 3-wire vga driver for niosii.
 * Author			: XiaomaGee@Gmail.com
 * Copyright		:
 *
 * History
 * --------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * --------------------
 */

//---------------- Include files ------------------------//
#include "..\include\gui.h"
#include "..\include\vga.h"
#include <stdio.h>
#include <stdlib.h>

//---------------- Function Prototype -------------------//
static int line(int x0,int y0,int x1,int y1,int color);
static int rect(RECT_T *);
static int circle(int x0,int y0,int r,int fill_flag ,int color);
static int set_cursor(int /* x */, int /* y */);
static int line_to(int x,int y,int color);

//---------------- Variable -----------------------------//
GUI_T gui = {
	.line = line,
	.rect = rect,
	.circle = circle,
	.set_cursor = set_cursor,
	.line_to = line_to
};

static int cursor_x = 0;
static int cursor_y = 0;

//-----------------Function------------------------------//
/*
 * Name				: line
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */

static int
line(int x1, int y1, int x2, int y2, int c)
{
	int dx = x2 - x1;
	int dy = y2 - y1;
	int ux = ((dx > 0) << 1) - 1; //xȡ-1
	int uy = ((dy > 0) << 1) - 1; //yȡ-1
	int x = x1, y = y1, eps; //epsΪۼ

	eps = 0; dx = abs(dx); dy = abs(dy);
	if (dx > dy) {
		for (x = x1; x != x2; x += ux) {
			vga.dot(x, y, c);
			eps += dy;
			if ((eps << 1) >= dx) {
				y += uy; eps -= dx;
			}
		}
	}else  {
		for (y = y1; y != y2; y += uy) {
			vga.dot(x, y, c);
			eps += dx;
			if ((eps << 1) >= dy) {
				x += ux; eps -= dy;
			}
		}
	}

	return 0;
}

/*
 * Name				: rect
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */
static int
rect(RECT_T * r)
{
	int i,j;




	if (r->fill_flag == 1) {
			for (i = 0; i < r->height; i++) {
				line(r->x,r->y + i,r->x + r->width,r->y  + i,r->color);

			}
	}else { //no fill
		line(r -> x,r -> y,r -> x + r->width - 1,r -> y,  r -> color);
		line(r -> x,r -> y + r -> height - 1 ,r -> x + r->width - 1,r -> y + r -> height - 1, r -> color);
		line(r -> x, r -> y, r -> x, r -> y + r -> height -  1, r -> color);
		line(r -> x + r -> width - 1, r -> y,r -> x + r -> width - 1, r -> y + r -> height , r -> color);
	}

	return 0;
}
/*
 * Name				: _draw_circle_8
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */
static
void _draw_circle_8(int xc, int yc, int x, int y,int c)
{
	vga.dot(xc + x, yc + y, c);
	vga.dot(xc - x, yc + y, c);
	vga.dot(xc + x, yc - y, c);
	vga.dot(xc - x, yc - y, c);
	vga.dot(xc + y, yc + x, c);
	vga.dot(xc - y, yc + x, c);
	vga.dot(xc + y, yc - x, c);
	vga.dot(xc - y, yc - x, c);
}

/*
 * Name				: circle
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */
static int
circle(int xc, int yc, int r, int fill, int c)
{
	int x = 0, y = r, yi, d;
	d = 3 - 2 * r;

	if (fill) {
		// 䣨ʵԲ

		while (x <= y) {
			for (yi = x; yi <= y; yi++)
				_draw_circle_8(xc, yc, x, yi, c);

			if (d < 0) {
				d = d + 4 * x + 6;
			} else {
				d = d + 4 * (x - y) + 10;
				y--;
			}
			x++;
		}
	} else {
		// 䣨Բ

		while (x <= y) {
			_draw_circle_8(xc, yc, x, y, c);
			if (d < 0) {
				d = d + 4 * x + 6;
			} else {
				d = d + 4 * (x - y) + 10;
				y--;
			}
			x++;
		}
	}
	return 0;
}

/*
 * Name				: line_to
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */
static int
line_to(int x, int y, int c)
{
	line(cursor_x, cursor_y, x, y, c);
	set_cursor(x, y);

	return 0;
}
/*
 * Name				: set_cursor
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */
static int
set_cursor(int x, int y)
{
	cursor_x = x;
	cursor_y = y;

	return 0;
}
