#include <Gos2\Gos.h>
#include "d3dx8.h"

_TCHAR *sz_1		= _T("Non-wrapping text scrolled using XPosition");
_TCHAR *sz_2TL		= _T("Top/Left alignment");
_TCHAR *sz_2CC		= _T("Center/Center alignment");
_TCHAR *sz_2BR		= _T("Bottom/Right alignment");
_TCHAR *sz_R		= _T("Text follows region");
_TCHAR *sz_B		= _T("A long string that is top aligned and will be bottom clipped as it exceeds the bounds");
_TCHAR *sz_G		= _T("Scaling up and down");

class TextApp : public GosApp
{
public:
	// Functions which the app must provide
	virtual bool	Initialize( _TCHAR *pCommandLine );			// Initialize the application's state
	virtual void	Render();				// Render a single frame
	virtual bool	Step();					// Move the scene forward one frame
	virtual void	Shutdown();				// Called at game shutdown time
	virtual TCHAR	*Name();				// Name of the game

	virtual void	PreferredDisplay( int &Windowed, unsigned int &Width, unsigned int &Height, D3DFORMAT &Format );

	// The following is specific to this app
	DWORD			m_bgColor;

	GosFont3D *		m_pOneFont;
	GosFont3D *		m_pTwoFont;
	GosFont3D *		m_pRedFont;
	GosFont3D *		m_pBluFont;
	GosFont3D *		m_pGrnFont;

	GosHeap *		m_pDefHeap;

	int				m_OneFontX;
	int				m_OneFontY;
	int				m_TwoFontX;
	int				m_TwoFontY;
	int				m_RedFontX;
	int				m_RedFontY;
	int				m_BluFontX;
	int				m_BluFontY;
	int				m_GrnFontX;
	int				m_GrnFontY;

	RECT			m_OneRect;
	RECT			m_TwoRect;
	RECT			m_RedRect;
	RECT			m_BluRect;
	RECT			m_GrnRect;

	DWORD			m_OneFG;
	DWORD			m_TwoFG;
	DWORD			m_RedFG;
	DWORD			m_BluFG;
	DWORD			m_GrnFG;

	DWORD			m_OneBG;
	DWORD			m_TwoBG;
	DWORD			m_RedBG;
	DWORD			m_BluBG;
	DWORD			m_GrnBG;

	DWORD			m_OneAlign;
	DWORD			m_TwoAlign;
	DWORD			m_RedAlign;
	DWORD			m_BluAlign;
	DWORD			m_GrnAlign;

	float			m_GrnScale;
	DWORD			m_RedY;
	DWORD			m_OneWidth;
	DWORD			m_OneHeight;
};

TextApp myApp;	// *The* Application

void TextApp::PreferredDisplay( int &Windowed, unsigned int &Width, unsigned int &Height, D3DFORMAT &Format )
{
#if 0
	Windowed = false;
#else
	Windowed = true;
#endif
	Width = 640;
	Height = 480;
}


bool TextApp::Initialize( _TCHAR *pCommandLine )
{
		
	//create a heap
	m_pDefHeap = GosHeap::Create(_T("DefHeap"),64000000);
	GosHeap::PushCurrent(m_pDefHeap);

	m_pOneFont = GosFont3D::CreateFromFile( _T("assets\\graphics\\arial black.ttf"), 0 );
	m_pTwoFont = GosFont3D::CreateFromFile( _T("assets\\graphics\\comic.d3f"), 0 );
	m_pRedFont = GosFont3D::CreateFromFile( _T("assets\\graphics\\Courier New.ttf"), 0 );
	m_pBluFont = GosFont3D::CreateFromFile( _T("assets\\graphics\\times new roman.ttf"), 0 );
	m_pGrnFont = GosFont3D::CreateFromFile( _T("assets\\graphics\\treb.d3f"), 0 );

	m_OneFontX = 0;
	m_OneFontY = 0;
	m_TwoFontX = 0;
	m_TwoFontY = 0;
	m_RedFontX = 0;
	m_RedFontY = 0;
	m_BluFontX = 0;
	m_BluFontY = 0;
	m_GrnFontX = 0;
	m_GrnFontY = 0;

	m_OneFG = 0xffffff00;
	m_TwoFG = 0xff00ffff;
	m_RedFG = 0xffff0000;
	m_BluFG = 0xff8080ff;
	m_GrnFG = 0xff00ff00;

	m_OneBG = 0x80f0f000;
	m_TwoBG = 0x80008080;
	m_RedBG = 0x80800000;
	m_BluBG = 0x80000080;
	m_GrnBG = 0x80008000;

	m_OneRect.left = 20;
	m_OneRect.right = 620;
	m_OneRect.top = 20;
	m_OneRect.bottom = 60;

	m_TwoRect.left = 330;
	m_TwoRect.right = 620;
	m_TwoRect.top = 80;
	m_TwoRect.bottom = 460;

	m_RedRect.left = 20;
	m_RedRect.right = 310;
	m_RedRect.top = 220;
	m_RedRect.bottom = 260;

	m_BluRect.left = 20;
	m_BluRect.right = 310;
	m_BluRect.top = 80;
	m_BluRect.bottom = 200;

	m_GrnRect.left = 20;
	m_GrnRect.right = 310;
	m_GrnRect.top = 340;
	m_GrnRect.bottom = 460;

	m_OneAlign = 0x00010002;
	m_RedAlign = 0x00010001;
	m_BluAlign = 0x00000000;
	m_GrnAlign = 0x00010002;

	m_bgColor = 0xff000000;

	GosText::Set(GosText::Size,1.f);

	GosText::SetAttributes( m_pOneFont, m_OneFG, -24, false, true, true, false, m_OneAlign);
	GosText::StringLength( &m_OneWidth, &m_OneHeight, sz_1 );
	GosText::Set(GosText::Size,1.f);

	m_GrnScale = .5f;
	m_RedY = m_RedRect.top;

	return true;	// true means the initialize succeeded
}

