whitespace
[btb/d2x.git] / main / credits.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #ifdef RCS
19 static char rcsid[] = "$Id: credits.c,v 1.5 2002-09-04 22:29:49 btb Exp $";
20 #endif
21
22 #ifdef WINDOWS
23 #include "desw.h"
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <ctype.h>
31
32 #include "pa_enabl.h"                   //$$POLY_ACC
33 #include "error.h"
34 #include "pstypes.h"
35 #include "gr.h"
36 #include "mono.h"
37 #include "key.h"
38 #include "palette.h"
39 #include "game.h"
40 #include "gamepal.h"
41 #include "timer.h"
42
43 #include "newmenu.h"
44 #include "gamefont.h"
45 #ifdef NETWORK
46 #include "network.h"
47 #endif
48 #include "iff.h"
49 #include "pcx.h"
50 #include "u_mem.h"
51 #include "mouse.h"
52 #include "joy.h"
53 #include "screens.h"
54 #include "digi.h"
55
56 #include "cfile.h"
57 #include "compbit.h"
58 #include "songs.h"
59 #include "menu.h"                       // for MenuHires
60
61 #if defined(POLY_ACC)
62 #include "poly_acc.h"
63 #endif
64
65 #define ROW_SPACING (MenuHires?26:11)
66 #define NUM_LINES_HIRES 21
67 #define NUM_LINES (MenuHires?NUM_LINES_HIRES:20)
68
69 ubyte fade_values[200] = { 1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,8,9,9,10,10,
70 11,11,12,12,12,13,13,14,14,15,15,15,16,16,17,17,17,18,18,19,19,19,20,20,
71 20,21,21,22,22,22,23,23,23,24,24,24,24,25,25,25,26,26,26,26,27,27,27,27,
72 28,28,28,28,28,29,29,29,29,29,29,30,30,30,30,30,30,30,30,30,31,31,31,31,
73 31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,30,30,30,30,30,
74 30,30,30,30,29,29,29,29,29,29,28,28,28,28,28,27,27,27,27,26,26,26,26,25,
75 25,25,24,24,24,24,23,23,23,22,22,22,21,21,20,20,20,19,19,19,18,18,17,17,
76 17,16,16,15,15,15,14,14,13,13,12,12,12,11,11,10,10,9,9,8,8,8,7,7,6,6,5,
77 5,4,4,3,3,2,2,1 };
78
79 ubyte fade_values_hires[480] = { 1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,
80 5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,9,9,10,10,10,10,10,10,11,11,11,11,11,12,12,12,12,12,12,
81 13,13,13,13,13,14,14,14,14,14,14,15,15,15,15,15,15,16,16,16,16,16,17,17,17,17,17,17,18,18,
82 18,18,18,18,18,19,19,19,19,19,19,20,20,20,20,20,20,20,21,21,21,21,21,21,22,22,22,22,22,22,
83 22,22,23,23,23,23,23,23,23,24,24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,25,26,26,26,26,
84 26,26,26,26,26,27,27,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,28,28,28,28,29,29,29,
85 29,29,29,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
86 30,30,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,
87 31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,30,30,30,
88 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,29,29,29,29,29,29,29,29,29,29,29,29,
89 29,29,28,28,28,28,28,28,28,28,28,28,28,28,27,27,27,27,27,27,27,27,27,27,26,26,26,26,26,26,
90 26,26,26,25,25,25,25,25,25,25,25,25,24,24,24,24,24,24,24,24,23,23,23,23,23,23,23,22,22,22,
91 22,22,22,22,22,21,21,21,21,21,21,20,20,20,20,20,20,20,19,19,19,19,19,19,18,18,18,18,18,18,
92 18,17,17,17,17,17,17,16,16,16,16,16,15,15,15,15,15,15,14,14,14,14,14,14,13,13,13,13,13,12,
93 12,12,12,12,12,11,11,11,11,11,10,10,10,10,10,10,9,9,9,9,9,8,8,8,8,8,7,7,7,7,7,6,6,6,6,6,5,5,5,5,
94 5,5,4,4,4,4,4,3,3,3,3,3,2,2,2,2,2,1,1};
95
96 extern ubyte *gr_bitblt_fade_table;
97 extern void gr_bm_bitblt(int w, int h, int dx, int dy, int sx, int sy, grs_bitmap * src, grs_bitmap * dest);
98
99 grs_font * header_font;
100 grs_font * title_font;
101 grs_font * names_font;
102
103 #ifdef SHAREWARE
104 #define ALLOWED_CHAR 'S'
105 #else
106 #define ALLOWED_CHAR 'R'
107 #endif
108
109 #ifdef RELEASE
110 #define CREDITS_BACKGROUND_FILENAME (MenuHires?"\x01starsb.pcx":"\x01stars.pcx")        //only read from hog file
111 #else
112 #define CREDITS_BACKGROUND_FILENAME (MenuHires?"starsb.pcx":"stars.pcx")
113 #endif
114
115 typedef struct box {
116         int left, top, width, height;
117 } box;
118
119 #define CREDITS_FILE    (cfexist("mcredits.tex")?"mcredits.tex":cfexist("ocredits.tex")?"ocredits.tex":"credits.tex")
120
121 //if filename passed is NULL, show normal credits
122 void credits_show(char *credits_filename)
123 {
124         int i, j, l, done;
125         CFILE * file;
126         char buffer[NUM_LINES_HIRES][80];
127         grs_bitmap backdrop;
128         ubyte backdrop_palette[768];
129         int pcx_error;
130         int buffer_line = 0;
131         fix last_time;
132 //      fix time_delay = 4180;                  // ~ F1_0 / 12.9
133 //      fix time_delay = 1784;
134         fix time_delay = 2800;
135         int first_line_offset,extra_inc=0;
136         int have_bin_file = 0;
137         char * tempp;
138         char filename[32];
139
140 WIN(int credinit = 0;)
141
142         box dirty_box[NUM_LINES_HIRES];
143         grs_canvas *CreditsOffscreenBuf=NULL;
144
145         WINDOS(
146                 dd_grs_canvas *save_canv,
147                 grs_canvas *save_canv
148         );
149
150         WINDOS(
151                 save_canv = dd_grd_curcanv,
152                 save_canv = grd_curcanv
153         );
154
155         // Clear out all tex buffer lines.
156         for (i=0; i<NUM_LINES; i++ ) 
157         {
158                 buffer[i][0] = 0;
159                 dirty_box[i].left = dirty_box[i].top = dirty_box[i].width = dirty_box[i].height = 0;
160         }
161
162
163         sprintf(filename, "%s", CREDITS_FILE);
164         have_bin_file = 0;
165         if (credits_filename) {
166                 strcpy(filename,credits_filename);
167                 have_bin_file = 1;
168         }
169         file = cfopen( filename, "rb" );
170         if (file == NULL) {
171                 char nfile[32];
172                 
173                 if (credits_filename)
174                         return;         //ok to not find special filename
175
176                 tempp = strchr(filename, '.');
177                 *tempp = '\0';
178                 sprintf(nfile, "%s.txb", filename);
179                 file = cfopen(nfile, "rb");
180                 if (file == NULL)
181                         Error("Missing CREDITS.TEX and CREDITS.TXB file\n");
182                 have_bin_file = 1;
183         }
184
185         set_screen_mode(SCREEN_MENU);
186
187         WIN(DEFINE_SCREEN(NULL));
188
189 #ifdef WINDOWS
190 CreditsPaint:
191 #endif
192         gr_use_palette_table( "credits.256" );
193 #if defined(POLY_ACC)
194         pa_update_clut(gr_palette, 0, 256, 0);
195 #endif
196         header_font = gr_init_font( MenuHires?"font1-1h.fnt":"font1-1.fnt" );
197         title_font = gr_init_font( MenuHires?"font2-3h.fnt":"font2-3.fnt" );
198         names_font = gr_init_font( MenuHires?"font2-2h.fnt":"font2-2.fnt" );
199         backdrop.bm_data=NULL;
200
201 //MWA  Made backdrop bitmap linear since it should always be.  the current canvas may not
202 //MWA  be linear, so we can't rely on grd_curcanv->cv_bitmap->bm_type.
203
204         pcx_error = pcx_read_bitmap(CREDITS_BACKGROUND_FILENAME,&backdrop, BM_LINEAR,backdrop_palette);
205         if (pcx_error != PCX_ERROR_NONE)                {
206                 cfclose(file);
207                 return;
208         }
209
210         songs_play_song( SONG_CREDITS, 1 );
211
212         gr_remap_bitmap_good( &backdrop,backdrop_palette, -1, -1 );
213
214 WINDOS(
215         dd_gr_set_current_canvas(NULL), 
216         gr_set_current_canvas(NULL)
217 );
218 WIN(DDGRLOCK(dd_grd_curcanv));
219         gr_bitmap(0,0,&backdrop);
220 WIN(DDGRUNLOCK(dd_grd_curcanv));
221         gr_update();
222         gr_palette_fade_in( gr_palette, 32, 0 );
223
224 //      Create a new offscreen buffer for the credits screen
225 //MWA  Let's be a little smarter about this and check the VR_offscreen buffer
226 //MWA  for size to determine if we can use that buffer.  If the game size
227 //MWA  matches what we need, then lets save memory.
228
229 #ifndef PA_3DFX_VOODOO
230 #ifndef WINDOWS
231         if (MenuHires && VR_offscreen_buffer->cv_w == 640)      {
232                 CreditsOffscreenBuf = VR_offscreen_buffer;
233         }
234         else if (MenuHires)     {
235                 CreditsOffscreenBuf = gr_create_canvas(640,480);
236         }
237         else {
238                 CreditsOffscreenBuf = gr_create_canvas(320,200);
239         }
240 #else
241         CreditsOffscreenBuf = gr_create_canvas(640,480);
242 #endif                           
243 #else
244         CreditsOffscreenBuf = gr_create_canvas(640,480);
245 #endif
246
247         if (!CreditsOffscreenBuf) 
248                 Error("Not enough memory to allocate Credits Buffer.");
249
250         //gr_clear_canvas(BM_XRGB(0,0,0));
251         key_flush();
252
253 #ifdef WINDOWS
254         if (!credinit)  
255 #endif
256         {
257                 last_time = timer_get_fixed_seconds();
258                 done = 0;
259                 first_line_offset = 0;
260         }
261
262         WIN(credinit = 1);
263
264         while( 1 )      {
265                 int k;
266
267                 do {
268                         buffer_line = (buffer_line+1) % NUM_LINES;
269 get_line:;
270                         if (cfgets( buffer[buffer_line], 80, file ))    {
271                                 char *p;
272                                 if (have_bin_file) {                            // is this a binary tbl file
273                                         for (i = 0; i < strlen(buffer[buffer_line]) - 1; i++) {
274                                                 encode_rotate_left(&(buffer[buffer_line][i]));
275                                                 buffer[buffer_line][i] ^= BITMAP_TBL_XOR;
276                                                 encode_rotate_left(&(buffer[buffer_line][i]));
277                                         }
278                                 }
279                                 p = buffer[buffer_line];
280                                 if (p[0] == ';')
281                                         goto get_line;
282
283                                 if (p[0] == '%')
284                                 {
285                                         if (p[1] == ALLOWED_CHAR)
286                                                 strcpy(p,p+2);
287                                         else
288                                                 goto get_line;
289                                 }
290
291                                 p = strchr(&buffer[buffer_line][0],'\n');
292                                 if (p) *p = '\0';
293                         } else  {
294                                 //fseek( file, 0, SEEK_SET);
295                                 buffer[buffer_line][0] = 0;
296                                 done++;
297                         }
298                 } while (extra_inc--);
299                 extra_inc = 0;
300
301 NO_DFX (for (i=0; i<ROW_SPACING; i += (MenuHires?2:1) ) {)
302 PA_DFX (for (i=0; i<ROW_SPACING; i += (MenuHires?2:1) ) {)
303                         int y;
304
305                         y = first_line_offset - i;
306
307                         gr_set_current_canvas(CreditsOffscreenBuf);
308                 
309                         gr_bitmap(0,0,&backdrop);
310
311                         for (j=0; j<NUM_LINES; j++ )    {
312                                 char *s;
313
314                                 l = (buffer_line + j + 1 ) %  NUM_LINES;
315                                 s = buffer[l];
316
317                                 if ( s[0] == '!' ) {
318                                         s++;
319                                 } else if ( s[0] == '$' )       {
320                                         grd_curcanv->cv_font = header_font;
321                                         s++;
322                                 } else if ( s[0] == '*' )       {
323                                         grd_curcanv->cv_font = title_font;
324                                         s++;
325                                 } else
326                                         grd_curcanv->cv_font = names_font;
327
328                                 gr_bitblt_fade_table = (MenuHires?fade_values_hires:fade_values);
329
330                                 tempp = strchr( s, '\t' );
331                                 if ( tempp )    {
332                                 //      Wacky Credits thing
333                                         int w, h, aw, w2, x1, x2;
334
335                                         *tempp = 0;
336                                         gr_get_string_size( s, &w, &h, &aw );
337                                         x1 = ((MenuHires?320:160)-w)/2;
338                                         gr_printf( x1 , y, s );
339                                         gr_get_string_size( &tempp[1], &w2, &h, &aw );
340                                         x2 = (MenuHires?320:160)+(((MenuHires?320:160)-w2)/2);
341                                         gr_printf( x2, y, &tempp[1] );
342
343                                         dirty_box[j].left = ((MenuHires?320:160)-w)/2;
344                                         dirty_box[j].top = y;
345                                         dirty_box[j].width =(x2+w2)-x1;
346                                         dirty_box[j].height = h;
347
348                                         *tempp = '\t';
349                 
350                                 } else {
351                                 // Wacky Fast Credits thing
352                                         int w, h, aw;
353
354                                         gr_get_string_size( s, &w, &h, &aw);
355                                         dirty_box[j].width = w;
356                                 dirty_box[j].height = h;
357                                 dirty_box[j].top = y;
358                                 dirty_box[j].left = ((MenuHires?640:320) - w) / 2;
359
360                                         gr_printf( 0x8000, y, s );
361                                 }
362                                 gr_bitblt_fade_table = NULL;
363                                 if (buffer[l][0] == '!')
364                                         y += ROW_SPACING/2;
365                                 else
366                                         y += ROW_SPACING;
367                         }
368
369                         {       // Wacky Fast Credits Thing
370                                 box     *new_box;
371                                 grs_bitmap *tempbmp;
372                                 
373                                 for (j=0; j<NUM_LINES; j++ )
374                                 {
375                                         new_box = &dirty_box[j];
376
377                                         tempbmp = &(CreditsOffscreenBuf->cv_bitmap);
378                           
379                                 WIN(DDGRSCREENLOCK);
380 #if defined(POLY_ACC)
381                                         if(new_box->width != 0)
382 #endif
383                                                 gr_bm_bitblt( new_box->width + 1, new_box->height +4,
384                                                                         new_box->left, new_box->top, new_box->left, new_box->top,
385                                                                         tempbmp, &(grd_curscreen->sc_canvas.cv_bitmap) );
386                                 WIN(DDGRSCREENUNLOCK);
387                                 }
388
389 #if defined(POLY_ACC)
390                 pa_flush();
391 #endif
392
393 #if !defined(POLY_ACC) || defined(MACINTOSH)
394                                 MAC( if(!PAEnabled) )                   // POLY_ACC always on for the macintosh
395                                 for (j=0; j<NUM_LINES; j++ )
396                                 {
397                                         new_box = &dirty_box[j];
398
399                                         tempbmp = &(CreditsOffscreenBuf->cv_bitmap);
400
401                                         gr_bm_bitblt(   new_box->width 
402                                                                         ,new_box->height+2
403                                                                         ,new_box->left
404                                                                         ,new_box->top
405                                                                         ,new_box->left
406                                                                         ,new_box->top
407                                                                         ,&backdrop
408                                                                         ,tempbmp );
409                                 }
410                                 
411 #endif
412                         gr_update();
413                                 
414                         }
415
416 //              Wacky Fast Credits thing doesn't need this (it's done above)
417 //@@            WINDOS(
418 //@@                    dd_gr_blt_notrans(CreditsOffscreenBuf, 0,0,0,0, dd_grd_screencanv, 0,0,0,0),
419 //@@                    gr_bm_ubitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(CreditsOffscreenBuf->cv_bitmap), &(grd_curscreen->sc_canvas.cv_bitmap) );
420 //@@            );
421
422 //                      mprintf( ( 0, "Fr = %d", (timer_get_fixed_seconds() - last_time) ));
423                         while( timer_get_fixed_seconds() < last_time+time_delay );
424                         last_time = timer_get_fixed_seconds();
425                 
426                 #ifdef WINDOWS
427                         {
428                                 MSG msg;
429
430                                 DoMessageStuff(&msg);
431
432                                 if (_RedrawScreen) {
433                                         _RedrawScreen = FALSE;
434
435                                         gr_close_font(header_font);
436                                         gr_close_font(title_font);
437                                         gr_close_font(names_font);
438
439                                         d_free(backdrop.bm_data);
440                                         gr_free_canvas(CreditsOffscreenBuf);
441                 
442                                         goto CreditsPaint;
443                                 }
444
445                                 DDGRRESTORE;
446                         }
447                 #endif
448
449                         //see if redbook song needs to be restarted
450                         songs_check_redbook_repeat();
451
452                         k = key_inkey();
453
454                         #ifndef NDEBUG
455                         if (k == KEY_BACKSP) {
456                                 Int3();
457                                 k=0;
458                         }
459                         #endif
460
461 //                      {
462 //                              fix ot = time_delay;
463 //                              time_delay += (keyd_pressed[KEY_X] - keyd_pressed[KEY_Z])*100;
464 //                              if (ot!=time_delay)     {
465 //                                      mprintf( (0, "[%x] ", time_delay ));
466 //                              }
467 //                      }
468
469                         if (k == KEY_PRINT_SCREEN) {
470                                 save_screen_shot(0);
471                                 k = 0;
472                         }
473
474                         if ((k>0)||(done>NUM_LINES))    {
475                                         gr_close_font(header_font);
476                                         gr_close_font(title_font);
477                                         gr_close_font(names_font);
478                                         gr_palette_fade_out( gr_palette, 32, 0 );
479                                         gr_use_palette_table( DEFAULT_PALETTE );
480                                         d_free(backdrop.bm_data);
481                                         cfclose(file);
482                                 WINDOS(
483                                         dd_gr_set_current_canvas(save_canv),
484                                         gr_set_current_canvas(save_canv)
485                                 );
486                                         songs_play_song( SONG_TITLE, 1 );
487
488                                 #ifdef WINDOWS
489                                         gr_free_canvas(CreditsOffscreenBuf);
490                                 #else                                   
491                                         if (CreditsOffscreenBuf != VR_offscreen_buffer)
492                                                 gr_free_canvas(CreditsOffscreenBuf);
493                                 #endif
494
495                                 WIN(DEFINE_SCREEN(Menu_pcx_name));
496                         
497                                 return;
498                         }
499                 }
500
501                 if (buffer[(buffer_line + 1 ) %  NUM_LINES][0] == '!') {
502                         first_line_offset -= ROW_SPACING-ROW_SPACING/2;
503                         if (first_line_offset <= -ROW_SPACING) {
504                                 first_line_offset += ROW_SPACING;
505                                 extra_inc++;
506                         }
507                 }
508         }
509
510 }