//-----------------------------------------------------------------------------------
// 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 |
//************************************************************************************
//************************************************************************************
//************************************************************************************
//------------------------------------------------------------------------------------  



#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <mosfps.h>
#include "mostemp.h"
#include "resource.h"





bool OyunuYukle(void);
bool OyunEkrani(void);
bool OyunuKapat(void);

//MOS2D SDK
MOSEkran pencere;
MOSResim resim;
MOSFont font;
MOSSprite mouse;
MOSSprite logo;
MOSSprite lava;
MOSSprite lava2;

MOSSprite arka;

MOSFps fps;

CDXInput Input;

//Ekran karti bilgileri
char *vendorgl;
char *renderirgl;
char *versiongl;
char *extensiongl;

CDXPacker pack;



bool OyunuYukle(void)
{
 
	g_log.bPrint("<html>");
   g_log.bPrint("<body>");
   g_log.bPrint("<pre>");
   g_log.bPrint("<title>TurkOP - www.turkop.cjb.net</title>");
 

   g_log.bPrint("<body bgcolor='#000000' text='#FFFFFF'>");
    g_log.bPrint(" <p align='center'><img border='0' src='data/spr/mos3d.bmp' width='260' height='160'><br> ");
	g_log.bPrint("<font size='5' color='#FF0000'><b>TURK-OP ( OYUN YAPIM GRUBU)<br>  ");
	g_log.bPrint(" VEDAT MEYDANERI&nbsp; 1999-2004</b></font></p> ");
	g_log.bPrint(" <p align='center'><font size='5' color='#FF0000'><b>Created MOS 2D/3D by VEDAT MEYDANERI ");
	g_log.bPrint(" <br>GAME DEBUG LOG</b></font></p> ");
    g_log.bPrint("<h3>");
    g_log.bPrint(" wwww.TURKOP.cjb.net ");

	if(!pencere.ZamaniBaslat())
	{
	}
	
	pencere.Olustur(640,480,16, PEN_SORU, "MOS2D - TEST");


	Input.Create( g_hInstance ,   g_hWnd ); 
	Input.SetMouseLimits(0,0,640 -9,480-9);

	font.Olustur(resim,pencere.g_EkranY,"data/spr/font");
	mouse.Olustur(resim,pencere.g_EkranY,"data/spr/mouse",true,"data/spr/mouse_m",32,32);
	logo.Olustur(resim,pencere.g_EkranY,"data/spr/mos3d",false,"data/spr/mos3d",256,160);
	lava.Olustur(resim,pencere.g_EkranY,"data/spr/lava",false,"data/spr/lava",256,160);
	lava2.Olustur(resim,pencere.g_EkranY,"data/spr/lava2",false,"data/spr/lava2",256,160);
	arka.Olustur(resim,pencere.g_EkranY,"data/spr/arka",false,"data/spr/arka",640,480);


   
	vendorgl = strdup((char *) glGetString(GL_VENDOR));
    renderirgl = strdup((char *) glGetString(GL_RENDERER));
    versiongl = strdup((char *) glGetString(GL_VERSION));

	char	*token;										
	int		cnt=0;
//	int			maxtokens;

	char* text=(char *)malloc(strlen((char *)glGetString(GL_EXTENSIONS))+1);	
	strcpy (text,(char *)glGetString(GL_EXTENSIONS));		
	token=strtok(text," ");	




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

	
		while(token!=NULL)										
	{

		g_log.bPrint(token);
		token=strtok(NULL," ");								
	}

	free(text);	
 
	 fps.OlusturTimer();



	return TRUE;
}
void Yazilar()
{
	CHAR yaz[200];

	sprintf(yaz,"FPS : %.0f",pencere.g_fFPS);
	font.Yaz(10,20,yaz,1);
	font.Yaz(10,40,"Ekran Karti Bilgileri:",1);
	sprintf(yaz,"Yapimci : %s",vendorgl);
	font.Yaz(10,60,yaz,1);
	sprintf(yaz,"Turu : %s",renderirgl);
	font.Yaz(10,80,yaz,1);
	sprintf(yaz,"Versionu : %s",versiongl);
	font.Yaz(10,100,yaz,1);
	sprintf(yaz,"MouseX : %d ,MouseY : %d",mx,my);
	font.Yaz(10,120,yaz,1);
}
bool OyunEkrani(void)
{


	pencere.EkraniTemizle();

	//Pencere LOOP kismi
	Klavye();
	

	static float r,g,b=0;


	//----------------------------------
	//Ekrani 2D yap
	//-----------------------------------
	pencere.Ekran2DCiz(ACIK);


	arka.SetPos(0,0);
	arka.Draw(GL_SPRITE_NORMAL,true);
	arka.SetRenk(1.3,1,1);


	logo.SetPos(350,10);
	logo.Draw(GL_SPRITE_NORMAL,true);

	lava.SetPos(350,10);
	lava.Draw(GL_SPRITE_TRANS_UV_UP,true);
	
	lava2.SetPos(350,10);
	lava2.Draw(GL_SPRITE_TRANS_UV_DOWN,true);

	
	mouse.SetPos((float)mx,(float)my);
	
	 r +=0.001;
	 g += 0.001;
	 b += 0.002;
	 if(r > 1)
	 {
		 r = 0;
	 }
 if(b > 1)
	 {
		 b = 0;
	 } if(g > 1)
	 {
		 g = 0;
	 }
	mouse.SetRenk(r,g,b);
	mouse.Draw(GL_SPRITE_TRANS,true);
	
     glTranslatef(0,0.0,0);
	pencere.Ekran2DCiz(KAPALI);
	//----------------------------------
	//Ekrani 2D yapma
	//-----------------------------------

		Yazilar();

	if(Input.GetKeyState(CDXKEY_F1)==CDXKEY_PRESS)
	{
		
		resim.EkranGoruntusu();

	}

	fps.FPSKisitla(FPS_ATMIS);
	pencere.ZamaniGuncelle();
	pencere.vBufferSwap();	
	return TRUE;
}
bool OyunuKapat(void)
{

     font.KillFont();
	pencere.vKapat();
	return false;
}
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))
				pencere.g_PencereAktif= PEN_AKTIF;

			else
				pencere.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:
			pencere.g_EkranX = LOWORD(a_lParam);
			pencere.g_EkranY= HIWORD(a_lParam);

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

	return DefWindowProc(a_hWnd, a_uiMsg, a_wParam, a_lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, 
				   HINSTANCE hPrevInstance, 
				   LPSTR	 lpCmdLine, 
				   int		 nCmdShow) 
{



	if(!OyunuYukle())
		return false;


	while(true)
	{
	
	if(!pencere.bHandleMessages())
			break;
		

		if(pencere.g_PencereAktif==PEN_AKTIF )
		{
			Input.Update();			

			if(!OyunEkrani())
				break;
		}
	} 

	OyunuKapat();
	return false;
}





