//-----------------------------------------------------------------------------------
// Bismillahirrahmanirrahim  
//-----------------------------------------------------------------------------------
//																					|
//               MOS 2D Engine ( MEYDANERĘ OpenGL Software Engine)					|
//																				    |
// ----------------------------------------------------------------------------------
//    Yapimci = Vedat Meydaneri														|
//     email = Chenmy1@yahoo.com													|
//     web = www.turkop.cjb.net														|
//-----------------------------------------------------------------------------------
// MOS 2D Engine (Vedat Meydaneri ) - Olusturma tarihi :10/ Temmuz /2003 saat: 15:00 |
//************************************************************************************
//************************************************************************************
//************************************************************************************
//------------------------------------------------------------------------------------  
#include "mosekran.h"


MOSEkran::MOSEkran(void)
{

}
MOSEkran::~MOSEkran(void)
{

}
//------------------------------------------------------------------//
//------------------------------------------------------------------//
//- GLOBALS --------------------------------------------------------//
//------------------------------------------------------------------//
//------------------------------------------------------------------//

 HINSTANCE g_hInstance;	
HWND	  g_hWnd;			
 HDC		  g_hDC;			
HGLRC	  g_hRC;

void MOSEkran::glResizeScenep(int a_iWidth, int a_iHeight,GLdouble Fovs)
{


	if(a_iHeight==0)										
		a_iHeight= 1;									

	glViewport(0, 0, a_iWidth, a_iHeight);

	glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();										


	gluPerspective(Fovs, a_iWidth/a_iHeight, 0.1f, 8000.0f);

	glMatrixMode(GL_MODELVIEW);	
	glLoadIdentity();	
}

void MOSEkran::glResizeSceneo(int a_iWidth, int a_iHeight)
{
	if(a_iHeight==0)			
		a_iHeight= 1;	

	glViewport(0, 0, a_iWidth, a_iHeight);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();						

	
	glOrtho(0, g_EkranX, 0, g_EkranY, -500, 500);

	glMatrixMode(GL_MODELVIEW);	
	glLoadIdentity();							
}

