// ------------------------------------------------------------------------
/*
	\author: Jaap Suter
   
	Copyright (C) Electronic Arts Canada Inc. 2002.  All rights reserved.	
*/
// ------------------------------------------------------------------------

/*! \file */

#include "reveal/renderer.h"

#include <assert.h>

namespace Reveal {
	
// ------------------------------------------------------------------------------
Renderer::~Renderer()
{
}

// ------------------------------------------------------------------------------
/*!
	\brief Draws a line

	\param x1, y1, x2, y2	End points of the line in GUI space;
*/
// ------------------------------------------------------------------------------
void Renderer::Line( int x1, int y1, int x2, int y2,  Reveal::uint32_t color ) const
{
    ConvertFromGuiSpaceToScreenSpace( x1, y1 );
	ConvertFromGuiSpaceToScreenSpace( x2, y2 );
	DoLine( x1, y1, x2, y2, color );
}


// ------------------------------------------------------------------------------
/*!
	\brief Draws a filled rectangle;

	\param x1, y1, x2, y2	Top left and bottom right corner of the bar in GUI space;
*/
// ------------------------------------------------------------------------------
void Renderer::Bar( int x1, int y1, int width, int height, Reveal::uint32_t color ) const
{
	int x2 = width + x1;
	int y2 = height + y1;
	ConvertFromGuiSpaceToScreenSpace( x1, y1 );	
	ConvertFromGuiSpaceToScreenSpace( x2, y2 );
	width = x2 - x1;
	height = y2 - y1;
	DoBar( x1, y1, width, height, color );
}

// ------------------------------------------------------------------------------
/*!
	\brief Draws a string

	\param x1, y1 Top left corner where the string will be printed, in GUI space;
*/
// ------------------------------------------------------------------------------
void Renderer::String( int x1, int y1, const char* str, Reveal::uint32_t color ) const
{
	ConvertFromGuiSpaceToScreenSpace( x1, y1 );
	DoString( x1, y1, str, color );
}

// ------------------------------------------------------------------------------
void Renderer::Triangle( int x1, int y1, int x2, int y2, int x3, int y3, Reveal::uint32_t color ) const
{
	ConvertFromGuiSpaceToScreenSpace( x1, y1 );
	ConvertFromGuiSpaceToScreenSpace( x2, y2 );
	ConvertFromGuiSpaceToScreenSpace( x3, y3 );
	DoTriangle( x1, y1, x2, y2, x3, y3, color );
}

// ------------------------------------------------------------------------------
/*!
	\brief Converts a GUI space pair to a screenspace pair;
*/
// ------------------------------------------------------------------------------
void Renderer::ConvertFromGuiSpaceToScreenSpace( int &x, int &y ) const
{
	x = DoGetScreenOriginX() + static_cast< int >( static_cast< float >( DoGetScreenWidth()  ) * static_cast< float >( x ) / static_cast< float >( Impl::GuiMetrics::SCREEN_WIDTH  ) );
	y = DoGetScreenOriginY() + static_cast< int >( static_cast< float >( DoGetScreenHeight() ) * static_cast< float >( y ) / static_cast< float >( Impl::GuiMetrics::SCREEN_HEIGHT ) );
}

// ------------------------------------------------------------------------------
bool Renderer::ScreenSpaceToWorldSpace( int guiX, int guiY, 
										float& worldspaceX, float& worldspaceY, float& worldspaceZ,
										float& rayX, float& rayY,  float& rayZ ) const
{
	ConvertFromGuiSpaceToScreenSpace( guiX, guiY );
	return DoScreenSpaceToWorldSpace( guiX, guiY, worldspaceX, worldspaceY, worldspaceZ, rayX, rayY, rayZ );
}

} // Namespace Reveal;
