summaryrefslogtreecommitdiff
path: root/src/uqm/credits.c
blob: 188948488fff4312f1efd12c71a0250418103c69 (plain)
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
//Copyright Paul Reiche, Fred Ford. 1992-2002

/*
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "credits.h"

#include "controls.h"
#include "colors.h"
#include "options.h"
#include "oscill.h"
#include "comm.h"
#include "resinst.h"
#include "nameref.h"
#include "settings.h"
#include "sounds.h"
#include "setup.h"
#include "libs/graphics/drawable.h"
#include <math.h>

// Rates in pixel lines per second
#define CREDITS_BASE_RATE   9
#define CREDITS_MAX_RATE    130
// Maximum frame rate
#define CREDITS_FRAME_RATE  36

#define CREDITS_TIMEOUT   (ONE_SECOND * 5)

#define TRANS_COLOR   BRIGHT_BLUE_COLOR

// Positive or negative scroll rate in pixel lines per second
static int CreditsRate;

static BOOLEAN OutTakesRunning;
static BOOLEAN CreditsRunning;
static STRING CreditsTab;
static FRAME CreditsBack;

// Context used for drawing to the screen
static CONTEXT DrawContext;
// Context used for pre-rendering a credits frame
static CONTEXT LocalContext;
// Pre-rendered frame, possibly with a cutout
static FRAME CreditsFrame;
// Size of the credits "window" (normally screen size)
static EXTENT CreditsExtent;

typedef struct
{
	FRAME frame;
	int strIndex;
} CreditTextFrame;

#define MAX_CREDIT_FRAMES 32
// Circular text frame buffer for scrolling
// Text frames are generated as needed, and when a text frame scrolls
// of the screen, it is destroyed
static CreditTextFrame textFrames[MAX_CREDIT_FRAMES];
// Index of first active frame in the circular buffer (the first frame
// is the one on top)
static int firstFrame;
// Index of last active frame in the circular buffer + 1
static int lastFrame;
// Total height of all active frames in the circular buffer
static int totalHeight;
// Current vertical offset into the first text frame
static int curFrameOfs;

typedef struct
{
	int size;
	RESOURCE res;
	FONT font;
} FONT_SIZE_DEF;

static FONT_SIZE_DEF CreditsFont[] =
{
	{ 13, PT13AA_FONT, 0 },
	{ 17, PT17AA_FONT, 0 },
	{ 45, PT45AA_FONT, 0 },
	{  0,           0, 0 },
};


static FRAME
Credits_MakeTransFrame (int w, int h, Color TransColor)
{
	FRAME OldFrame;
	FRAME f;

	f = CaptureDrawable (CreateDrawable (WANT_PIXMAP, w, h, 1));
	SetFrameTransparentColor (f, TransColor);

	OldFrame = SetContextFGFrame (f);
	SetContextBackGroundColor (TransColor);
	ClearDrawable ();
	SetContextFGFrame (OldFrame);

	return f;
}

static int
ParseTextLines (TEXT *Lines, int MaxLines, char *Buffer)
{
	int i;
	const char* pEnd = Buffer + strlen (Buffer);

	for (i = 0; i < MaxLines && Buffer < pEnd; ++i, ++Lines)
	{
		char* pTerm = strchr (Buffer, '\n');
		if (!pTerm)
			pTerm = Buffer + strlen (Buffer);
		*pTerm = '\0'; /* terminate string */
		Lines->pStr = Buffer;
		Lines->CharCount = ~0;
		Buffer = pTerm + 1;
	}
	return i;
}

#define MAX_TEXT_LINES 50
#define MAX_TEXT_COLS  5

