// GraphicsTemplate.cpp
// 
//////////////////////////////////////////////////////////////////////////////////////////
// includes 
//////////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Circle.h"

using namespace std;

//////////////////////////////////////////////////////////////////////////////////////////
// externals 
//////////////////////////////////////////////////////////////////////////////////////////
extern cRenderClass graphics;

const int CIRCLE_NUM = 400;

int circleCount = 0;

bool isShowOrigin = true;

//Define pointer array
Circle *circleArray[CIRCLE_NUM];

Circle pointCircle;

bool stop = false;

void initArray()
{
	for(int i = 0; i < CIRCLE_NUM;i++)
	{
		circleArray[i] = NULL;
		circleCount = 0;
	}
}

void freeArray()
{
	for(int i = 0; i < CIRCLE_NUM;i++)
	{
		if (circleArray[i] != NULL)
		{
			delete(circleArray[i]);
			circleArray[i] = NULL;
			circleCount--;
		}
	}
}

void initOneCircle(int i)
{
	if (i >= CIRCLE_NUM)
	{
		return;
	}
	circleArray[i] = new Circle();
	circleArray[i]->Create();
	circleCount++;
}

void freeOneCircle(int i)
{
	if (i >= CIRCLE_NUM)
	{
		return;
	}

	if (circleArray[i] != NULL)
	{
		delete(circleArray[i]);
		circleArray[i] = NULL;
		circleCount --;
	}
}



//////////////////////////////////////////////////////////////////////////////////////////
// renderScene() - render the scene
//////////////////////////////////////////////////////////////////////////////////////////
void renderScene()
{
	// set a colour
	graphics.setColour( 1.0f, 0.5f, 0.1f );
	
	// set pixel size..
	graphics.setPointSize(10);

	/////////////////////////////////////////////
	// render your particle here..
	/////////////////////////////////////////////

	if((pointCircle.m_y <= SCREEN_HEIGHT/2)&&(!stop))
	{
		graphics.setColour(pointCircle.m_color.r, pointCircle.m_color.g, pointCircle.m_color.b);
		// set pixel size..
		graphics.setPointSize(pointCircle.m_size);
		graphics.drawPixel(pointCircle.m_x, pointCircle.m_y);
	}
	
	for(int i = 0; i < CIRCLE_NUM; i++)
	{
		Circle *c = circleArray[i];
		if(c != NULL)
		{
			graphics.setColour(c->m_color.r, c->m_color.g, c->m_color.b);
			// set pixel size..
			graphics.setPointSize(c->m_size);
			graphics.drawPixel(SCREEN_WIDTH/2 + c->m_x, SCREEN_HEIGHT/2 + c->m_y);
		}
	}

	// draw to an off screen buffer
	//graphics.drawPixel(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);

	// render the scene
	graphics.render();
}

//////////////////////////////////////////////////////////////////////////////////////////
// update() - update function
//////////////////////////////////////////////////////////////////////////////////////////
void update()
{
	/////////////////////////////////////////////
	// update your particle here..
	/////////////////////////////////////////////

	if((pointCircle.m_y <= SCREEN_HEIGHT/2)&&(!stop))
	{
		pointCircle.m_y += pointCircle.m_speed;
	}
	else
	{
		pointCircle.m_y += pointCircle.m_speed;	
		

		if(pointCircle.m_y > SCREEN_HEIGHT)
		{
			isShowOrigin = false;
			if(circleCount <= 0)
			{
				pointCircle.InitOrigin();
				isShowOrigin = true;
				
				if(!stop)
				{					
					freeArray();
				}
				
			}
					
		}
		//else
		{
			for(int i = 0; i < CIRCLE_NUM; i++)
			{
				Circle *c = circleArray[i];
				if(c == NULL)
				{
					if(!stop)
					{
						if((pointCircle.m_y < SCREEN_HEIGHT) && (pointCircle.m_y > SCREEN_HEIGHT/2) && isShowOrigin)
						{
							initOneCircle(i);
						}
						
					}			
			
				}
				else if(c->IsDead() || c->IsLeaveWindow())
				{
					if(!stop && isShowOrigin)
					{
						c->Create();
					}
					else
					{
						freeOneCircle(i);
					}
			
				}
				else
				{
					c->Move();
				}
			}
		}
		

	}
	
	// always re-render the scene..
	renderScene();
}


void keyEvent(int key, int x, int y)
{
	switch(key){
		case GLUT_KEY_F1:
			stop = true;
			break;
		case GLUT_KEY_F2:
			stop = false;
			pointCircle.InitOrigin();
			freeArray();
			break;
		default:
			break;

	}	
	
}


//////////////////////////////////////////////////////////////////////////////////////////
// _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..
	initArray();
	pointCircle.InitOrigin();
	isShowOrigin = true;

	glutSpecialFunc(keyEvent);


	// enter game loop..
	graphics.loop();	


	freeArray();

	return 0;
}




