/
fi_main.c
1757 lines (1515 loc) · 46.4 KB
/
fi_main.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-2010 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2010 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
*/
// HEADER FILES ------------------------------------------------------------
#include "de_base.h"
#include "de_console.h"
#include "de_play.h"
#include "de_defs.h"
#include "de_graphics.h"
#include "de_refresh.h"
#include "de_render.h"
#include "de_network.h"
#include "de_audio.h"
#include "de_infine.h"
#include "de_misc.h"
#include "finaleinterpreter.h"
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
/**
* A Finale instance contains the high-level state of an InFine script.
*
* @see FinaleInterpreter (interactive script interpreter)
*
* @ingroup InFine
*/
typedef struct {
/// @see finaleFlags
int flags;
/// Unique identifier/reference (chosen automatically).
finaleid_t id;
/// Interpreter for this script.
finaleinterpreter_t* _interpreter;
/// Interpreter is active?
boolean active;
} finale_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
finale_t* P_CreateFinale(void);
void P_DestroyFinale(finale_t* f);
fidata_text_t* P_CreateText(fi_objectid_t id, const char* name);
void P_DestroyText(fidata_text_t* text);
fidata_pic_t* P_CreatePic(fi_objectid_t id, const char* name);
void P_DestroyPic(fidata_pic_t* pic);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static boolean inited = false;
/// Scripts.
static uint finalesSize;
static finale_t* finales;
/// Allow stretching to fill the screen at near 4:3 aspect ratios?
static byte noStretch = false;
static uint numPages;
static fi_page_t** pages;
/// Global object store.
static fi_object_collection_t objects;
// CODE --------------------------------------------------------------------
void FI_Register(void)
{
C_VAR_BYTE("finale-nostretch", &noStretch, 0, 0, 1);
}
static fi_page_t* pagesAdd(fi_page_t* p)
{
pages = Z_Realloc(pages, sizeof(*pages) * ++numPages, PU_STATIC);
pages[numPages-1] = p;
return p;
}
static fi_page_t* pagesRemove(fi_page_t* p)
{
uint i;
for(i = 0; i < numPages; ++i)
{
if(pages[i] != p)
continue;
if(i != numPages-1)
memmove(&pages[i], &pages[i+1], sizeof(*pages) * (numPages-i));
if(numPages > 1)
{
pages = Z_Realloc(pages, sizeof(*pages) * --numPages, PU_STATIC);
}
else
{
Z_Free(pages); pages = 0;
numPages = 0;
}
break;
}
return p;
}
/**
* Clear the specified page to the default, blank state.
*/
static void pageClear(fi_page_t* p)
{
p->_timer = 0;
p->_bgMaterial = 0; // No background material.
if(p->_objects.vector)
{
Z_Free(p->_objects.vector);
}
p->_objects.vector = 0;
p->_objects.size = 0;
AnimatorVector3_Init(p->_offset, 0, 0, 0);
AnimatorVector4_Init(p->_bgColor, 1, 1, 1, 0);
AnimatorVector4_Init(p->_filter, 0, 0, 0, 0);
{uint i;
for(i = 0; i < 9; ++i)
{
AnimatorVector3_Init(p->_textColor[i], 1, 1, 1);
}}
}
static fi_page_t* newPage(void)
{
fi_page_t* p = Z_Calloc(sizeof(*p), PU_STATIC, 0);
p->drawer = FIPage_Drawer;
pageClear(p);
return p;
}
static void objectsThink(fi_object_collection_t* c)
{
uint i;
for(i = 0; i < c->size; ++i)
{
fi_object_t* obj = c->vector[i];
obj->thinker(obj);
}
}
static void objectsDraw(fi_object_collection_t* c, fi_obtype_e type,
const float worldOrigin[3])
{
uint i;
for(i = 0; i < c->size; ++i)
{
fi_object_t* obj = c->vector[i];
if(type != FI_NONE && obj->type != type)
continue;
obj->drawer(obj, worldOrigin);
}
}
static uint objectsToIndex(fi_object_collection_t* c, fi_object_t* obj)
{
if(obj)
{
uint i;
for(i = 0; i < c->size; ++i)
{
fi_object_t* other = c->vector[i];
if(other == obj)
return i+1;
}
}
return 0;
}
static __inline boolean objectsIsPresent(fi_object_collection_t* c, fi_object_t* obj)
{
return objectsToIndex(c, obj) != 0;
}
/**
* \note Does not check if the object already exists in this collection.
*/
static fi_object_t* objectsAdd(fi_object_collection_t* c, fi_object_t* obj)
{
c->vector = Z_Realloc(c->vector, sizeof(*c->vector) * ++c->size, PU_STATIC);
c->vector[c->size-1] = obj;
return obj;
}
/**
* \assume There is at most one reference to the object in this collection.
*/
static fi_object_t* objectsRemove(fi_object_collection_t* c, fi_object_t* obj)
{
uint idx;
if((idx = objectsToIndex(c, obj)))
{
idx -= 1; // Indices are 1-based.
if(idx != c->size-1)
memmove(&c->vector[idx], &c->vector[idx+1], sizeof(*c->vector) * (c->size-idx));
if(c->size > 1)
{
c->vector = Z_Realloc(c->vector, sizeof(*c->vector) * --c->size, PU_STATIC);
}
else
{
Z_Free(c->vector); c->vector = 0;
c->size = 0;
}
}
return obj;
}
static void objectsEmpty(fi_object_collection_t* c)
{
if(c->size)
{
uint i;
for(i = 0; i < c->size; ++i)
{
fi_object_t* obj = c->vector[i];
switch(obj->type)
{
case FI_PIC: P_DestroyPic((fidata_pic_t*)obj); break;
case FI_TEXT: P_DestroyText((fidata_text_t*)obj); break;
default:
Con_Error("InFine: Unknown object type %i in objectsEmpty.", (int)obj->type);
}
}
Z_Free(c->vector);
}
c->vector = 0;
c->size = 0;
}
static fi_object_t* objectsById(fi_object_collection_t* c, fi_objectid_t id)
{
if(id != 0)
{
uint i;
for(i = 0; i < c->size; ++i)
{
fi_object_t* obj = c->vector[i];
if(obj->id == id)
return obj;
}
}
return 0;
}
/**
* @return A new (unused) unique object id.
*/
static fi_objectid_t objectsUniqueId(fi_object_collection_t* c)
{
fi_objectid_t id = 0;
while(objectsById(c, ++id));
return id;
}
static finale_t* finalesById(finaleid_t id)
{
if(id != 0)
{
uint i;
for(i = 0; i < finalesSize; ++i)
{
finale_t* f = &finales[i];
if(f->id == id)
return f;
}
}
return 0;
}
static void stopFinale(finale_t* f)
{
if(!f || !f->active)
return;
f->active = false;
P_DestroyFinaleInterpreter(f->_interpreter);
}
/**
* @return A new (unused) unique script id.
*/
static finaleid_t finalesUniqueId(void)
{
finaleid_t id = 0;
while(finalesById(++id));
return id;
}
static void picFrameDeleteXImage(fidata_pic_frame_t* f)
{
DGL_DeleteTextures(1, (DGLuint*)&f->texRef.tex);
f->texRef.tex = 0;
}
static fidata_pic_frame_t* createPicFrame(int type, int tics, void* texRef, short sound, boolean flagFlipH)
{
fidata_pic_frame_t* f = (fidata_pic_frame_t*) Z_Malloc(sizeof(*f), PU_STATIC, 0);
f->flags.flip = flagFlipH;
f->type = type;
f->tics = tics;
switch(f->type)
{
case PFT_MATERIAL: f->texRef.material = ((material_t*)texRef); break;
case PFT_PATCH: f->texRef.patch = *((patchid_t*)texRef); break;
case PFT_RAW: f->texRef.lump = *((lumpnum_t*)texRef); break;
case PFT_XIMAGE: f->texRef.tex = *((DGLuint*)texRef); break;
default:
Con_Error("Error - InFine: unknown frame type %i.", (int)type);
}
f->sound = sound;
return f;
}
static void destroyPicFrame(fidata_pic_frame_t* f)
{
if(f->type == PFT_XIMAGE)
picFrameDeleteXImage(f);
Z_Free(f);
}
static fidata_pic_frame_t* picAddFrame(fidata_pic_t* p, fidata_pic_frame_t* f)
{
p->frames = Z_Realloc(p->frames, sizeof(*p->frames) * ++p->numFrames, PU_STATIC);
p->frames[p->numFrames-1] = f;
return f;
}
static void objectSetName(fi_object_t* obj, const char* name)
{
dd_snprintf(obj->name, FI_NAME_MAX_LENGTH, "%s", name);
}
finale_t* P_CreateFinale(void)
{
finale_t* f;
finales = Z_Realloc(finales, sizeof(*finales) * ++finalesSize, PU_STATIC);
f = &finales[finalesSize-1];
f->id = finalesUniqueId();
f->_interpreter = P_CreateFinaleInterpreter();
f->_interpreter->_id = f->id;
f->active = true;
return f;
}
void P_DestroyFinale(finale_t* f)
{
assert(f);
{uint i;
for(i = 0; i < finalesSize; ++i)
{
if(&finales[i] != f)
continue;
if(i != finalesSize-1)
memmove(&finales[i], &finales[i+1], sizeof(*finales) * (finalesSize-i));
if(finalesSize > 1)
{
finales = Z_Realloc(finales, sizeof(*finales) * --finalesSize, PU_STATIC);
}
else
{
Z_Free(finales); finales = 0;
finalesSize = 0;
}
break;
}}
}
boolean FI_ScriptRequestSkip(finaleid_t id)
{
finale_t* f;
if(!inited)
Con_Error("FI_ScriptRequestSkip: Not initialized yet!");
f = finalesById(id);
if(!f)
Con_Error("FI_ScriptRequestSkip: Unknown finaleid %u.", id);
return FinaleInterpreter_Skip(f->_interpreter);
}
int FI_ScriptFlags(finaleid_t id)
{
finale_t* f;
if(!inited)
Con_Error("FI_ScriptFlags: Not initialized yet!");
f = finalesById(id);
if(!f)
Con_Error("FI_ScriptFlags: Unknown finaleid %u.", id);
return f->flags;
}
boolean FI_ScriptIsMenuTrigger(finaleid_t id)
{
finale_t* f;
if(!inited)
Con_Error("FI_ScriptIsMenuTrigger: Not initialized yet!");
f = finalesById(id);
if(!f)
Con_Error("FI_ScriptIsMenuTrigger: Unknown finaleid %u.", id);
if(f->active)
{
return FinaleInterpreter_IsMenuTrigger(f->_interpreter);
}
return false;
}
void FIObject_Destructor(fi_object_t* obj)
{
assert(obj);
// Destroy all references to this object on all pages.
{uint i;
for(i = 0; i < numPages; ++i)
FIPage_RemoveObject(pages[i], obj);
}
objectsRemove(&objects, obj);
Z_Free(obj);
}
fidata_pic_t* P_CreatePic(fi_objectid_t id, const char* name)
{
fidata_pic_t* p = Z_Calloc(sizeof(*p), PU_STATIC, 0);
p->type = FI_PIC;
p->drawer = FIData_PicDraw;
p->thinker = FIData_PicThink;
p->id = id;
p->flags.looping = false;
p->animComplete = true;
objectSetName((fi_object_t*)p, name);
AnimatorVector4_Init(p->color, 1, 1, 1, 1);
AnimatorVector3_Init(p->scale, 1, 1, 1);
FIData_PicClearAnimation((fi_object_t*)p);
return p;
}
void P_DestroyPic(fidata_pic_t* pic)
{
assert(pic);
FIData_PicClearAnimation((fi_object_t*)pic);
// Call parent destructor.
FIObject_Destructor((fi_object_t*)pic);
}
fidata_text_t* P_CreateText(fi_objectid_t id, const char* name)
{
#define LEADING (11.f/7-1)
fidata_text_t* t = Z_Calloc(sizeof(*t), PU_STATIC, 0);
t->type = FI_TEXT;
t->drawer = FIData_TextDraw;
t->thinker = FIData_TextThink;
t->id = id;
t->flags.looping = false;
t->animComplete = true;
t->textFlags = DTF_ALIGN_TOPLEFT|DTF_NO_EFFECTS;
objectSetName((fi_object_t*)t, name);
AnimatorVector4_Init(t->color, 1, 1, 1, 1);
AnimatorVector3_Init(t->scale, 1, 1, 1);
t->wait = 3;
t->font = R_CompositeFontNumForName("a");
t->lineHeight = LEADING;
return t;
#undef LEADING
}
void P_DestroyText(fidata_text_t* text)
{
assert(text);
if(text->text)
{
Z_Free(text->text); text->text = 0;
}
// Call parent destructor.
FIObject_Destructor((fi_object_t*)text);
}
void FIObject_Think(fi_object_t* obj)
{
assert(obj);
AnimatorVector3_Think(obj->pos);
AnimatorVector3_Think(obj->scale);
Animator_Think(&obj->angle);
}
boolean FI_ScriptActive(finaleid_t id)
{
finale_t* f;
if(!inited)
{
#ifdef _DEBUG
Con_Printf("FI_ScriptActive: Not initialized yet!\n");
#endif
return false;
}
f = finalesById(id);
if(!f)
Con_Error("FI_ScriptActive: Unknown finaleid %u.", id);
return (f->active != 0);
}
void FI_Init(void)
{
if(inited)
return; // Already been here.
memset(&objects, 0, sizeof(objects));
finales = 0; finalesSize = 0;
pages = 0; numPages = 0;
inited = true;
}
void FI_Shutdown(void)
{
if(!inited)
return; // Huh?
if(finalesSize)
{
uint i;
for(i = 0; i < finalesSize; ++i)
{
finale_t* f = &finales[i];
P_DestroyFinaleInterpreter(f->_interpreter);
}
Z_Free(finales);
}
finales = 0; finalesSize = 0;
// Garbage collection.
objectsEmpty(&objects);
if(numPages)
{
uint i;
for(i = 0; i < numPages; ++i)
{
fi_page_t* p = pages[i];
pageClear(p);
Z_Free(p);
}
Z_Free(pages);
}
pages = 0; numPages = 0;
inited = false;
}
boolean FI_ScriptCmdExecuted(finaleid_t id)
{
finale_t* f;
if(!inited)
{
#ifdef _DEBUG
Con_Printf("FI_ScriptCmdExecuted: Not initialized yet!\n");
#endif
return false;
}
f = finalesById(id);
if(!f)
Con_Error("FI_ScriptCmdExecuted: Unknown finaleid %u.", id);
return FinaleInterpreter_CommandExecuted(f->_interpreter);
}
fi_page_t* FI_NewPage(void)
{
return pagesAdd(newPage());
}
void FI_DeletePage(fi_page_t* p)
{
if(!p) Con_Error("FI_DeletePage: Invalid page.");
pageClear(p);
pagesRemove(p);
Z_Free(p);
}
finaleid_t FI_Execute(const char* scriptSrc, int flags)
{
if(!inited)
{
#ifdef _DEBUG
Con_Printf("FI_Execute: Not initialized yet!\n");
#endif
return 0;
}
if(!scriptSrc || !scriptSrc[0])
{
#ifdef _DEBUG
Con_Printf("FI_Execute: Warning, attempt to play empty script.\n");
#endif
return 0;
}
if((flags & FF_LOCAL) && isDedicated)
{ // Dedicated servers do not play local Finales.
#ifdef _DEBUG
Con_Printf("FI_Execute: No local finales in dedicated mode.\n");
#endif
return 0;
}
{finale_t* f = P_CreateFinale();
f->flags = flags;
FinaleInterpreter_LoadScript(f->_interpreter, scriptSrc);
if(!(flags & FF_LOCAL) && isServer)
{ // Instruct clients to start playing this Finale.
Sv_Finale(FINF_BEGIN|FINF_SCRIPT, scriptSrc);
}
#ifdef _DEBUG
Con_Printf("Finale Begin - id:%u '%.30s'\n", f->id, scriptSrc);
#endif
return f->id; }
}
void FI_ScriptTerminate(finaleid_t id)
{
finale_t* f;
if(!inited)
{
#ifdef _DEBUG
Con_Printf("FI_ScriptTerminate: Not initialized yet!\n");
#endif
return;
}
f = finalesById(id);
if(!f)
Con_Error("FI_ScriptTerminate: Unknown finaleid %u.", id);
if(f->active)
{
stopFinale(f);
P_DestroyFinale(f);
}
}
fi_object_t* FI_Object(fi_objectid_t id)
{
if(!inited)
{
#ifdef _DEBUG
Con_Printf("FI_Object: Not initialized yet!\n");
#endif
return 0;
}
return objectsById(&objects, id);
}
fi_object_t* FI_NewObject(fi_obtype_e type, const char* name)
{
fi_object_t* obj;
switch(type)
{
case FI_TEXT: obj = (fi_object_t*) P_CreateText(objectsUniqueId(&objects), name); break;
case FI_PIC: obj = (fi_object_t*) P_CreatePic(objectsUniqueId(&objects), name); break;
default:
Con_Error("FI_NewObject: Unknown type %i.", type);
}
return objectsAdd(&objects, obj);
}
void FI_DeleteObject(fi_object_t* obj)
{
assert(obj);
if(!inited)
{
#ifdef _DEBUG
Con_Printf("FI_DeleteObject: Not initialized yet!\n");
#endif
return;
}
switch(obj->type)
{
case FI_PIC: P_DestroyPic((fidata_pic_t*)obj); break;
case FI_TEXT: P_DestroyText((fidata_text_t*)obj); break;
default:
Con_Error("FI_DeleteObject: Invalid type %i.", (int) obj->type);
}
}
static void useColor(const animator_t* color, int components)
{
if(components == 3)
{
glColor3f(color[0].value, color[1].value, color[2].value);
}
else if(components == 4)
{
glColor4f(color[0].value, color[1].value, color[2].value, color[3].value);
}
}
void FIPage_Drawer(fi_page_t* p)
{
if(!p) Con_Error("FIPage_Drawer: Invalid page.");
if(p->flags.hidden)
return;
// First, draw the background.
if(p->_bgMaterial)
{
useColor(p->_bgColor, 4);
DGL_SetMaterial(p->_bgMaterial);
DGL_DrawRectTiled(0, 0, SCREENWIDTH, SCREENHEIGHT, 64, 64);
}
else if(p->_bgColor[3].value > 0)
{
// Just clear the screen, then.
DGL_Disable(DGL_TEXTURING);
DGL_DrawRect(0, 0, SCREENWIDTH, SCREENHEIGHT, p->_bgColor[0].value, p->_bgColor[1].value, p->_bgColor[2].value, p->_bgColor[3].value);
DGL_Enable(DGL_TEXTURING);
}
// Now lets go into 3D mode for drawing the p objects.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
GL_SetMultisample(true);
// The 3D projection matrix.
// We're assuming pixels are squares.
{float aspect = theWindow->width / (float) theWindow->height;
yfov = 2 * RAD2DEG(atan(tan(DEG2RAD(90) / 2) / aspect));
GL_InfinitePerspective(yfov, aspect, .05f);}
// We need a left-handed yflipped coordinate system.
glScalef(1, -1, -1);
// Clear Z buffer (prevent the objects being clipped by nearby polygons).
glClear(GL_DEPTH_BUFFER_BIT);
if(renderWireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//glEnable(GL_CULL_FACE);
glEnable(GL_ALPHA_TEST);
{vec3_t worldOrigin;
V3_Set(worldOrigin, -SCREENWIDTH/2 - p->_offset[VX].value, -SCREENHEIGHT/2 - p->_offset[VY].value, .05f - p->_offset[VZ].value);
objectsDraw(&p->_objects, FI_NONE/* treated as 'any' */, worldOrigin);
/*{rendmodelparams_t params;
memset(¶ms, 0, sizeof(params));
glEnable(GL_DEPTH_TEST);
worldOrigin[VY] += 50.f / SCREENWIDTH * (40);
worldOrigin[VZ] += 20; // Suitable default?
setupModelParamsForFIObject(¶ms, "testmodel", worldOrigin);
Rend_RenderModel(¶ms);
worldOrigin[VX] -= 160.f / SCREENWIDTH * (40);
setupModelParamsForFIObject(¶ms, "testmodel", worldOrigin);
Rend_RenderModel(¶ms);
worldOrigin[VX] += 320.f / SCREENWIDTH * (40);
setupModelParamsForFIObject(¶ms, "testmodel", worldOrigin);
Rend_RenderModel(¶ms);
glDisable(GL_DEPTH_TEST);}*/
}
// Restore original matrices and state: back to normal 2D.
glDisable(GL_ALPHA_TEST);
//glDisable(GL_CULL_FACE);
// Back from wireframe mode?
if(renderWireframe)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// Filter on top of everything. Only draw if necessary.
if(p->_filter[3].value > 0)
{
DGL_Disable(DGL_TEXTURING);
useColor(p->_filter, 4);
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(SCREENWIDTH, 0);
glVertex2f(SCREENWIDTH, SCREENHEIGHT);
glVertex2f(0, SCREENHEIGHT);
glEnd();
DGL_Enable(DGL_TEXTURING);
}
GL_SetMultisample(false);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
void FIPage_MakeVisible(fi_page_t* p, boolean yes)
{
if(!p) Con_Error("FIPage_MakeVisible: Invalid page.");
p->flags.hidden = !yes;
}
void FIPage_Pause(fi_page_t* p, boolean yes)
{
if(!p) Con_Error("FIPage_Pause: Invalid page.");
p->flags.paused = yes;
}
void FIPage_RunTic(fi_page_t* p, timespan_t ticLength)
{
if(!p) Con_Error("FIPage_RunTic: Invalid page.");
if(!M_CheckTrigger(&sharedFixedTrigger, ticLength))
return;
// A new 'sharp' tick has begun.
p->_timer++;
objectsThink(&p->_objects);
AnimatorVector3_Think(p->_offset);
AnimatorVector4_Think(p->_bgColor);
AnimatorVector4_Think(p->_filter);
{uint i;
for(i = 0; i < 9; ++i)
AnimatorVector3_Think(p->_textColor[i]);
}
}
boolean FIPage_HasObject(fi_page_t* p, fi_object_t* obj)
{
if(!p) Con_Error("FIPage_HasObject: Invalid page.");
return objectsIsPresent(&p->_objects, obj);
}
fi_object_t* FIPage_AddObject(fi_page_t* p, fi_object_t* obj)
{
if(!p) Con_Error("FIPage_AddObject: Invalid page.");
if(obj && !objectsIsPresent(&p->_objects, obj))
{
return objectsAdd(&p->_objects, obj);
}
return obj;
}
fi_object_t* FIPage_RemoveObject(fi_page_t* p, fi_object_t* obj)
{
if(!p) Con_Error("FIPage_RemoveObject: Invalid page.");
if(obj && objectsIsPresent(&p->_objects, obj))
{
return objectsRemove(&p->_objects, obj);
}
return obj;
}
material_t* FIPage_Background(fi_page_t* p)
{
if(!p) Con_Error("FIPage_Background: Invalid page.");
return p->_bgMaterial;
}
void FIPage_SetBackground(fi_page_t* p, material_t* mat)
{
if(!p) Con_Error("FIPage_SetBackground: Invalid page.");
p->_bgMaterial = mat;
}
void FIPage_SetBackgroundColor(fi_page_t* p, float red, float green, float blue, int steps)
{
if(!p) Con_Error("FIPage_SetBackgroundColor: Invalid page.");
AnimatorVector3_Set(p->_bgColor, red, green, blue, steps);
}
void FIPage_SetBackgroundColorAndAlpha(fi_page_t* p, float red, float green, float blue, float alpha, int steps)
{
if(!p) Con_Error("FIPage_SetBackgroundColorAndAlpha: Invalid page.");
AnimatorVector4_Set(p->_bgColor, red, green, blue, alpha, steps);
}
void FIPage_SetOffsetX(fi_page_t* p, float x, int steps)
{
if(!p) Con_Error("FIPage_SetOffsetX: Invalid page.");
Animator_Set(&p->_offset[VX], x, steps);
}
void FIPage_SetOffsetY(fi_page_t* p, float y, int steps)
{
if(!p) Con_Error("FIPage_SetOffsetY: Invalid page.");
Animator_Set(&p->_offset[VY], y, steps);
}
void FIPage_SetOffsetZ(fi_page_t* p, float y, int steps)
{
if(!p) Con_Error("FIPage_SetOffsetY: Invalid page.");
Animator_Set(&p->_offset[VZ], y, steps);
}
void FIPage_SetOffsetXYZ(fi_page_t* p, float x, float y, float z, int steps)
{
if(!p) Con_Error("FIPage_SetOffsetXYZ: Invalid page.");
AnimatorVector3_Set(p->_offset, x, y, z, steps);
}
void FIPage_SetFilterColorAndAlpha(fi_page_t* p, float red, float green, float blue, float alpha, int steps)
{
if(!p) Con_Error("FIPage_SetFilterColorAndAlpha: Invalid page.");
AnimatorVector4_Set(p->_filter, red, green, blue, alpha, steps);
}
void FIPage_SetPredefinedColor(fi_page_t* p, uint idx, float red, float green, float blue, int steps)
{
if(!p) Con_Error("FIPage_SetPredefinedColor: Invalid page.");
AnimatorVector3_Set(p->_textColor[idx], red, green, blue, steps);
}
void FI_Ticker(timespan_t ticLength)
{
// Always tic.
R_TextTicker(ticLength);
/*if(!inited)
{
#ifdef _DEBUG
Con_Printf("FI_Ticker: Not initialized yet!\n");
#endif
return;
}*/
// All pages tic unless paused.
{ uint i;
for(i = 0; i < numPages; ++i)
{
FIPage_RunTic(pages[i], ticLength);
}}
if(!M_CheckTrigger(&sharedFixedTrigger, ticLength))
return;
// A new 'sharp' tick has begun.
// All finales tic unless inactive.
{ uint i;
for(i = 0; i < finalesSize; ++i)
{
finale_t* f = &finales[i];
if(!f->active)
continue;
if(FinaleInterpreter_RunTic(f->_interpreter))
{ // The script has ended!
stopFinale(f);
P_DestroyFinale(f);
}
}}
}
void FI_ScriptSuspend(finaleid_t id)
{
finale_t* f;
if(!inited)
Con_Error("FI_ScriptSuspend: Not initialized yet!");
f = finalesById(id);
if(!f)
Con_Error("FI_ScriptSuspend: Unknown finaleid %u.", id);
f->active = false;
FinaleInterpreter_Suspend(f->_interpreter);
}