
#include "cGraphicsLayer.h"
#include "cGame.h"
#include "cLog.h"
#include <stdio.h>

extern cGame Game;
cGraphicsLayer::cGraphicsLayer()
{
	g_pD3D = NULL;
	g_pD3DDevice = NULL;
	g_pSprite = NULL;
	font = NULL;
	ticsTilt=0;
}

cGraphicsLayer::~cGraphicsLayer(){}

bool cGraphicsLayer::Init(HWND hWnd)
{
	cLog *Log = cLog::Instance();
	HRESULT hr;

	D3DVIEWPORT9 viewPort = { 0, 0, SCREEN_RES_X, SCREEN_RES_Y, 0.0f, 1.0f };

	g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
	if(g_pD3D==NULL)
	{
		Log->Msg("Error creating Direct3D object");
		return false;
	}

	D3DPRESENT_PARAMETERS d3dpp; 
	ZeroMemory( &d3dpp, sizeof( d3dpp ) );

	d3dpp.Windowed               = true;
	d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;	//Efficient page flipping
	d3dpp.BackBufferWidth        = SCREEN_RES_X;
    d3dpp.BackBufferHeight       = SCREEN_RES_Y;
    d3dpp.BackBufferFormat       = D3DFMT_X8R8G8B8;

	hr = g_pD3D->CreateDevice(	D3DADAPTER_DEFAULT, 
								D3DDEVTYPE_HAL, 
								hWnd,
								D3DCREATE_SOFTWARE_VERTEXPROCESSING,
								&d3dpp, 
								&g_pD3DDevice );
	if(FAILED(hr))
	{
		Log->Error(hr,"Creating Direct3D device");
		return false;
	}

	// Configure for 2d operations
    hr = g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
    hr = g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
	hr = g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	if(FAILED(hr))
	{
		Log->Error(hr,"Setting render state");
		return false;
	}

	hr = g_pD3DDevice->SetViewport(&viewPort);
	if(FAILED(hr))
	{
		Log->Error(hr,"Setting viewport");
		return false;
	}

	hr = D3DXCreateFont(g_pD3DDevice, 18, 0, FW_BOLD, 0, FALSE, 
			DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
			DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), 
			&font );

	return true;
}

void cGraphicsLayer::Finalize()
{
	if(g_pD3DDevice)
	{
		g_pD3DDevice->Release();
		g_pD3DDevice = NULL;
	}
	if(g_pD3D)
	{
		g_pD3D->Release();
		g_pD3D = NULL;
	}
}

void cGraphicsLayer::LoadData()
{
	D3DXCreateSprite( g_pD3DDevice, &g_pSprite ); 

	//Main menu
	D3DXCreateTextureFromFileEx(g_pD3DDevice,"main2.png",0,0,1,0,D3DFMT_UNKNOWN,
								D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,
								NULL,NULL,NULL,&texMain);
	//GUI game
	D3DXCreateTextureFromFileEx(g_pD3DDevice,"interfaz.png",0,0,1,0,D3DFMT_UNKNOWN,
								D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,
								0xffffffff,NULL,NULL,&texGame);
	//Tiles
	D3DXCreateTextureFromFileEx(g_pD3DDevice,"tiles_mapa.png",0,0,1,0,D3DFMT_UNKNOWN,
								D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,
								0x00ff00ff,NULL,NULL,&texTiles);
	//Characters
	D3DXCreateTextureFromFileEx(g_pD3DDevice,"characters.png",0,0,1,0,D3DFMT_UNKNOWN,
								D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,
								0x00ff00ff,NULL,NULL,&texCharacters);
	//Mouse pointers
	D3DXCreateTextureFromFileEx(g_pD3DDevice,"mouse.png",0,0,1,0,D3DFMT_UNKNOWN,
								D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,
								0x00ff00ff,NULL,NULL,&texMouse);
	//Fog of war
	D3DXCreateTextureFromFileEx(g_pD3DDevice, "niebla.png",0,0,1,0,D3DFMT_UNKNOWN,
								D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,
								0xffffffff,NULL,NULL,&texFog);
	//Fog of war 2
	D3DXCreateTextureFromFileEx(g_pD3DDevice, "niebla2.png",0,0,1,0,D3DFMT_UNKNOWN,
								D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,
								0xffffffff,NULL,NULL,&texVisited);
	//Dialogs
	D3DXCreateTextureFromFileEx(g_pD3DDevice, "Dialogs.png",0,0,1,0,D3DFMT_UNKNOWN,
								D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,
								0xffffffff,NULL,NULL,&texDialog);
	//Animated Tiles
	D3DXCreateTextureFromFileEx(g_pD3DDevice, "animated.png",0,0,1,0,D3DFMT_UNKNOWN,
								D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,
								0xffffffff,NULL,NULL,&animatedTiles);

	//WIN
	D3DXCreateTextureFromFileEx(g_pD3DDevice,"win.png",0,0,1,0,D3DFMT_UNKNOWN,
								D3DPOOL_DEFAULT,D3DX_FILTER_NONE,D3DX_FILTER_NONE,
								NULL,NULL,NULL,&texWin);

}