static FRAME
Credits_RenderTextFrame (CONTEXT TempContext, int *istr, int dir,
		Color BackColor, Color ForeColor)
{
	FRAME f;
	CONTEXT OldContext;
	FRAME OldFrame;
	TEXT TextLines[MAX_TEXT_LINES];
	char *pStr = NULL;
	int size;
	char salign[32];
	char *scol;
	int scaned;
	int i, rows, cnt;
	char buf[2048];
	FONT_SIZE_DEF *fdef;
	SIZE leading;
	TEXT t;
	RECT r;
	typedef struct
	{
		TEXT_ALIGN align;
		COORD basex;
	} col_format_t;
	col_format_t colfmt[MAX_TEXT_COLS];

	if (*istr < 0 || *istr >= GetStringTableCount (CreditsTab))
	{	// check if next one is within range
		int next_s = *istr + dir;

		if (next_s < 0 || next_s >= GetStringTableCount (CreditsTab))
			return 0;

		*istr = next_s;
	}

	// skip empty lines
	while (*istr >= 0 && *istr < GetStringTableCount (CreditsTab))
	{
		pStr = GetStringAddress (
				SetAbsStringTableIndex (CreditsTab, *istr));
		*istr += dir;
		if (pStr && *pStr != '\0')
			break;
	}

	if (!pStr || *pStr == '\0')
		return 0;
	
	if (2 != sscanf (pStr, "%d %31s %n", &size, salign, &scaned)
			|| size <= 0)
		return 0;
	pStr += scaned;
	
	utf8StringCopy (buf, sizeof (buf), pStr);
	rows = ParseTextLines (TextLines, MAX_TEXT_LINES, buf);
	if (rows == 0)
		return 0;
	// parse text columns
	for (i = 0, cnt = rows; i < rows; ++i)
	{
		char *nextcol;
		int icol;

		// we abuse the baseline here, but only a tiny bit
		// every line starts at col 0
		TextLines[i].baseline.x = 0;
		TextLines[i].baseline.y = i + 1;

		for (icol = 1, nextcol = strchr (TextLines[i].pStr, '\t');
				icol < MAX_TEXT_COLS && nextcol;
				++icol, nextcol = strchr (nextcol, '\t'))
		{
			*nextcol = '\0';
			++nextcol;

			if (cnt < MAX_TEXT_LINES)
			{
				TextLines[cnt].pStr = nextcol;
				TextLines[cnt].CharCount = ~0;
				TextLines[cnt].baseline.x = icol;
				TextLines[cnt].baseline.y = i + 1;
				++cnt;
			}
		}
	}

	// init alignments
	for (i = 0; i < MAX_TEXT_COLS; ++i)
	{
		colfmt[i].align = ALIGN_LEFT;
		colfmt[i].basex = CreditsExtent.width / 64;
	}

	// find the right font
	for (fdef = CreditsFont; fdef->size && size > fdef->size; ++fdef)
		;
	if (!fdef->size)
		return 0;

	t.align = ALIGN_LEFT;
	t.baseline.x = 100; // any value will do
	t.baseline.y = 100; // any value will do
	t.pStr = " ";
	t.CharCount = 1;

	OldContext = SetContext (TempContext);

	// get font dimensions
	SetContextFont (fdef->font);
	GetContextFontLeading (&leading);
	// get left/right margin
	TextRect (&t, &r, NULL);

	// parse text column alignment
	for (i = 0, scol = strtok (salign, ",");
			scol && i < MAX_TEXT_COLS;
			++i, scol = strtok (NULL, ","))
	{
		char c;
		int x;
		int n;

		// default
		colfmt[i].align = ALIGN_LEFT;
		colfmt[i].basex = r.extent.width;

		n = sscanf (scol, "%c/%d", &c, &x);
		if (n < 1)
		{	// DOES NOT COMPUTE! :)
			continue;
		}

		switch (c)
		{
			case 'L':
				colfmt[i].align = ALIGN_LEFT;
				if (n >= 2)
					colfmt[i].basex = x;
				break;
			case 'C':
				colfmt[i].align = ALIGN_CENTER;
				if (n >= 2)
					colfmt[i].basex = x;
				else
					colfmt[i].basex = CreditsExtent.width / 2;
				break;
			case 'R':
				colfmt[i].align = ALIGN_RIGHT;
				if (n >= 2)
					colfmt[i].basex = x;
				else
					colfmt[i].basex = CreditsExtent.width - r.extent.width;
				break;
		}
	}

	for (i = 0; i < cnt; ++i)
	{	
		// baseline contains coords in row/col quantities
		col_format_t *fmt = colfmt + TextLines[i].baseline.x;
		
		TextLines[i].align = fmt->align;
		TextLines[i].baseline.x = fmt->basex;
		TextLines[i].baseline.y *= leading;
	}

	f = Credits_MakeTransFrame (CreditsExtent.width, leading * rows + (leading >> 1),
			BackColor);
	OldFrame = SetContextFGFrame (f);
	// draw text
	SetContextForeGroundColor (ForeColor);
	for (i = 0; i < cnt; ++i)
		font_DrawText (TextLines + i);
	
	SetContextFGFrame (OldFrame);
	SetContext (OldContext);

	return f;
}

