/
p_materialmanager.c
1105 lines (921 loc) · 29.9 KB
/
p_materialmanager.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2009 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2009 Daniel Swanson <danij@dengine.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* p_materialmanager.c: Materials manager.
*/
// HEADER FILES ------------------------------------------------------------
#include <ctype.h> // For tolower()
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_network.h"
#include "de_refresh.h"
#include "de_graphics.h"
#include "de_misc.h"
#include "de_audio.h" // For texture, environmental audio properties.
// MACROS ------------------------------------------------------------------
#define MATERIALS_BLOCK_ALLOC (32) // Num materials to allocate per block.
#define MATERIAL_NAME_HASH_SIZE (512)
// TYPES -------------------------------------------------------------------
typedef struct materialbind_s {
char name[9];
material_t* mat;
uint hashNext; // 1-based index
} materialbind_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
D_CMD(ListMaterials);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static void animateAnimGroups(void);
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
extern boolean mapSetup;
// PUBLIC DATA DEFINITIONS -------------------------------------------------
materialnum_t numMaterialBinds = 0;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static boolean initedOk = false;
/**
* The following data structures and variables are intrinsically linked and
* are inter-dependant. The scheme used is somewhat complicated due to the
* required traits of the materials themselves and in of the system itself:
*
* 1) Pointers to material_t are eternal, they are always valid and continue
* to reference the same logical material data even after engine reset.
* 2) Public material identifiers (materialnum_t) are similarly eternal.
* Note that they are used to index the material name bindings array.
* 3) Dynamic creation/update of materials.
* 4) Material name bindings are semi-independant from the materials. There
* may be multiple name bindings for a given material (aliases).
* The only requirement is that the name is unique among materials in
* a given material mnamespace.
* 5) Super-fast look up by public material identifier.
* 6) Fast look up by material name (a hashing scheme is used).
*/
static zblockset_t* materialsBlockSet;
static material_t* materialsHead; // Head of the linked list of materials.
static materialbind_t* materialBinds;
static materialnum_t maxMaterialBinds;
static uint hashTable[NUM_MATERIAL_NAMESPACES][MATERIAL_NAME_HASH_SIZE];
// CODE --------------------------------------------------------------------
void P_MaterialManagerRegister(void)
{
C_CMD("listmaterials", NULL, ListMaterials);
}
/**
* Is the specified id a valid (known) material namespace.
*
* \note The special case MN_ANY is considered invalid here as it does not
* relate to one specific mnamespace.
*
* @return @c true, iff the id is valid.
*/
static boolean isKnownMNamespace(material_namespace_t mnamespace)
{
if(mnamespace >= MN_FIRST && mnamespace < NUM_MATERIAL_NAMESPACES)
return true;
return false;
}
/**
* This is a hash function. Given a material name it generates a
* somewhat-random number between 0 and MATERIAL_NAME_HASH_SIZE.
*
* @return The generated hash index.
*/
static uint hashForName(const char* name)
{
ushort key = 0;
int i;
// Stop when the name ends.
for(i = 0; *name; ++i, name++)
{
if(i == 0)
key ^= (int) (*name);
else if(i == 1)
key *= (int) (*name);
else if(i == 2)
{
key -= (int) (*name);
i = -1;
}
}
return key % MATERIAL_NAME_HASH_SIZE;
}
/**
* Given a name and namespace, search the materials db for a match.
* \assume Caller knows what it's doing; params arn't validity checked.
*
* @param name Name of the material to search for. Must have been
* transformed to all lower case.
* @param mnamespace Specific MG_* material namespace NOT @c MN_ANY.
* @return Unique number of the found material, else zero.
*/
static materialnum_t getMaterialNumForName(const char* name, uint hash,
material_namespace_t mnamespace)
{
// Go through the candidates.
if(hashTable[mnamespace][hash])
{
materialbind_t* mb = &materialBinds[
hashTable[mnamespace][hash] - 1];
for(;;)
{
material_t* mat;
mat = mb->mat;
if(!strncmp(mb->name, name, 8))
return ((mb) - materialBinds) + 1;
if(!mb->hashNext)
break;
mb = &materialBinds[mb->hashNext - 1];
}
}
return 0; // Not found.
}
/**
* Given an index and namespace, search the materials db for a match.
* \assume Caller knows what it's doing; params arn't validity checked.
*
* @param id gltextureid to search for.
* @param mnamespace Specific MG_* material namespace NOT @c MN_ANY.
* @return Unique number of the found material, else zero.
*/
static materialnum_t getMaterialNumForIndex(uint idx,
material_namespace_t mnamespace)
{
materialnum_t i;
// Go through the candidates.
for(i = 0; i < numMaterialBinds; ++i)
{
materialbind_t* mb = &materialBinds[i];
material_t* mat = mb->mat;
if(mat->mnamespace == mnamespace &&
GL_GetGLTexture(mat->layers[0].tex)->ofTypeID == idx)
return ((mb) - materialBinds) + 1;
}
return 0; // Not found.
}
static void newMaterialNameBinding(material_t* mat, const char* name,
material_namespace_t mnamespace,
uint hash)
{
materialbind_t* mb;
if(++numMaterialBinds > maxMaterialBinds)
{ // Allocate more memory.
maxMaterialBinds += MATERIALS_BLOCK_ALLOC;
materialBinds =
Z_Realloc(materialBinds, sizeof(*materialBinds) * maxMaterialBinds,
PU_STATIC);
}
// Add the new material to the end.
mb = &materialBinds[numMaterialBinds - 1];
strncpy(mb->name, name, 8);
mb->name[8] = '\0';
mb->mat = mat;
// We also hash the name for faster searching.
mb->hashNext = hashTable[mnamespace][hash];
hashTable[mnamespace][hash] = (mb - materialBinds) + 1;
}
/**
* One time initialization of the materials list. Called during init.
*/
void P_InitMaterialManager(void)
{
if(initedOk)
return; // Already been here.
materialsBlockSet = Z_BlockCreate(sizeof(material_t),
MATERIALS_BLOCK_ALLOC, PU_STATIC);
materialsHead = NULL;
materialBinds = NULL;
numMaterialBinds = maxMaterialBinds = 0;
// Clear the name bind hash tables.
memset(hashTable, 0, sizeof(hashTable));
initedOk = true;
}
/**
* Release all memory acquired for the materials list.
* Called during shutdown.
*/
void P_ShutdownMaterialManager(void)
{
if(!initedOk)
return;
Z_BlockDestroy(materialsBlockSet);
materialsBlockSet = NULL;
materialsHead = NULL;
// Destroy the bindings.
if(materialBinds)
{
Z_Free(materialBinds);
materialBinds = NULL;
}
numMaterialBinds = maxMaterialBinds = 0;
initedOk = false;
}
/**
* Deletes all GL texture instances, linked to materials.
*
* @param mnamespace @c MN_ANY = delete everything, ELSE
* Only delete those currently in use by materials
* in the specified namespace.
*/
void P_DeleteMaterialTextures(material_namespace_t mnamespace)
{
if(mnamespace == MN_ANY)
{ // Delete the lot.
GL_DeleteAllTexturesForGLTextures(GLT_ANY);
return;
}
if(!isKnownMNamespace(mnamespace))
Con_Error("P_DeleteMaterialTextures: Internal error, "
"invalid materialgroup '%i'.", (int) mnamespace);
if(materialsHead)
{
material_t* mat;
mat = materialsHead;
do
{
if(mat->mnamespace == mnamespace)
{
uint i;
for(i = 0; i < mat->numLayers; ++i)
GL_ReleaseGLTexture(mat->layers[i].tex);
}
} while((mat = mat->globalNext));
}
}
static material_t* createMaterial(short width, short height, byte flags,
material_namespace_t mnamespace,
ded_material_t* def, gltextureid_t tex)
{
uint i;
material_t* mat = Z_BlockNewElement(materialsBlockSet);
memset(mat, 0, sizeof(*mat));
mat->header.type = DMU_MATERIAL;
mat->mnamespace = mnamespace;
mat->width = width;
mat->height = height;
mat->flags = flags;
mat->envClass = MEC_UNKNOWN;
mat->current = mat->next = mat;
mat->def = def;
mat->decoration = NULL;
mat->ptcGen = NULL;
mat->detail = NULL;
mat->layers[0].tex = tex;
mat->numLayers = 1;
// Is this a custom material?
mat->flags &= ~MATF_CUSTOM;
for(i = 0; i < mat->numLayers; ++i)
{
if(!GLTexture_IsFromIWAD(GL_GetGLTexture(mat->layers[i].tex)))
{
mat->flags |= MATF_CUSTOM;
break;
}
}
// Link the new material into the global list of materials.
mat->globalNext = materialsHead;
materialsHead = mat;
return mat;
}
static material_t* getMaterialByNum(materialnum_t num)
{
if(num < numMaterialBinds)
return materialBinds[num].mat;
return NULL;
}
/**
* Given a unique material number return the associated material.
*
* @param num Unique material number.
*
* @return The associated material, ELSE @c NULL.
*/
material_t* P_ToMaterial(materialnum_t num)
{
if(!initedOk)
return NULL;
if(num != 0)
return getMaterialByNum(num - 1); // 1-based index.
return NULL;
}
/**
* Retrieve the unique material number for the given material.
*
* @param mat The material to retrieve the unique number of.
*
* @return The associated unique number.
*/
materialnum_t P_ToMaterialNum(const material_t* mat)
{
if(mat)
{
materialnum_t i;
for(i = 0; i < numMaterialBinds; ++i)
if(materialBinds[i].mat == mat)
return i + 1; // 1-based index.
}
return 0;
}
/**
* Create a new material. If there exists one by the same name and in the
* same namespace, it is returned else a new material is created.
*
* \note: May fail if the name is invalid.
*
* @param name Name of the new material.
* @param width Width of the material (not of the texture).
* @param height Height of the material (not of the texture).
* @param flags MATF_* material flags
* @param tex Texture to use with this material.
* @param mnamespace MG_* material namespace.
* MN_ANY is only valid when updating an existing material.
*
* @return The created material, ELSE @c NULL.
*/
material_t* P_MaterialCreate(const char* rawName, short width, short height,
byte flags, gltextureid_t tex,
material_namespace_t mnamespace,
ded_material_t* def)
{
int n;
uint hash;
char name[9];
materialnum_t oldMat;
material_t* mat;
if(!initedOk)
return NULL;
// In original DOOM, texture name references beginning with the
// hypen '-' character are always treated as meaning "no reference"
// or "invalid texture" and surfaces using them were not drawn.
if(!rawName || !rawName[0] || rawName[0] == '-')
{
#if _DEBUG
Con_Message("P_MaterialCreate: Warning, attempted to create material with "
"NULL name\n.");
#endif
return NULL;
}
// Prepare 'name'.
for(n = 0; *rawName && n < 8; ++n, rawName++)
name[n] = tolower(*rawName);
name[n] = '\0';
hash = hashForName(name);
// Check if we've already created a material for this.
if(mnamespace == MN_ANY)
{ // Caller doesn't care which namespace. This is only valid if we
// can find a material by this name using a priority search order.
oldMat = getMaterialNumForName(name, hash, MN_SPRITES);
if(!oldMat)
oldMat = getMaterialNumForName(name, hash, MN_TEXTURES);
if(!oldMat)
oldMat = getMaterialNumForName(name, hash, MN_FLATS);
}
else
{
if(!isKnownMNamespace(mnamespace))
{
#if _DEBUG
Con_Message("P_MaterialCreate: Warning, attempted to create material in "
"unknown namespace '%i'.\n", (int) mnamespace);
#endif
return NULL;
}
oldMat = getMaterialNumForName(name, hash, mnamespace);
}
if(oldMat)
{ // We are updating an existing material.
materialbind_t* mb = &materialBinds[oldMat - 1];
mat = mb->mat;
// Update the (possibly new) meta data.
if(tex)
mat->layers[0].tex = tex;
mat->flags = flags;
if(width > 0)
mat->width = width;
if(height > 0)
mat->height = height;
mat->inAnimGroup = false;
mat->current = mat->next = mat;
mat->def = def;
mat->inter = 0;
mat->decoration = NULL;
mat->ptcGen = NULL;
mat->detail = NULL;
mat->envClass = MEC_UNKNOWN;
mat->mnamespace = mnamespace;
return mat; // Yep, return it.
}
if(mnamespace == MN_ANY)
{
#if _DEBUG
Con_Message("P_MaterialCreate: Warning, attempted to create material "
"without specifying a namespace.\n");
#endif
return NULL;
}
/**
* A new material.
*/
// Sanity checks.
assert(tex != 0);
assert(width > 0);
assert(height > 0);
mat = createMaterial(width, height, flags, mnamespace, def, tex);
// Now create a name binding for it.
newMaterialNameBinding(mat, name, mnamespace, hash);
return mat;
}
/**
* Given a Texture/Flat/Sprite etc idx and a MG_* namespace, search
* for a matching material.
*
* @param ofTypeID Texture/Flat/Sprite etc idx.
* @param mnamespace Specific MG_* namespace (not MN_ANY).
*
* @return The associated material, ELSE @c NULL.
*/
material_t* P_GetMaterial(int ofTypeID, material_namespace_t mnamespace)
{
if(!initedOk)
return NULL;
if(!isKnownMNamespace(mnamespace)) // MN_ANY is considered invalid.
{
#if _DEBUG
Con_Message("P_GetMaterial: Internal error, invalid namespace '%i'\n",
(int) mnamespace);
#endif
return NULL;
}
if(materialsHead)
{
material_t* mat;
mat = materialsHead;
do
{
if(mnamespace == mat->mnamespace &&
GL_GetGLTexture(mat->layers[0].tex)->ofTypeID == ofTypeID)
{
if(mat->flags & MATF_NO_DRAW)
return NULL;
return mat;
}
} while((mat = mat->globalNext));
}
return NULL;
}
materialnum_t P_MaterialCheckNumForIndex(uint idx,
material_namespace_t mnamespace)
{
if(!initedOk)
return 0;
// Caller wants a material in a specific namespace.
if(!isKnownMNamespace(mnamespace))
{
#if _DEBUG
Con_Message("P_GetMaterial: Internal error, invalid namespace '%i'\n",
(int) mnamespace);
#endif
return 0;
}
return getMaterialNumForIndex(idx, mnamespace);
}
/**
* Given a texture/flat/sprite/etc index num, search the materials db for
* a name-bound material.
* \note Part of the Doomsday public API.
* \note2 Sames as P_MaterialCheckNumForIndex except will log an error
* message if the material being searched for is not found.
*
* @param idx Index of the texture/flat/sprite/etc.
* @param mnamespace MG_* namespace.
*
* @return Unique identifier of the found material, else zero.
*/
materialnum_t P_MaterialNumForIndex(uint idx, material_namespace_t mnamespace)
{
materialnum_t result = P_MaterialCheckNumForIndex(idx, mnamespace);
// Not found? Don't announce during map setup or if not yet inited.
if(result == 0 && (!mapSetup || !initedOk))
Con_Message("P_MaterialNumForIndex: %u in namespace %i not found!\n",
idx, mnamespace);
return result;
}
/**
* Given a name and namespace, search the materials db for a match.
* \note Part of the Doomsday public API.
*
* @param name Name of the material to search for.
* @param mnamespace Specific MG_* namespace ELSE,
* @c MN_ANY = no namespace requirement in which case
* the material is searched for among all namespaces
* using a logic which prioritizes the namespaces as
* follows:
* 1st: MN_SPRITES
* 2nd: MN_TEXTURES
* 3rd: MN_FLATS
*
* @return Unique number of the found material, else zero.
*/
materialnum_t P_MaterialCheckNumForName(const char* rawName,
material_namespace_t mnamespace)
{
int n;
uint hash;
char name[9];
if(!initedOk)
return 0;
// In original DOOM, texture name references beginning with the
// hypen '-' character are always treated as meaning "no reference"
// or "invalid texture" and surfaces using them were not drawn.
if(!rawName || !rawName[0] || rawName[0] == '-')
return 0;
if(mnamespace != MN_ANY && !isKnownMNamespace(mnamespace))
{
#if _DEBUG
Con_Message("P_GetMaterial: Internal error, invalid namespace '%i'\n",
(int) mnamespace);
#endif
return 0;
}
// Prepare 'name'.
for(n = 0; *rawName && n < 8; ++n, rawName++)
name[n] = tolower(*rawName);
name[n] = '\0';
hash = hashForName(name);
if(mnamespace == MN_ANY)
{ // Caller doesn't care which namespace.
materialnum_t matNum;
// Check for the material in these namespaces, in priority order.
if((matNum = getMaterialNumForName(name, hash, MN_SPRITES)))
return matNum;
if((matNum = getMaterialNumForName(name, hash, MN_TEXTURES)))
return matNum;
if((matNum = getMaterialNumForName(name, hash, MN_FLATS)))
return matNum;
return 0; // Not found.
}
// Caller wants a material in a specific namespace.
return getMaterialNumForName(name, hash, mnamespace);
}
/**
* Given a name and namespace, search the materials db for a match.
* \note Part of the Doomsday public API.
* \note2 Same as P_MaterialCheckNumForName except will log an error
* message if the material being searched for is not found.
*
* @param name Name of the material to search for.
* @param mnamespace MG_* namespace.
*
* @return Unique identifier of the found material, else zero.
*/
materialnum_t P_MaterialNumForName(const char* name,
material_namespace_t mnamespace)
{
materialnum_t result;
if(!initedOk)
return 0;
// In original DOOM, texture name references beginning with the
// hypen '-' character are always treated as meaning "no reference"
// or "invalid texture" and surfaces using them were not drawn.
if(!name || !name[0] || name[0] == '-')
return 0;
result = P_MaterialCheckNumForName(name, mnamespace);
// Not found?
if(result == 0 && !mapSetup) // Don't announce during map setup.
Con_Message("P_MaterialNumForName: \"%.8s\" in namespace %i not found!\n",
name, mnamespace);
return result;
}
/**
* Given a unique material identifier, lookup the associated name.
* \note Part of the Doomsday public API.
*
* @param mat The material to lookup the name for.
*
* @return The associated name.
*/
const char* P_GetMaterialName(material_t* mat)
{
materialnum_t num;
if(!initedOk)
return NULL;
if(mat && (num = P_ToMaterialNum(mat)))
return materialBinds[num-1].name;
return "NOMAT"; // Should never happen.
}
/**
* Precache the specified material.
* \note Part of the Doomsday public API.
*
* @param mat The material to be precached.
*/
void P_MaterialPrecache(material_t* mat)
{
if(!initedOk)
return;
Material_Precache(mat);
}
/**
* Called every tic by P_Ticker.
*/
void P_MaterialManagerTicker(timespan_t time)
{
// material_t* mat;
static trigger_t fixed = { 1.0 / 35, 0 };
// The animation will only progress when the game is not paused.
if(clientPaused)
return;
#if 0
mat = materialsHead;
while(mat)
{
Material_Ticker(mat, time);
mat = mat->globalNext;
}
#endif
if(!M_RunTrigger(&fixed, time))
return;
animateAnimGroups();
}
static void printMaterials(material_namespace_t mnamespace, const char* like)
{
materialnum_t i, numDigits;
if(!(mnamespace < NUM_MATERIAL_NAMESPACES))
return;
if(mnamespace == MN_ANY)
Con_Printf("Known Materials (IDX - Name (Namespace) [width, height]):\n");
else
Con_Printf("Known Materials in Namespace %i (IDX - Name "
"[width, height]):\n", mnamespace);
numDigits = M_NumDigits(numMaterialBinds);
for(i = 0; i < numMaterialBinds; ++i)
{
uint j;
materialbind_t* mb = &materialBinds[i];
material_t* mat = mb->mat;
if(mnamespace != MN_ANY && mat->mnamespace != mnamespace)
continue;
if(like && like[0] && strnicmp(mb->name, like, strlen(like)))
continue;
Con_Printf(" %*lu - \"%s\"", numDigits, i, mb->name);
if(mnamespace == MN_ANY)
Con_Printf(" (%i)", mat->mnamespace);
Con_Printf(" [%i, %i]", mat->width, mat->height);
for(j = 0; j < mat->numLayers; ++j)
{
Con_Printf(" %i:%s", j, GL_GetGLTexture(mat->layers[j].tex)->name);
}
Con_Printf("\n");
}
}
D_CMD(ListMaterials)
{
material_namespace_t mnamespace = MN_ANY;
if(argc > 1)
{
mnamespace = atoi(argv[1]);
if(mnamespace < MN_FIRST)
{
mnamespace = MN_ANY;
}
else if(!(mnamespace < NUM_MATERIAL_NAMESPACES))
{
Con_Printf("Invalid namespace \"%s\".\n", argv[1]);
return false;
}
}
printMaterials(mnamespace, argc > 2? argv[2] : NULL);
return true;
}
// Code bellow needs re-implementing --------------------------------------
typedef struct animframe_s {
material_t* mat;
ushort tics;
ushort random;
} animframe_t;
typedef struct animgroup_s {
int id;
int flags;
int index;
int maxTimer;
int timer;
int count;
animframe_t* frames;
} animgroup_t;
static int numgroups;
static animgroup_t* groups;
static animgroup_t* getAnimGroup(int number)
{
if(--number < 0 || number >= numgroups)
return NULL;
return &groups[number];
}
static boolean isInAnimGroup(animgroup_t* group, const material_t* mat)
{
int i;
if(!mat || !group)
return false;
// Is it in there?
for(i = 0; i < group->count; ++i)
{
animframe_t* frame = &group->frames[i];
if(frame->mat == mat)
return true;
}
return false;
}
boolean R_IsInAnimGroup(int groupNum, material_t* mat)
{
return isInAnimGroup(getAnimGroup(groupNum), mat);
}
int R_NumAnimGroups(void)
{
return numgroups;
}
/**
* Create a new animation group. Returns the group number.
* \note Part of the Doomsday public API.
*/
int R_CreateAnimGroup(int flags)
{
animgroup_t* group;
// Allocating one by one is inefficient, but it doesn't really matter.
groups =
Z_Realloc(groups, sizeof(animgroup_t) * (numgroups + 1), PU_STATIC);
// Init the new group.
group = &groups[numgroups];
memset(group, 0, sizeof(*group));
// The group number is (index + 1).
group->id = ++numgroups;
group->flags = flags;
return group->id;
}
/**
* Called during engine reset to clear the existing animation groups.
*/
void R_DestroyAnimGroups(void)
{
int i;
if(numgroups > 0)
{
for(i = 0; i < numgroups; ++i)
{
animgroup_t* group = &groups[i];
Z_Free(group->frames);
}
Z_Free(groups);
groups = NULL;
numgroups = 0;
}
}
/**
* \note Part of the Doomsday public API.
*/
void R_AddToAnimGroup(int groupNum, materialnum_t num, int tics,
int randomTics)
{
animgroup_t* group;
animframe_t* frame;
material_t* mat;
group = getAnimGroup(groupNum);
if(!group)
Con_Error("R_AddToAnimGroup: Unknown anim group '%i'\n.", groupNum);
if(!num || !(mat = getMaterialByNum(num - 1)))
{
Con_Message("R_AddToAnimGroup: Invalid material num '%i'\n.", num);
return;
}
// Mark the material as being in an animgroup.
mat->inAnimGroup = true;
// Allocate a new animframe.
group->frames =
Z_Realloc(group->frames, sizeof(animframe_t) * ++group->count,
PU_STATIC);
frame = &group->frames[group->count - 1];
frame->mat = mat;
frame->tics = tics;
frame->random = randomTics;
}
boolean R_IsPrecacheGroup(int groupNum)
{
animgroup_t* group;
if(group = getAnimGroup(groupNum))
{
return (group->flags & AGF_PRECACHE)? true : false;
}
return false;
}
static void animateAnimGroups(void)
{
int i, timer, k;
animgroup_t* group;
for(i = 0, group = groups; i < numgroups; ++i, group++)
{