/*
 *	WEngine by eMan
 *  WWW : www.eman.wz.cz
 *  PATH : .\SDK\WCode\App\CBaseGL.h
 */

#ifndef _CBASEGL_H
#define _CBASEGL_H

#include "EngineH.h"
#include "CBase.h"
#include "CMapMsg.h"

#pragma comment (lib,"opengl32.lib")
#pragma comment (lib,"glu32.lib")
#pragma comment (lib,"glaux.lib")
#include <gl\gl.h>			
#include <gl\glu.h>			
#include <gl\glaux.h>	

// gl define
const char W_GL_2D = 0;
const char W_GL_3D = 1;

// mouse define
const char W_MOUSE_NONE			= -1;
const char W_MOUSE_LEFT			= 0;
const char W_MOUSE_RIGHT		= 1;
const char W_MOUSE_MIDDLE		= 2;
const char W_DC_MOUSE_LEFT		= 3;
const char W_DC_MOUSE_RIGHT		= 4;
const char W_DC_MOUSE_MIDDLE	= 5;
const char W_MOUSE_UNLEFT		= 6;
const char W_MOUSE_UNRIGHT		= 7;
const char W_MOUSE_UNMIDDLE		= 8;


static int number_win_gl=0;
static C_MAP_MSG_0 g_MapMsg_gl;
const uint TIMER_DEF = 10;

// TYPEDEF * pointery na funkce
typedef LONG (*pToWndProc)(HWND, UINT, WPARAM, LPARAM);
typedef void (*pToKeyboard)(bool[]);
typedef void (*pToMouse)(int,int,int);
typedef void (*pToDraw)();
typedef void (*pToTimer)();

class C_BASE_GL_0 : public C_BASE_0
{
private :
	pToWndProc _pWndProc;
	pToKeyboard _pKey;
	pToMouse _pMouse;
	pToDraw _pDraw;
	pToTimer _pTimer;
	uint _timer_value;
	bool (*_pInitGL)();
	void (*_pResizeGL)(int,int);

	bool _stopDraw;
	bool _keys[255];
	HDC	_hDC;	
	HGLRC _hRC;	
	int _bpp;
	bool _fullscreen;
	char _stav;						// budto 2D or 3D, aby program vedel jak se ma inicializovat

	//**//  Funkce  //**//
	void Init();
	bool CreateGLWindow(int bits,int width,int height,bool fullscreen);
	void KillGLWindow();
		
public :
	C_BASE_GL_0();
	C_BASE_GL_0(const char *cTitle,const char * cWnd=0);
	C_BASE_GL_0(pToWndProc p,const char *cTitle=0,const char * cWnd=0);
	~C_BASE_GL_0();

public :
	void SetFullscreen(bool f){_fullscreen = f;}
	bool GetFullscreen()const {return _fullscreen;}
	void ToSetCreate()
	{
		HWND hWnd = GetHwnd();
		RECT rect = GetRect();
		DestroyWindow(hWnd);
		hWnd = NULL;
		_hDC = NULL;
		_hRC = NULL;
		ChangeDisplaySettings(NULL,0);
		CreateGLWindow(_bpp, rect.right, rect.bottom, _fullscreen);
	}
	void SetBpp(int h) {_bpp = h;}
	int Create(const char *cTitle=0,const char * cWndName=0);
	bool Fullscreen(bool val);
	void WndProcGLFunc(pToWndProc p){_pWndProc = p;}
	void KeyboardGLFunc(pToKeyboard p) {_pKey = p;}
	void MouseGLFunc(pToMouse p){_pMouse = p;}
	void RenderGLFunc(pToDraw p){_pDraw = p;}
	void SetAktWglMakeCurrent();
	void SetNullWglMakeCurrent();
	// INITGLFUNC - nastavý funkci v pointru
	void InitGLFunc( bool(*p)() )
	{
		_pInitGL = p;
	}
	// TIMERGLFUNC - nasta
	void TimerGLFunc( pToTimer p, uint casovac )
	{
		_pTimer = p;
		_timer_value = casovac;
	}
	// RESIZEGLFUNC - nastavý funkci v pointru
	void ResizeGLFunc( void (*p)(int,int) )
	{
		_pResizeGL = p;
	}
	inline TPoint GetScreen()
	{
		RECT r = GetRect();
		TPoint p;
		p.x = (float)r.right;
		p.y = (float)r.bottom;
		return p;
	}
	virtual bool Register();
	virtual void Render();
	
protected :
	static LONG WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
	static bool InitGL();
	static void ResizeGL(int width,int height);
	static void KeyboardGL(bool keys[255]);
	static void MouseGL(int button,int x,int y);
	static void Draw();
};


#endif _CBASEGL_H