bool TextApp::Step()
{
	static long	lastTime = GosUtil::Time_mS();
	long			time = GosUtil::Time_mS();

	if( time-lastTime > 50 )
	{
		static	float	scaleStep = .5f;
		static	int		yStep = 2;

		m_GrnScale += scaleStep;

		if( m_GrnScale > 2.f )
		{
			m_GrnScale = 2.f;
			scaleStep = -.1f;
		}
		else if( m_GrnScale < .5f )
		{
			m_GrnScale = .5f;
			scaleStep = .1f;
		}

		m_RedY += yStep;

		if( m_RedY >= 300 )
		{
			yStep = -2;
		}
		else if( m_RedY <= 200 )
		{
			yStep = 2;
		}

		m_RedRect.top = m_RedY;
		m_RedRect.bottom = m_RedY+40;

		m_OneFontX -=4;

		if((-(int)m_OneFontX) > (m_OneRect.right - m_OneRect.left))
			m_OneFontX = m_OneWidth;

		lastTime = time;
	}

	return true;	// true means keep going
}

void TextApp::Render()
{
	float savedSize;

	// Clear the background to blue and begin the scene
	Gos3D::Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, m_bgColor, 1.0f, 0L );
	Gos3D::BeginScene();


	GosText::SetRegion( m_OneRect.left, m_OneRect.top, m_OneRect.right, m_OneRect.bottom);
	GosText::DrawBackground(0,0,1000,1000,m_OneBG);
	GosText::Get(GosText::Size,&savedSize);
	GosText::Set(GosText::Position, m_OneFontX, m_OneFontY);
	GosText::SetAttributes( m_pOneFont, m_OneFG, -24, false, false, false, false, m_OneAlign);
	GosText::Draw(sz_1);
	GosText::Set(GosText::Size,savedSize);

	GosText::SetRegion( m_TwoRect.left, m_TwoRect.top, m_TwoRect.right, m_TwoRect.bottom);
	GosText::DrawBackground(0,0,1000,1000,m_TwoBG);
	GosText::Get(GosText::Size,&savedSize);
	GosText::Set(GosText::Position, m_TwoFontX, m_TwoFontX);
	GosText::SetAttributes( m_pTwoFont, m_TwoFG, -18, true, true, true, false, 0x00000000);
	GosText::Draw(sz_2TL);
	GosText::Set(GosText::Alignment, 0x00010001);
	GosText::Draw(sz_2CC);
	GosText::Set(GosText::Alignment, 0x00020002);
	GosText::Draw(sz_2BR);
	GosText::Set(GosText::Size,savedSize);

	GosText::SetRegion( m_RedRect.left, m_RedRect.top, m_RedRect.right, m_RedRect.bottom);
	GosText::DrawBackground(0,0,1000,1000,m_RedBG);
	GosText::Get(GosText::Size,&savedSize);
	GosText::Set(GosText::Position, m_RedFontX, m_RedFontY);
	GosText::SetAttributes( m_pRedFont, m_RedFG, -18, true, true, true, false, m_RedAlign);
	GosText::Draw(sz_R);
	GosText::Set(GosText::Size,savedSize);

	GosText::SetRegion( m_BluRect.left, m_BluRect.top, m_BluRect.right, m_BluRect.bottom);
	GosText::DrawBackground(0,0,1000,1000,m_BluBG);
	GosText::Get(GosText::Size,&savedSize);
	GosText::Set(GosText::Position, m_BluFontX, m_BluFontY);
	GosText::SetAttributes( m_pBluFont, m_BluFG, -24, true, true, true, false, m_BluAlign);
	GosText::Draw(sz_B);
	GosText::Set(GosText::Size,savedSize);

	GosText::SetRegion( m_GrnRect.left, m_GrnRect.top, m_GrnRect.right, m_GrnRect.bottom);
	GosText::DrawBackground(0,0,1000,1000,m_GrnBG);
	GosText::Get(GosText::Size,&savedSize);
	GosText::Set(GosText::Position, m_GrnFontX, m_GrnFontY);
	GosText::SetAttributes( m_pGrnFont, m_GrnFG, m_GrnScale, true, true, true, false, m_GrnAlign);
	GosText::Draw(sz_G);
	GosText::Set(GosText::Size,savedSize);

	// Done rendering, end the scene
	Gos3D::EndScene();
}

void TextApp::Shutdown()
{
	if(m_pOneFont)
		delete m_pOneFont;
	if(m_pTwoFont)
		delete m_pTwoFont;
	if(m_pRedFont)
		delete m_pRedFont;
	if(m_pBluFont)
		delete m_pBluFont;
	if(m_pGrnFont)
		delete m_pGrnFont;
}

TCHAR *TextApp::Name()
{
	return _T("Text Application");
}