static inline int
frameIndex (int index)
{
	// Make sure index is positive before %
	return (index + MAX_CREDIT_FRAMES) % MAX_CREDIT_FRAMES;
}

static void
RenderCreditsScreen (CONTEXT targetContext)
{
	CONTEXT oldContext;
	STAMP s;
	int i;

	oldContext = SetContext (targetContext);
	// draw background
	s.origin.x = 0;
	s.origin.y = 0;
	s.frame = CreditsBack;
	DrawStamp (&s);
	
	// draw text frames
	s.origin.y = -curFrameOfs;
	for (i = firstFrame; i != lastFrame; i = frameIndex (i + 1))
	{
		RECT fr;

		s.frame = textFrames[i].frame;
		DrawStamp (&s);
		GetFrameRect (s.frame, &fr);
		s.origin.y += fr.extent.height;
	}

	if (OutTakesRunning)
	{	// Cut out the Outtakes rect
		SetContextForeGroundColor (TRANS_COLOR);
		DrawFilledRectangle (&CommWndRect);
	}
	
	SetContext (oldContext);
}

static void
InitCredits (void)
{
	RECT ctxRect;
	CONTEXT oldContext;
	FRAME targetFrame;

	memset (textFrames, 0, sizeof textFrames);

	LocalContext = CreateContext ("Credits.LocalContext");
	DrawContext = CreateContext ("Credits.DrawContext");

	targetFrame = GetContextFGFrame ();
	GetContextClipRect (&ctxRect);
	CreditsExtent = ctxRect.extent;

	// prep our local context
	oldContext = SetContext (LocalContext);
	// Local screen copy. We draw everything to this frame, then cut
	// the Outtakes rect out and draw this frame to the screen.
	CreditsFrame = Credits_MakeTransFrame (CreditsExtent.width,
			CreditsExtent.height, TRANS_COLOR);
	SetContextFGFrame (CreditsFrame);
	
	// The first credits frame is fake, the height of the screen,
	// so that the credits can roll in from the bottom
	textFrames[0].frame = Credits_MakeTransFrame (1, CreditsExtent.height,
			TRANS_COLOR);
	textFrames[0].strIndex = -1;
	firstFrame = 0;
	lastFrame = firstFrame + 1;

	totalHeight = GetFrameHeight (textFrames[0].frame);
	curFrameOfs = 0;
	
	// We use an own screen draw context to avoid collisions
	SetContext (DrawContext);
	SetContextFGFrame (targetFrame);

	SetContext (oldContext);

	// Prepare the first screen frame
	RenderCreditsScreen (LocalContext);

	CreditsRate = CREDITS_BASE_RATE;
	CreditsRunning = TRUE;
}

static void
freeCreditTextFrame (CreditTextFrame *tf)
{
	DestroyDrawable (ReleaseDrawable (tf->frame));
	tf->frame = NULL;
}

static void
UninitCredits (void)
{
	DestroyContext (DrawContext);
	DrawContext = NULL;
	DestroyContext (LocalContext);
	LocalContext = NULL;

	// free remaining frames
	DestroyDrawable (ReleaseDrawable (CreditsFrame));
	CreditsFrame = NULL;
	for ( ; firstFrame != lastFrame; firstFrame = frameIndex (firstFrame + 1))
		freeCreditTextFrame (&textFrames[firstFrame]);
}

