/** 
 * \file MyDrawSprite.cpp
 * \brief File containing functions for drawing 2D sprites in 3D world.
 *
 * Why did I make my own sprite drawing functions in separate file? 2D Sprites are drawn everywhere in the game.
 * And newer DirectX version have diferent API for drawing sprites. If you want to move your game to another 
 * DirectX SDK there is no need to search the entire game code looking for all places sprites are drawn - 
 * you just change them in here. I happened to work on two close DirectX SDK - august and october 2006, and 
 * there was this difference in sprite API. Basicly - if I made all graphic calls in one separate layer
 * I would have completely portable game. If I wanted to move my game to OpenGL then only one layer would be changed.
 * But you don't do that for such small game like this unless youre bitten to.
 * 
 * \author Petar Bajic, MPE (C) All Rights Reserved, Homepage: www.mystic-peanut.com
 * \date January, 16 2007.
 */

#include "MyDrawSprite.h"

/**
 * \brief my_DrawSprite draws sprite with the given billion parameters.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void my_DrawSprite(D3DXMATRIX *mat,D3DXVECTOR2 *scaling,D3DXVECTOR2 *spriteCentre,float rotation,
				   D3DXVECTOR2 *trans, LPDIRECT3DTEXTURE9 tex, LPD3DXSPRITE sprite, bool enabled, 
				   LPRECT rect, WCHAR* strText, LPD3DXFONT pFont, D3DCOLOR color)
{
	//D3DXMatrixTransformation2D(mat,NULL,0.0,scaling,spriteCentre,rotation,trans);
	D3DXMatrixTransformation2D(mat,NULL,0.0,scaling,spriteCentre,rotation,trans);
	sprite->SetTransform(mat);
	if (enabled)
		sprite->Draw(tex,NULL,NULL,NULL,0xFFFFFFFF);
	else
		sprite->Draw(tex,NULL,NULL,NULL,0x8FFFFFFF);

	//draw text if any
	if(rect != NULL)
		if(strText != NULL)
			if(pFont != NULL)
			{
				D3DXMATRIX nmat;
				D3DXVECTOR2 scal;
				scal.x = 1.0f;
				scal.y = 1.0f;
				D3DXMatrixTransformation2D(&nmat,NULL,0.0,&scal,spriteCentre,rotation,trans);
				sprite->SetTransform(&nmat);
				pFont->DrawTextW(sprite, strText, -1, rect, DT_VCENTER||DT_CENTER, color);
				sprite->SetTransform(mat);
			}
}

/**
 * \brief Ahh, my_DrawOutlineText, this is how I draw fancy outlined text used for intro movie.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void my_DrawOutlineText(D3DXMATRIX *mat,D3DXVECTOR2 *scaling,D3DXVECTOR2 *spriteCentre,float rotation,
				   D3DXVECTOR2 *trans, LPD3DXSPRITE sprite, LPD3DXFONT pFont, WCHAR* strText, LPRECT rect,
				   D3DCOLOR color, D3DCOLOR outlineColor)
{
	//Render Movie Text with black outline (1 pixel to the left, 2 pixel to the right...)
	my_DrawText(mat,scaling,spriteCentre,rotation,trans,sprite,pFont,strText,rect,outlineColor);
	(*trans).x +=2.0f;
	my_DrawText(mat,scaling,spriteCentre,rotation,trans,sprite,pFont,strText,rect,outlineColor);
	(*trans).x -=1.0f; 
	(*trans).y -=1.0f;
	my_DrawText(mat,scaling,spriteCentre,rotation,trans,sprite,pFont,strText,rect,outlineColor);
	(*trans).y +=2.0f;
	my_DrawText(mat,scaling,spriteCentre,rotation,trans,sprite,pFont,strText,rect,outlineColor);
	(*trans).y -=1.0f;
	my_DrawText(mat,scaling,spriteCentre,rotation,trans,sprite,pFont,strText,rect,color);
	(*trans).x -=1.0f;
}

/**
 * \brief Draws text on sprite.
 * \author Petar Bajic 
 * \date January, 16 2007.
 */
void my_DrawText(D3DXMATRIX *mat,D3DXVECTOR2 *scaling,D3DXVECTOR2 *spriteCentre,float rotation, D3DXVECTOR2 *trans, 
				 LPD3DXSPRITE sprite, LPD3DXFONT pFont, WCHAR* strText, LPRECT rect, D3DCOLOR color)
{
	//D3DXMatrixTransformation2D(mat,NULL,0.0,scaling,spriteCentre,rotation,trans);
	//sprite->SetTransform(mat);
	D3DXMATRIX rmat, tmat, smat;
	D3DXMatrixRotationZ(&rmat, rotation);
	D3DXMatrixScaling(&smat, scaling->x, scaling->y, 0.0f);
	D3DXMatrixTranslation(&tmat, trans->x, trans->y, .0f);
	D3DXMatrixMultiply(mat, &rmat, &smat);
	D3DXMatrixMultiply(mat, mat, &tmat);
	sprite->SetTransform(mat);

	//pFont->Begin();
	HRESULT hr = pFont->DrawTextW(sprite, strText, -1, rect, /*DT_VCENTER|*/DT_CENTER|DT_WORDBREAK, color);
	//pFont->End();
    if( FAILED(hr) )
        //error msg
		rect = NULL; //I truly don't remember what is this
}