DeclSkin.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. idDeclSkin::Size
  25. =================
  26. */
  27. size_t idDeclSkin::Size( void ) const {
  28. return sizeof( idDeclSkin );
  29. }
  30. /*
  31. ================
  32. idDeclSkin::FreeData
  33. ================
  34. */
  35. void idDeclSkin::FreeData( void ) {
  36. mappings.Clear();
  37. }
  38. /*
  39. ================
  40. idDeclSkin::Parse
  41. ================
  42. */
  43. bool idDeclSkin::Parse( const char *text, const int textLength ) {
  44. idLexer src;
  45. idToken token, token2;
  46. src.LoadMemory( text, textLength, GetFileName(), GetLineNum() );
  47. src.SetFlags( DECL_LEXER_FLAGS );
  48. src.SkipUntilString( "{" );
  49. associatedModels.Clear();
  50. while (1) {
  51. if ( !src.ReadToken( &token ) ) {
  52. break;
  53. }
  54. if ( !token.Icmp( "}" ) ) {
  55. break;
  56. }
  57. if ( !src.ReadToken( &token2 ) ) {
  58. src.Warning( "Unexpected end of file" );
  59. MakeDefault();
  60. return false;
  61. }
  62. if ( !token.Icmp( "model" ) ) {
  63. associatedModels.Append( token2 );
  64. continue;
  65. }
  66. skinMapping_t map;
  67. if ( !token.Icmp( "*" ) ) {
  68. // wildcard
  69. map.from = NULL;
  70. } else {
  71. map.from = declManager->FindMaterial( token );
  72. }
  73. map.to = declManager->FindMaterial( token2 );
  74. mappings.Append( map );
  75. }
  76. return false;
  77. }
  78. /*
  79. ================
  80. idDeclSkin::SetDefaultText
  81. ================
  82. */
  83. bool idDeclSkin::SetDefaultText( void ) {
  84. // if there exists a material with the same name
  85. if ( declManager->FindType( DECL_MATERIAL, GetName(), false ) ) {
  86. char generated[2048];
  87. idStr::snPrintf( generated, sizeof( generated ),
  88. "skin %s // IMPLICITLY GENERATED\n"
  89. "{\n"
  90. "_default %s\n"
  91. "}\n", GetName(), GetName() );
  92. SetText( generated );
  93. return true;
  94. } else {
  95. return false;
  96. }
  97. }
  98. /*
  99. ================
  100. idDeclSkin::DefaultDefinition
  101. ================
  102. */
  103. const char *idDeclSkin::DefaultDefinition( void ) const {
  104. return
  105. "{\n"
  106. "\t" "\"*\"\t\"_default\"\n"
  107. "}";
  108. }
  109. /*
  110. ================
  111. idDeclSkin::GetNumModelAssociations
  112. ================
  113. */
  114. const int idDeclSkin::GetNumModelAssociations(void ) const {
  115. return associatedModels.Num();
  116. }
  117. /*
  118. ================
  119. idDeclSkin::GetAssociatedModel
  120. ================
  121. */
  122. const char *idDeclSkin::GetAssociatedModel( int index ) const {
  123. if ( index >= 0 && index < associatedModels.Num() ) {
  124. return associatedModels[ index ];
  125. }
  126. return "";
  127. }
  128. /*
  129. ===============
  130. RemapShaderBySkin
  131. ===============
  132. */
  133. const idMaterial *idDeclSkin::RemapShaderBySkin( const idMaterial *shader ) const {
  134. int i;
  135. if ( !shader ) {
  136. return NULL;
  137. }
  138. // never remap surfaces that were originally nodraw, like collision hulls
  139. if ( !shader->IsDrawn() ) {
  140. return shader;
  141. }
  142. for ( i = 0; i < mappings.Num() ; i++ ) {
  143. const skinMapping_t *map = &mappings[i];
  144. // NULL = wildcard match
  145. if ( !map->from || map->from == shader ) {
  146. return map->to;
  147. }
  148. }
  149. // didn't find a match or wildcard, so stay the same
  150. return shader;
  151. }