static int
calcDeficitHeight (void)
{
	int i;
	int maxPos;

	maxPos = -curFrameOfs;
	for (i = firstFrame; i != lastFrame; i = frameIndex (i + 1))
	{
		RECT fr;

		GetFrameRect (textFrames[i].frame, &fr);
		maxPos += fr.extent.height;
	}

	return CreditsExtent.height - maxPos;
}

static void
processCreditsFrame (void)
{
	static TimeCount NextTime;
	TimeCount Now = GetTimeCounter ();

	if (Now >= NextTime)
	{
		RECT fr;
		CONTEXT OldContext;
		int rate, direction, dirstep;
		int deficitHeight;
		STAMP s;

		rate = abs (CreditsRate);
		if (rate != 0)
		{
			// scroll direction; forward or backward
			direction = CreditsRate / rate;
			// step in pixels
			dirstep = (rate + CREDITS_FRAME_RATE - 1) / CREDITS_FRAME_RATE;
			rate = ONE_SECOND * dirstep / rate;
			// step is also directional
			dirstep *= direction;
		}
		else
		{	// scroll stopped
			direction = 0;
			dirstep = 0;
			// one second interframe
			rate = ONE_SECOND;
		}

		NextTime = GetTimeCounter () + rate;

		// draw the credits
		//  comm animations play with contexts so we need to make
		//  sure the context is not desynced
		s.origin.x = 0;
		s.origin.y = 0;
		s.frame = CreditsFrame;

		OldContext = SetContext (DrawContext);
		DrawStamp (&s);
		SetContext (OldContext);
		FlushGraphics ();

		// prepare next screen frame
		deficitHeight = calcDeficitHeight ();
		curFrameOfs += dirstep;
		// cap scroll
		if (curFrameOfs < -(CreditsExtent.height / 20))
		{	// at the begining, deceleration
			if (CreditsRate < 0)
				CreditsRate -= CreditsRate / 10 - 1;
		}
		else if (deficitHeight > CreditsExtent.height / 25)
		{	// frame deficit -- credits almost over, deceleration
			if (CreditsRate > 0)
				CreditsRate -= CreditsRate / 10 + 1;

			CreditsRunning = (CreditsRate != 0);
		}
		else if (!CreditsRunning)
		{	// resumed
			CreditsRunning = TRUE;
		}

		if (firstFrame != lastFrame)
		{	// clean up frames that scrolled off the screen
			if (direction > 0)
			{	// forward scroll
				GetFrameRect (textFrames[firstFrame].frame, &fr);
				if (curFrameOfs >= fr.extent.height)
				{	// past this frame already
					totalHeight -= fr.extent.height;
					freeCreditTextFrame (&textFrames[firstFrame]);
					// next frame
					firstFrame = frameIndex (firstFrame + 1);
					curFrameOfs -= fr.extent.height;
				}
			}
			else if (direction < 0)
			{	// backward scroll
				int index = frameIndex (lastFrame - 1);
				int framePos;

				GetFrameRect (textFrames[index].frame, &fr);
				framePos = totalHeight - curFrameOfs - fr.extent.height;
				if (framePos >= CreditsExtent.height)
				{	// past this frame already
					lastFrame = index;
					totalHeight -= fr.extent.height;
					freeCreditTextFrame (&textFrames[lastFrame]);
				}
			}
		}

		// render new text frames if needed
		if (direction > 0)
		{	// forward scroll
			int next_s = 0;

			// get next string
			if (firstFrame != lastFrame)
				next_s = textFrames[frameIndex (lastFrame - 1)].strIndex + 1;

			while (totalHeight - curFrameOfs < CreditsExtent.height
					&& next_s < GetStringTableCount (CreditsTab))
			{
				CreditTextFrame *tf = &textFrames[lastFrame];

				tf->frame = Credits_RenderTextFrame (LocalContext, &next_s,
						direction, BLACK_COLOR, CREDITS_TEXT_COLOR);
				tf->strIndex = next_s - 1;
				if (tf->frame)
				{
					GetFrameRect (tf->frame, &fr);
					totalHeight += fr.extent.height;
					
					lastFrame = frameIndex (lastFrame + 1);
				}
			}
		}
		else if (direction < 0)
		{	// backward scroll
			int next_s = GetStringTableCount (CreditsTab) - 1;

			// get next string
			if (firstFrame != lastFrame)
				next_s = textFrames[firstFrame].strIndex - 1;
			
			while (curFrameOfs < 0 && next_s >= 0)
			{
				int index = frameIndex (firstFrame - 1);
				CreditTextFrame *tf = &textFrames[index];

				tf->frame = Credits_RenderTextFrame (LocalContext, &next_s,
						direction, BLACK_COLOR, CREDITS_TEXT_COLOR);
				tf->strIndex = next_s + 1;
				if (tf->frame)
				{
					GetFrameRect (tf->frame, &fr);
					totalHeight += fr.extent.height;
					
					firstFrame = index;
					curFrameOfs += fr.extent.height;
				}
			}
		}

		// draw next screen frame
		RenderCreditsScreen (LocalContext);
	}
}

