#include <../arch/arch.h>

vec_t alpha = 0, beta = 0;
vboolean autowarp = false;
vboolean enable_kb_repeat = false;

#include "dumpevt.c"

int main(void)
{
	ui_event_t ev;

	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);

	assert(vglInit("test",0,0,640,480,true,false,false,false,false));
	vglGetVersionString();	

	glClearColor(0,0.5,0.5,0.5);

	for(;;)
	{
		while(uiPending())
		{
			uiNextEvent(&ev);
			dump_event(0, &ev);

			if(ev.type == ui_window_expose_event || autowarp)
			{
				glClear(GL_COLOR_BUFFER_BIT);

				glPushMatrix();

				glTranslatef(0, 0, -2);

				if(autowarp)
				{
					glRotatef(alpha += ev.client_dx * 0.1,0,1,0);
					glRotatef(beta += ev.client_dy * 0.1,1,0,0);
				}

				glScalef(0.01, 0.01, 1);
				glBegin(GL_TRIANGLES);
					glColor3f(1, 0, 0);
					glVertex3f(0, -50, 0);
					glColor3f(0, 1, 0);
					glVertex3f(-50, 50, 0);
					glColor3f(0, 0, 1);
					glVertex3f(50, 50, 0);
				glEnd();
				
				glPopMatrix();

				vglSwap();
				if(!autowarp)
					continue;
			}
			if(ev.type != ui_key_event)
				continue;
			switch(ev.key.scan)
			{
				case sc_escape:
					if(!ev.key.press)
						goto close_win;
					break;
				case sc_s:
					/*
					archWinShow(w, false);
					archWinPoll(w, false);
					while(uiPending())
					{
						uiNextEvent(&ev);
						dump_event(0, &ev);
					}
					timeDelay(3000);
					archWinShow(w, true);
					*/
					break;
				case sc_p:
					// archWinShowPointer(w, true);
					break;
				case sc_o:
					// archWinShowPointer(w, false);
					break;
				case sc_a:
					if(ev.key.press)
						uiAutoRepeat(enable_kb_repeat = !enable_kb_repeat);
					break;
				case sc_w:
					if(ev.key.press)
						uiAutoWarp(autowarp = !autowarp);
					break;
				case sc_wheelup:
					glMatrixMode(GL_COLOR);
						glScalef(0.9,0.9,0.9);
					glMatrixMode(GL_MODELVIEW);
					break;
				case sc_wheeldown:
					glMatrixMode(GL_COLOR);
						glScalef(1.1,1.1,1.1);
					glMatrixMode(GL_MODELVIEW);
					break;
				default:
					break;
			}
		}
	}

close_win:;
	vglDestroy();

	memPrintStats();
	return 0;
}
