#include "Fps.h"
#include <Bild.h>
#include <Prozess.h>
#include <Schrift.h>
#include <Text.h>

using namespace Framework;

// Inhalt der Fps Klasse aus Fps.h
// Konstruktor 
Fps::Fps()
    : Zeichnung()
{
	pr = new Prozess();
	i = 0;
	fpsCount = 0;
	nowFps = 0;
	nowCpu = 0;
	nowMem = 0;
    textRd = 0;
	sFarbe = 0;
}

// Destruktor 
Fps::~Fps()
{
	delete pr;
	if( textRd )
		textRd->release();
}

// nicht constant
void Fps::setSchriftZ( Schrift *schrift ) // setzt die Schrift
{
    if( !this->textRd )
        textRd = new TextRenderer( schrift );
    else
        textRd->setSchriftZ( schrift );
    textRd->setSchriftSize( 12 );
	rend = 1;
}

void Fps::setSFarbe( int f ) // setzt die Schrift Farbe
{
	sFarbe = f;
	rend = 1;
}

bool Fps::tick( double tickval ) // tick
{
	i += tickval * 60;
	if( i >= 60 )
	{
		nowFps = fpsCount;
		nowCpu = (int)pr->getCPU();
		nowMem = (int)(pr->getMem() / 1024);
		fpsCount = 0;
		i -= 60;
		rend = 1;
	}
	bool ret = rend;
	rend = 0;
	return ret;
}

void Fps::render( Bild &zrObj ) // zeichnet nach zrObj
{
	fpsCount++;
	if( textRd && sFarbe )
	{
		Text renderT( "FPS: " );
		renderT.append( nowFps );
		renderT.append( "\nCPU: " );
		renderT.append( nowCpu );
		renderT.append( "%\nMem: " );
		renderT.append( nowMem );
		renderT.append( "kb" );
        textRd->renderText( 10, 10, renderT, zrObj, sFarbe );
	}
}

// constant
int Fps::getFps() const // gibt fps zur�ck
{
	return nowFps;
}

int Fps::getCpu() const // gibt die Cpu zur�ck
{
	return nowCpu;
}

int Fps::getMem() const // gibt den Arbeitsspeicher zur�ck
{
	return nowMem;
}

Schrift *Fps::getSchrift() const // gibt die Schrift zur�ck
{
	return textRd ? textRd->getSchrift() : 0;
}

Schrift *Fps::zSchrift() const
{
	return textRd ? textRd->zSchrift() : 0;
}

int Fps::getFarbe() const // gibt die Farbe zur�ck
{
	return sFarbe;
}