/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/sgx/graphics/metasurface.cpp

*/

#include "sgx/core/core.hpp"
#include "domains/graphics/graphics.hpp"
#include "sgx/graphics/graphics.hpp"

//
// Maintenance and handlers
//
CSGXMetaSurface::CSGXMetaSurface(CSGXDrawSurface *pSurface) {
	m_pSurface = pSurface;
	m_Width = m_pSurface->getWidth();
	m_Height = m_pSurface->getHeight();
}

CSGXMetaSurface::CSGXMetaSurface(CSGXDrawSurface *pSurface, const tUINT32 width, const tUINT32 height) {
	m_pSurface = pSurface;
	m_Width = width;
	m_Height = height;
}

void CSGXMetaSurface::fillQuad(const tPOINT x1, const tPOINT y1, const tPOINT x2, const tPOINT y2, const tPOINT x3, const tPOINT y3, const tPOINT x4, const tPOINT y4) {

	sgxVector2 p1 = m_Xform.m.mul(x1,y1);
	sgxVector2 p2 = m_Xform.m.mul(x2,y2);
	sgxVector2 p3 = m_Xform.m.mul(x3,y3);
	sgxVector2 p4 = m_Xform.m.mul(x4,y4);
	tPOINT p1x = (tPOINT)p1.x;
	tPOINT p1y = (tPOINT)p1.y;
	tPOINT p2x = (tPOINT)p2.x;
	tPOINT p2y = (tPOINT)p2.y;
	tPOINT p3x = (tPOINT)p3.x;
	tPOINT p3y = (tPOINT)p3.y;
	tPOINT p4x = (tPOINT)p4.x;
	tPOINT p4y = (tPOINT)p4.y;

	m_pSurface->setFillTexture(m_pTexture, m_CurrentUVRegion);
	m_pSurface->setFillColor(m_CurrentFillColor);
	m_pSurface->setDrawColor(m_CurrentDrawColor);
	m_pSurface->fillQuad(p1x,p1y, p2x,p2y, p3x,p3y, p4x,p4y);
}

void 
CSGXMetaSurface::drawText(const char *pStr, tINT32 x, tINT32 y, const CSGXFontFormatting &fmt) {
	sgxVector2 pt = m_Xform.m.mul(x,y);
	m_pSurface->setCurrentFont(m_pCurrentFont);
	m_pSurface->setFontColor(m_CurrentFontColor);
	// TODO: text is not squashed :(
	tPOINT xp = (tPOINT)pt.x;
	tPOINT yp = (tPOINT)pt.y;
	m_pSurface->drawText(pStr, xp, yp, fmt);
}
