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