//------------------------------------------------------------------//
bool MOSEkran::Olustur(int a_iEkranX, int a_iEkranY, int a_iEkranBPP, int a_iPencereTamEkran, const char* a_szPencereadi)
{
	WNDCLASS winclass;				
	DEVMODE  dmScreenSettings;		
	GLuint	 PixelFormat;		
	DWORD	 dwExStyle;				
	DWORD	 dwStyle;			
	RECT     winrect;   

	kameraFOV=75.0f;

	g_EkranX = a_iEkranX;
	g_EkranY = a_iEkranY;
	g_BPP = a_iEkranBPP;


	strcpy(pu_szPencereadi, a_szPencereadi);

	winrect.left  = (long)0;	
	winrect.top   = (long)0;	
	winrect.right = (long)g_EkranX;
	winrect.bottom= (long)g_EkranY;

	g_hInstance			  = GetModuleHandle(NULL);			
	winclass.style		  = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	
	winclass.lpfnWndProc  = WindowProc;
	winclass.cbClsExtra	  = 0;									
	winclass.cbWndExtra	  = 0;								
	winclass.hInstance	  = g_hInstance;					
	winclass.hIcon		  = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON1));	
	winclass.hCursor	  = LoadCursor(NULL, IDC_ARROW);	
	winclass.hbrBackground= NULL;							
	winclass.lpszMenuName = NULL;							
	winclass.lpszClassName= PEN_CLASSADI;					


	if(!RegisterClass(&winclass))	
	{

		return false;		
	}
	else
	{

	}
	
	g_log.bPrint("-------------------------------------");
	g_log.bPrint("MOS2D Engine : TURKOP");
	g_log.bPrint("  VEDAT MEYDANERI  2004-0cak-18");
	g_log.bPrint("-------------------------------------\n");

	if(a_iPencereTamEkran==PEN_SORU)
	{
		if(MessageBox(g_hWnd, "Mos2d Engine Test oyunu  \nBu oyun sadece deneme surumudur.\nUYARI oyunun hizli calismasi icin ekran karti son suruculeri yukleyin.\nXP de yavas calisabilir!",pu_szPencereadi , MB_OK | MB_ICONQUESTION)==IDOK)
			pu_bTamekran= PEN_TAMEKRAN_ACIK;				
		else
			pu_bTamekran= PEN_TAMEKRAN_ACIK;
	}


	if(a_iPencereTamEkran==PEN_PENCERE)
		pu_bTamekran= PEN_TAMEKRAN_KAPALI;

	if(pu_bTamekran==PEN_TAMEKRAN_ACIK || a_iPencereTamEkran==PEN_TAMEKRAN)				
	{
		pu_bTamekran= PEN_TAMEKRAN_ACIK;

		memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));			
		dmScreenSettings.dmSize		 = sizeof(dmScreenSettings);	
		dmScreenSettings.dmPelsWidth = g_EkranX;				
		dmScreenSettings.dmPelsHeight= g_EkranY;			
		dmScreenSettings.dmBitsPerPel= g_BPP;			
		dmScreenSettings.dmFields	 = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

			if(ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
		{
			if(MessageBox(g_hWnd,"Tam ekrani desteklemiyor\nSizin ekran kartiniz. Pencerede acilsin mi?","ERROR",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
			{
			
				pu_bTamekran= PEN_TAMEKRAN_KAPALI;
			}

			else
			{
			
				MessageBox(g_hWnd, "Program kapatildi.", "HATA", MB_OK | MB_ICONSTOP);
				return false;
			}
		}
	}

	if(pu_bTamekran==PEN_TAMEKRAN_ACIK)		
	{
		dwExStyle= WS_EX_APPWINDOW;				
		dwStyle	 = WS_POPUP;				
		ShowCursor(false);					
	}

	else
	{
		dwExStyle= WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;		
		dwStyle	 = WS_OVERLAPPEDWINDOW;						
	}

	AdjustWindowRectEx(&winrect, dwStyle, FALSE, dwExStyle);

	if(pu_bTamekran==PEN_TAMEKRAN_KAPALI)
	{
		if(!(g_hWnd=CreateWindowEx(dwExStyle,			
								   PEN_CLASSADI,									
								   pu_szPencereadi,				
								   WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle,
								   0, 0,					
								   winrect.right -winrect.left,			
								   winrect.bottom-winrect.top,			
								   NULL,							
								   NULL,								
								   g_hInstance,							
								   NULL)))								
		{
			
			vKapat();								
			return false;			
		}
	}
	else if(pu_bTamekran==PEN_TAMEKRAN_ACIK)
	{
		if(!(g_hWnd=CreateWindowEx(dwExStyle,		
								   PEN_CLASSADI,			
								   pu_szPencereadi,			
								   WS_POPUP | WS_VISIBLE,	
								   0, 0,					
								   winrect.right -winrect.left,
								   winrect.bottom-winrect.top,	
								   NULL,						
								   NULL,						
								   g_hInstance,					
								   NULL)))						
		{
			vKapat();								
			return false;			
		}
	}

	static PIXELFORMATDESCRIPTOR pfd=
	{
		sizeof(PIXELFORMATDESCRIPTOR),	
		1,								
		PFD_DRAW_TO_WINDOW |			
		PFD_SUPPORT_OPENGL |		
		PFD_DOUBLEBUFFER,			
		PFD_TYPE_RGBA,				
		g_BPP,				
		0, 0, 0, 0, 0, 0,			
		0,							
		0,							
		0,							
		0, 0, 0, 0,					
		16,							
		0,							
		0,							
		PFD_MAIN_PLANE,				
		0,							
		0, 0, 0						
	};
	
	if(!(g_hDC=GetDC(g_hWnd)))
	{
		vKapat();		
		return false;								
	}
	else
	{
	}
	

	if(!(PixelFormat=ChoosePixelFormat(g_hDC, &pfd)))
	{
		vKapat();
		return false;						
	}
	else
	{
	}

	if(!SetPixelFormat(g_hDC,PixelFormat, &pfd))
	{
		vKapat();
		return false;						
	}
	else
		{
	}
	

	if(!(g_hRC= wglCreateContext(g_hDC)))
	{
		vKapat();			
				return false;		
	}
	else
	{
	}


	if(!wglMakeCurrent(g_hDC, g_hRC))
	{
		vKapat();		
		
		return false;			
	}
	else
{
	}

	glEnable(GL_TEXTURE_2D);						
	glShadeModel(GL_SMOOTH);					
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);		
	glClearDepth(1.0);							
	glEnable(GL_DEPTH_TEST);				
	glDepthFunc(GL_LESS);	
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	ShowWindow(g_hWnd, SW_SHOW);					
	SetForegroundWindow(g_hWnd);					
	SetFocus(g_hWnd);								
	glResizeScenep(g_EkranX, g_EkranY,75.0f);

	multitex();

	g_log.bPrint("");
	g_log.bPrint("     |--------------------------------------|");
	g_log.bPrint("     |---------------TURKOP-----------------|");
	g_log.bPrint("     |------------MOS2D ENGINE--------------|");
	g_log.bPrint("     |-----------VEDAT MEYDANERI------------|");
	g_log.bPrint("     |----------www.turkop.cjb.net----------|");
	g_log.bPrint("     |----------chenmy1@yahoo.com-----------|");
	g_log.bPrint("     |--------------------------------------|");
	g_log.bPrint("");

	g_PencereAktif= PEN_AKTIF;


	return true;
}
void MOSEkran::Ekran2DCiz(int acikkapali)
{
	if (acikkapali == ACIK)
	{
		glMatrixMode(GL_PROJECTION);
		glPushMatrix();
		glLoadIdentity();
		glDisable(GL_BLEND);
		glOrtho(0, g_EkranX, 0, g_EkranY, -1,1);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glDisable(GL_DEPTH_TEST);

	}
	else if (acikkapali == KAPALI)
	{
		glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
		glEnable(GL_DEPTH_TEST);
		glDisable(GL_BLEND);
	}
}
/*LRESULT CALLBACK WindowProc(HWND a_hWnd, UINT a_uiMsg, WPARAM a_wParam, LPARAM a_lParam)
{
	switch(a_uiMsg)
	{
		case WM_ACTIVATE:
			if(!HIWORD(a_wParam))
			g_PencereAktif= PEN_AKTIF;

			else
				g_PencereAktif= PEN_PASIF;
			return 0;
			break;

		case WM_CHAR:
		{
		
		}
		return 0;
		break;

		case WM_SYSCOMMAND:
			switch(a_wParam)
			{
				case SC_SCREENSAVE:
				case SC_MONITORPOWER:
				return 0;
			}
			break;

		case WM_CLOSE:
			PostQuitMessage(0);
			return 0;
			break;
			
		case WM_KEYDOWN:
		{
		
		}
		return 0;
		break;


		case WM_SIZE:
			g_EkranX = LOWORD(a_lParam);
			g_EkranY= HIWORD(a_lParam);

			glResizeScenep(g_EkranX, g_EkranY,kameraFOV);
			return 0;
			break;
	}

	return DefWindowProc(a_hWnd, a_uiMsg, a_wParam, a_lParam);
}*/
void MOSEkran::EkraniTemizle()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	glLoadIdentity();
}
void MOSEkran::ZamaniGuncelle(void)
{
	g_fFPS		  =	GetFPS();
	g_ZamanSayaci = GetElapsedSeconds();
}
void MOSEkran::vKapat(void)
{
	if(pu_bTamekran==PEN_TAMEKRAN_ACIK)
		ChangeDisplaySettings(NULL,0);
	
	ShowCursor(false);				

	g_log.bPrint("\n");
	g_log.bPrint("-------------------------------------");
	g_log.bPrint("MOS2D Engine : TURKOP");
	g_log.bPrint("  VEDAT MEYDANERI  2004-0cak-18");
	g_log.bPrint("-------------------------------------\n");
  

	//g_log.bPrint("</h3>");
	g_log.bPrint("</pre>");
    g_log.bPrint("</body>");
   g_log.bPrint("</html>");

 

	if(g_hRC)
	{
		if(!wglMakeCurrent(NULL,NULL))	
		{
		}else
		{
		}

		if(!wglDeleteContext(g_hRC))		
		{

		}
		else{
		}
		
		g_hRC= NULL;		
	}


	if(g_hDC && !ReleaseDC(g_hWnd, g_hDC))
	{
		g_hDC= NULL;				
	}
	else
	{

	}


	if(g_hWnd && !DestroyWindow(g_hWnd))
	{
		g_hWnd= NULL;					
	}
	else
	{

	}

	if(!UnregisterClass(PEN_CLASSADI, g_hInstance))
	{
	
		g_hInstance= NULL;			
	}
	else
	{
	}
}
void MOSEkran::vBufferSwap(void)
{
	glFlush();
	SwapBuffers(g_hDC);
}

