/*****************************************************************************
 * $Id: eng_tex.hh,v 1.16 2006/08/10 03:18:58 devel Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Texture code.
 *//*
 * LEGAL:	COPYRIGHT (C) 2004 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 *****************************************************************************/

#ifndef ENG_TEX_HH
#define ENG_TEX_HH 1

// A type for 4 2D textures coords (for a quad).
typedef Array<8,fp> Texcoord8;

////////////////////////////////////////////////////////////////////////////////
/// @brief Textured bi-quad.
/// @remarks A textured bi-quad is two intersecting quads forming an extruded cross.
/// Useful for small objects that exist in multitudes (eg trees).
/// The same texture is applied to both quads.
/// If center is specified, the world vertexs will be calculated.
/// Otherwise, the caller must pass them to Draw().
/// @verbatim
///       +Z                      +Z
///        0                       4        
///        |                       |         
/// -X  1--+--3  bottom     -X  5--+--7  top
///        |                       |         
///        2                       6
/// @endverbatim
class TexBiquad
{
PREVENT_COPYING(TexBiquad)
private:	enum { VERTEX_CNT = 8, TEXCOORD_CNT = 4*2 };	// same texture is reused twice

public:							TexBiquad( void );
public:							~TexBiquad();
private:	void				Init( void );

// Long Set()/constructor and Draw(void) are paired (world vertexs calculated from center position)
// Short Set()/constructor and Draw(WorldVertex) are paired (caller specifies world vertexs)

//------------------------------------------------------------------------------
// Variants to calculate world vertexs from a given position,width,height.
public:		TexBiquad( const Position& center,
                       const fp width,
                       const fp height,
                       TexturePtrc texture,
                       const Texcoord8& texcoords,
                       const RGBA rgba )
			{
				Init();
				Set( center, width, height, texture, texcoords, rgba );
			}
public:		void Set( const Position& center,
                      const fp width,
                      const fp height,
                      TexturePtrc texture,
                      const Texcoord8& texcoords,
                      const RGBA rgba );
public:		void Draw( void );

// -----------------------------------------------------------------------------
// Variants for when caller will pass WorldVertex array to Draw().
public:		TexBiquad( TexturePtrc texture,
                       const Texcoord8& texcoords,
                       const RGBA rgba )
			{
				Init();
				Set( texture, texcoords, rgba );
			}
public:		void Set( TexturePtrc texture,
                      const Texcoord8& texcoords,
                      const RGBA rgba );
public:		void Draw( const WorldVertex wv[8] );

// -----------------------------------------------------------------------------
// This is a common object (eg used for Trees), so minimize it to save memory.
// Use the Engine's SharedVal<RGBA> to maximize sharing RGBAs.
private:	WorldVertex*		mWorldVertexs;
private:	TexturePtr			mTexture;
private:	const Texcoord8*	mTexcoords;			// shared
private:	RGBA				mRgba;
};

////////////////////////////////////////////////////////////////////////////////
/// @brief A single textured quad.
class TexQuad
{
PREVENT_COPYING(TexQuad)
private:	enum { VERTEX_CNT = 4 };

// Set() assigns all members except the public mWorldVertexs[]
// which user can do directly for speed.

// Methods:
public:					TexQuad( void );
public:					~TexQuad();
public:		void		Set( TexturePtrc texture, const Texcoord8& texcoords, const RGBA rgba );
public:		void		Draw( eGpAlpha alpha1 = GP_SRC_ALPHA, eGpAlpha alpha2 = GP_DST_ALPHA );
public:		uint		GetVertexCnt( void ) { return VERTEX_CNT; }

// Data (minimize to save memory):
public:		WorldVertex			mWorldVertexs[VERTEX_CNT];	// TexQuad::Draw() will transform world to eye
private:	TexturePtr			mTexture;
private:	const Texcoord8*	mTexcoords;			// shared
private:	RGBA				mRgba;
};

#endif // ENG_TEX_HH