void cGraphicsLayer::UnLoadData()
{
	if(texMain)
	{
		texMain->Release();
		texMain = NULL;
	}
	if(texGame)
	{
		texGame->Release();
		texGame = NULL;
	}
	if(texTiles)
	{
		texTiles->Release();
		texTiles = NULL;
	}
	if(texCharacters)
	{
		texCharacters->Release();
		texCharacters = NULL;
	}
	if(texMouse)
	{
		texMouse->Release();
		texMouse = NULL;
	}
	if(texFog)
	{
		texFog->Release();
		texFog = NULL;
	}
	if(texVisited)
	{
		texVisited->Release();
		texVisited = NULL;
	}
	if(texDialog)
	{
		texDialog->Release();
		texDialog = NULL;
	}
	if(g_pSprite)
	{
		g_pSprite->Release();
		g_pSprite = NULL;
	}
	if (animatedTiles)
	{
		animatedTiles->Release();
		animatedTiles = NULL;

	}

if(texWin)
	{
		texWin->Release();
		texWin = NULL;
	}

}

bool cGraphicsLayer::Render(int state,cMouse *Mouse,cScene *Scene,cCritter *Critter,cEnemy** Enemies, cDialog* pDialog)
{
	//HRESULT Draw( LPDIRECT3DTEXTURE9 pTexture, CONST RECT *pSrcRect,
	//				CONST D3DXVECTOR3 *pCenter,  CONST D3DXVECTOR3 *pPosition,
	//				D3DCOLOR Color);
	g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, 0xFF000000, 0, 0 );
	g_pD3DDevice->BeginScene();
	static bool bPrimeraVez = false;
		//--- SPRITES ---
		g_pSprite->Begin(D3DXSPRITE_ALPHABLEND);

			switch(state)
			{
				case STATE_MAIN:
								g_pSprite->Draw(texMain,NULL,NULL,&D3DXVECTOR3(0.0f,0.0f,0.0f),0xFFFFFFFF);
								break;

				case STATE_GAME:
					{
								DrawScene(Scene);
								DrawUnits(Scene,Critter,Enemies);
								DrawVisibility(Scene, Critter);
								
								if(Critter->GetLives() == CRITTER_LIVES)
									bPrimeraVez = true;
								
								RECT r;
								int tam = 139;
								int CoordY;

								if(bPrimeraVez)
								{
									CoordY = tam * (Critter->GetLives() - 1);
									bPrimeraVez = false;
								}
								else
									CoordY = tam * Critter->GetLives();

								SetRect(&r, 0, CoordY, 800,(Critter->GetLives() * tam) + tam);
								g_pSprite->Draw(texGame,&r,NULL,&D3DXVECTOR3(0.0f,460.0f,0.0f),0xFFFFFFFF);
								DrawRadar(Scene);
								
								if(pDialog->getState() == DIALOG_STATE_SHOW)
								{
									//DrawDialogBackground(pDialog);
									DrawDialog(pDialog);
								}
								break;
					}
					case STATE_WIN:
								g_pSprite->Draw(texWin,NULL,NULL,&D3DXVECTOR3(0.0f,0.0f,0.0f),0xFFFFFFFF);
								break;

			}

		g_pSprite->End();
		
		//--- INFO ---
		if(state==STATE_GAME)
		{
			if(pDialog->getState() == DIALOG_STATE_SHOW)
			{
				RECT rc;
				int x, y, xf, yf;

				pDialog->getPos(&x, &y);
				pDialog->getSize(&xf, &yf);
				xf+=x;
				yf+=y;

				SetRect( &rc, x+40, y+40, 50, 400 );
				font->DrawText(	NULL, pDialog->getText(), -1, &rc, DT_NOCLIP, D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ) );
			}
		}
		DrawMouse(Mouse);

	g_pD3DDevice->EndScene();
	g_pD3DDevice->Present( NULL, NULL, NULL, NULL );

	return true;
}