bool MOSEkran::bHandleMessages(void)
{
	MSG msg;

	while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
	{ 
	    TranslateMessage(&msg);
	    DispatchMessage(&msg); 

		if(msg.message==WM_QUIT)
			return false;
	}
	return true;
}	

bool MOSEkran::ZamaniBaslat(void)
{
	if(!QueryPerformanceFrequency(&p_ticksPerSecond))
	{
		//System doesn't support hi-res timer
		return false;
	}
	else
	{
		QueryPerformanceCounter(&p_startTime);
		return true;
	}
}

float MOSEkran::GetElapsedSeconds(void)
{
	static LARGE_INTEGER lastTime= p_startTime;
	LARGE_INTEGER		 currentTime;
	float				 seconds;

	QueryPerformanceCounter(&currentTime);

	seconds= (((float)currentTime.QuadPart-(float)lastTime.QuadPart)/(float)p_ticksPerSecond.QuadPart);

	lastTime= currentTime;
	return seconds;
}

float MOSEkran::GetFPS(void)
{
	static LARGE_INTEGER lastTime= p_startTime;
	LARGE_INTEGER		 currentTime;
	float				 fps;

	QueryPerformanceCounter(&currentTime);

	fps= 1.0f*(float)p_ticksPerSecond.QuadPart/((float)currentTime.QuadPart-(float)lastTime.QuadPart);


	lastTime= currentTime;
	return fps;
}
void MOSEkran::Bakis3DPozisyon(float x, float y,float z)
{
	gluLookAt(x,y,z,0,0,0,0,0,0);
}

				
