/*
 * FILE				: main.c
 * DESCRIPTION		: This file is 3-wire vga driver for niosii main.
 * Author			: XiaomaGee@Gmail.com
 * Copyright		:
 *
 * History
 * --------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * --------------------
 */
//---------------- Include files ------------------------//
#include <stdio.h>
#include <stdlib.h>
#include "altera_avalon_pio_regs.h"
#include "system.h"
#include "io.h"

#include <math.h>
#include <stdlib.h>
#include "..\include\vga.h"
#include "..\include\gui.h"
#include "..\include\font.h"

//---------------- Function Prototype -------------------//
static int  random(int seed);
static int draw_frame(void);
static int initialize(void);
static int color_demo(void);
static int line_demo(void);
static int tree_demo(void);
static int line_to_demo(int);
static int font_demo(void);
static int circle_demo(void);
static int wave_demo(void);



//---------------- Variable -----------------------------//
//-----------------Function------------------------------//
/*
 * Name				: main
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */
int main(void)
{
		initialize();

		while(1){

				color_demo();
				line_demo();
				tree_demo();
				line_to_demo(18);
				circle_demo();
				wave_demo();
				font_demo();
		}

}

/*
 * Name				: random
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * -----------------------
 * Rev	: 0.00
 * Date	: 03/05/2012
 *
 * create.
 * ----------------------
 */
		static  int
random(int seed)
{
		unsigned short int temp;

		temp = rand();
		temp = temp % seed;

		return(temp);
}
/*
 * Name				: initialize
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */
		static int
initialize(void)
{
		printf("Hello from Nios II!\n");

		vga.initialize();
		vga.clear(COLOR_BLACK);
		vga.on();
		font.initialize();

		return 0;
}
/*
 * Name				: draw_frame
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */
		static int
draw_frame(void)
{
		STRING_T s;
		RECT_T r;

		vga.clear(COLOR_BLACK);

		r.x = 0;
		r.y = 30;
		r.width = 800;
		r.height = 570;
		r.fill_flag = 0;
		r.color = COLOR_GREEN;
		gui.rect(&r);

		r.x = 215;
		r.y = 1;
		r.width = 390;
		r.height = 60;
		r.fill_flag = 1;
		r.color = COLOR_GREEN;
		r.color = COLOR_BLACK;

		gui.rect(&r);

		r.x = 215;
		r.y = 1;
		r.width = 390;
		r.height = 60;
		r.fill_flag = 0;
		r.color = COLOR_GREEN;

		gui.rect(&r);

		font._default.single_byte = &tahoma26;
		font._default.double_byte = &yahei32;

		s.x = 218 ;
		s.y = 10;
		s.inverse = NULL;
		s.color = COLOR_RED;
		s.background_color = COLOR_BLACK;
		s.space.line = 0;
		s.space.word = 0;

		font.printf(&s,"iCore VGA ʾ V1.0\n");

		return 0;
}
/*
 * Name				: color_demo
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */
		static int
color_demo(void)
{
		int i;
		STRING_T s;
		RECT_T r;
		char  * color_string[]={
				"ɫ BLUE","ɫ GREEN","ɫ CYAN","ɫ RED","Ʒ FUCHSINE","ɫ YELLOW","ɫ WHITE"
		};

		draw_frame();
		for(i = 0;i<7;i++){
				r.x = 30;
				r.y = 70 * i + 80;
				r.width = 500;
				r.height = 60;
				r.fill_flag = 1;
				r.color = i + 1;

				gui.rect(&r);

				font._default.single_byte = &tahoma26;
				font._default.double_byte = &yahei32;

				s.x = 550 ;
				s.y = 70*i + 90;
				s.inverse = NULL;
				s.color = i + 1;
				s.background_color = COLOR_BLACK;
				s.space.line = 5;
				s.space.word = 0;
				font.printf(&s,"%s",color_string[i]);
		}
		usleep(5000000);

		return 0;
}

/*
 * Name				: line_demo
 * Description		: ---
 * Author			: XiaomaGee.
 *
 * History
 * ----------------------
 * Rev	: 0.00
 * Date	: 03/08/2012
 *
 * create.
 * ----------------------
 */
		static int
line_demo(void)
{
		int i, x0, y0, x1, y1;
		int color;

		draw_frame();

		for (i = 0; i < 20000; i++) {
				x0 = random(750);
				y0 = random(500);

				x1 =  750 - x0;
				y1 =  500 - y0;

				gui.line(x0 + 25, y0 + 75, x1 + 25, y1 + 75, i%6 + 1);
		}

		return 0;
}
/*
 * Name						: _draw_tree
 * Description		: ---
 * Author					: XiaomaGee.
 *
 * History
 * -----------------------
 * Rev	: 0.00
 * Date	: 03/05/2012
 *
 * create.
 * ----------------------
 */

		static int
