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