doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RegExp.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 #include "RegExp.h"
33 #include "DeviceContext.h"
34 #include "Window.h"
35 #include "UserInterfaceLocal.h"
36 
37 int idRegister::REGCOUNT[NUMTYPES] = {4, 1, 1, 1, 0, 2, 3, 4};
38 
39 /*
40 ====================
41 idRegister::SetToRegs
42 ====================
43 */
44 void idRegister::SetToRegs( float *registers ) {
45  int i;
46  idVec4 v;
47  idVec2 v2;
48  idVec3 v3;
49  idRectangle rect;
50 
51  if ( !enabled || var == NULL || ( var && ( var->GetDict() || !var->GetEval() ) ) ) {
52  return;
53  }
54 
55  switch( type ) {
56  case VEC4: {
57  v = *static_cast<idWinVec4*>(var);
58  break;
59  }
60  case RECTANGLE: {
61  rect = *static_cast<idWinRectangle*>(var);
62  v = rect.ToVec4();
63  break;
64  }
65  case VEC2: {
66  v2 = *static_cast<idWinVec2*>(var);
67  v[0] = v2[0];
68  v[1] = v2[1];
69  break;
70  }
71  case VEC3: {
72  v3 = *static_cast<idWinVec3*>(var);
73  v[0] = v3[0];
74  v[1] = v3[1];
75  v[2] = v3[2];
76  break;
77  }
78  case FLOAT: {
79  v[0] = *static_cast<idWinFloat*>(var);
80  break;
81  }
82  case INT: {
83  v[0] = *static_cast<idWinInt*>(var);
84  break;
85  }
86  case BOOL: {
87  v[0] = *static_cast<idWinBool*>(var);
88  break;
89  }
90  default: {
91  common->FatalError( "idRegister::SetToRegs: bad reg type" );
92  break;
93  }
94  }
95  for ( i = 0; i < regCount; i++ ) {
96  registers[ regs[ i ] ] = v[i];
97  }
98 }
99 
100 /*
101 =================
102 idRegister::GetFromRegs
103 =================
104 */
105 void idRegister::GetFromRegs( float *registers ) {
106  idVec4 v;
107  idRectangle rect;
108 
109  if (!enabled || var == NULL || (var && (var->GetDict() || !var->GetEval()))) {
110  return;
111  }
112 
113  for ( int i = 0; i < regCount; i++ ) {
114  v[i] = registers[regs[i]];
115  }
116 
117  switch( type ) {
118  case VEC4: {
119  *dynamic_cast<idWinVec4*>(var) = v;
120  break;
121  }
122  case RECTANGLE: {
123  rect.x = v.x;
124  rect.y = v.y;
125  rect.w = v.z;
126  rect.h = v.w;
127  *static_cast<idWinRectangle*>(var) = rect;
128  break;
129  }
130  case VEC2: {
131  *static_cast<idWinVec2*>(var) = v.ToVec2();
132  break;
133  }
134  case VEC3: {
135  *static_cast<idWinVec3*>(var) = v.ToVec3();
136  break;
137  }
138  case FLOAT: {
139  *static_cast<idWinFloat*>(var) = v[0];
140  break;
141  }
142  case INT: {
143  *static_cast<idWinInt*>(var) = v[0];
144  break;
145  }
146  case BOOL: {
147  *static_cast<idWinBool*>(var) = ( v[0] != 0.0f );
148  break;
149  }
150  default: {
151  common->FatalError( "idRegister::GetFromRegs: bad reg type" );
152  break;
153  }
154  }
155 }
156 
157 /*
158 =================
159 idRegister::ReadFromDemoFile
160 =================
161 */
163  f->ReadBool( enabled );
164  f->ReadShort( type );
165  f->ReadInt( regCount );
166  for ( int i = 0; i < 4; i++ )
167  f->ReadUnsignedShort( regs[i] );
168  name = f->ReadHashString();
169 }
170 
171 /*
172 =================
173 idRegister::WriteToDemoFile
174 =================
175 */
177  f->WriteBool( enabled );
178  f->WriteShort( type );
179  f->WriteInt( regCount );
180  for (int i = 0; i < 4; i++)
181  f->WriteUnsignedShort( regs[i] );
182  f->WriteHashString( name );
183 }
184 
185 /*
186 =================
187 idRegister::WriteToSaveGame
188 =================
189 */
191  int len;
192 
193  savefile->Write( &enabled, sizeof( enabled ) );
194  savefile->Write( &type, sizeof( type ) );
195  savefile->Write( &regCount, sizeof( regCount ) );
196  savefile->Write( &regs[0], sizeof( regs ) );
197 
198  len = name.Length();
199  savefile->Write( &len, sizeof( len ) );
200  savefile->Write( name.c_str(), len );
201 
202  var->WriteToSaveGame( savefile );
203 }
204 
205 /*
206 ================
207 idRegister::ReadFromSaveGame
208 ================
209 */
211  int len;
212 
213  savefile->Read( &enabled, sizeof( enabled ) );
214  savefile->Read( &type, sizeof( type ) );
215  savefile->Read( &regCount, sizeof( regCount ) );
216  savefile->Read( &regs[0], sizeof( regs ) );
217 
218  savefile->Read( &len, sizeof( len ) );
219  name.Fill( ' ', len );
220  savefile->Read( &name[0], len );
221 
222  var->ReadFromSaveGame( savefile );
223 }
224 
225 /*
226 ====================
227 idRegisterList::AddReg
228 ====================
229 */
230 void idRegisterList::AddReg( const char *name, int type, idVec4 data, idWindow *win, idWinVar *var ) {
231  if ( FindReg( name ) == NULL ) {
232  assert( type >= 0 && type < idRegister::NUMTYPES );
233  int numRegs = idRegister::REGCOUNT[type];
234  idRegister *reg = new idRegister( name, type );
235  reg->var = var;
236  for ( int i = 0; i < numRegs; i++ ) {
237  reg->regs[i] = win->ExpressionConstant(data[i]);
238  }
239  int hash = regHash.GenerateKey( name, false );
240  regHash.Add( hash, regs.Append( reg ) );
241  }
242 }
243 
244 /*
245 ====================
246 idRegisterList::AddReg
247 ====================
248 */
249 void idRegisterList::AddReg( const char *name, int type, idParser *src, idWindow *win, idWinVar *var ) {
250  idRegister* reg;
251 
252  reg = FindReg( name );
253 
254  if ( reg == NULL ) {
255  assert(type >= 0 && type < idRegister::NUMTYPES);
256  int numRegs = idRegister::REGCOUNT[type];
257  reg = new idRegister( name, type );
258  reg->var = var;
259  if ( type == idRegister::STRING ) {
260  idToken tok;
261  if ( src->ReadToken( &tok ) ) {
262  tok = common->GetLanguageDict()->GetString( tok );
263  var->Init( tok, win );
264  }
265  } else {
266  for ( int i = 0; i < numRegs; i++ ) {
267  reg->regs[i] = win->ParseExpression(src, NULL);
268  if ( i < numRegs-1 ) {
269  src->ExpectTokenString(",");
270  }
271  }
272  }
273  int hash = regHash.GenerateKey( name, false );
274  regHash.Add( hash, regs.Append( reg ) );
275  } else {
276  int numRegs = idRegister::REGCOUNT[type];
277  reg->var = var;
278  if ( type == idRegister::STRING ) {
279  idToken tok;
280  if ( src->ReadToken( &tok ) ) {
281  var->Init( tok, win );
282  }
283  } else {
284  for ( int i = 0; i < numRegs; i++ ) {
285  reg->regs[i] = win->ParseExpression( src, NULL );
286  if ( i < numRegs-1 ) {
287  src->ExpectTokenString(",");
288  }
289  }
290  }
291  }
292 }
293 
294 /*
295 ====================
296 idRegisterList::GetFromRegs
297 ====================
298 */
299 void idRegisterList::GetFromRegs(float *registers) {
300  for ( int i = 0; i < regs.Num(); i++ ) {
301  regs[i]->GetFromRegs( registers );
302  }
303 }
304 
305 /*
306 ====================
307 idRegisterList::SetToRegs
308 ====================
309 */
310 
311 void idRegisterList::SetToRegs( float *registers ) {
312  int i;
313  for ( i = 0; i < regs.Num(); i++ ) {
314  regs[i]->SetToRegs( registers );
315  }
316 }
317 
318 /*
319 ====================
320 idRegisterList::FindReg
321 ====================
322 */
324  int hash = regHash.GenerateKey( name, false );
325  for ( int i = regHash.First( hash ); i != -1; i = regHash.Next( i ) ) {
326  if ( regs[i]->name.Icmp( name ) == 0 ) {
327  return regs[i];
328  }
329  }
330  return NULL;
331 }
332 
333 /*
334 ====================
335 idRegisterList::Reset
336 ====================
337 */
339  regs.DeleteContents( true );
340  regHash.Clear();
341 }
342 
343 /*
344 ====================
345 idRegisterList::ReadFromSaveGame
346 ====================
347 */
349  int c;
350 
351  f->ReadInt( c );
352  regs.DeleteContents( true );
353  for ( int i = 0; i < c; i++ ) {
354  idRegister *reg = new idRegister;
355  reg->ReadFromDemoFile( f );
356  regs.Append( reg );
357  }
358 }
359 
360 /*
361 ====================
362 idRegisterList::ReadFromSaveGame
363 ====================
364 */
366  int c = regs.Num();
367 
368  f->WriteInt( c );
369  for ( int i = 0 ; i < c; i++ ) {
370  regs[i]->WriteToDemoFile(f);
371  }
372 }
373 
374 /*
375 =====================
376 idRegisterList::WriteToSaveGame
377 =====================
378 */
380  int i, num;
381 
382  num = regs.Num();
383  savefile->Write( &num, sizeof( num ) );
384 
385  for ( i = 0; i < num; i++ ) {
386  regs[i]->WriteToSaveGame( savefile );
387  }
388 }
389 
390 /*
391 ====================
392 idRegisterList::ReadFromSaveGame
393 ====================
394 */
396  int i, num;
397 
398  savefile->Read( &num, sizeof( num ) );
399  for ( i = 0; i < num; i++ ) {
400  regs[i]->ReadFromSaveGame( savefile );
401  }
402 }
virtual void WriteToSaveGame(idFile *savefile)=0
virtual int ReadShort(short &value)
Definition: File.cpp:333
void ReadFromDemoFile(idDemoFile *f)
Definition: RegExp.cpp:348
void ReadFromDemoFile(idDemoFile *f)
Definition: RegExp.cpp:162
virtual int virtual int ReadInt(int &value)
Definition: File.cpp:311
assert(prefInfo.fullscreenBtn)
int ExpressionConstant(float f)
Definition: Window.cpp:2702
int Next(const int index) const
Definition: HashIndex.h:247
unsigned short regs[4]
Definition: RegExp.h:47
float y
Definition: Vector.h:811
virtual void virtual void virtual const idLangDict * GetLanguageDict(void)=0
const GLdouble * v
Definition: glext.h:2936
void GetFromRegs(float *registers)
Definition: RegExp.cpp:299
void GetFromRegs(float *registers)
Definition: RegExp.cpp:105
float z
Definition: Vector.h:812
Definition: Vector.h:316
void WriteToSaveGame(idFile *savefile)
Definition: RegExp.cpp:190
GLuint GLuint GLsizei GLenum type
Definition: glext.h:2845
int ExpectTokenString(const char *string)
Definition: Parser.cpp:2402
Definition: Token.h:71
GLuint src
Definition: glext.h:5390
GLenum GLsizei len
Definition: glext.h:3472
int i
Definition: process.py:33
GLuint GLuint num
Definition: glext.h:5390
void AddReg(const char *name, int type, idParser *src, idWindow *win, idWinVar *var)
Definition: RegExp.cpp:249
int First(const int key) const
Definition: HashIndex.h:238
idList< idRegister * > regs
Definition: RegExp.h:98
void ReadFromSaveGame(idFile *savefile)
Definition: RegExp.cpp:395
Definition: File.h:50
int ReadToken(idToken *token)
Definition: Parser.cpp:2338
GLfloat GLfloat GLfloat v2
Definition: glext.h:3608
virtual int ReadUnsignedShort(unsigned short &value)
Definition: File.cpp:344
Definition: Vector.h:52
virtual int WriteInt(const int value)
Definition: File.cpp:468
const idVec4 & ToVec4() const
Definition: Rectangle.h:115
idRegister * FindReg(const char *name)
Definition: RegExp.cpp:323
const GLubyte * c
Definition: glext.h:4677
Definition: Vector.h:808
void WriteToDemoFile(idDemoFile *f)
Definition: RegExp.cpp:365
void SetToRegs(float *registers)
Definition: RegExp.cpp:44
void Reset()
Definition: RegExp.cpp:338
virtual void Init(const char *_name, idWindow *win)=0
Definition: Winvar.cpp:53
float y
Definition: Rectangle.h:37
idCommon * common
Definition: Common.cpp:206
#define NULL
Definition: Lib.h:88
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:2853
virtual void virtual void FatalError(const char *fmt,...) id_attribute((format(printf
virtual void ReadFromSaveGame(idFile *savefile)=0
virtual int Read(void *buffer, int len)
Definition: File.cpp:179
const char * GetString(const char *str) const
Definition: LangDict.cpp:148
float w
Definition: Vector.h:813
void ReadFromSaveGame(idFile *savefile)
Definition: RegExp.cpp:210
void DeleteContents(bool clear)
Definition: List.h:207
bool GetEval()
Definition: Winvar.h:85
idWinVar * var
Definition: RegExp.h:48
virtual int ReadBool(bool &value)
Definition: File.cpp:384
float x
Definition: Rectangle.h:36
int GenerateKey(const char *string, bool caseSensitive=true) const
Definition: HashIndex.h:379
void WriteToSaveGame(idFile *savefile)
Definition: RegExp.cpp:379
void SetToRegs(float *registers)
Definition: RegExp.cpp:311
int ParseExpression(idParser *src, idWinVar *var=NULL, int component=0)
Definition: Window.cpp:3003
GLfloat GLfloat GLfloat GLfloat v3
Definition: glext.h:3609
int Append(const type &obj)
Definition: List.h:646
idDict * GetDict() const
Definition: Winvar.h:68
tuple f
Definition: idal.py:89
virtual int WriteShort(const short value)
Definition: File.cpp:488
int Num(void) const
Definition: List.h:265
bool enabled
Definition: RegExp.h:43
const GLcharARB * name
Definition: glext.h:3629
virtual int Write(const void *buffer, int len)
Definition: File.cpp:189
static int REGCOUNT[NUMTYPES]
Definition: RegExp.h:41
float w
Definition: Rectangle.h:38
Window win
Definition: glimp.cpp:46
void Clear(void)
Definition: HashIndex.h:328
int regCount
Definition: RegExp.h:46
const idVec3 & ToVec3(void) const
Definition: Vector.h:1043
const char * ReadHashString()
Definition: DemoFile.cpp:212
float h
Definition: Rectangle.h:39
void Add(const int key, const int index)
Definition: HashIndex.h:193
const idVec2 & ToVec2(void) const
Definition: Vector.h:1035
virtual int WriteBool(const bool value)
Definition: File.cpp:536
float x
Definition: Vector.h:810
virtual int WriteUnsignedShort(unsigned short value)
Definition: File.cpp:498
idHashIndex regHash
Definition: RegExp.h:99
void WriteHashString(const char *str)
Definition: DemoFile.cpp:248
void WriteToDemoFile(idDemoFile *f)
Definition: RegExp.cpp:176