// GraphicsTemplate.cpp
// 
//////////////////////////////////////////////////////////////////////////////////////////
// includes 
//////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"

//////////////////////////////////////////////////////////////////////////////////////////
// externals 
//////////////////////////////////////////////////////////////////////////////////////////
extern cRenderClass graphics;
vector<cParticle> Firework;
cParticle pList;
Utils tool;

//////////////////////////////////////////////////////////////////////////////////////////
// renderScene() - render the scene
//////////////////////////////////////////////////////////////////////////////////////////
void renderScene()
{
	// set a colour
	graphics.setColour(pList.RGBAPtr[0], pList.RGBAPtr[1], pList.RGBAPtr[2]);

	// set pixel size..
	graphics.setPointSize(5);
	

	/////////////////////////////////////////////
	// render your particle here..
	/////////////////////////////////////////////
	
	// draw to an off screen buffer
	for (int i = 0; i < Firework.size(); i++) {
		graphics.drawPixel(Firework[i].Pos_x, Firework[i].Pos_y);
	}

	pList.endtime = GetTickCount();
	// render the scene
	graphics.render();
}

//////////////////////////////////////////////////////////////////////////////////////////
// update() - update function
//////////////////////////////////////////////////////////////////////////////////////////
void update()
{
	/////////////////////////////////////////////
	// update your particle here..
	/////////////////////////////////////////////
	//pList.checkStart();
	pList.checkStop();

	if (abs(pList.endtime - pList.starttime) / 1000 <= pList.lifespan) {
		for (int i = 0; i < Firework.size(); i++) {
			Firework[i].update();
		}
	}
	else if ((pList.endtime - pList.starttime) / 1000 > pList.lifespan) {
		//Recalculate the time difference.
		pList.starttime = GetTickCount();
		pList.endtime = GetTickCount();

		//Release the memory occupied by the vector.
		vector<cParticle>().swap(Firework);

		//Regenerate the location and color of the fireworks.
		pList.setColor();
		pList.setRandomPosition();

		//Regenerate the fireworks if the user does not pause the program
		if (pList.isAlive == true) {
			for (int i = 0; i < tool.unirand(56, 128); i++) {
				pList.setVel();
				Firework.push_back(pList);
			}
		}
		else if(pList.isAlive == false){
			pList.checkStart();
		}
	}

	// always re-render the scene..
	renderScene();
}



//////////////////////////////////////////////////////////////////////////////////////////
// _tmain() - program entry point
//////////////////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{	
	// init glut stuff..	
	graphics.create(argc, argv);

	// good place for one-off initialisations and objects creation..
	pList.setColor();
	for (int i = 0; i < tool.unirand(56,128); i++) {
		pList.setVel();
		Firework.push_back(pList);
	}
	cout << "Press Enter to stop" << endl;

	// enter game loop..
	pList.starttime = GetTickCount();
	graphics.loop();	

	return 0;
}