doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DeclTable.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 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 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::FtoiFast( 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( void ) const {
84  return sizeof( idDeclTable ) + values.Allocated();
85 }
86 
87 /*
88 =================
89 idDeclTable::FreeData
90 =================
91 */
92 void idDeclTable::FreeData( void ) {
93  snap = false;
94  clamp = false;
95  values.Clear();
96 }
97 
98 /*
99 =================
100 idDeclTable::DefaultDefinition
101 =================
102 */
103 const char *idDeclTable::DefaultDefinition( void ) const {
104  return "{ { 0 } }";
105 }
106 
107 /*
108 =================
109 idDeclTable::Parse
110 =================
111 */
112 bool idDeclTable::Parse( const char *text, const int textLength ) {
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 }
int GetLineNum(void) const
Definition: DeclManager.h:168
bool clamp
Definition: DeclTable.h:51
GLboolean GLenum GLenum GLvoid * values
Definition: glext.h:2868
const char * GetFileName(void) const
Definition: DeclManager.h:171
const int DECL_LEXER_FLAGS
Definition: DeclManager.h:93
const GLdouble * v
Definition: glext.h:2936
virtual void FreeData(void)
Definition: DeclTable.cpp:92
Definition: Token.h:71
void SetFlags(int flags)
Definition: Lexer.h:298
float ParseFloat(bool *errorFlag=NULL)
Definition: Lexer.cpp:1264
GLuint src
Definition: glext.h:5390
int Icmp(const char *text) const
Definition: Str.h:667
static int Ftoi(float f)
Definition: Math.h:797
Definition: Lexer.h:137
GLuint index
Definition: glext.h:3476
static int FtoiFast(float f)
Definition: Math.h:801
static float Floor(float f)
Definition: Math.h:785
virtual bool Parse(const char *text, const int textLength)
Definition: DeclTable.cpp:112
int LoadMemory(const char *ptr, int length, const char *name, int startLine=1)
Definition: Lexer.cpp:1646
virtual size_t Size(void) const
Definition: DeclTable.cpp:83
static float Ceil(float f)
Definition: Math.h:789
bool snap
Definition: DeclTable.h:52
void void Warning(const char *str,...) id_attribute((format(printf
Definition: Lexer.cpp:241
const char * c_str(void) const
Definition: Str.h:487
int SkipUntilString(const char *string)
Definition: Lexer.cpp:1097
void MakeDefault(void)
Definition: DeclManager.h:190
virtual const char * DefaultDefinition(void) const
Definition: DeclTable.cpp:103
int ReadToken(idToken *token)
Definition: Lexer.cpp:820
float TableLookup(float index) const
Definition: DeclTable.cpp:38