Debian Codegraph @

Descent3/credits.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/credits.cpp $
21 * $Revision: 17 $
22 * $Date: 10/05/99 3:00p $
23 * $Author: Kevin $
24 *
25 * Credits system
26 *
27 * $Log: /DescentIII/main/credits.cpp $
28 *
29 * 17 10/05/99 3:00p Kevin
30 * No longer display static credits for demo
31 *
32 * 16 5/13/99 6:41p Samir
33 * don't reset mouse when ending credits. causes problems.
34 *
35 * 15 5/08/99 1:43p Jason
36 * made credits scroll a little faster
37 *
38 * 14 5/05/99 7:38p Jason
39 * added scrolling credits system
40 *
41 * 13 5/02/99 12:08a Jason
42 * changes to credits
43 *
44 * 12 4/30/99 5:42p Jason
45 * more updates to credits system
46 *
47 * 11 4/29/99 11:57a Samir
48 * added music.
49 *
50 * 10 4/19/99 12:46p Jason
51 * fixed credits
52 *
53 * 9 4/17/99 3:29p Jason
54 * first pass at credit system
55 *
56 * 8 4/15/99 2:32p Kevin
57 * Added some code for the Demo
58 *
59 * 7 4/14/99 4:19a Jeff
60 * more case mismatch fixes in #includes
61 *
62 * 6 4/13/99 12:24p Jason
63 * made credits warp faster
64 *
65 * 5 4/02/99 5:47p Jeff
66 * text effect in
67 *
68 * 4 4/02/99 6:03a Jeff
69 * testing a new text effect...not done yet
70 *
71 * 3 3/31/99 3:04p Jeff
72 * added _real_ temporary credits system...
73 *
74 * 2 3/31/99 12:11p Jeff
75 * added empty credits file
76 *
77 * $NoKeywords: $
78 */
79
80 #include "mono.h"
81 #include "pstypes.h"
82 #include "ddio.h"
83 #include "grtext.h"
84 #include "renderer.h"
85 #include "gamefont.h"
86 #include "game.h"
87 #include "bitmap.h"
88 #include "descent.h"
89 #include "mem.h"
90 #include "3d.h"
91 #include "d3music.h"
92 #include "hlsoundlib.h"
93 #include <string.h>
94 #include <math.h>
95 #include "psrand.h"
96
97 #if defined(POSIX)
98 #include "linux_fix.h"
99 #endif
100
101 /*
102 $$TABLE_GAMEFILE "GameCredits.txt"
103 $$TABLE_GAMEFILE "credits.omf"
104 */
105
106 // music regions for credits
107 #define CREDITS1_MUSIC_REGION 0
108
109 // other stuff
110 #define CLTYPE_HEADING 0
111 #define CLTYPE_TEXT 1
112 #define CLTYPE_END 2
113 #define CLTYPE_BLANK 3
114
115 struct creditline {
116 ddgr_color color;
117 uint8_t type;
118 char *text;
119 creditline *next;
120 uint16_t startx, starty, endx, endy;
121 float displaytime;
122 };
123
124 static void Credits_Render(creditline *, float);
125 static creditline Creditlines;
126
127 static ddgr_color CreditTextColor, CreditHeadingColor;
128 static int CreditStartX, CreditStartY, CreditEndX, CreditEndY;
129 static float CreditDisplayTime;
130 static chunked_bitmap Credits_bm;
131
132 #define MAX_CREDIT_LEN 200
133
134 static bool Credits_IsKeyPressed(void) {
135 if (ddio_KeyInKey())
136 return true;
137
138 return false;
139 }
140
141 // Parses a credit line
142 // Returns 1 if new text was read, else 0
143 static int Credits_ParseLine(char *line, creditline *credit) {
144 int line_len = strlen(line);
145 int new_text = 0;
146
147 if (line_len < 1) {
148 ASSERT(credit->next == NULL);
149 ASSERT(credit->text == NULL);
150
151 credit->text = NULL;
152
153 credit->type = CLTYPE_BLANK;
154 new_text = 1;
155
156 } else if (line[0] == ';') // Comment
157 return 0;
158 else if (line[0] == '*') // Movement data
159 {
160 int x1, x2, y1, y2;
161 float timedelay;
162
163 int num_found = sscanf(line, "*%d %d %d %d %f", &x1, &y1, &x2, &y2, &timedelay);
164 if (num_found != 5) {
165 mprintf(0, "Bad movement data in credit text (%s)\n", line);
166 return 0;
167 } else {
168 CreditStartX = x1;
169 CreditStartY = y1;
170 CreditEndX = x2;
171 CreditEndY = y2;
172 CreditDisplayTime = timedelay;
173 }
174
175 } else if (line[0] == '!') // Heading
176 {
177 ASSERT(credit->next == NULL);
178 ASSERT(credit->text == NULL);
179
180 credit->text = (char *)mem_malloc(line_len + 1);
181 ASSERT(credit->text);
182
183 strcpy(credit->text, &line[1]);
184 credit->startx = CreditStartX;
185 credit->starty = CreditStartY;
186 credit->endx = CreditEndX;
187 credit->endy = CreditEndY;
188 credit->displaytime = CreditDisplayTime;
189 mprintf(0, "Read header %s\n", line);
190
191 credit->color = CreditHeadingColor;
192 credit->type = CLTYPE_HEADING;
193 new_text = 1;
194
195 } else if (line[0] == '$') // text color
196 {
197 int r, g, b;
198 int num_found = sscanf(line, "$%d %d %d", &r, &g, &b);
199 if (num_found != 3) {
200 mprintf(0, "Bad color in credit text (%s)\n", line);
201 return 0;
202 } else {
203 CreditTextColor = GR_RGB(r, g, b);
204 }
205
206 } else if (line[0] == '^') // heading color
207 {
208 int r, g, b;
209 int num_found = sscanf(line, "^%d %d %d", &r, &g, &b);
210 if (num_found != 3) {
211 mprintf(0, "Bad color in credit text (%s)\n", line);
212 return 0;
213 } else {
214 CreditHeadingColor = GR_RGB(r, g, b);
215 }
216
217 } else {
218 ASSERT(credit->next == NULL);
219 ASSERT(credit->text == NULL);
220
221 if (!strnicmp("Jason Leighton", line, 14) && (ps_rand() % 100) == 0) {
222 strcat(line, " (Hi mom!)");
223 line_len += 10;
224 }
225
226 credit->text = (char *)mem_malloc(line_len + 1);
227 ASSERT(credit->text);
228
229 strcpy(credit->text, line);
230 credit->color = CreditTextColor;
231 credit->type = CLTYPE_TEXT;
232 // mprintf(0,"Read textline %s\n",line);
233 new_text = 1;
234 }
235
236 return new_text;
237 }
238
239 // Given a filename, attempts to load that filename as credit text
240 static bool Credits_LoadCredits(const char *filename) {
241 CFILE *infile;
242 char curline[MAX_CREDIT_LEN];
243
244 // Reset our data
245 CreditTextColor = GR_RGB(0, 128, 255);
246 CreditHeadingColor = GR_RGB(255, 255, 255);
247 CreditStartX = CreditEndX = 320;
248 CreditStartY = CreditEndY = 50;
249 CreditDisplayTime = 10;
250
251 creditline *cur_credit = &Creditlines;
252 Creditlines.next = NULL;
253 Creditlines.text = NULL;
254
255 // Open the file, bail on failure
256 infile = cfopen(filename, "rt");
257
258 if (!infile) {
259 mprintf(0, "Couldn't open credit file to read credits!\n");
260 return false;
261 }
262
263 for (int i = 0; i < 27; i++) {
264 // Generate blank lines for the start
265 cur_credit->text = NULL;
266 cur_credit->type = CLTYPE_BLANK;
267 cur_credit->next = (creditline *)mem_malloc(sizeof(creditline));
268 ASSERT(cur_credit->next);
269 cur_credit = cur_credit->next;
270 cur_credit->next = NULL;
271 cur_credit->text = NULL;
272 }
273
274 // Read in each line of the credit file, allocing memory and putting it into a correct category
275 int done = 0;
276 while (!done) {
277 if (cfeof(infile)) {
278 done = 1;
279 continue;
280 }
281
282 // Read a line and parse it
283 cf_ReadString(curline, MAX_CREDIT_LEN, infile);
284 // mprintf(0,"Read string %s\n",curline);
285
286 if (!strnicmp("END", curline, 3)) {
287 done = 1;
288 continue;
289 }
290
291 if (Credits_ParseLine(curline, cur_credit)) {
292 // Make a new line
293 cur_credit->next = (creditline *)mem_malloc(sizeof(creditline));
294 ASSERT(cur_credit->next);
295 cur_credit = cur_credit->next;
296 cur_credit->next = NULL;
297 cur_credit->text = NULL;
298 }
299 }
300
301 cur_credit->type = CLTYPE_END;
302
303 cfclose(infile);
304 return true;
305 }
306
307 extern void ShowStaticScreen(char *bitmap_filename, bool timed = false, float delay_time = 0.0f);
308
309 #define CREDIT_PIXELS_PER_SECOND 22
310
311 void Credits_Display(void) {
312 #ifdef DEMO
313 // ShowStaticScreen("democredits1.ogf");
314 // ShowStaticScreen("democredits2.ogf");
315 // return;
316 #else
317 // Load background image
318 int bm_handle = bm_AllocLoadFileBitmap("CreditsBackground.tga", 0);
319
320 if (bm_handle < 0) {
321 mprintf(0, "Failed to load background credit screen!\n");
322 return;
323 }
324
325 if (!bm_CreateChunkedBitmap(bm_handle, &Credits_bm)) {
326 Error("Failed to slice up credits screen!");
327 }
328
329 bm_FreeBitmap(bm_handle);
330
331 // start up the music
332 D3MusicStart("credits.omf");
333 D3MusicSetRegion(CREDITS1_MUSIC_REGION);
334
335 mprintf(0, "Chillin in credits\n");
336
337 // Load our credits
338 if (!Credits_LoadCredits("GameCredits.txt")) {
339 mprintf(0, "There was an error loading game credits!\n");
340 } else {
341 // First count how many headers there are so we know how many to allocate
342 int count = 0;
343 int done = 0;
344 creditline *credit = &Creditlines;
345
346 while (!done) {
347 if (credit->type != CLTYPE_END)
348 count++;
349 credit = credit->next;
350 if (!credit || credit->type == CLTYPE_END)
351 done = 1;
352 }
353
354 int font_height = grfont_GetHeight(BIG_BRIEFING_FONT);
355 int total_pixel_height = font_height * count;
356
357 int cur_line = 0;
358 float total_pixel_count = 0;
359 float cur_pixel_count = 0;
360 float last_time = timer_GetTime();
361
362 // Now show these guys
363 done = 0;
364 credit = &Creditlines;
365 while (cur_line < count) {
366 StartFrame(0, 0, Game_window_w, Game_window_h);
367 rend_DrawChunkedBitmap(&Credits_bm, 0, 0, 255);
368 Credits_Render(credit, cur_pixel_count);
369 EndFrame();
370 rend_Flip();
371
372 float frametime = timer_GetTime() - last_time;
373 last_time = timer_GetTime();
374
375 cur_pixel_count += frametime * CREDIT_PIXELS_PER_SECOND;
376
377 while (cur_pixel_count >= font_height) {
378 credit = credit->next;
379 cur_line++;
380 if (credit == NULL || credit->type == CLTYPE_END) {
381 cur_pixel_count = -1;
382 cur_line = count;
383 } else {
384 cur_pixel_count -= font_height;
385 }
386 }
387
388 // Player music for this frame
389 tMusicSeqInfo music_info;
390 Sound_system.BeginSoundFrame(false);
391 music_info.frametime = frametime;
392 music_info.player_dead = false;
393 music_info.started_level = false;
394 D3MusicDoFrame(&music_info);
395 Sound_system.EndSoundFrame();
396
397 // Check for keys
398 Descent->defer();
399 if (Credits_IsKeyPressed()) {
400 cur_line = count;
401 }
402 }
403 }
404
405 // Free credit text memory
406 creditline *cur_credit = &Creditlines;
407 while (cur_credit) {
408 if (cur_credit->text)
409 mem_free(cur_credit->text);
410
411 creditline *next_credit = cur_credit->next;
412
413 if (cur_credit != &Creditlines) {
414 mem_free(cur_credit);
415 }
416
417 cur_credit = next_credit;
418 }
419
420 // Free bitmaps
421 bm_DestroyChunkedBitmap(&Credits_bm);
422
423 // end the music.
424 D3MusicStop();
425 ddio_KeyFlush();
426 // ddio_MouseReset(); // -mouse shouldn't be reset. ui system gets hosed
427 // unfortunately under NT.
428 #endif
429 }
430
431 void Credits_Render(creditline *header, float pixels_in) {
432 bool done = false;
433 creditline *credit = header;
434
435 grtext_Reset();
436 grtext_SetFont(BIG_BRIEFING_FONT);
437 grtext_SetFontScale(1.0);
438 int text_height = grfont_GetHeight(BIG_BRIEFING_FONT);
439
440 float pixels_this_frame = 0;
441 int cur_pixel = -pixels_in;
442 int cur_line = 0;
443 while ((pixels_this_frame < 480 + text_height) && !done) {
444 if (credit->type == CLTYPE_BLANK) {
445 credit = credit->next;
446 cur_line++;
447
448 pixels_this_frame += text_height;
449 cur_pixel += text_height;
450
451 if (credit == NULL)
452 done = 1;
453 else if (credit->type == CLTYPE_END)
454 done = 1;
455
456 continue;
457 }
458 grtext_SetColor(credit->color);
459
460 // Figure out alpha
461 if (cur_pixel < 0) {
462 float norm = 1.0 - ((-cur_pixel) / (float)text_height);
463 grtext_SetAlpha(norm * 255);
464 } else if ((cur_pixel + text_height) > 480) {
465 float norm = 1.0 - (((cur_pixel + text_height) - 480) / (float)text_height);
466 grtext_SetAlpha(norm * 255);
467 } else
468 grtext_SetAlpha(255);
469
470 // Figure out text placement
471 int texty = cur_pixel;
472
473 grtext_CenteredPrintf(0, texty, "%s", credit->text);
474
475 credit = credit->next;
476
477 if (credit == NULL)
478 done = 1;
479 else if (credit->type == CLTYPE_END)
480 done = 1;
481
482 cur_line++;
483 pixels_this_frame += text_height;
484 cur_pixel += text_height;
485 }
486
487 grtext_Flush();
488 }