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

#include "stdafx.h"
#include "CBaseGL.h"
#include "../Config/CConfig.h"

const int MAX_WINDOW_GL = 10;
static bool g_loadConfig = false;
HWND act_hWnd;

C_CONFIG_0 glConfig("eng_config.cfi"); //config.txt

C_BASE_GL_0* GetPGL()
{
	C_BASE_GL_0 *pBaseGL = (C_BASE_GL_0*)g_MapMsg_gl.GetPWnd(act_hWnd);
	return pBaseGL;
}

//////////////////////////////////////////////////////////////////////////
// CONSTRUKTOR - bezparametricky konstruktor
//////////////////////////////////////////////////////////////////////////
C_BASE_GL_0::C_BASE_GL_0()
{
	Init();
	Create();
}

//////////////////////////////////////////////////////////////////////////
// CONSTRUKTOR - zahlavy, className
//////////////////////////////////////////////////////////////////////////
C_BASE_GL_0::C_BASE_GL_0(const char *cTitle,const char * cWnd)
{
	Init();
	Create(cTitle,cWnd);
}

//////////////////////////////////////////////////////////////////////////
// DESTRUKTOR - destruktor
//////////////////////////////////////////////////////////////////////////
C_BASE_GL_0::~C_BASE_GL_0()
{
	this->KillGLWindow();
}

//////////////////////////////////////////////////////////////////////////
// CONSTRUKTOR - ukazatel na fce(WndProc), titulek v okne, lpszClassName
//////////////////////////////////////////////////////////////////////////
C_BASE_GL_0::C_BASE_GL_0(pToWndProc p,const char *cTitle,const char * cWnd)
{
	Init();
	_pWndProc = p;
	Create(cTitle,cWnd);
}

//////////////////////////////////////////////////////////////////////////
// INIT - pred praci s tridou musi byt spusten,implicitne je v konstruk.
//////////////////////////////////////////////////////////////////////////
void C_BASE_GL_0::Init()
{
	_fullscreen = FALSE;
	_bpp = 16;
	_timer_value = 0;
	_pWndProc = WndProc;
	_pKey = KeyboardGL;
	_pMouse = MouseGL;
	_pDraw = Draw;
	RECT r = {0,0,320,240};
	// LOAD CONFIG - predelat
		if(glConfig.LoadFile())
		{
			glConfig.LoadInteger("GL_APP","rectL",(int&)r.left);
			glConfig.LoadInteger("GL_APP","rectT",(int&)r.top);
			glConfig.LoadInteger("GL_APP","rectR",(int&)r.right);
			glConfig.LoadInteger("GL_APP","rectB",(int&)r.bottom);
			glConfig.LoadInteger("GL_APP","fullscreen",(int&)_fullscreen);
			glConfig.LoadInteger("GL_APP","bpp",(int&)_bpp);
		}else
		{
			// Error
			cError.ErrorMsg(0,true,true,"Nemuzu nacist configuracni soubor");
		}
	// END
	SetRect(r);

	this->SetNameP("Engine Base Code by eMan");
	this->SetNamePZ("z Z z");
	this->SetNamePL("Loading............");

	_pInitGL = InitGL;
	_pResizeGL = ResizeGL;
	_stav = W_GL_3D;
	_stopDraw = false;
	for(register int i=0;i<256;i++) _keys[i]=FALSE;
}

