/** 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/dynamic_texture.cpp

*/

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

CDynamicTexture::CDynamicTexture(const tUINT32 width, const tUINT32 height, const CImageData::tFormat format) : m_pTexture(NULL)
{
	m_pData = new CImageData(width, height, format);
	// You must additionally called initialize. This is because the vtable
	// is not setup until _after_ the ctor, making a call to update impossible.
}

void 
CDynamicTexture::initialize()
{
	if (!m_pTexture) {
		m_pTexture = CSGXTextureManager::get()->createTexture(*m_pData, CSGXSurface::eReasonDynamic);
	}
	reset();
}

void
CDynamicTexture::reset()
{
	m_fTimecum = 0;
	m_fTimeFrame = 0;
	update(0);
}

CDynamicTexture::~CDynamicTexture()
{
	delete m_pTexture;
	delete m_pData;
}

void CDynamicTexture::update(const tREAL32 telaps)
{
	m_fTimecum += telaps;
}

void CDynamicTexture::rebuild()
{
	m_pTexture->rebuild();
}

tBOOL CDynamicTexture::frameLimitedUpdate(const tREAL32 telaps, const tREAL32 max_fps)
{
	m_fTimecum += telaps;
	m_fTimeFrame += telaps;

	if (m_fTimeFrame > 1.0f/max_fps) {
		update(m_fTimeFrame);
		m_fTimeFrame = 0;
		return TRUE;
	}

	return FALSE;
}
