DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

DeclTable.cpp (4359B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #include "../idlib/precompiled.h"
     30 #pragma hdrstop
     31 
     32 
     33 /*
     34 =================
     35 idDeclTable::TableLookup
     36 =================
     37 */
     38 float idDeclTable::TableLookup( float index ) const {
     39 	int iIndex;
     40 	float iFrac;
     41 	
     42 	int domain = values.Num() - 1;
     43 
     44 	if ( domain <= 1 ) {
     45 		return 1.0f;
     46 	}
     47 
     48 	if ( clamp ) {
     49 		index *= (domain-1);
     50 		if ( index >= domain - 1 ) {
     51 			return values[domain - 1];
     52 		} else if ( index <= 0 ) {
     53 			return values[0];
     54 		}
     55 		iIndex = idMath::Ftoi( index );
     56 		iFrac = index - iIndex;
     57 	} else {
     58 		index *= domain;
     59 
     60 		if ( index < 0 ) {
     61 			index += domain * idMath::Ceil( -index / domain );
     62 		}
     63 
     64 		iIndex = idMath::Ftoi( idMath::Floor( index ) );
     65 		iFrac = index - iIndex;
     66 		iIndex = iIndex % domain;
     67 	}
     68 
     69 	if ( !snap ) {
     70 		// we duplicated the 0 index at the end at creation time, so we
     71 		// don't need to worry about wrapping the filter
     72 		return values[iIndex] * ( 1.0f - iFrac ) + values[iIndex + 1] * iFrac;
     73 	}
     74 	
     75 	return values[iIndex];
     76 }
     77 
     78 /*
     79 =================
     80 idDeclTable::Size
     81 =================
     82 */
     83 size_t idDeclTable::Size() const {
     84 	return sizeof( idDeclTable ) + values.Allocated();
     85 }
     86 
     87 /*
     88 =================
     89 idDeclTable::FreeData
     90 =================
     91 */
     92 void idDeclTable::FreeData() {
     93 	snap = false;
     94 	clamp = false;
     95 	values.Clear();
     96 }
     97 
     98 /*
     99 =================
    100 idDeclTable::DefaultDefinition
    101 =================
    102 */
    103 const char *idDeclTable::DefaultDefinition() const {
    104 	return "{ { 0 } }";
    105 }
    106 
    107 /*
    108 =================
    109 idDeclTable::Parse
    110 =================
    111 */
    112 bool idDeclTable::Parse( const char *text, const int textLength, bool allowBinaryVersion ) {
    113 	idLexer src;
    114 	idToken token;
    115 	float v;
    116 
    117 	src.LoadMemory( text, textLength, GetFileName(), GetLineNum() );
    118 	src.SetFlags( DECL_LEXER_FLAGS );
    119 	src.SkipUntilString( "{" );
    120 
    121 	snap = false;
    122 	clamp = false;
    123 	values.Clear();
    124 
    125 	while ( 1 ) {
    126 		if ( !src.ReadToken( &token ) ) {
    127 			break;
    128 		}
    129 
    130 		if ( token == "}" ) {
    131 			break;
    132 		}
    133 
    134 		if ( token.Icmp( "snap" ) == 0 ) {
    135 			snap = true;
    136 		} else if ( token.Icmp( "clamp" ) == 0 ) {
    137 			clamp = true;
    138 		} else if ( token.Icmp( "{" ) == 0 ) {
    139 
    140 			while ( 1 ) {
    141 				bool errorFlag;
    142 
    143 				v = src.ParseFloat( &errorFlag );
    144 				if ( errorFlag ) {
    145 					// we got something non-numeric
    146 					MakeDefault();
    147 					return false;
    148 				}
    149 
    150 				values.Append( v );
    151 
    152 				src.ReadToken( &token );
    153 				if ( token == "}" ) {
    154 					break;
    155 				}
    156 				if ( token == "," ) {
    157 					continue;
    158 				}
    159 				src.Warning( "expected comma or brace" );
    160 				MakeDefault();
    161 				return false;
    162 			}
    163 
    164 		} else {
    165 			src.Warning( "unknown token '%s'", token.c_str() );
    166 			MakeDefault();
    167 			return false;
    168 		}
    169 	}
    170 
    171 	// copy the 0 element to the end, so lerping doesn't
    172 	// need to worry about the wrap case
    173 	float val = values[0];		// template bug requires this to not be in the Append()?
    174 	values.Append( val );
    175 
    176 	return true;
    177 }