//////////////////////////////////////////////////////////////////////////
// CREATE - vytvori WndClass ale nezaregistruje !!
//////////////////////////////////////////////////////////////////////////
int C_BASE_GL_0::Create(const char *cTitle,const char * cWndName)
{
	logEngine.ExWrite("Create GL okno ....");
	if(number_win_gl >= MAX_WINDOW_GL) return 1;
	if(cTitle) SetNameP(cTitle);
	std::string nc;

	if(!cWndName)
	{ 
		char temp[255];
		sprintf(temp,"gl_%d", number_win_gl);
		nc = temp;
	}else
	{
		nc = cWndName;
	}
	WNDCLASS wndclass;
	// WndClass Deklaration
	wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
	wndclass.lpfnWndProc   = (WNDPROC)_pWndProc;
	wndclass.cbClsExtra    = 0 ;
	wndclass.cbWndExtra    = 0 ;
    wndclass.hInstance     = GetModuleHandle(NULL);
	wndclass.hIcon         = LoadIcon (NULL,  0  ) ;
	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
	wndclass.hbrBackground = NULL ;
	wndclass.lpszMenuName  = NULL ;
	wndclass.lpszClassName = (LPTSTR)nc.c_str();
	this->SetWndClass(wndclass);
	this->SetNameClass(nc);
	number_win_gl++;
	this->SetBoolDis(false);
	this->InkNumWin();
	logEngine.Write("OK");
	return 0;
}
static int bu=W_MOUSE_NONE,xx=0,yy=0;
void set(int button, int x, int y)
{
	bu = button;
	xx = x;
	yy = y;
}