_draw_tree(int x_start, int y_start, double length, double angle, int num)
{
		int x_end, y_end;

		if (num == 0) return 0;  // 

		x_end = x_start + (int)(length * cos(angle));
		y_end = y_start - (int)(length * sin(angle));

		// 
		gui.line(x_start, y_start, x_end, y_end, COLOR_GREEN);

		// ֦
		_draw_tree(x_end, y_end,
						length * 0.6,
						angle + 0.624,
						num - 1);

		// в֦
		_draw_tree(x_end, y_end, length * .88, angle + 0.1, num - 1);
		_draw_tree(x_end, y_end, length * .75, angle - 0.6, num - 1);

		return 0;
}
/*
 * Name						: draw_tree
 * Description		: ---
 * Author					: XiaomaGee.
 *
 * History
 * -----------------------
 * Rev	: 0.00
 * Date	: 03/05/2012
 *
 * create.
 * ----------------------
 */
		static int
tree_demo(void)
{
		int i;
		STRING_T s;

		draw_frame();

		_draw_tree(250, 580, 110, 1.57, 7);

		font._default.single_byte = &tahoma26;
		font._default.double_byte = &yahei32;

		s.x = 540 ;
		s.y = 200;
		s.inverse = NULL;
		s.color =COLOR_RED;
		s.background_color = COLOR_BLACK;
		s.space.line = 5;
		s.space.word = 0;
		font.printf(&s,"ͨgui.line \nݹã\nڷԭ\nһ");

		usleep(5000000);

		return 0;

}

/*
 * Name						: line_to_demo
 * Description		: ---
 * Author					: XiaomaGee.
 *
 * History
 * -----------------------
 * Rev	: 0.00
 * Date	: 03/05/2012
 *
 * create.
 * ----------------------
 */

#define PI 3.1415926

struct POINT_T {
		int x, y;
};

		static int
line_to_demo(int pt)
{
		struct POINT_T points[20];
		int i, j, h, w, xcenter, ycenter;
		int radius, angle, step;
		double rads;
		STRING_T s;

		draw_frame();

		h = 500;
		w = 700;

		xcenter = w / 2 -80 ;                /* Determine the center of circle */
		ycenter = h / 2 + 75;
		radius = (h - 30) / (2);
		step = 360 / pt;        /* Determine # of increments	*/

		angle = 0;                      /* Begin at zero degrees	*/
		for (i = 0; i < pt; ++i) {      /* Determine circle intercepts	*/
				rads = (double)angle * PI / 180.0; /* Convert angle to radians	*/
				points[i].x = xcenter + (int)(cos(rads) * radius);
				points[i].y = ycenter - (int)(sin(rads) * radius);
				angle += step;          /* Move to next increment	*/
		}

		gui.circle(xcenter, ycenter, radius, 0, COLOR_FUCHSINE); /* Draw bounding circle    */

		for (i = 0; i < pt; ++i) {      /* Draw the cords to the circle */
				for (j = i; j < pt; ++j) { /* For each remaining intersect */
						gui.set_cursor(points[i].x, points[i].y); /* Move to beginning of cord	*/
						gui.line_to(points[j].x, points[j].y, COLOR_FUCHSINE); /* Draw the cord		*/
				}
		}
		font._default.single_byte = &tahoma26;
		font._default.double_byte = &yahei32;

		s.x = 530 ;
		s.y = 200;
		s.inverse = NULL;
		s.color =COLOR_RED;
		s.background_color = COLOR_BLACK;
		s.space.line = 5;
		s.space.word = 0;
		font.printf(&s,"ͨgui.line_to \ngui.circle Բ\nɵһ\nͼΡ");


		usleep(5000000);

		return 0;
}
/*
 * Name						: draw_tree
 * Description		: ---
 * Author					: XiaomaGee.
 *
 * History
 * -----------------------
 * Rev	: 0.00
 * Date	: 03/05/2012
 *
 * create.
 * ----------------------
 */
		static int
circle_demo(void)
{
		int i;
		STRING_T s;
		int x,y,r,color;

		draw_frame();
		for (i = 0; i < 2000; i++) {
				x = random(560);
				y = random(300);
				r = random(100);
				color = random(6) + 1;

				gui.circle(x + 120,y + 180,r,1,color);
		}

		return 0;

}
/*
 * Name						: wave_demo
 * Description		: ---
 * Author					: XiaomaGee.
 *
 * History
 * -----------------------
 * Rev	: 0.00
 * Date	: 03/05/2012
 *
 * create.
 * ----------------------
 */
		static int
