// GraphicsTemplate.cpp
// 
//////////////////////////////////////////////////////////////////////////////////////////
// includes 
//////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Particle.h"
#include<list>

#define PAR_NUM 455

using namespace std;

//////////////////////////////////////////////////////////////////////////////////////////
// externals 
//////////////////////////////////////////////////////////////////////////////////////////
extern cRenderClass graphics;

list<Particle> g_particleList;
bool g_isCancel = false;

//////////////////////////////////////////////////////////////////////////////////////////
// renderScene() - render the scene
//////////////////////////////////////////////////////////////////////////////////////////
void renderScene()
{
	// set a colour
	graphics.setColour( 1.0f, 0.0f, 0.1f );	
	// set pixel size..
	graphics.setPointSize(10);
	/////////////////////////////////////////////
	// render your particle here..
	for (list<Particle>::iterator it = g_particleList.begin();
		it != g_particleList.end(); it++) {
		graphics.setColour(it->m_color.r, it->m_color.g, it->m_color.b);
		// set pixel size..
		graphics.setPointSize(it->m_size);
		graphics.drawPixel(250.0f + it->m_posX, 250.0f + it->m_posY);
	}

	/////////////////////////////////////////////
	// draw to an off screen buffer
	graphics.drawPixel(250.0f, 250.0f);
	// render the scene
	graphics.render();
}

//////////////////////////////////////////////////////////////////////////////////////////
// update() - update function
//////////////////////////////////////////////////////////////////////////////////////////
void update()
{
	/////////////////////////////////////////////
	// update your particle here..
	/////////////////////////////////////////////
	for (list<Particle>::iterator it = g_particleList.begin();
		it != g_particleList.end(); ) {
		if (it->isOutBoundary() || it->isDead()) {
			if (!g_isCancel) {
				it->initData();
				it++;
			}
			else
				it = g_particleList.erase(it);
		}
		else {
			it->calNewPos();
			it++;
		}
		
	}
	// always re-render the scene..
	renderScene();
}

void processSpecialKeys(int key, int x, int y)
{
	if(key == GLUT_KEY_F1)
		g_isCancel = true;
	if (key == GLUT_KEY_F2) {
		g_isCancel = false;
		g_particleList.resize(PAR_NUM);
	}
}





//////////////////////////////////////////////////////////////////////////////////////////
// _tmain() - program entry point
//////////////////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{	
	g_particleList.resize(PAR_NUM);
	// init glut stuff..
	graphics.create(argc, argv);

	// good place for one-off initialisations and objects creation..
	glutSpecialFunc(processSpecialKeys);





	// enter game loop..
	graphics.loop();	

	return 0;
}