//////////////////////////////////////////////////////////////////////////
// WNDPROC - pokud uzivatel nezada vlastni WndProc,bude pouzita tato
//////////////////////////////////////////////////////////////////////////
LONG C_BASE_GL_0::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{   
/*	HDC         hdc ;
	PAINTSTRUCT ps ;
	RECT        rect ; */
	act_hWnd = hWnd;
	C_BASE_GL_0 *pBaseGL = GetPGL();
	std::string temp;
	
	static bool ted_create=1;
	if(pBaseGL && ted_create)
	{
		ted_create=0;
		if(pBaseGL->_timer_value > 0) SetTimer(pBaseGL->GetHwnd(), TIMER_DEF, pBaseGL->_timer_value, NULL);
	}
	
	switch (uMsg)
	{
		// WMCREATE - spusti se pri zaregistrovani WndProc
		case WM_CREATE:
		{
			ted_create = 1;
			return 0 ;		
		}
		
		
		// WMSETFOCUS - okno ma focus
		case WM_SETFOCUS:
		{	
			if(pBaseGL)
			{
				temp = pBaseGL->GetNameP();
				pBaseGL->SetWindowText(temp.c_str());
				pBaseGL->_stopDraw = false;
			}
			break;
		}

		// WMKILLFOCUS - okno nema focus
		case WM_KILLFOCUS:
		{
			if(pBaseGL)
			{
				temp = pBaseGL->GetNamePZ();
				pBaseGL->SetWindowText(temp.c_str());
				pBaseGL->_stopDraw = true;
			}
			break;
		}

		// WMSYSCOMMAND - zabrani prechosu do usporneho rezimu,a do sporice obrazovky
		case WM_SYSCOMMAND:
		{
			switch (wParam)
			{				
				case SC_SCREENSAVE:
				case SC_MONITORPOWER:
				return 0;
			}	
			break;
		}
		
		// WMKEYDOWN - zaznamenava kazdou stisknutou klavesu
		case WM_KEYDOWN:
		{
			pBaseGL->_keys[wParam] = TRUE;
			return 0;
		}

		// WMKEYUP - zaznamenava kazdou uvolnenou klavesu
		case WM_KEYUP:
		{	
			pBaseGL->_keys[wParam] = FALSE;
			return 0;
		}

		// WMSIZE - zmena velikosti okna
		case WM_SIZE:// Změna velikosti okna
		{
			if(pBaseGL != NULL)
			{
				RECT cRect;
				GetClientRect(hWnd, &cRect);
				//(*(pBaseGL->_pResizeGL))(LOWORD(lParam),HIWORD(lParam));
				(*(pBaseGL->_pResizeGL))(cRect.right, cRect.bottom);
			}
			return 0;
		}

		//////////////////////////////////////////////////////////////////////////
		// MOUSE REPORTS - obsluha zprav z mysi
		//////////////////////////////////////////////////////////////////////////
		// WMLBUTTONDOWN - levy stisk tlacitka
		set(W_MOUSE_NONE,0,0);
		case WM_LBUTTONDOWN:
		{
			if(pBaseGL != NULL)
			{
				(*(pBaseGL->_pMouse))((int)W_MOUSE_LEFT,(int)LOWORD(lParam),(int)HIWORD(lParam));
				//set((int)W_MOUSE_LEFT,(int)LOWORD(lParam),(int)HIWORD(lParam));
			}
			return 0;
		}
		// WMLBUTTONDOWN - leve uvolneni tlacitka
		case WM_LBUTTONUP:
		{
			if(pBaseGL != NULL)
			{
				(*(pBaseGL->_pMouse))((int)W_MOUSE_UNLEFT,(int)LOWORD(lParam),(int)HIWORD(lParam));
				//set((int)W_MOUSE_UNLEFT,(int)LOWORD(lParam),(int)HIWORD(lParam));
			}
			return 0;
		}
		// WMRBUTTONDOWN - pravy stisk tlacitka
		case WM_RBUTTONDOWN:
		{
			if(pBaseGL != NULL)
			{
				(*(pBaseGL->_pMouse))((int)W_MOUSE_RIGHT,(int)LOWORD(lParam),(int)HIWORD(lParam));
			}
			return 0;
		}
		// WM_RBUTTONUP - prave uvolneni tlacika
		case WM_RBUTTONUP:
		{
			if(pBaseGL != NULL)
			{
				(*(pBaseGL->_pMouse))((int)W_MOUSE_UNRIGHT,(int)LOWORD(lParam),(int)HIWORD(lParam));
			}
			return 0;
		}
		// WMMBUTTONDOWN - prostredni stisk tlacitka
		case WM_MBUTTONDOWN:
		{
			if(pBaseGL != NULL)
			{
				(*(pBaseGL->_pMouse))((int)W_MOUSE_MIDDLE,(int)LOWORD(lParam),(int)HIWORD(lParam));
			}
			return 0;
		}	
		// WM_MBUTTONDOWN - prostredni uvolneni tlacitka
		case WM_MBUTTONUP:
		{
			if(pBaseGL != NULL)
			{
				(*(pBaseGL->_pMouse))((int)W_MOUSE_UNMIDDLE,(int)LOWORD(lParam),(int)HIWORD(lParam));
			}
			return 0;
		}
		// WMLBUTTONDBLCLK - dvoj click leveho buttonu
		case WM_LBUTTONDBLCLK:
		{
			if(pBaseGL != NULL)
			{
				(*(pBaseGL->_pMouse))((int)W_DC_MOUSE_LEFT,(int)LOWORD(lParam),(int)HIWORD(lParam));
			}
			break;//return 0;
		}
		// WMRBUTTONDBLCLK - dvoj click praveho buttonu
		case WM_RBUTTONDBLCLK:
		{
			if(pBaseGL != NULL)
			{
				(*(pBaseGL->_pMouse))((int)W_DC_MOUSE_RIGHT,(int)LOWORD(lParam),(int)HIWORD(lParam));
			}
			return 0;
		}
		// WMMBUTTONDBLCLK - dvoj click stredniho buttonu
		case WM_MBUTTONDBLCLK:
		{
			if(pBaseGL != NULL)
			{
				(*(pBaseGL->_pMouse))((int)W_DC_MOUSE_MIDDLE,(int)LOWORD(lParam),(int)HIWORD(lParam));
			}
			return 0;
		}
		case WM_TIMER:
		{
			switch(wParam)
			{
			case TIMER_DEF:
				(*(pBaseGL->_pTimer))();
				break;
			}
			return 0;
		}
		// END MOUSE REPORTS
		//////////////////////////////////////////////////////////////////////////
				
		case WM_ACTIVATEAPP: 
		if (wParam) {
		// Prepnuti do okna
			//MessageBox(0,"1","",0);
		} else {
		// Prepnuti z naseho okna
			//MessageBox(0,"2","",0);
			
		};
break;
		// WMPAINT - preklesleni okna
		/*case WM_PAINT:
		{
			hdc = BeginPaint (hWnd, &ps) ;
			GetClientRect (hWnd, &rect) ;
			DrawText (hdc, TEXT ("eMan Engine ver. 1.0.0.0"), -1, &rect,
				DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          
			EndPaint (hWnd, &ps) ;
			return 0;
		}*/

		// WMDESTROY - vola se pri zruseni okna
		/*case WM_DESTROY:
			//PostQuitMessage(0);
		return 0 ;*/
	}
	return DefWindowProc (hWnd, uMsg, wParam, lParam) ;
}