bool cGraphicsLayer::DrawRadar(cScene *Scene)
{
	RECT rc;
	int x,y,fx,fy;
		//pantx,panty;

	//Tile based map
	fx=Scene->cx+SCENE_WIDTH;
	fy=Scene->cy+SCENE_HEIGHT;

	for(x = 0; x < SCENE_AREA; x++)
	{
		for(y = 0; y < SCENE_AREA; y++)
		{
			int idxTile=(y*SCENE_AREA) + x;
			Scene->GetMiniRect(idxTile, &rc);
			g_pSprite->Draw(texTiles, &rc, NULL, &D3DXVECTOR3((float)(x * 2 + RADAR_Xo), (float)(y * 2 + RADAR_Yo), 0.0f), 0xFFFFFFFF);
		}
	}

	//Dibujar el rectángulo con la escena actual del mapa:
	//cada tile ocupa 2 pixels en el mapa del radar
    
	x = RADAR_Xo + (Scene->cx)*2;// << 2);
	y = RADAR_Yo + (Scene->cy)*2;// << 2);
	fx = RADAR_Xo + (Scene->cx+SCENE_WIDTH)*2;//<<2));
	fy = RADAR_Yo + (Scene->cy+SCENE_HEIGHT)*2;//<<2));

	SetRect(&rc,0,32,2,34); //un cuadrado de 2X2 blanco, para dibujar el rectángulo en el radar

	for(int i = x; i < fx; i++)
	{
		g_pSprite->Draw(texTiles, &rc, NULL, &D3DXVECTOR3(float(i), float(y), 0.0f), 0xFFFFFFFF);
		g_pSprite->Draw(texTiles, &rc, NULL, &D3DXVECTOR3(float(i), float(fy), 0.0f), 0xFFFFFFFF);
	}

	for(int j = y; j < fy; j++)
	{
		g_pSprite->Draw(texTiles, &rc, NULL, &D3DXVECTOR3(float(x), float(j), 0.0f), 0xFFFFFFFF);
		g_pSprite->Draw(texTiles, &rc, NULL, &D3DXVECTOR3(float(fx), float(j), 0.0f), 0xFFFFFFFF);
	}
	
	return true;
}

