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