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