static BOOLEAN
LoadCredits (void)
{
	FONT_SIZE_DEF *fdef;

	CreditsTab = CaptureStringTable (LoadStringTable (CREDITS_STRTAB));
	if (!CreditsTab)
		return FALSE;
	CreditsBack = CaptureDrawable (LoadGraphic (CREDITS_BACK_ANIM));
	// load fonts
	for (fdef = CreditsFont; fdef->size; ++fdef)
		fdef->font = LoadFont (fdef->res);

	return TRUE;
}

static void
FreeCredits (void)
{
	FONT_SIZE_DEF *fdef;

	DestroyStringTable (ReleaseStringTable (CreditsTab));
	CreditsTab = NULL;
	
	DestroyDrawable (ReleaseDrawable (CreditsBack));
	CreditsBack = NULL;

	// free fonts
	for (fdef = CreditsFont; fdef->size; ++fdef)
	{
		DestroyFont (fdef->font);
		fdef->font = NULL;
	}
}

static void
OutTakes (void)
{
#define NUM_OUTTAKES 15
	static CONVERSATION outtake_list[NUM_OUTTAKES] =
	{
		ZOQFOTPIK_CONVERSATION,
		TALKING_PET_CONVERSATION,
		ORZ_CONVERSATION,
		UTWIG_CONVERSATION,
		THRADD_CONVERSATION,
		SUPOX_CONVERSATION,
		SYREEN_CONVERSATION,
		SHOFIXTI_CONVERSATION,
		PKUNK_CONVERSATION,
		YEHAT_CONVERSATION,
		DRUUGE_CONVERSATION,
		URQUAN_CONVERSATION,
		VUX_CONVERSATION,
		BLACKURQ_CONVERSATION,
		ARILOU_CONVERSATION
	};

	BOOLEAN oldsubtitles = optSubtitles;
	int i = 0;

	// Outtakes have no voice tracks, so the subtitles are always on
	optSubtitles = TRUE;
	sliderDisabled = TRUE;
	oscillDisabled = TRUE;

	for (i = 0; (i < NUM_OUTTAKES) &&
			!(GLOBAL (CurrentActivity) & CHECK_ABORT); i++)
	{
		SetCommIntroMode (CIM_CROSSFADE_WINDOW, 0);
		InitCommunication (outtake_list[i]);
	}

	optSubtitles = oldsubtitles;
	sliderDisabled = FALSE;
	oscillDisabled = FALSE;
}

typedef struct
{
	// standard state required by DoInput
	BOOLEAN (*InputFunc) (void *pInputState);

	BOOLEAN AllowCancel;
	BOOLEAN AllowSpeedChange;
	BOOLEAN CloseWhenDone;
	DWORD CloseTimeOut;

} CREDITS_INPUT_STATE;