bool cGraphicsLayer::DrawScene(cScene *Scene)
{
	RECT rc;
	int x,y,//n,
		fx,fy,
		pantx,panty;

	int miniMapaX = 672;
	int miniMapaY = 60;

	//Tile based map
	fx=Scene->cx+SCENE_WIDTH;
	fy=Scene->cy+SCENE_HEIGHT;
	
	int offsetTilt=CalculaOffsetTilt();

	for(y=Scene->cy;y<fy;y++)
	{
		
		
		panty = SCENE_Yo + ((y-Scene->cy)<<5)+offsetTilt;
		
		for(x=Scene->cx;x<fx;x++)
		{
			pantx = SCENE_Xo + ((x-Scene->cx)<<5)+offsetTilt;


			SetRect(&rc, 0, 0, 32, 32);
			g_pSprite->Draw(texTiles,&rc,NULL, 
				&D3DXVECTOR3(float(pantx),float(panty),0.0f), 
				0xFFFFFFFF);

			int idxTile=(y*SCENE_AREA)+x;

			//n=Scene->getTileType(idxTile); //VMH new version
			Scene->GetRect(idxTile,&rc);

			if (Scene->IsCellAnimated(x,y))
			{
				g_pSprite->Draw(animatedTiles,&rc,NULL, 
						&D3DXVECTOR3(float(pantx),float(panty),0.0f), 
						0xFFFFFFFF);
			}
			else
			{
				g_pSprite->Draw(texTiles,&rc,NULL, 
						&D3DXVECTOR3(float(pantx),float(panty),0.0f), 
						0xFFFFFFFF);
			}
		}
	}

		/*for(x = 0; x < SCENE_AREA; x++)
		{
			for(y = 0; y < SCENE_AREA; y++)
			{
				int idxTile=(y<<5) + x;
				Scene->GetMiniRect(idxTile, &rc);
				g_pSprite->Draw(texTiles, &rc, NULL, &D3DXVECTOR3((float)(x * 4 + RADAR_Xo), (float)(y * 4 + RADAR_Yo), 0.0f), 0xFFFFFFFF);
			}
		}*/

	//Draw radar
	/*x=RADAR_Xo+(Scene->cx<<2);
	y=RADAR_Yo+(Scene->cy<<2);
	SetRect(&rc,0,32,80,100);
	g_pSprite->Draw(texTiles,&rc,NULL, 
					&D3DXVECTOR3(float(x),float(y),0.0f), 
					0xFFFFFFFF);*/
	return true;
}

bool cGraphicsLayer::DrawVisibility(cScene *Scene, cCritter *Critter)
{
	RECT rcScene, rcVisible;
	int x,y,//n,
		fx,fy,
		pantx,panty;
	int critPosX, critPosY;
	bool bPintar = true;
	int cxinit   = 0;
	int cxfin    = 0;
	int cyinit   = 0;
	int cyfin    = 0;

	//Tile based map
	fx = Scene->cx + SCENE_WIDTH;
	fy = Scene->cy + SCENE_HEIGHT;
	
	SetRect(&rcVisible, 32, 32, 64, 64);

	for(y = Scene->cy; y < fy; y++)
	{
		panty = SCENE_Yo + ((y-Scene->cy)<<5);

		for(x=Scene->cx;x<fx;x++)
		{
			pantx = SCENE_Xo + ((x-Scene->cx)<<5);
			int idxTile=(y*SCENE_AREA)+x;

			Scene->GetRect(idxTile,&rcScene);
			
			if(Scene->IsCellVisited(x,y))
			{
				bPintar = true;
				
				Critter->GetCell(&critPosX, &critPosY);

				cxinit = (critPosX - RADIO_VISIBLE) >= 0 ? critPosX - RADIO_VISIBLE : 0;
				cxfin  = (critPosX + RADIO_VISIBLE) < SCENE_AREA ? critPosX + RADIO_VISIBLE : SCENE_AREA - 1;
				cyinit = (critPosY - RADIO_VISIBLE) >= 0 ? critPosY - RADIO_VISIBLE : 0;
				cyfin  = (critPosY + RADIO_VISIBLE) < SCENE_AREA ? critPosY + RADIO_VISIBLE : SCENE_AREA - 1;

				for(int fila = cyinit; fila <= cyfin; fila++)
				{
					for(int col = cxinit; col <= cxfin; col++)
					{
						if(y == fila && x == col)
						{
							bPintar = false;
							break;
						}
					}
					if(!bPintar)
						break;
				}

				if(bPintar)
				{
					g_pSprite->Draw(texVisited,&rcVisible,NULL, 
							&D3DXVECTOR3(float(pantx),float(panty),0.0f), 
							0xFFFFFFFF);
				}
			}

			// LHA
			if(Scene->IsFogged(x, y))
			{
				SetRect(&rcScene, 0, 0, 64, 64);
				g_pSprite->Draw(texVisited,&rcVisible,NULL, 
							&D3DXVECTOR3(float(pantx),float(panty),0.0f), 
							0xFFFFFFFF);
				g_pSprite->Draw(texFog,&rcScene,NULL, 
							&D3DXVECTOR3(float(pantx-16),float(panty-16),0.0f), 
							0xFFFFFFFF);
			}
		}
	}

	return true;
}

