Files
kraken/KREngine/KREngine/Classes/KRTextureAnimated.cpp
kearwood 23004557fd Physics system in progress
Now export writes krbundles directly

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40194
2012-12-20 01:23:57 +00:00

156 lines
5.2 KiB
C++

//
// KRTextureAnimated.cpp
// KREngine
//
// Copyright 2012 Kearwood Gilbert. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#import <assert.h>
#include "KRTextureAnimated.h"
#include "KRTexture2D.h"
#include "KRContext.h"
KRTextureAnimated::KRTextureAnimated(KRContext &context, std::string name) : KRTexture(context, name)
{
// Format of name:
// animate:texturebasename,xx,yy
// Where - texturebasename is a prefix for the other textures
// - xx is the number of frames
// - yy is the framerate
// TODO - Add error handling for mal-formatted animated texture formats
int first_comma_pos = name.find(",");
int second_comma_pos = name.find(",", first_comma_pos + 1);
m_texture_base_name = name.substr(8, first_comma_pos - 9);
m_frame_count = atoi(name.substr(first_comma_pos+1, second_comma_pos - first_comma_pos -1).c_str());
m_frame_rate = atof(name.substr(second_comma_pos+1).c_str());
m_max_lod_max_dim = 2048;
m_min_lod_max_dim = 64;
for(int i=0; i<m_frame_count; i++) {
KRTexture2D *frame_texture = textureForFrame(i);
if(frame_texture) {
if(frame_texture->getMaxMipMap() < m_max_lod_max_dim) m_max_lod_max_dim = frame_texture->getMaxMipMap();
if(frame_texture->getMinMipMap() > m_min_lod_max_dim) m_min_lod_max_dim = frame_texture->getMinMipMap();
}
}
}
std::string KRTextureAnimated::textureNameForFrame(int frame)
{
char szFrameNumber[10];
sprintf(szFrameNumber, "%i", frame);
return m_texture_base_name + szFrameNumber;
}
KRTexture2D *KRTextureAnimated::textureForFrame(int frame)
{
return (KRTexture2D *)getContext().getTextureManager()->getTexture(textureNameForFrame(frame).c_str());
}
KRTextureAnimated::~KRTextureAnimated()
{
}
bool KRTextureAnimated::createGLTexture(int lod_max_dim)
{
return true;
}
long KRTextureAnimated::getMemRequiredForSize(int max_dim)
{
int target_dim = max_dim;
if(target_dim < m_min_lod_max_dim) target_dim = m_min_lod_max_dim;
long memoryRequired = 0;
for(int i=0; i<m_frame_count; i++) {
KRTexture2D *frame_texture = textureForFrame(i);
if(frame_texture) {
memoryRequired += frame_texture->getMemRequiredForSize(target_dim);
}
}
return memoryRequired;
}
void KRTextureAnimated::resetPoolExpiry()
{
KRTexture::resetPoolExpiry();
for(int i=0; i<m_frame_count; i++) {
KRTexture2D *frame_texture = textureForFrame(i);
if(frame_texture) {
frame_texture->resetPoolExpiry(); // Ensure that frames of animated textures do not expire from the texture pool prematurely, as they are referenced indirectly
}
}
}
void KRTextureAnimated::bind()
{
int frame_number = (int)floor(fmodf(getContext().getAbsoluteTime() * m_frame_rate,m_frame_count));
KRTexture2D *frame_texture = textureForFrame(frame_number);
if(frame_texture) {
frame_texture->bind();
}
}
long KRTextureAnimated::getReferencedMemSize()
{
long referenced_mem = 0;
for(int i=0; i<m_frame_count; i++) {
KRTexture2D *frame_texture = textureForFrame(i);
if(frame_texture) {
referenced_mem += frame_texture->getMemSize();
}
}
return referenced_mem;
}
bool KRTextureAnimated::isAnimated()
{
return true;
}
std::string KRTextureAnimated::getExtension()
{
return ""; // Animated textures are just references; there are no files to output
}
bool KRTextureAnimated::save(const std::string &path)
{
return true; // Animated textures are just references; there are no files to output
}
bool KRTextureAnimated::save(KRDataBlock &data)
{
return true; // Animated textures are just references; there are no files to output
}