doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DeclSkin.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 idDeclSkin::Size
36 =================
37 */
38 size_t idDeclSkin::Size( void ) const {
39  return sizeof( idDeclSkin );
40 }
41 
42 /*
43 ================
44 idDeclSkin::FreeData
45 ================
46 */
47 void idDeclSkin::FreeData( void ) {
48  mappings.Clear();
49 }
50 
51 /*
52 ================
53 idDeclSkin::Parse
54 ================
55 */
56 bool idDeclSkin::Parse( const char *text, const int textLength ) {
57  idLexer src;
58  idToken token, token2;
59 
60  src.LoadMemory( text, textLength, GetFileName(), GetLineNum() );
62  src.SkipUntilString( "{" );
63 
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 */
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( void ) 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 }
int GetLineNum(void) const
Definition: DeclManager.h:168
static int snPrintf(char *dest, int size, const char *fmt,...) id_attribute((format(printf
Definition: Str.cpp:1465
const idMaterial * from
Definition: DeclSkin.h:41
const idMaterial * to
Definition: DeclSkin.h:42
const char * GetFileName(void) const
Definition: DeclManager.h:171
const int DECL_LEXER_FLAGS
Definition: DeclManager.h:93
const idMaterial * RemapShaderBySkin(const idMaterial *shader) const
Definition: DeclSkin.cpp:162
const char * GetName(void) const
Definition: DeclManager.h:140
Definition: Token.h:71
void SetFlags(int flags)
Definition: Lexer.h:298
virtual const idMaterial * FindMaterial(const char *name, bool makeDefault=true)=0
GLuint src
Definition: glext.h:5390
int i
Definition: process.py:33
bool IsDrawn(void) const
Definition: Material.h:378
int Icmp(const char *text) const
Definition: Str.h:667
virtual bool Parse(const char *text, const int textLength)
Definition: DeclSkin.cpp:56
Definition: Lexer.h:137
idStrList associatedModels
Definition: DeclSkin.h:61
GLuint index
Definition: glext.h:3476
virtual size_t Size(void) const
Definition: DeclSkin.cpp:38
#define NULL
Definition: Lib.h:88
virtual const idDecl * FindType(declType_t type, const char *name, bool makeDefault=true)=0
virtual bool SetDefaultText(void)
Definition: DeclSkin.cpp:107
virtual void FreeData(void)
Definition: DeclSkin.cpp:47
int LoadMemory(const char *ptr, int length, const char *name, int startLine=1)
Definition: Lexer.cpp:1646
void SetText(const char *text)
Definition: DeclManager.h:180
idDeclManager * declManager
int Append(const type &obj)
Definition: List.h:646
void void Warning(const char *str,...) id_attribute((format(printf
Definition: Lexer.cpp:241
int Num(void) const
Definition: List.h:265
int SkipUntilString(const char *string)
Definition: Lexer.cpp:1097
const int GetNumModelAssociations() const
Definition: DeclSkin.cpp:141
void MakeDefault(void)
Definition: DeclManager.h:190
virtual const char * DefaultDefinition(void) const
Definition: DeclSkin.cpp:129
idList< skinMapping_t > mappings
Definition: DeclSkin.h:60
int ReadToken(idToken *token)
Definition: Lexer.cpp:820
const char * GetAssociatedModel(int index) const
Definition: DeclSkin.cpp:150
void Clear(void)
Definition: List.h:184