bool cGraphicsLayer::DrawUnits(cScene *Scene,cCritter *Critter,cEnemy** Enemies)
{
	int cx,cy,posx,posy;
	RECT rc;

	//Draw Critter
	Critter->GetCell(&cx,&cy);
	//VMH: Update visibility in the map. Will be drawn next tic.

	if(Scene->Visible(cx,cy))
	{
		Critter->GetRect(&rc,&posx,&posy,Scene); /*Obtiene del critter, la posición EN PANTALLA donde pintar, y el rectángulo dentro del bitmap que pintar*/
		g_pSprite->Draw(texCharacters,&rc,NULL, 
						&D3DXVECTOR3(float(posx),float(posy),0.0f), 
						0xFFFFFFFF);
		if(Critter->GetSelected())
		{
			Critter->GetRectLife(&rc,&posx,&posy,Scene);
			g_pSprite->Draw(texMouse,&rc,NULL, 
							&D3DXVECTOR3(float(posx),float(posy),0.0f), 
							0xFFFFFFFF);
		}
	}
	Critter->GetRectRadar(&rc,&posx,&posy);
	g_pSprite->Draw(texTiles,&rc,NULL, 
					&D3DXVECTOR3(float(posx),float(posy),0.0f), 
					0xFFFFFFFF);
	//Draw Enemies:
	for (int i=0;i<Game.GetNumEnemies();i++)
	{
		cEnemy* bicho=Enemies[i];
		bicho->GetCell(&cx,&cy);
		if(Scene->IsCellActive(cx,cy))
		{
			bicho->GetRect(&rc,&posx,&posy,Scene); /*Obtiene del critter, la posición EN PANTALLA donde pintar, y el rectángulo dentro del bitmap que pintar*/
			g_pSprite->Draw(texCharacters,&rc,NULL, 
						&D3DXVECTOR3(float(posx),float(posy),0.0f), 
						0xFFFFFFFF);
		}
	}
	
	////Draw Fire
	//if(Critter->GetShooting())
	//{
	//	if(Critter->IsFiring())
	//	{
	//		//Advance animation & draw
	//		Critter->GetRectShoot(&rc,&posx,&posy,Scene);
	//		g_pSprite->Draw(texCharacters,&rc,NULL, 
	//						&D3DXVECTOR3(float(posx),float(posy),0.0f), 
	//						0xFFFFFFFF);
	//	}
	//	else
	//	{
	//		//Advance animation
	//		Critter->GetRectShoot(&rc,&posx,&posy,Scene);
	//	}
	//}
	return true;
}

