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