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