/****************************************************************************** libx42.net - skinned vertex animation library (.NET API) Copyright (C) 2007 HermitWorks Entertainment Corporation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA. ******************************************************************************/ #include "Stdafx.h" #include "UtilTypes.h" using namespace System; using namespace System::Collections::Generic; namespace Libx42 { TempMem::TempMem( void ) { Init( 0 ); } TempMem::TempMem( int maxSize ) { if( maxSize < 0 ) throw gcnew ArgumentOutOfRangeException(); Init( maxSize ); } void TempMem::Init( int maxSize ) { innerMem = x42_CreateTempMem( maxSize ); if( !innerMem ) throw gcnew Exception( "Unable to allocate scratch memory area." ); } TempMem::~TempMem( void ) { TempMem::!TempMem(); } TempMem::!TempMem( void ) { x42_FreeTempMem( innerMem ); innerMem = 0; } OptimizationContext::OptimizationContext( void ) { buf_helper optsHelper( x42_GetOptsSize() ); opts = x42_InitOpts( optsHelper.get() ); if( !opts ) throw gcnew Exception( "Unable to create the cptimization context object." ); optsBuf = optsHelper.get(); optsHelper.release(); } OptimizationContext::~OptimizationContext( void ) { OptimizationContext::!OptimizationContext(); } OptimizationContext::!OptimizationContext( void ) { buf_helper::free( optsBuf ); opts = 0; } int Helpers::ElementCountToPrimitiveCount( PrimitiveType primType, int elemCount ) { if( elemCount < 0 ) throw gcnew ArgumentOutOfRangeException( "elemCount" ); switch( primType ) { case PrimitiveType::PointList: return elemCount; case PrimitiveType::LineList: return elemCount / 2; case PrimitiveType::LineStrip: return elemCount >= 2 ? elemCount - 1 : 0; case PrimitiveType::TriangleList: return elemCount / 3; case PrimitiveType::TriangleStrip: case PrimitiveType::TriangleFan: return elemCount >= 3 ? elemCount - 2 : 0; default: throw gcnew ArgumentException( "Invalid primType." ); } } int Helpers::PrimitiveCountToElementCount( PrimitiveType primType, int primCount ) { if( primCount < 0 ) throw gcnew ArgumentOutOfRangeException( "primCount" ); switch( primType ) { case PrimitiveType::PointList: return primCount; case PrimitiveType::LineList: return primCount * 2; case PrimitiveType::LineStrip: return primCount ? primCount + 1 : 0; case PrimitiveType::TriangleList: return primCount * 3; case PrimitiveType::TriangleStrip: case PrimitiveType::TriangleFan: return primCount ? primCount + 2 : 0; default: throw gcnew ArgumentException( "Invalid primType." ); } } void Helpers::MergeKeyTimes( List< UInt16 > ^list, IEnumerable< UInt16 > ^timesToAdd ) { if( !list || !timesToAdd ) throw gcnew ArgumentNullException(); if( !list->Count ) { list->AddRange( timesToAdd ); list->Sort(); return; } for each( UInt16 t in timesToAdd ) { int idx = list->BinarySearch( t ); if( idx >= 0 ) continue; list->Insert( ~idx, t ); } } };