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