This commit was manufactured by cvs2svn to create tag 'd2x-0_1_2'.
[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.4 2001-10-25 02:15:55 bradleyb 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 #ifdef MACINTOSH
120 #define CREDITS_FILE    "mcredits.tex"
121 #else
122 #ifdef D2_OEM
123 #define CREDITS_FILE    "ocredits.tex"
124 #else
125 #define CREDITS_FILE    "credits.tex"
126 #endif
127 #endif
128
129 //if filename passed is NULL, show normal credits
130 void credits_show(char *credits_filename)
131 {
132         int i, j, l, done;
133         CFILE * file;
134         char buffer[NUM_LINES_HIRES][80];
135         grs_bitmap backdrop;
136         ubyte backdrop_palette[768];
137         int pcx_error;
138         int buffer_line = 0;
139         fix last_time;
140 //      fix time_delay = 4180;                  // ~ F1_0 / 12.9
141 //      fix time_delay = 1784;
142         fix time_delay = 2800;
143         int first_line_offset,extra_inc=0;
144         int have_bin_file = 0;
145         char * tempp;
146         char filename[32];
147
148 WIN(int credinit = 0;)
149
150         box dirty_box[NUM_LINES_HIRES];
151         grs_canvas *CreditsOffscreenBuf=NULL;
152
153         WINDOS(
154                 dd_grs_canvas *save_canv,
155                 grs_canvas *save_canv
156         );
157
158         WINDOS(
159                 save_canv = dd_grd_curcanv,
160                 save_canv = grd_curcanv
161         );
162
163         // Clear out all tex buffer lines.
164         for (i=0; i<NUM_LINES; i++ ) 
165         {
166                 buffer[i][0] = 0;
167                 dirty_box[i].left = dirty_box[i].top = dirty_box[i].width = dirty_box[i].height = 0;
168         }
169
170
171         sprintf(filename, "%s", CREDITS_FILE);
172         have_bin_file = 0;
173         if (credits_filename) {
174                 strcpy(filename,credits_filename);
175                 have_bin_file = 1;
176         }
177         file = cfopen( filename, "rb" );
178         if (file == NULL) {
179                 char nfile[32];
180                 
181                 if (credits_filename)
182                         return;         //ok to not find special filename
183
184                 tempp = strchr(filename, '.');
185                 *tempp = '\0';
186                 sprintf(nfile, "%s.txb", filename);
187                 file = cfopen(nfile, "rb");
188                 if (file == NULL)
189                         Error("Missing CREDITS.TEX and CREDITS.TXB file\n");
190                 have_bin_file = 1;
191         }
192
193         set_screen_mode(SCREEN_MENU);
194
195         WIN(DEFINE_SCREEN(NULL));
196
197 #ifdef WINDOWS
198 CreditsPaint:
199 #endif
200         gr_use_palette_table( "credits.256" );
201 #if defined(POLY_ACC)
202         pa_update_clut(gr_palette, 0, 256, 0);
203 #endif
204         header_font = gr_init_font( MenuHires?"font1-1h.fnt":"font1-1.fnt" );
205         title_font = gr_init_font( MenuHires?"font2-3h.fnt":"font2-3.fnt" );
206         names_font = gr_init_font( MenuHires?"font2-2h.fnt":"font2-2.fnt" );
207         backdrop.bm_data=NULL;
208
209 //MWA  Made backdrop bitmap linear since it should always be.  the current canvas may not
210 //MWA  be linear, so we can't rely on grd_curcanv->cv_bitmap->bm_type.
211
212         pcx_error = pcx_read_bitmap(CREDITS_BACKGROUND_FILENAME,&backdrop, BM_LINEAR,backdrop_palette);
213         if (pcx_error != PCX_ERROR_NONE)                {
214                 cfclose(file);
215                 return;
216         }
217
218         songs_play_song( SONG_CREDITS, 1 );
219
220         gr_remap_bitmap_good( &backdrop,backdrop_palette, -1, -1 );
221
222 WINDOS(
223         dd_gr_set_current_canvas(NULL), 
224         gr_set_current_canvas(NULL)
225 );
226 WIN(DDGRLOCK(dd_grd_curcanv));
227         gr_bitmap(0,0,&backdrop);
228 WIN(DDGRUNLOCK(dd_grd_curcanv));
229         gr_update();
230         gr_palette_fade_in( gr_palette, 32, 0 );
231
232 //      Create a new offscreen buffer for the credits screen
233 //MWA  Let's be a little smarter about this and check the VR_offscreen buffer
234 //MWA  for size to determine if we can use that buffer.  If the game size
235 //MWA  matches what we need, then lets save memory.
236
237 #ifndef PA_3DFX_VOODOO
238 #ifndef WINDOWS
239         if (MenuHires && VR_offscreen_buffer->cv_w == 640)      {
240                 CreditsOffscreenBuf = VR_offscreen_buffer;
241         }
242         else if (MenuHires)     {
243                 CreditsOffscreenBuf = gr_create_canvas(640,480);
244         }
245         else {
246                 CreditsOffscreenBuf = gr_create_canvas(320,200);
247         }
248 #else
249         CreditsOffscreenBuf = gr_create_canvas(640,480);
250 #endif                           
251 #else
252         CreditsOffscreenBuf = gr_create_canvas(640,480);
253 #endif
254
255         if (!CreditsOffscreenBuf) 
256                 Error("Not enough memory to allocate Credits Buffer.");
257
258         //gr_clear_canvas(BM_XRGB(0,0,0));
259         key_flush();
260
261 #ifdef WINDOWS
262         if (!credinit)  
263 #endif
264         {
265                 last_time = timer_get_fixed_seconds();
266                 done = 0;
267                 first_line_offset = 0;
268         }
269
270         WIN(credinit = 1);
271
272         while( 1 )      {
273                 int k;
274
275                 do {
276                         buffer_line = (buffer_line+1) % NUM_LINES;
277 get_line:;
278                         if (cfgets( buffer[buffer_line], 80, file ))    {
279                                 char *p;
280                                 if (have_bin_file) {                            // is this a binary tbl file
281                                         for (i = 0; i < strlen(buffer[buffer_line]) - 1; i++) {
282                                                 encode_rotate_left(&(buffer[buffer_line][i]));
283                                                 buffer[buffer_line][i] ^= BITMAP_TBL_XOR;
284                                                 encode_rotate_left(&(buffer[buffer_line][i]));
285                                         }
286                                 }
287                                 p = buffer[buffer_line];
288                                 if (p[0] == ';')
289                                         goto get_line;
290
291                                 if (p[0] == '%')
292                                 {
293                                         if (p[1] == ALLOWED_CHAR)
294                                                 strcpy(p,p+2);
295                                         else
296                                                 goto get_line;
297                                 }
298
299                                 p = strchr(&buffer[buffer_line][0],'\n');
300                                 if (p) *p = '\0';
301                         } else  {
302                                 //fseek( file, 0, SEEK_SET);
303                                 buffer[buffer_line][0] = 0;
304                                 done++;
305                         }
306                 } while (extra_inc--);
307                 extra_inc = 0;
308
309 NO_DFX (for (i=0; i<ROW_SPACING; i += (MenuHires?2:1) ) {)
310 PA_DFX (for (i=0; i<ROW_SPACING; i += (MenuHires?2:1) ) {)
311                         int y;
312
313                         y = first_line_offset - i;
314
315                         gr_set_current_canvas(CreditsOffscreenBuf);
316                 
317                         gr_bitmap(0,0,&backdrop);
318
319                         for (j=0; j<NUM_LINES; j++ )    {
320                                 char *s;
321
322                                 l = (buffer_line + j + 1 ) %  NUM_LINES;
323                                 s = buffer[l];
324
325                                 if ( s[0] == '!' ) {
326                                         s++;
327                                 } else if ( s[0] == '$' )       {
328                                         grd_curcanv->cv_font = header_font;
329                                         s++;
330                                 } else if ( s[0] == '*' )       {
331                                         grd_curcanv->cv_font = title_font;
332                                         s++;
333                                 } else
334                                         grd_curcanv->cv_font = names_font;
335
336                                 gr_bitblt_fade_table = (MenuHires?fade_values_hires:fade_values);
337
338                                 tempp = strchr( s, '\t' );
339                                 if ( tempp )    {
340                                 //      Wacky Credits thing
341                                         int w, h, aw, w2, x1, x2;
342
343                                         *tempp = 0;
344                                         gr_get_string_size( s, &w, &h, &aw );
345                                         x1 = ((MenuHires?320:160)-w)/2;
346                                         gr_printf( x1 , y, s );
347                                         gr_get_string_size( &tempp[1], &w2, &h, &aw );
348                                         x2 = (MenuHires?320:160)+(((MenuHires?320:160)-w2)/2);
349                                         gr_printf( x2, y, &tempp[1] );
350
351                                         dirty_box[j].left = ((MenuHires?320:160)-w)/2;
352                                         dirty_box[j].top = y;
353                                         dirty_box[j].width =(x2+w2)-x1;
354                                         dirty_box[j].height = h;
355
356                                         *tempp = '\t';
357                 
358                                 } else {
359                                 // Wacky Fast Credits thing
360                                         int w, h, aw;
361
362                                         gr_get_string_size( s, &w, &h, &aw);
363                                         dirty_box[j].width = w;
364                                 dirty_box[j].height = h;
365                                 dirty_box[j].top = y;
366                                 dirty_box[j].left = ((MenuHires?640:320) - w) / 2;
367
368                                         gr_printf( 0x8000, y, s );
369                                 }
370                                 gr_bitblt_fade_table = NULL;
371                                 if (buffer[l][0] == '!')
372                                         y += ROW_SPACING/2;
373                                 else
374                                         y += ROW_SPACING;
375                         }
376
377                         {       // Wacky Fast Credits Thing
378                                 box     *new_box;
379                                 grs_bitmap *tempbmp;
380                                 
381                                 for (j=0; j<NUM_LINES; j++ )
382                                 {
383                                         new_box = &dirty_box[j];
384
385                                         tempbmp = &(CreditsOffscreenBuf->cv_bitmap);
386                           
387                                 WIN(DDGRSCREENLOCK);
388 #if defined(POLY_ACC)
389                                         if(new_box->width != 0)
390 #endif
391                                                 gr_bm_bitblt( new_box->width + 1, new_box->height +4,
392                                                                         new_box->left, new_box->top, new_box->left, new_box->top,
393                                                                         tempbmp, &(grd_curscreen->sc_canvas.cv_bitmap) );
394                                 WIN(DDGRSCREENUNLOCK);
395                                 }
396
397 #if defined(POLY_ACC)
398                 pa_flush();
399 #endif
400
401 #if !defined(POLY_ACC) || defined(MACINTOSH)
402                                 MAC( if(!PAEnabled) )                   // POLY_ACC always on for the macintosh
403                                 for (j=0; j<NUM_LINES; j++ )
404                                 {
405                                         new_box = &dirty_box[j];
406
407                                         tempbmp = &(CreditsOffscreenBuf->cv_bitmap);
408
409                                         gr_bm_bitblt(   new_box->width 
410                                                                         ,new_box->height+2
411                                                                         ,new_box->left
412                                                                         ,new_box->top
413                                                                         ,new_box->left
414                                                                         ,new_box->top
415                                                                         ,&backdrop
416                                                                         ,tempbmp );
417                                 }
418                                 
419 #endif
420                         gr_update();
421                                 
422                         }
423
424 //              Wacky Fast Credits thing doesn't need this (it's done above)
425 //@@            WINDOS(
426 //@@                    dd_gr_blt_notrans(CreditsOffscreenBuf, 0,0,0,0, dd_grd_screencanv, 0,0,0,0),
427 //@@                    gr_bm_ubitblt(grd_curcanv->cv_w, grd_curcanv->cv_h, 0, 0, 0, 0, &(CreditsOffscreenBuf->cv_bitmap), &(grd_curscreen->sc_canvas.cv_bitmap) );
428 //@@            );
429
430 //                      mprintf( ( 0, "Fr = %d", (timer_get_fixed_seconds() - last_time) ));
431                         while( timer_get_fixed_seconds() < last_time+time_delay );
432                         last_time = timer_get_fixed_seconds();
433                 
434                 #ifdef WINDOWS
435                         {
436                                 MSG msg;
437
438                                 DoMessageStuff(&msg);
439
440                                 if (_RedrawScreen) {
441                                         _RedrawScreen = FALSE;
442
443                                         gr_close_font(header_font);
444                                         gr_close_font(title_font);
445                                         gr_close_font(names_font);
446
447                                         d_free(backdrop.bm_data);
448                                         gr_free_canvas(CreditsOffscreenBuf);
449                 
450                                         goto CreditsPaint;
451                                 }
452
453                                 DDGRRESTORE;
454                         }
455                 #endif
456
457                         //see if redbook song needs to be restarted
458                         songs_check_redbook_repeat();
459
460                         k = key_inkey();
461
462                         #ifndef NDEBUG
463                         if (k == KEY_BACKSP) {
464                                 Int3();
465                                 k=0;
466                         }
467                         #endif
468
469 //                      {
470 //                              fix ot = time_delay;
471 //                              time_delay += (keyd_pressed[KEY_X] - keyd_pressed[KEY_Z])*100;
472 //                              if (ot!=time_delay)     {
473 //                                      mprintf( (0, "[%x] ", time_delay ));
474 //                              }
475 //                      }
476
477                         if (k == KEY_PRINT_SCREEN) {
478                                 save_screen_shot(0);
479                                 k = 0;
480                         }
481
482                         if ((k>0)||(done>NUM_LINES))    {
483                                         gr_close_font(header_font);
484                                         gr_close_font(title_font);
485                                         gr_close_font(names_font);
486                                         gr_palette_fade_out( gr_palette, 32, 0 );
487                                         gr_use_palette_table( DEFAULT_PALETTE );
488                                         d_free(backdrop.bm_data);
489                                         cfclose(file);
490                                 WINDOS(
491                                         dd_gr_set_current_canvas(save_canv),
492                                         gr_set_current_canvas(save_canv)
493                                 );
494                                         songs_play_song( SONG_TITLE, 1 );
495
496                                 #ifdef WINDOWS
497                                         gr_free_canvas(CreditsOffscreenBuf);
498                                 #else                                   
499                                         if (CreditsOffscreenBuf != VR_offscreen_buffer)
500                                                 gr_free_canvas(CreditsOffscreenBuf);
501                                 #endif
502
503                                 WIN(DEFINE_SCREEN(Menu_pcx_name));
504                         
505                                 return;
506                         }
507                 }
508
509                 if (buffer[(buffer_line + 1 ) %  NUM_LINES][0] == '!') {
510                         first_line_offset -= ROW_SPACING-ROW_SPACING/2;
511                         if (first_line_offset <= -ROW_SPACING) {
512                                 first_line_offset += ROW_SPACING;
513                                 extra_inc++;
514                         }
515                 }
516         }
517
518 }