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