#ifndef DEF_GRAPHWINDOW
 #define DEF_GRAPHWINDOW

//------------------------------------------//
//				TKA4 CGraphWin				//
//				03. 07. 2007				//
//------------------------------------------//

//--- Pragma 
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glu32.lib")
#pragma comment (lib, "glut32.lib")
//=== Pragma 


//--- Includes
#include <windows.h>
#include <stdio.h>
#include <time.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include "../GL/glut.h"

#include "TX_Engine.h"
//=== Includes 


//--- Defines
#define DEFAULT_TITLE  "tx"

#define BEGIN_X 0
#define BEGIN_Y 0
#define BEGIN_W 640
#define BEGIN_H 480


#define USING_GLUT  true
//=== Defines


//--- Classes
class txGraphWindow;
//=== Classes 

//--- Vars
txGraphWindow  *GraphWin;
//=== Vars


//---  Class Graph TX  ---//
//	Graph TX is window    // 
//	with OpenGL context.  // 

class txGraphWindow {
protected:
	HDC  hDC;
	
	//---
	int x, y, w, h;
	char* title;
	
public:
	txEngine *Engine;

public:
	 txGraphWindow() { 
		title = DEFAULT_TITLE; 
		Engine = NULL;
		x = BEGIN_X; y = BEGIN_Y; w = BEGIN_W; h = BEGIN_H;
	};
	 
	txGraphWindow(txEngine *Eng_, char* s = DEFAULT_TITLE, 
		int x_ = 100, int y_ = 100, int w_ = 640, int h_ = 480)
			: x(x_), y(y_), w(w_), h(h_), title(s)
	{ 
		SetEngine(Eng_);
		Activate();
	};

	~txGraphWindow() { if (title != NULL) delete title; }

	virtual inline void  SetEngine(txEngine *Eng) { Engine = Eng; }

	virtual inline HDC  GetHDC() { return hDC; }

	virtual inline void Activate() { GraphWin = this; }

	virtual inline void WinMain();
};


//------------------------- :: Functions :: ---------------------------

//--- CGraphTX - On Paint
void  OnPaint() 
{
int begin_time = clock();
	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();
	
	GraphWin -> Engine -> Go();
	
	glPopMatrix();
	glutSwapBuffers();
		
int res_time = clock() - begin_time;
};


//--- CGraphTX - On Mouse
void  OnMouse(int but, int state, int x, int y)
{
	switch (but) {
		case GLUT_WHEEL_UP : GraphWin -> Engine -> OnZoomIn(); break;
		case GLUT_WHEEL_DOWN : GraphWin -> Engine -> OnZoomOut(); break;
	};
	glutPostRedisplay();
};


//--- CGraphTX - WIN MAIN Function
void  txGraphWindow :: WinMain()
{
	int a = 1;
	char **s = new char*;
	(*s) = new char;

	glutInit( &a, s);
	glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );

	glutInitWindowPosition(x, y);
	glutInitWindowSize(w, h);
	glutCreateWindow(title);

	glutDisplayFunc( OnPaint );
	glutMouseFunc( OnMouse );

	Engine->Init();

	glutMainLoop();
};


#endif