//////////////////////////////////////////////////////////////////////////
// - KILLGLWINDOW
//////////////////////////////////////////////////////////////////////////
GLvoid C_BASE_GL_0::KillGLWindow()								// Properly Kill The Window
{
	std::string name_class = this->GetNameClass();
	_fullscreen = false;
	
	wglMakeCurrent(_hDC,_hRC);
	if (!wglDeleteContext(_hRC))						// Are We Able To Delete The RC?
	{
			//MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
			cError.ErrorMsg(0,true,true,"SHUTDOWN ERROR","Release Rendering Context Failed");
	}
/*	if (_fullscreen)										// Are We In Fullscreen Mode?
	{
		ChangeDisplaySettings(NULL,0);					// If So Switch Back To The Desktop
		ShowCursor(TRUE);								// Show Mouse Pointer
	}
	
	if (_hRC)											// Do We Have A Rendering Context?
	{
		if (!wglMakeCurrent(NULL,NULL))					// Are We Able To Release The DC And RC Contexts?
		{
			MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		}

		if (!wglDeleteContext(_hRC))						// Are We Able To Delete The RC?
		{
			MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		}
		_hRC=NULL;										// Set RC To NULL
	}

/*	HWND hWnd = GetHwnd();
	if (_hDC && !ReleaseDC(hWnd,_hDC))					// Are We Able To Release The DC
	{
		MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		_hDC=NULL;										// Set DC To NULL
	}

	if (hWnd && !DestroyWindow(hWnd))					// Are We Able To Destroy The Window?
	{
		MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hWnd=NULL;										// Set hWnd To NULL
	}
	

	WNDCLASS wnd = this->GetWndClass();
	if (!UnregisterClass(name_class.c_str(),wnd.hInstance))			// Are We Able To Unregister Class
	{
		MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		wnd.hInstance=NULL;									// Set hInstance To NULL
		
	}
	SetWndClass(wnd);
	SetHwnd(hWnd);*/
}

