#include "BreakoutScoreCounter.h"

#include "GameConfig.h"
#include <deque>
#include <cstdlib>
#include <cstdio>

BreakoutScoreCounter::BreakoutScoreCounter(const Vector2i &pos,  const ColourRGB &colour, int val /* = 0 */)
: m_position(pos), m_value(val)
{
	for(int i = 0; i < 10; i++)
	{
		char buffer[2]; //Single digit and null terminator \0
		//itoa(i, buffer, 10);
#ifdef _WIN32
		sprintf_s(buffer, 2, "%d", i);
#else
		sprintf(buffer, "%d", i);  // GP2X compatible
#endif

		m_digits[i] = Visualisation::getSingleton()->CreateSpriteText(buffer, colour);
	}
}

BreakoutScoreCounter::~BreakoutScoreCounter()
{
	for(int i = 0; i < 10; i++)
		Visualisation::getSingleton()->RemoveSprite(m_digits[i]);
}

void BreakoutScoreCounter::Update(const int &val)
{
	m_value = val;
}

void BreakoutScoreCounter::Draw()
{
	std::deque<int> vIndividualDigits;
	vIndividualDigits.push_front(m_value % 10);
	int divider = 10;
	int result = m_value / divider;
	while(result >= 10)
	{
		vIndividualDigits.push_front(result % 10);

		divider *= 10;
		result = m_value / divider;
	}

	vIndividualDigits.push_front(result);

	Visualisation* pVis = Visualisation::getSingleton();
	Vector2i currPos = m_position;
	for(size_t i = 0; i < vIndividualDigits.size(); i++)
	{
		pVis->DrawSprite(m_digits[vIndividualDigits[i]], currPos.x, currPos.y);
		currPos.x += CHARACTER_WIDTH;
	}
}
