]> icculus.org git repositories - btb/d2x.git/blob - main/movie.c
revert 12fe67a367e459cdc79340bff03732158336feb4
[btb/d2x.git] / main / movie.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  * Movie Playing Stuff
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <string.h>
25 #ifndef macintosh
26 # ifndef _WIN32_WCE
27 #  include <sys/types.h>
28 #  include <sys/stat.h>
29 #  include <fcntl.h>
30 # endif
31 # ifndef _MSC_VER
32 #  include <unistd.h>
33 # endif
34 #endif // ! macintosh
35 #include <ctype.h>
36
37 #include "movie.h"
38 #include "console.h"
39 #include "args.h"
40 #include "key.h"
41 #include "digi.h"
42 #include "songs.h"
43 #include "inferno.h"
44 #include "palette.h"
45 #include "strutil.h"
46 #include "error.h"
47 #include "u_mem.h"
48 #include "byteswap.h"
49 #include "gr.h"
50 #include "gamefont.h"
51 #include "cfile.h"
52 #include "menu.h"
53 #include "libmve.h"
54 #include "text.h"
55 #include "screens.h"
56 #include "physfsrwops.h"
57
58 extern int MenuHiresAvailable;
59 extern char CDROM_dir[];
60
61
62 // Subtitle data
63 typedef struct {
64         short first_frame,last_frame;
65         char *msg;
66 } subtitle;
67
68 #define MAX_SUBTITLES 500
69 #define MAX_ACTIVE_SUBTITLES 3
70 subtitle Subtitles[MAX_SUBTITLES];
71 int Num_subtitles;
72
73 // Movielib data
74
75 #ifdef D2_OEM
76 char movielib_files[5][FILENAME_LEN] = {"intro","other","robots","oem"};
77 #else
78 char movielib_files[4][FILENAME_LEN] = {"intro","other","robots"};
79 #endif
80
81 #define N_MOVIE_LIBS (sizeof(movielib_files) / sizeof(*movielib_files))
82 #define N_BUILTIN_MOVIE_LIBS (N_MOVIE_LIBS - 1)
83 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
84
85 cvar_t MovieHires = { "MovieHires", "1", 1 }; //default is highres
86
87 SDL_RWops *RoboFile;
88
89 // Function Prototypes
90 int RunMovie(char *filename, int highres_flag, int allow_abort,int dx,int dy);
91
92 void decode_text_line(char *p);
93 void draw_subtitles(int frame_num);
94
95
96 // ----------------------------------------------------------------------
97 void* MPlayAlloc(unsigned size)
98 {
99     return d_malloc(size);
100 }
101
102 void MPlayFree(void *p)
103 {
104     d_free(p);
105 }
106
107
108 //-----------------------------------------------------------------------
109
110 unsigned int FileRead(void *handle, void *buf, unsigned int count)
111 {
112     unsigned numread;
113     numread = SDL_RWread((SDL_RWops *)handle, buf, 1, count);
114     return (numread == count);
115 }
116
117
118 //-----------------------------------------------------------------------
119
120
121 //filename will actually get modified to be either low-res or high-res
122 //returns status.  see values in movie.h
123 int PlayMovie(const char *filename, int must_have)
124 {
125         char name[FILENAME_LEN],*p;
126         int c, ret;
127
128         if (FindArg("-nomovies"))
129                 return MOVIE_NOT_PLAYED;
130
131         strcpy(name,filename);
132
133         if ((p=strchr(name,'.')) == NULL)               //add extension, if missing
134                 strcat(name,".mve");
135
136         //check for escape already pressed & abort if so
137         while ((c=key_inkey()) != 0)
138                 if (c == KEY_ESC)
139                         return MOVIE_ABORTED;
140
141         // Stop all digital sounds currently playing.
142         digi_stop_all();
143
144         // Stop all songs
145         songs_stop_all();
146
147         digi_close();
148
149         // Start sound
150         if (!FindArg("-nosound"))
151                 MVE_sndInit(1);
152         else
153                 MVE_sndInit(-1);
154
155         ret = RunMovie(name, MovieHires.intval, must_have, -1, -1);
156
157         if (!FindArg("-nosound"))
158                 digi_init();
159
160         Screen_mode = -1;               //force screen reset
161
162         return ret;
163 }
164
165
166 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
167                                         uint w, uint h, uint dstx, uint dsty)
168 {
169         grs_bitmap source_bm;
170
171         //mprintf((0,"MovieShowFrame %d,%d  %d,%d  %d,%d  %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
172
173         Assert(bufw == w && bufh == h);
174
175         source_bm.bm_x = source_bm.bm_y = 0;
176         source_bm.bm_w = source_bm.bm_rowsize = bufw;
177         source_bm.bm_h = bufh;
178         source_bm.bm_type = BM_LINEAR;
179         source_bm.bm_flags = 0;
180         source_bm.bm_data = buf;
181
182         gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
183 }
184
185 //our routine to set the pallete, called from the movie code
186 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
187 {
188         if (count == 0)
189                 return;
190
191         //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
192
193         //Color 0 should be black, and we get color 255
194         Assert(start>=1 && start+count-1<=254);
195
196         //Set color 0 to be black
197         gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
198
199         //Set color 255 to be our subtitle color
200         gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
201
202         //movie libs palette into our array
203         memcpy(gr_palette+start*3,p+start*3,count*3);
204
205         //finally set the palette in the hardware
206         gr_palette_load(gr_palette);
207
208         //MVE_SetPalette(p, start, count);
209 }
210
211
212 #if 0
213 typedef struct bkg {
214         short x, y, w, h;           // The location of the menu.
215         grs_bitmap * bmp;               // The background under the menu.
216 } bkg;
217
218 bkg movie_bg = {0,0,0,0,NULL};
219 #endif
220
221 #define BOX_BORDER (MenuHires?40:20)
222
223
224 void show_pause_message(char *msg)
225 {
226         int w,h,aw;
227         int x,y;
228
229         gr_set_current_canvas(NULL);
230         gr_set_curfont( SMALL_FONT );
231
232         gr_get_string_size(msg,&w,&h,&aw);
233
234         x = (grd_curscreen->sc_w-w)/2;
235         y = (grd_curscreen->sc_h-h)/2;
236
237 #if 0
238         if (movie_bg.bmp) {
239                 gr_free_bitmap(movie_bg.bmp);
240                 movie_bg.bmp = NULL;
241         }
242
243         // Save the background of the display
244         movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
245
246         movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
247
248         gr_bm_ubitblt(w+BOX_BORDER, h+BOX_BORDER, 0, 0, x-BOX_BORDER/2, y-BOX_BORDER/2, &(grd_curcanv->cv_bitmap), movie_bg.bmp );
249 #endif
250
251         gr_setcolor(0);
252         gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
253
254         gr_set_fontcolor( 255, -1 );
255
256         gr_ustring( 0x8000, y, msg );
257
258         gr_update();
259 }
260
261 void clear_pause_message()
262 {
263 #if 0
264         if (movie_bg.bmp) {
265
266                 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
267
268                 gr_free_bitmap(movie_bg.bmp);
269                 movie_bg.bmp = NULL;
270         }
271 #endif
272 }
273
274
275 //returns status.  see movie.h
276 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
277 {
278         SDL_RWops *filehndl;
279         int result=1,aborted=0;
280         int track = 0;
281         int frame_num;
282         int key;
283
284         result=1;
285
286         // Open Movie file.  If it doesn't exist, no movie, just return.
287
288         filehndl = PHYSFSRWOPS_openRead(filename);
289
290         if (!filehndl)
291         {
292                 if (must_have)
293                         con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
294                 return MOVIE_NOT_PLAYED;
295         }
296
297         MVE_memCallbacks(MPlayAlloc, MPlayFree);
298         MVE_ioCallbacks(FileRead);
299         MVE_sfCallbacks(MovieShowFrame);
300         MVE_palCallbacks(MovieSetPalette);
301
302         if (hires_flag) {
303                 gr_set_mode(SM(640,480));
304         } else {
305                 gr_set_mode(SM(320,200));
306         }
307 #ifdef OGL
308         set_screen_mode(SCREEN_MENU);
309 #endif
310
311         if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
312                 Int3();
313                 return MOVIE_NOT_PLAYED;
314         }
315
316         frame_num = 0;
317
318         FontHires = FontHiresAvailable && hires_flag;
319
320         while((result = MVE_rmStepMovie()) == 0) {
321
322                 draw_subtitles(frame_num);
323
324                 gr_update();
325
326                 key = key_inkey();
327
328                 // If ESCAPE pressed, then quit movie.
329                 if (key == KEY_ESC) {
330                         result = aborted = 1;
331                         break;
332                 }
333
334                 // If PAUSE pressed, then pause movie
335                 if (key == KEY_PAUSE) {
336                         MVE_rmHoldMovie();
337                         show_pause_message(TXT_PAUSE);
338                         while (!key_inkey()) ;
339                         clear_pause_message();
340                 }
341
342 #ifdef GR_SUPPORTS_FULLSCREEN_TOGGLE
343                 if ((key == KEY_COMMAND+KEY_SHIFTED+KEY_F) ||
344                         (key == KEY_ALTED+KEY_ENTER) ||
345                     (key == KEY_ALTED+KEY_PADENTER))
346                         gr_toggle_fullscreen();
347 #endif
348
349                 frame_num++;
350         }
351
352         Assert(aborted || result == MVE_ERR_EOF);        ///movie should be over
353
354     MVE_rmEndMovie();
355
356         SDL_RWclose(filehndl); // Close Movie File
357
358         // Restore old graphic state
359
360         Screen_mode=-1;  //force reset of screen mode
361
362         return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
363 }
364
365
366 int InitMovieBriefing()
367 {
368 #if 0
369         if (MenuHires)
370                 gr_set_mode(SM(640,480));
371         else
372                 gr_set_mode(SM(320,200));
373
374         gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
375         gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
376 #endif
377
378         return 1;
379 }
380
381
382 //returns 1 if frame updated ok
383 int RotateRobot()
384 {
385         int err;
386
387         err = MVE_rmStepMovie();
388
389         if (err == MVE_ERR_EOF)     //end of movie, so reset
390         {
391                 SDL_RWseek(RoboFile, 0, SEEK_SET);
392                 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0))
393                 {
394                         Int3();
395                         return 0;
396                 }
397         }
398         else if (err) {
399                 Int3();
400                 return 0;
401         }
402
403         return 1;
404 }
405
406
407 void DeInitRobotMovie(void)
408 {
409         MVE_rmEndMovie();
410         SDL_RWclose(RoboFile); // Close Movie File
411 }
412
413
414 int InitRobotMovie(char *filename)
415 {
416         if (FindArg("-nomovies"))
417                 return 0;
418
419         con_printf(CON_DEBUG, "RoboFile=%s\n", filename);
420
421         MVE_sndInit(-1);        //tell movies to play no sound for robots
422
423         RoboFile = PHYSFSRWOPS_openRead(filename);
424
425         if (!RoboFile)
426         {
427                 con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
428                 return MOVIE_NOT_PLAYED;
429         }
430
431         if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
432                 Int3();
433                 return 0;
434         }
435
436         return 1;
437 }
438
439
440 /*
441  *              Subtitle system code
442  */
443
444 char *subtitle_raw_data;
445
446
447 //search for next field following whitespace 
448 char *next_field (char *p)
449 {
450         while (*p && !isspace(*p))
451                 p++;
452
453         if (!*p)
454                 return NULL;
455
456         while (*p && isspace(*p))
457                 p++;
458
459         if (!*p)
460                 return NULL;
461
462         return p;
463 }
464
465
466 int init_subtitles(char *filename)
467 {
468         CFILE *ifile;
469         int size,read_count;
470         char *p;
471         int have_binary = 0;
472
473         Num_subtitles = 0;
474
475         if (! FindArg("-subtitles"))
476                 return 0;
477
478         ifile = cfopen(filename,"rb");          //try text version
479
480         if (!ifile) {                                                           //no text version, try binary version
481                 char filename2[FILENAME_LEN];
482                 change_filename_extension(filename2, filename, ".TXB");
483                 ifile = cfopen(filename2,"rb");
484                 if (!ifile)
485                         return 0;
486                 have_binary = 1;
487         }
488
489         size = cfilelength(ifile);
490
491         MALLOC (subtitle_raw_data, char, size+1);
492
493         read_count = cfread(subtitle_raw_data, 1, size, ifile);
494
495         cfclose(ifile);
496
497         subtitle_raw_data[size] = 0;
498
499         if (read_count != size) {
500                 d_free(subtitle_raw_data);
501                 return 0;
502         }
503
504         p = subtitle_raw_data;
505
506         while (p && p < subtitle_raw_data+size) {
507                 char *endp;
508
509                 endp = strchr(p,'\n'); 
510                 if (endp) {
511                         if (endp[-1] == '\r')
512                                 endp[-1] = 0;           //handle 0d0a pair
513                         *endp = 0;                      //string termintor
514                 }
515
516                 if (have_binary)
517                         decode_text_line(p);
518
519                 if (*p != ';') {
520                         Subtitles[Num_subtitles].first_frame = atoi(p);
521                         p = next_field(p); if (!p) continue;
522                         Subtitles[Num_subtitles].last_frame = atoi(p);
523                         p = next_field(p); if (!p) continue;
524                         Subtitles[Num_subtitles].msg = p;
525
526                         Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
527                         Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
528
529                         Num_subtitles++;
530                 }
531
532                 p = endp+1;
533
534         }
535
536         return 1;
537 }
538
539
540 void close_subtitles()
541 {
542         if (subtitle_raw_data)
543                 d_free(subtitle_raw_data);
544         subtitle_raw_data = NULL;
545         Num_subtitles = 0;
546 }
547
548
549 //draw the subtitles for this frame
550 void draw_subtitles(int frame_num)
551 {
552         static int active_subtitles[MAX_ACTIVE_SUBTITLES];
553         static int num_active_subtitles,next_subtitle,line_spacing;
554         int t,y;
555         int must_erase=0;
556
557         if (frame_num == 0) {
558                 num_active_subtitles = 0;
559                 next_subtitle = 0;
560                 gr_set_curfont( GAME_FONT );
561                 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
562                 gr_set_fontcolor(255,-1);
563         }
564
565         //get rid of any subtitles that have expired
566         for (t=0;t<num_active_subtitles;)
567                 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
568                         int t2;
569                         for (t2=t;t2<num_active_subtitles-1;t2++)
570                                 active_subtitles[t2] = active_subtitles[t2+1];
571                         num_active_subtitles--;
572                         must_erase = 1;
573                 }
574                 else
575                         t++;
576
577         //get any subtitles new for this frame 
578         while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
579                 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
580                         Error("Too many active subtitles!");
581                 active_subtitles[num_active_subtitles++] = next_subtitle;
582                 next_subtitle++;
583         }
584
585         //find y coordinate for first line of subtitles
586         y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
587
588         //erase old subtitles if necessary
589         if (must_erase) {
590                 gr_setcolor(0);
591                 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
592         }
593
594         //now draw the current subtitles
595         for (t=0;t<num_active_subtitles;t++)
596                 if (active_subtitles[t] != -1) {
597                         gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
598                         y += line_spacing+1;
599                 }
600 }
601
602
603 void close_movie(char *movielib, int is_robots)
604 {
605         int high_res;
606         char filename[FILENAME_LEN];
607
608         if (is_robots)
609                 high_res = MenuHiresAvailable;
610         else
611                 high_res = MovieHires.intval;
612
613         sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
614
615         if (!cfile_close(filename))
616         {
617                 con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
618                 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
619
620                 if (!cfile_close(filename))
621                         con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
622         }
623 }
624
625 void close_movies()
626 {
627         int i, is_robots;
628
629         for (i = 0 ; i < N_BUILTIN_MOVIE_LIBS ; i++)
630         {
631                 if (!strnicmp(movielib_files[i], "robot", 5))
632                         is_robots = 1;
633                 else
634                         is_robots = 0;
635
636                 close_movie(movielib_files[i], is_robots);
637         }
638 }
639
640
641 //ask user to put the D2 CD in.
642 //returns -1 if ESC pressed, 0 if OK chosen
643 //CD may not have been inserted
644 int request_cd(void)
645 {
646 #if 0
647         ubyte save_pal[256*3];
648         grs_canvas *save_canv,*tcanv;
649         int ret,was_faded=gr_palette_faded_out;
650
651         gr_palette_clear();
652
653         save_canv = grd_curcanv;
654         tcanv = gr_create_canvas(grd_curcanv->cv_w,grd_curcanv->cv_h);
655
656         gr_set_current_canvas(tcanv);
657         gr_ubitmap(0,0,&save_canv->cv_bitmap);
658         gr_set_current_canvas(save_canv);
659
660         gr_clear_canvas(BM_XRGB(0,0,0));
661
662         memcpy(save_pal,gr_palette,sizeof(save_pal));
663
664         memcpy(gr_palette,last_palette_for_color_fonts,sizeof(gr_palette));
665
666  try_again:;
667
668         ret = nm_messagebox( "CD ERROR", 1, "Ok", "Please insert your Descent II CD");
669
670         if (ret == -1) {
671                 int ret2;
672
673                 ret2 = nm_messagebox( "CD ERROR", 2, "Try Again", "Leave Game", "You must insert your\nDescent II CD to Continue");
674
675                 if (ret2 == -1 || ret2 == 0)
676                         goto try_again;
677         }
678
679         force_rb_register = 1;  //disc has changed; force register new CD
680
681         gr_palette_clear();
682
683         memcpy(gr_palette,save_pal,sizeof(save_pal));
684
685         gr_ubitmap(0,0,&tcanv->cv_bitmap);
686
687         if (!was_faded)
688                 gr_palette_load(gr_palette);
689
690         gr_free_canvas(tcanv);
691
692         return ret;
693 #else
694         con_printf(CON_DEBUG, "STUB: movie: request_cd\n");
695         return 0;
696 #endif
697 }
698
699
700 void init_movie(char *movielib, int is_robots, int required)
701 {
702         int high_res;
703         char filename[FILENAME_LEN];
704
705         //for robots, load highres versions if highres menus set
706         if (is_robots)
707                 high_res = MenuHiresAvailable;
708         else
709                 high_res = MovieHires.intval;
710
711         sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
712
713         if (!cfile_init(filename))
714         {
715                 if (required)
716                         con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
717
718                 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
719
720                 if (!cfile_init(filename))
721                         if (required)
722                                 con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
723         }
724 }
725
726
727 //find and initialize the movie libraries
728 void init_movies()
729 {
730         int i;
731         int is_robots;
732
733         if (FindArg("-nomovies"))
734                 return;
735
736         for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
737
738                 if (!strnicmp(movielib_files[i],"robot",5))
739                         is_robots = 1;
740                 else
741                         is_robots = 0;
742
743                 init_movie(movielib_files[i], is_robots, 1);
744         }
745
746         atexit(close_movies);
747 }
748
749
750 void close_extra_robot_movie(void)
751 {
752         if (strlen(movielib_files[EXTRA_ROBOT_LIB]))
753                 if (!cfile_close(movielib_files[EXTRA_ROBOT_LIB]))
754                         con_printf(CON_URGENT, "Can't close robot movielib: %s\n", PHYSFS_getLastError());
755 }
756
757 void init_extra_robot_movie(char *movielib)
758 {
759         if (FindArg("-nomovies"))
760                 return;
761
762         close_extra_robot_movie();
763         init_movie(movielib, 1, 0);
764         strcpy(movielib_files[EXTRA_ROBOT_LIB], movielib);
765         atexit(close_extra_robot_movie);
766 }