/*
 *	WEngine by eMan
 *  WWW : www.eman.wz.cz
 *  PATH : SDK\Code\WinMain.cpp
 */

// Killing GL-okno
// fullscreen mode GL
// InitSoubor ** dodelat
// xx

#include "stdafx.h"
#include "..\WCode\Engine.h"
#include "Game.h"
#include "../WCode/Config/CConfig.h"

int SCRHeight = 768;
int SCRWidth = 1024;
HWND g_hWnd;  
int g_nasGfx=0;
C_CONFIG_0		g_gameConfig("gam_config.cfi");

LONG WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
C_APP_WIN_0 app;

bool			b_full_screen = 0;
const bool		DEBUG_OKNO = 0;

C_BASE_GL_0 *gl2 = new C_BASE_GL_0("W-Engine - Hra 2");

int g_button;

void konec()
{
	app.Exit();
}

void PredRender()
{
	Render();
	POINT pMouse;
	GetCursorPos(&pMouse);
	ScreenToClient(g_hWnd, &pMouse);
	onMouseMove(g_button, pMouse.x, pMouse.y);
}

void PredOnMouse(int button, int x, int y)
{
	g_button = button;
	onMouse(button, x, y);
}

//////////////////////////////////////////////////////////////////////////
// WINMAIN - hlavni funkce prohramu main
//////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
	C_BASE_WIN_0 *okno = new C_BASE_WIN_0(WndProc, "Debug okno");
	
	// Funkce pro  GL aplikace //
	gl2->InitGLFunc(onInit);
	gl2->ResizeGLFunc(onResize);
	gl2->RenderGLFunc(PredRender);
	gl2->KeyboardGLFunc(onKeyboard);
	gl2->MouseGLFunc(PredOnMouse);
	gl2->TimerGLFunc(onTimer, 200);
	// sdl

	TPoint r_screen = gl2->GetScreen();
	SCRWidth = r_screen.x;
	SCRHeight = r_screen.y;
	
	// Registrace oken //
	if(DEBUG_OKNO) app.Register(okno);
	app.Register(gl2);
	//app.Register(sdl_okno);

	if(!DEBUG_OKNO) textREC.SetWrite(0);
	g_hWnd = gl2->GetHwnd();
	gl2->SetFocus();
	
	// NEHE CODE
	/*if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?", MB_YESNO | MB_ICONQUESTION) == IDNO)
	{
		b_full_screen = 0;// Běh v okně
	}*/
	// END NEHE

	if(b_full_screen)
	{
		gl2->SetFullscreen(true);
		gl2->ToSetCreate();
	}
	W_GLEW_Init();

	// nacteni game configu 
	if( !g_gameConfig.LoadFile() ) 
		cError.ErrorMsg(0, true, true, "Configuracni soubor", "Nebyl nalezen conf. soubor gam_config.cfi");
	g_gameConfig.LoadInteger("SETGFX", "gfx", g_nasGfx );
	bool bAnis=0;
	g_gameConfig.LoadInteger("SETGFX", "anisotropic", (int&)bAnis );
	
	if( bAnis && W_GLEW_Support("GL_EXT_texture_filter_anisotropic") )
	{
		float max_anisotropic;
		max_anisotropic = W_EXT_GetF(WEXT_MAX_ANISOTROPIC);
		if(max_anisotropic > 4.0) max_anisotropic = 4.0f;
		W_EXT_SetF(WEXT_MAX_ANISOTROPIC, max_anisotropic);
	}

	/*// Vertikalni sznchroniyace
	if( W_GLEW_Support("WGL_EXT_swap_control"))
	{
		wglSwapIntervalEXT(1);
	}*/

	
	/*if(akt_anisotropic > max_anisotropic) akt_anisotropic = max_anisotropic;

	if ( !glutExtensionSupported("GL_EXT_texture_filter_anisotropic"))
	{
		C_ERROR_REPORT_0 *r = new C_ERROR_REPORT_0(ERROR_NO_EXTENSIONS,false,false,"Extensions","Karta nepodporuje GL_EXT_texture_filter_anisotropic");
		cError.Error(*r);
		b_anisotropic_filter = 0;
	}*/

	//if(b_anisotropic_filter) glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &akt_anisotropic);
	//textREC.Add("max anisotropic : ", max_anisotropic);
	//textREC.Add("akt anisotropic : ", akt_anisotropic);

	
	app.Loop();
	return 0;
}

//////////////////////////////////////////////////////////////////////////
// WndProc - Zpracovani zprav okna
//////////////////////////////////////////////////////////////////////////
LONG WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{   
	HDC         hdc ;
	PAINTSTRUCT ps ;
	RECT        rect ; 
	C_BASE_0 *pWnd = app.GetPWnd(hWnd);
	 
     switch (uMsg)
     {
     case WM_CREATE:
			textREC.SetHwnd(hWnd);
          return 0 ;          
     case WM_PAINT:
          hdc = BeginPaint (hWnd, &ps) ;
          GetClientRect (hWnd, &rect) ;
          
		  textREC.DrawText(hWnd);
           
          EndPaint (hWnd, &ps) ;
          return 0 ;
          
	 case WM_LBUTTONDOWN:
		 return 0;
		
     case WM_DESTROY:
		 app.Exit();
          return 0 ;
     }
     return DefWindowProc (hWnd, uMsg, wParam, lParam) ;
}