wave_demo(void)
{
		float temp;
		int i,y;
		RECT_T r;
		STRING_T s;
		char * wave_string[]={
				"Ҳ\nSine","\nSquare","ǲ\nTriangle"
		};

		draw_frame();

		for(i = 0; i < 3; i++ ){
				r.x = 28;
				r.y = i * 170 + 85;
				r.width = 604;
				r.height = 150;
				r.fill_flag = 0;
				r.color = COLOR_GREEN;
				gui.rect(&r);
		}

		//sine
		gui.set_cursor(31,85 + 75);
		for(i = 1; i< 598; i++){
				temp = i;
				temp /= 598;
				temp *= 2*PI;
				temp *= 10;

				temp = sin(temp);
				temp += 1;
				temp /= 2;

				gui.line_to(i + 31,85 + 5 + 140*temp,COLOR_YELLOW);
		}
		//squa
		gui.set_cursor(32,85 + 75 + 170);

		for(i = 1; i< 598; i++){

				temp = i % 60;

				if(temp < 30)y =1;
				else y = 0;

				gui.line_to(i + 31,85 + 5 + 170 + 140*y,COLOR_YELLOW);
		}

		//squa
		gui.set_cursor(32,85 + 75 + 170*2);

		for(i = 1; i< 598; i++){

				temp = (i + 15) % 60;

				temp /= 30;

				if(temp >1)temp = 2- temp;

				gui.line_to(i + 31,85 + 5 + 170*2 + 140*temp,COLOR_YELLOW);
		}

		font._default.single_byte = &tahoma26;
		font._default.double_byte = &yahei32;

		for(i = 0; i < 3; i ++ ){
				s.x = 650 ;
				s.y = i* 170 + 110;
				s.inverse = NULL;
				s.color =COLOR_GREEN;
				s.background_color = COLOR_BLACK;
				s.space.line = 0;
				s.space.word = 0;
				font.printf(&s,"%s",wave_string[i]);
		}

		usleep(5000000);

		return 0;
}
/*
 * Name						: font_demo
 * Description		: ---
 * Author					: XiaomaGee.
 *
 * History
 * -----------------------
 * Rev	: 0.00
 * Date	: 03/05/2012
 *
 * create.
 * ----------------------
 */
		static int
font_demo(void)
{
		STRING_T s;
		SINGLE_BYTE_FONT_T * slist[]={
				&tahoma8,
				&tahoma9,
				&tahoma10,
				&tahoma11,
				&monaco,
				&courier,
				&fixedsys,
				&borlandTE,
				&tahoma12,
				&tahoma26
		};
		char * sname[]={
				"Tahoma8",
				"Tahoma9",
				"Tahoma10",
				"Tahoma11",
				"monaco",
				"courier",
				"fixedsys",
				"BoarlandTE",
				"Tahoma12",
				"Tahoma26",
		};
		int i;

		draw_frame();

		font._default.double_byte = & simsun16;

		s.x = 20 ;
		s.y = 75;
		s.inverse = NULL;
		s.color =COLOR_GREEN;
		s.background_color = COLOR_BLACK;
		s.space.line = 5;
		s.space.word = 0;

		for(i = 0; i< 10 ; i ++){
				font._default.single_byte = slist[i];
				s.x = 20 ;
				font.printf(&s,"%s: The quick brown fox jumps over the lazy dog.",sname[i]);
				font.printf(&s,"\n");
		}


		font._default.double_byte = & simsun16;
		font._default.single_byte = & fixedsys;
		s.x = 20;
		font.printf(&s,"\n16壺ǰ¹⣬ǵ˪ͷ£ͷ˼磡\n");

		font._default.double_byte = & yahei32;
		font._default.single_byte = & tahoma26;
		s.x = 20;
		font.printf(&s,"32ź壺ǰ¹⣬ǵ˪ͷ£ͷ˼磡");
		font.printf(&s,"\n");

		s.color =COLOR_RED;
		font._default.double_byte = & simsun16;
		font._default.single_byte = & nix48;
		s.x = 20;
		font.printf(&s,"0123456789.");
		s.y += 75;

		font._default.double_byte = & simsun16;
		font._default.single_byte = & nix96;
		s.x = 20;
		font.printf(&s,"01234567.");

		usleep(5000000);

		return 0;
}