bool cGraphicsLayer::DrawMouse(cMouse *Mouse)
{
	RECT rc;
	int mx,my,posx,posy;

	//Mouse selection box
	Mouse->GetPosition(&mx,&my);

	if(Mouse->GetSelection()==SELECT_SCENE)
	{
		int sx,sy;
		Mouse->GetSelectionPoint(&sx,&sy);
		SetRect(&rc,sx,sy,mx,my);
		DrawRect(rc,0x0000ff00);
	}

	//Mouse
	g_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
		
		Mouse->GetRect(&rc,&posx,&posy);
		HRESULT hr = g_pSprite->Draw(texMouse,&rc,NULL,&D3DXVECTOR3(float(mx+posx),float(my+posy),0.0f),0xFFFFFFFF);
		if(FAILED(hr))
		{
			cLog *Log = cLog::Instance();
			Log->Error(hr,"mouse pointer");
			return false;
		}

	g_pSprite->End();

	return true;
}

bool cGraphicsLayer::DrawRect(RECT rc, D3DCOLOR color)
{
	RECT rect;
	int xo,yo,xf,yf;

	if((rc.left==rc.right)&&(rc.top==rc.bottom)) return false;

	if(rc.left < rc.right)
	{
		xo = rc.left;	xf = rc.right;
	}
	else
	{
		xo = rc.right;	xf = rc.left;
	}
	if(rc.top < rc.bottom)
	{
		yo = rc.top;	yf = rc.bottom;
	}
	else
	{
		yo = rc.bottom;	yf = rc.top;
	}

	//Top
	SetRect(&rect,xo,yo,xf+1,yo+1);
	g_pD3DDevice->Clear(1,(D3DRECT *)&rect,D3DCLEAR_TARGET,color,1.0f,0);
	//Bottom
	SetRect(&rect,xo,yf,xf,yf+1);
	g_pD3DDevice->Clear(1,(D3DRECT *)&rect,D3DCLEAR_TARGET,color,1.0f,0);
	//Left
	SetRect(&rect,xo,yo,xo+1,yf+1);
	g_pD3DDevice->Clear(1,(D3DRECT *)&rect,D3DCLEAR_TARGET,color,1.0f,0);
	//Right
	SetRect(&rect,xf,yo,xf+1,yf+1);
	g_pD3DDevice->Clear(1,(D3DRECT *)&rect,D3DCLEAR_TARGET,color,1.0f,0);
	
	return true;
}

bool cGraphicsLayer::DrawDialogBackground(cDialog* pDialog)
{
	RECT r;
	int x, y, xf, yf;
	int xfin, yfin;
	pDialog->getPos(&x, &y);
	pDialog->getSize(&xf, &yf);
	xfin = xf;
	yfin = yf;
	xf+=x;
	yf+=y;

	// Draw the background
	SetRect(&r, DIALOG_IMG_BKG_X, DIALOG_IMG_BKG_Y, DIALOG_IMG_BKG_X + 2, DIALOG_IMG_BKG_Y + 2);
/*	for(int i = (x - DIALOG_BOX_OFFSET); i < (xf + DIALOG_BOX_OFFSET); i++)
	{
		for(int j = (y - DIALOG_BOX_OFFSET); j < (yf + DIALOG_BOX_OFFSET); j++)
		{*/
			g_pSprite->Draw(texDialog, &r, NULL, &D3DXVECTOR3((float)x, (float)y, 0.0f), 0xFFFFFFFF);
		/*}
	}*/

	return true;
}

