//loops.cpp - wrapper around all the scary code


/*******************************************************
//
//	Name: loops.cpp
//
//	Author: Dave Ratti
//
//	Date:10-10-2001
//
//	Modified: 2-25-2002
//
//	Notes: Ok this is really the only file you need to be concerned
//	about. It shows how to use all the graphics and input classes
//	as well as some very basic game programming concepts. 
//
//	There are 3 main functions you need to concern yourself with:
//	
//	GameMain();
		
			This is the important function that is going to be called
			60 times a second. The basic procedure is:

			-GetInput (check keyboard/commands/mouse) and react occordingly.
			-DoGameLogic update positions, do AI, etc
			-DrawScreen draws all the necessary graphics on the screen

	GameInit();

			This gets called ONCE before the GameMain starts getting called. 
			This is where we setup the DirectDrawManager, CImageManager, load the CImages, etc.
			setup variables, initial conditions, etc.

	GameShutDown();

			Pretty simple cleanup. Deallocate any allocated memory, etc.

//
//
*********************************************************/



#include <winsock2.h>
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h> 
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <time.h>
#include <dinput.h>


#include "Assert.h"
#include "CGraphicSystem.h"
#include "CD3DManager.h"
#include "CImage.h"
#include "CTimer.h"
#include "CConsole.h"
#include "CDInputManager.h"
#include "CGame.h"
#include "CObjectManager.h"
#include "CDAudioManager.h"
#include "CParticleManager.h"

// DEFINES ////////////////////////////////////////////////
// defines for windows 


// default screen size: You could change this to like: 640x480, 800x600, 1024x768... even 1600x1200 if you want
#define SCREEN_WIDTH_    1024  // size of screen
#define SCREEN_HEIGHT_   768





// TYPES //////////////////////////////////////////////////////

// basic unsigned types
typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char  UCHAR;
typedef unsigned char  BYTE;

// MACROS /////////////////////////////////////////////////

//quick way to find if a key is pressed withOUT directinput, just uses for exiting the program in case something happens with DirectInput
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) 


// EXTERNS/////////////////////////////////////////////////

extern HWND      main_window_handle;	//we need this to send a quit message to windows... dont worry bout it

// GLOBAL VARIABLES////////////////////////////////////////


///////////////////////////////////////////////////////////


void GameMain()
{
	
	static DWORD lastTime=0;
	static int fps=0, fpsCount=0;
	
	fpsCount++;

	DWORD startTime = timeGetTime();

	if(startTime - lastTime > 1000)
	{
		lastTime = timeGetTime();
		fps =fpsCount;				
		fpsCount=0;
	}


	g_Timer.SetSpeedFactor();
	g_DInputManager.GetInput();

	g_ParticleManager.Update();

	g_GraphicSystem.PrepFrame();	
	g_GraphicSystem.DrawFrame();

	g_Game.MainLoop();		

	g_ParticleManager.Draw();
	
	g_GraphicSystem.PrintText(STRINGC("FPS: ") + STRINGC(fps),1,1);	
	
	g_Console.Draw();
	g_GraphicSystem.ReleaseFrame();
	
	if(KEYDOWN(VK_ESCAPE))
	{
		SendMessage(main_window_handle,WM_CLOSE,0,0);
		return;
	}

	//I really hate doing this. But we're gonna lock this at 60fps. Most people playing in window mode this will be no problem
	//while(timeGetTime() - startTime < 16.667)
	{
		//nothing!
	}




} // end Game_Main

////////////////////////////////////////////////////////////



void GameInit()
{
	srand(time(NULL));
	
	CTimer* t = new CTimer();
	t->Init(60);

	CDInputManager* inputManager = new CDInputManager();

	CObjectManager* ObjectManager = new CObjectManager();
	ObjectManager->Init();

	CConsole* console = new CConsole();
	g_Console.Init();

	CDAudioManager* am = new CDAudioManager();
	am->Init();

	CParticleManager* pm = new CParticleManager();
	pm->Init();	

	CGame* game = new CGame();
	game->Init();
	

	
	
			 

} // end Game_Init

/////////////////////////////////////////////////////////////

void GameShutDown()
{

	


} // end Game_Shutdown


//////////////////////////////////////////////////////////////
