DeclTable.cpp 4.3 KB

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