bool cGraphicsLayer::DrawDialog(cDialog* pDialog)
{
	RECT r;
	int x, y, xf, yf;
	int xfin, yfin;
	int xb, yb;

	pDialog->getPos(&x, &y);
	pDialog->getSize(&xf, &yf);
	xfin = xf;
	yfin = yf;
	xf+=x;
	yf+=y;

	// Draw the upper left corner
	SetRect(&r, DIALOG_IMG_UL_X, DIALOG_IMG_UL_Y, DIALOG_IMG_UL_X + DIALOG_IMG_SIZE, DIALOG_IMG_UL_Y + DIALOG_IMG_SIZE);
	g_pSprite->Draw(texDialog, &r, NULL, &D3DXVECTOR3((float)x, (float)y, 0.0f), 0xFFFFFFFF);

	// Draw the upper right corner
	SetRect(&r, DIALOG_IMG_UR_X, DIALOG_IMG_UR_Y, DIALOG_IMG_UR_X + DIALOG_IMG_SIZE, DIALOG_IMG_UR_Y + DIALOG_IMG_SIZE);
	g_pSprite->Draw(texDialog, &r, NULL, &D3DXVECTOR3((float)xf, (float)y, 0.0f), 0xFFFFFFFF);

	// Draw the bottom left corner
	SetRect(&r, DIALOG_IMG_BL_X, DIALOG_IMG_BL_Y, DIALOG_IMG_BL_X + DIALOG_IMG_SIZE, DIALOG_IMG_BL_Y + DIALOG_IMG_SIZE);
	g_pSprite->Draw(texDialog, &r, NULL, &D3DXVECTOR3((float)x, (float)yf, 0.0f), 0xFFFFFFFF);

	// Draw the bottom right corner
	SetRect(&r, DIALOG_IMG_BR_X, DIALOG_IMG_BR_Y, DIALOG_IMG_BR_X + DIALOG_IMG_SIZE, DIALOG_IMG_BR_Y + DIALOG_IMG_SIZE);
	g_pSprite->Draw(texDialog, &r, NULL, &D3DXVECTOR3((float)xf, (float)yf, 0.0f), 0xFFFFFFFF);

	// Draw the horizontal lines
	SetRect(&r, DIALOG_IMG_HOR_X, DIALOG_IMG_HOR_Y, DIALOG_IMG_HOR_X + 1, DIALOG_IMG_HOR_Y + DIALOG_IMG_HOR_SIZE);
	for(int i = 0; i < xfin - DIALOG_IMG_SIZE; i++)
	{
		g_pSprite->Draw(texDialog, &r, NULL, &D3DXVECTOR3((float)(x+DIALOG_IMG_SIZE+i), (float)y+9, 0.0f), 0xFFFFFFFF);
		g_pSprite->Draw(texDialog, &r, NULL, &D3DXVECTOR3((float)(x+DIALOG_IMG_SIZE+i), (float)yf+27, 0.0f), 0xFFFFFFFF);
	}

	// Draw the vertical lines
	SetRect(&r, DIALOG_IMG_VERT_X, DIALOG_IMG_VERT_Y, DIALOG_IMG_VERT_X + DIALOG_IMG_VERT_SIZE, DIALOG_IMG_VERT_Y + 1);
	for(int i = 0; i < yfin - DIALOG_IMG_SIZE; i++)
	{
		g_pSprite->Draw(texDialog, &r, NULL, &D3DXVECTOR3((float)x+9, (float)y+DIALOG_IMG_SIZE+i, 0.0f), 0xFFFFFFFF);
		g_pSprite->Draw(texDialog, &r, NULL, &D3DXVECTOR3((float)xf+31, (float)y+DIALOG_IMG_SIZE+i, 0.0f), 0xFFFFFFFF);
	}


	pDialog->getButtonPos(&xb, &yb);
	SetRect(&r, DIALOG_BUTTON_UL_X, DIALOG_BUTTON_UL_Y, DIALOG_BUTTON_BR_X, DIALOG_BUTTON_BR_Y);
	g_pSprite->Draw(texDialog, &r, NULL, &D3DXVECTOR3((float)xb, (float)yb, 0.0f), 0xFFFFFFFF);


	return true;
}

void cGraphicsLayer::EfectoTilt()
{
	ticsTilt=15;
}

int cGraphicsLayer::CalculaOffsetTilt()
{
	static int tics=0; //para no hacerlo a cada tick

	int offset=0;

	if (ticsTilt!=0)
	{
		tics++;
		if (!(tics%2)) //Para hacer el tembleque un pelín más lento
		{
			int inc=-1;
			ticsTilt--;
		
			if (ticsTilt%2)
				inc=-1;
			else
				inc=1;
			
			offset=inc*5;
		}
	}
	
	return offset;
}