static BOOLEAN
DoCreditsInput (void *pIS)
{
	CREDITS_INPUT_STATE *pCIS = (CREDITS_INPUT_STATE *) pIS;

	if (CreditsRunning)
	{	// cancel timeout if resumed (or just running)
		pCIS->CloseTimeOut = 0;
	}

	if ((GLOBAL (CurrentActivity) & CHECK_ABORT)
			|| (pCIS->AllowCancel &&
				(PulsedInputState.menu[KEY_MENU_SELECT] ||
				PulsedInputState.menu[KEY_MENU_CANCEL]))
		)
	{	// aborted
		return FALSE;
	}
	
	if (pCIS->AllowSpeedChange
			&& (PulsedInputState.menu[KEY_MENU_UP]
			|| PulsedInputState.menu[KEY_MENU_DOWN]))
	{	// speed adjustment
		int newrate = CreditsRate;
		int step = abs (CreditsRate) / 5 + 1;

		if (PulsedInputState.menu[KEY_MENU_DOWN])
			newrate += step;
		else if (PulsedInputState.menu[KEY_MENU_UP])
			newrate -= step;
		if (newrate < -CREDITS_MAX_RATE)
			newrate = -CREDITS_MAX_RATE;
		else if (newrate > CREDITS_MAX_RATE)
			newrate = CREDITS_MAX_RATE;
			
		CreditsRate = newrate;
	}

	if (!CreditsRunning)
	{	// always allow cancelling once credits run through
		pCIS->AllowCancel = TRUE;
	}

	if (!CreditsRunning && pCIS->CloseWhenDone)
	{	// auto-close controlled by timeout
		if (pCIS->CloseTimeOut == 0)
		{	// set timeout
			pCIS->CloseTimeOut = GetTimeCounter () + CREDITS_TIMEOUT;
		}
		else if (GetTimeCounter () > pCIS->CloseTimeOut)
		{	// all done!
			return FALSE;
		}
	}
	
	if (!CreditsRunning
			&& (PulsedInputState.menu[KEY_MENU_SELECT]
			|| PulsedInputState.menu[KEY_MENU_CANCEL]))
	{	// credits finished and exit requested
		return FALSE;
	}

	SleepThread (ONE_SECOND / CREDITS_FRAME_RATE);

	return TRUE;
}

static void
on_input_frame (void)
{
	processCreditsFrame ();
}

void
Credits (BOOLEAN WithOuttakes)
{
	MUSIC_REF hMusic;
	CREDITS_INPUT_STATE cis;
	RECT screenRect;
	STAMP s;

	hMusic = LoadMusic (CREDITS_MUSIC);

	SetContext (ScreenContext);
	SetContextClipRect (NULL);
	GetContextClipRect (&screenRect);
	SetContextBackGroundColor (BLACK_COLOR);
	ClearDrawable ();

	if (!LoadCredits ())
		return;

	// Fade in the background
	s.origin.x = 0;
	s.origin.y = 0;
	s.frame = CreditsBack;
	DrawStamp (&s);
	FadeScreen (FadeAllToColor, ONE_SECOND / 2);

	// set the position of outtakes comm
	CommWndRect.corner.x = (screenRect.extent.width - CommWndRect.extent.width)
			/ 2;
	CommWndRect.corner.y = 5;
	
	InitCredits ();
	SetInputCallback (on_input_frame);

	if (WithOuttakes)
	{
		OutTakesRunning = TRUE;
		OutTakes ();
		OutTakesRunning = FALSE;
	}
	
	if (!(GLOBAL (CurrentActivity) & CHECK_ABORT))
	{
		if (hMusic)
			PlayMusic (hMusic, TRUE, 1);

		// nothing to do now but wait until credits
		//  are done or canceled by user
		cis.InputFunc = DoCreditsInput;
		cis.AllowCancel = !WithOuttakes;
		cis.CloseWhenDone = !WithOuttakes;
		cis.AllowSpeedChange = TRUE;
		SetMenuSounds (MENU_SOUND_NONE, MENU_SOUND_NONE);
		DoInput (&cis, TRUE);
	}

	SetInputCallback (NULL);
	FadeMusic (0, ONE_SECOND / 2);
	UninitCredits ();

	SetContext (ScreenContext);
	SleepThreadUntil (FadeScreen (FadeAllToBlack, ONE_SECOND / 2));
	FlushColorXForms ();

	if (hMusic)
	{
		StopMusic ();
		DestroyMusic (hMusic);
	}
	FadeMusic (NORMAL_VOLUME, 0);

	FreeCredits ();
}