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