//////////////////////////////////////////////////////////////////////////
// REGISTER - 
//////////////////////////////////////////////////////////////////////////
bool C_BASE_GL_0::Register()
{	
	std::string name_class = GetNameClass();
	if (!RegisterClass (&GetWndClass()))
	{
		//MessageBox (NULL, TEXT ("Nepodařilo se zaregistrovat třídu"),name_class.c_str(), MB_ICONERROR);
		cError.ErrorMsg(ERROR_REGISTRED,true,true,"name_class.c_str()","Nepodařilo se zaregistrovat třídu");
		return FALSE;
	}
	RECT r = GetRect();
	CreateGLWindow(_bpp, r.right, r.bottom, _fullscreen);	
	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
// CREATEGLWINDOW - 
//////////////////////////////////////////////////////////////////////////
bool C_BASE_GL_0::CreateGLWindow(int bits,int width,int height,bool fullscreen)
{
	GLuint PixelFormat;
	WNDCLASS w = GetWndClass();
	HINSTANCE hInstance =  w.hInstance;
	DWORD dwExStyle;			
	DWORD dwStyle;
	std::string name_class = GetNameClass();
	std::string zahlavi = GetNameP();
	RECT rect = GetRect();	
	
	if (fullscreen)												// Attempt Fullscreen Mode?
	{
		DEVMODE dmScreenSettings;								// Device Mode
		memset(&dmScreenSettings,0,sizeof(dmScreenSettings));	// Makes Sure Memory's Cleared
		dmScreenSettings.dmSize=sizeof(dmScreenSettings);		// Size Of The Devmode Structure
		dmScreenSettings.dmPelsWidth	= width;				// Selected Screen Width
		dmScreenSettings.dmPelsHeight	= height;				// Selected Screen Height
		dmScreenSettings.dmBitsPerPel	= bits;					// Selected Bits Per Pixel
		dmScreenSettings.dmFields= DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

		// Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
		if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
		{
			// If The Mode Fails, Offer Two Options.  Quit Or Use Windowed Mode.
			if (MessageBox(NULL,"Tento mod neni podporovan vasi grafickou kartou. Zmente nastaveni v Setupu. \n Chcete spustit program v okne?","W Engine",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
			{
				fullscreen=FALSE;		// Windowed Mode Selected.  Fullscreen = FALSE
			}
			else
			{
				// Pop Up A Message Box Letting User Know The Program Is Closing.
				//MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
				return FALSE;									// Return FALSE
			}
		}
	}


	dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			
	dwStyle=WS_OVERLAPPEDWINDOW;
	
	if (fullscreen)												// Are We Still In Fullscreen Mode?
	{
		dwExStyle=WS_EX_APPWINDOW;								// Window Extended Style
		dwStyle=WS_POPUP;										// Windows Style
	//	ShowCursor(FALSE);										// Hide Mouse Pointer
		rect.left = 0;
		rect.top = 0;
		rect.right = width;
		rect.bottom = height;
		this->SetRect(rect);
		//AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
	}
	//AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
    HWND hWnd = CreateWindowEx (dwExStyle,
								name_class.c_str(),         
								zahlavi.c_str(),			
								dwStyle |							
								WS_CLIPSIBLINGS |					
								WS_CLIPCHILDREN,        
								rect.left,              
								rect.top,              
								rect.right,              
								rect.bottom,              
								NULL,                       
								NULL,                       
								hInstance,                  
								0) ;                     

	static	PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be
	{
		sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
		1,											// Version Number
		PFD_DRAW_TO_WINDOW |						// Format Must Support Window
		PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
		PFD_DOUBLEBUFFER,							// Must Support Double Buffering
		PFD_TYPE_RGBA,								// Request An RGBA Format
		bits,										// Select Our Color Depth
		0, 0, 0, 0, 0, 0,							// Color Bits Ignored
		0,											// No Alpha Buffer
		0,											// Shift Bit Ignored
		0,											// No Accumulation Buffer
		0, 0, 0, 0,									// Accumulation Bits Ignored
		24,											// 24Bit Z-Buffer (Depth Buffer)  
		0,											// No Stencil Buffer
		0,											// No Auxiliary Buffer
		PFD_MAIN_PLANE,								// Main Drawing Layer
		0,											// Reserved
		0, 0, 0										// Layer Masks Ignored
	};
	
	if (!(_hDC=GetDC(hWnd)))							// Did We Get A Device Context?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	if (!(PixelFormat=ChoosePixelFormat(_hDC,&pfd)))	// Did Windows Find A Matching Pixel Format?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	if(!SetPixelFormat(_hDC,PixelFormat,&pfd))		// Are We Able To Set The Pixel Format?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	if (!(_hRC=wglCreateContext(_hDC)))				// Are We Able To Get A Rendering Context?
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	if(!wglMakeCurrent(_hDC,_hRC))					// Try To Activate The Rendering Context
	{
		KillGLWindow();								// Reset The Display
		MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;								// Return FALSE
	}

	this->SetHwnd(hWnd);
	::ShowWindow(hWnd,SW_SHOW);
	::UpdateWindow(hWnd);
	::SetForegroundWindow(hWnd);						
	::SetFocus(hWnd);	
	g_MapMsg_gl.Register(this);
		

	uint time1 = timeGetTime();
	(*_pInitGL)();
	uint time2 = timeGetTime();
		
	logEngine.ExWrite("OnInit time loding : ");
	char string[255];
	sprintf(string, "%fs", (float)(((float)(time2-time1))/1000.0f));
	logEngine.ExWriteln(string);
	
	RECT cRect;
	GetClientRect(hWnd, &cRect);
	(*_pResizeGL)(cRect.right,cRect.bottom);	
	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
// INITGL - zakladní inicializace GL-okna,pokuď uživatel nezadá jinou
//////////////////////////////////////////////////////////////////////////
bool C_BASE_GL_0::InitGL()
{
	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.0f, 1.0f, 1.0f, 0.0f);				// Black Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();
	
	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
// RESIZEGL - zakládní fce pro změnu velikosti okna
//////////////////////////////////////////////////////////////////////////
void C_BASE_GL_0::ResizeGL(int width,int height)
{
	C_BASE_GL_0 *pBaseGL = (C_BASE_GL_0*)g_MapMsg_gl.GetPWnd(act_hWnd);
	if(pBaseGL->_stav == W_GL_2D)
	{
		glViewport(0,0,width,height);						
		glMatrixMode(GL_PROJECTION);						
		glLoadIdentity();									
		glOrtho(0.0f,width,0,height,-1.0f,1.0f);		
		glScalef(1, -1, 1);   
		glTranslatef(0, -height, 0);
		glMatrixMode(GL_MODELVIEW);
	}else
	if(pBaseGL->_stav == W_GL_3D)
	{
		glViewport(0,0,width,height);						
		glMatrixMode(GL_PROJECTION);						
		glLoadIdentity();									
		gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,300.0f);
		glMatrixMode(GL_MODELVIEW);							
		glLoadIdentity();	
	}
}

//////////////////////////////////////////////////////////////////////////
// RENDERING - rendering
//////////////////////////////////////////////////////////////////////////
void C_BASE_GL_0::Render()
{
	wglMakeCurrent(_hDC,_hRC);
	(*_pKey)(_keys);			// obsluha klavesnice
	//(_pMouse)(bu,xx,yy);
	if(!_stopDraw) (_pDraw)();
	SwapBuffers(_hDC);	
	wglMakeCurrent(NULL, NULL);
}

//////////////////////////////////////////////////////////////////////////
// 
//////////////////////////////////////////////////////////////////////////
void C_BASE_GL_0::SetAktWglMakeCurrent()
{
	wglMakeCurrent(_hDC,_hRC);
}

//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
void C_BASE_GL_0::SetNullWglMakeCurrent()
{
	wglMakeCurrent(NULL, NULL);
}

//////////////////////////////////////////////////////////////////////////
// DRAW - základní fce pro vykreslování,pokuď uživatel nezada jinou
//////////////////////////////////////////////////////////////////////////
void C_BASE_GL_0::Draw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	glLoadIdentity();
	glTranslatef(0,0,0);
	static rot;rot++;
	glRotatef(rot,0,0,1);
	glBegin(GL_TRIANGLES);
		glColor3ub(0,255,0);
		glVertex3f(0,0,-5);
		glColor3ub(0,0,255);
		glVertex3f(-1,-1,-5);
		glColor3ub(255,0,0);
		glVertex3f(-1,0,-5);
	glEnd();
}

//////////////////////////////////////////////////////////////////////////
// KeyboardGL - zachytava stisknute tlacitka
//////////////////////////////////////////////////////////////////////////
void C_BASE_GL_0::KeyboardGL(bool keys[255])
{
	C_BASE_GL_0 *pBaseGL = GetPGL();
	if(keys[27]) pBaseGL->Close();
}

//////////////////////////////////////////////////////////////////////////
// MOUSEGL - pri stisku tlacitka se spusti
//////////////////////////////////////////////////////////////////////////
void C_BASE_GL_0::MouseGL(int button,int x,int y)
{
	
}