]> icculus.org git repositories - btb/d2x.git/blob - main/movie.c
use the orientation parameter of g3_draw_bitmap
[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 "dxxerror.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 #include "ignorecase.h"
50 #include "timer.h"
51
52
53 // Subtitle data
54 typedef struct {
55         short first_frame,last_frame;
56         char *msg;
57 } subtitle;
58
59 #define BUFFER_MOVIE
60
61 #define MAX_SUBTITLES 500
62 #define MAX_ACTIVE_SUBTITLES 3
63 subtitle Subtitles[MAX_SUBTITLES];
64 int Num_subtitles;
65
66 // Movielib data
67
68 #ifdef D2_OEM
69 char movielib_files[5][FILENAME_LEN] = {"intro","other","robots","oem"};
70 #else
71 char movielib_files[4][FILENAME_LEN] = {"intro","other","robots"};
72 #endif
73
74 #define N_MOVIE_LIBS (sizeof(movielib_files) / sizeof(*movielib_files))
75 #define N_BUILTIN_MOVIE_LIBS (N_MOVIE_LIBS - 1)
76 #define EXTRA_ROBOT_LIB N_BUILTIN_MOVIE_LIBS
77
78 cvar_t MovieHires = { "MovieHires", "1", CVAR_ARCHIVE }; // default is highres
79
80 #ifdef BUFFER_MOVIE
81 char *RoboBuffer[50];
82 unsigned char RobBufCount = 0, PlayingBuf = 0, RobBufLimit = 0;
83 int RoboFilePos = 0;
84 #endif
85
86 SDL_RWops *RoboFile = NULL;
87 MVE_videoSpec MVESpec;
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 // Abstraction for movie files
96 static SDL_RWops *open_movie_file(const char *filename, int must_have);
97 #define read_movie_file(handle, buf, count) SDL_RWread((SDL_RWops *)handle, buf, 1, count);
98 #define reset_movie_file(handle) SDL_RWseek(handle, 0, SEEK_SET)
99 #define close_movie_file(handle) SDL_RWclose(handle)
100
101
102 // Callbacks for MVE lib
103
104 // ----------------------------------------------------------------------
105 void* MPlayAlloc(unsigned size)
106 {
107     return d_malloc(size);
108 }
109
110 void MPlayFree(void *p)
111 {
112     d_free(p);
113 }
114
115
116 //-----------------------------------------------------------------------
117
118 unsigned int FileRead(void *handle, void *buf, unsigned int count)
119 {
120     unsigned numread;
121     numread = read_movie_file(handle, buf, count);
122     return (numread == count);
123 }
124
125
126 //-----------------------------------------------------------------------
127
128
129 //filename will actually get modified to be either low-res or high-res
130 //returns status.  see values in movie.h
131 int PlayMovie(const char *filename, int must_have)
132 {
133         char name[FILENAME_LEN],*p;
134         int c, ret;
135
136         if (FindArg("-nomovies"))
137                 return MOVIE_NOT_PLAYED;
138
139         strcpy(name,filename);
140
141         if ((p=strchr(name,'.')) == NULL)               //add extension, if missing
142                 strcat(name,".mve");
143
144         //check for escape already pressed & abort if so
145         while ((c = newmenu_inkey()) != 0)
146                 if (c == KEY_ESC)
147                         return MOVIE_ABORTED;
148
149         // Stop all digital sounds currently playing.
150         digi_stop_all();
151
152         // Stop all songs
153         songs_stop_all();
154
155         digi_close();
156
157         // Start sound
158         if (!FindArg("-nosound"))
159                 MVE_sndInit(1);
160         else
161                 MVE_sndInit(-1);
162
163         ret = RunMovie(name, MovieHires.intval, must_have, -1, -1);
164
165         if (!FindArg("-nosound"))
166                 digi_init();
167
168         Screen_mode = -1;               //force screen reset
169
170         return ret;
171 }
172
173
174 void MovieShowFrame(ubyte *buf, uint bufw, uint bufh, uint sx, uint sy,
175                                         uint w, uint h, uint dstx, uint dsty)
176 {
177         grs_bitmap source_bm;
178         grs_canvas *dest_canv, *save_canv;
179
180         //mprintf((0,"MovieShowFrame %d,%d  %d,%d  %d,%d  %d,%d\n",bufw,bufh,sx,sy,w,h,dstx,dsty));
181
182         Assert(bufw == w && bufh == h);
183
184         source_bm.bm_x = source_bm.bm_y = 0;
185         source_bm.bm_w = source_bm.bm_rowsize = bufw;
186         source_bm.bm_h = bufh;
187         source_bm.bm_type = BM_LINEAR;
188         source_bm.bm_flags = 0;
189         source_bm.bm_data = buf;
190
191 #ifdef BUFFER_MOVIE
192         if (RoboFile)
193                 memcpy(RoboBuffer[RobBufCount++], buf, bufw * bufh);
194 #endif
195
196         if (menu_use_game_res.intval) {
197                 float aspect = (float)w / (float)h;
198
199                 if (RoboFile) {
200                         h = h * GHEIGHT / MVESpec.screenHeight;
201                         w = h * aspect;
202                         dstx = dstx * GWIDTH / MVESpec.screenWidth;
203                         dsty = dsty * GHEIGHT / MVESpec.screenHeight;
204                 } else {
205                         w = w * GWIDTH / MVESpec.screenWidth;
206                         h = w / aspect;
207                         dstx = dstx * GWIDTH / MVESpec.screenWidth;
208                         dsty = GHEIGHT / 2 - h / 2;
209                 }
210                 dest_canv = gr_create_sub_canvas(grd_curcanv, dstx, dsty, w, h);
211                 save_canv = grd_curcanv;
212                 gr_set_current_canvas(dest_canv);
213                 gr_bitmap_fullscr(&source_bm);
214                 gr_set_current_canvas(save_canv);
215                 gr_free_sub_canvas(dest_canv);
216         } else
217                 gr_bm_ubitblt(bufw,bufh,dstx,dsty,sx,sy,&source_bm,&grd_curcanv->cv_bitmap);
218 }
219
220 //our routine to set the pallete, called from the movie code
221 void MovieSetPalette(unsigned char *p, unsigned start, unsigned count)
222 {
223         if (count == 0)
224                 return;
225
226         //mprintf((0,"SetPalette p=%x, start=%d, count=%d\n",p,start,count));
227
228         //Color 0 should be black, and we get color 255
229         Assert(start>=1 && start+count-1<=254);
230
231         //Set color 0 to be black
232         gr_palette[0] = gr_palette[1] = gr_palette[2] = 0;
233
234         //Set color 255 to be our subtitle color
235         gr_palette[765] = gr_palette[766] = gr_palette[767] = 50;
236
237         //movie libs palette into our array
238         memcpy(gr_palette+start*3,p+start*3,count*3);
239
240         //finally set the palette in the hardware
241         gr_palette_load(gr_palette);
242
243         //MVE_SetPalette(p, start, count);
244 }
245
246
247 #if 0
248 typedef struct bkg {
249         short x, y, w, h;           // The location of the menu.
250         grs_bitmap * bmp;               // The background under the menu.
251 } bkg;
252
253 bkg movie_bg = {0,0,0,0,NULL};
254 #endif
255
256 #define BOX_BORDER (MenuHires?40:20)
257
258
259 void show_pause_message(char *msg)
260 {
261         int w,h,aw;
262         int x,y;
263
264         gr_set_current_canvas(NULL);
265         gr_set_curfont( SMALL_FONT );
266
267         gr_get_string_size(msg,&w,&h,&aw);
268
269         x = (grd_curscreen->sc_w-w)/2;
270         y = (grd_curscreen->sc_h-h)/2;
271
272 #if 0
273         if (movie_bg.bmp) {
274                 gr_free_bitmap(movie_bg.bmp);
275                 movie_bg.bmp = NULL;
276         }
277
278         // Save the background of the display
279         movie_bg.x=x; movie_bg.y=y; movie_bg.w=w; movie_bg.h=h;
280
281         movie_bg.bmp = gr_create_bitmap( w+BOX_BORDER, h+BOX_BORDER );
282
283         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 );
284 #endif
285
286         gr_setcolor(0);
287         gr_rect(x-BOX_BORDER/2,y-BOX_BORDER/2,x+w+BOX_BORDER/2-1,y+h+BOX_BORDER/2-1);
288
289         gr_set_fontcolor( 255, -1 );
290
291         gr_ustring( 0x8000, y, msg );
292
293         vid_update();
294 }
295
296 void clear_pause_message()
297 {
298 #if 0
299         if (movie_bg.bmp) {
300
301                 gr_bitmap(movie_bg.x-BOX_BORDER/2, movie_bg.y-BOX_BORDER/2, movie_bg.bmp);
302
303                 gr_free_bitmap(movie_bg.bmp);
304                 movie_bg.bmp = NULL;
305         }
306 #endif
307 }
308
309
310 //returns status.  see movie.h
311 int RunMovie(char *filename, int hires_flag, int must_have,int dx,int dy)
312 {
313         SDL_RWops *filehndl;
314         int result=1,aborted=0;
315         int track = 0;
316         int frame_num;
317         int key;
318
319         result=1;
320
321         // Open Movie file.  If it doesn't exist, no movie, just return.
322
323         filehndl = open_movie_file(filename, must_have);
324
325         if (!filehndl)
326         {
327                 if (must_have)
328                         con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
329                 return MOVIE_NOT_PLAYED;
330         }
331
332         MVE_memCallbacks(MPlayAlloc, MPlayFree);
333         MVE_ioCallbacks(FileRead);
334         MVE_sfCallbacks(MovieShowFrame);
335         MVE_palCallbacks(MovieSetPalette);
336
337         vid_set_mode(MOVIE_SCREEN_MODE);
338 #ifdef OGL
339         set_screen_mode(SCREEN_MENU);
340 #endif
341
342         if (MVE_rmPrepMovie((void *)filehndl, dx, dy, track)) {
343                 Int3();
344                 return MOVIE_NOT_PLAYED;
345         }
346
347         MVE_getVideoSpec(&MVESpec);
348
349         frame_num = 0;
350
351         FontHires = FontHiresAvailable && hires_flag;
352
353         while((result = MVE_rmStepMovie()) == 0) {
354
355                 draw_subtitles(frame_num);
356
357                 vid_update();
358
359                 key = newmenu_inkey();
360
361                 // If ESCAPE pressed, then quit movie.
362                 if (key == KEY_ESC) {
363                         result = aborted = 1;
364                         break;
365                 }
366
367                 // If PAUSE pressed, then pause movie
368                 if (key == KEY_PAUSE) {
369                         MVE_rmHoldMovie();
370                         show_pause_message(TXT_PAUSE);
371                         while (!newmenu_inkey()) ;
372                         clear_pause_message();
373                 }
374
375 #ifdef VID_SUPPORTS_FULLSCREEN_TOGGLE
376                 if ((key == KEY_COMMAND+KEY_SHIFTED+KEY_F) ||
377                         (key == KEY_ALTED+KEY_ENTER) ||
378                     (key == KEY_ALTED+KEY_PADENTER))
379                         vid_toggle_fullscreen();
380 #endif
381
382                 frame_num++;
383         }
384
385         Assert(aborted || result == MVE_ERR_EOF);        ///movie should be over
386
387     MVE_rmEndMovie();
388
389         close_movie_file(filehndl); // Close Movie File
390
391         // Restore old graphic state
392
393         Screen_mode=-1;  //force reset of screen mode
394
395         return (aborted?MOVIE_ABORTED:MOVIE_PLAYED_FULL);
396 }
397
398
399 int InitMovieBriefing()
400 {
401 #if 0
402         if (MenuHires)
403                 vid_set_mode(SM(640,480));
404         else
405                 vid_set_mode(SM(320,200));
406
407         gr_init_sub_canvas( &VR_screen_pages[0], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
408         gr_init_sub_canvas( &VR_screen_pages[1], &grd_curscreen->sc_canvas, 0, 0, grd_curscreen->sc_w, grd_curscreen->sc_h );
409 #endif
410
411         return 1;
412 }
413
414
415 #ifdef BUFFER_MOVIE
416 static fix RobBufTime = 0;
417 #endif
418
419
420 void ShowRobotBuffer()
421 {
422         // shows a frame from the robot buffer
423
424 #ifndef BUFFER_MOVIE
425         Int3(); // Get Jason...how'd we get here?
426         return;
427 #else
428         grs_bitmap source_bm;
429         grs_canvas *dest_canv, *save_canv;
430         int rw, rh, rdx, rdy;
431
432
433         if (timer_get_approx_seconds() < (RobBufTime + fixdiv(F1_0, i2f(15))))
434                 return;
435
436         RobBufTime = timer_get_approx_seconds();
437
438         if (MenuHires) {
439                 rw=320; rh=200; rdx=280; rdy=200;
440         } else {
441                 rw=160; rh=100; rdx=140; rdy=80;
442         }
443
444         source_bm.bm_x = source_bm.bm_y = 0;
445         source_bm.bm_w = source_bm.bm_rowsize = rw;
446         source_bm.bm_h = rh;
447         source_bm.bm_type = BM_LINEAR;
448         source_bm.bm_flags = 0;
449
450         source_bm.bm_data = (unsigned char *)RoboBuffer[RobBufCount];
451
452         if (menu_use_game_res.intval) {
453                 float aspect = (float)rw / (float)rh;
454
455                 rh = rh * GHEIGHT / MVESpec.screenHeight;
456                 rw = rh * aspect;
457                 rdx = rdx * GWIDTH / MVESpec.screenWidth;
458                 rdy = rdy * GHEIGHT / MVESpec.screenHeight;
459
460                 dest_canv = gr_create_sub_canvas(grd_curcanv, rdx, rdy, rw, rh);
461                 save_canv = grd_curcanv;
462                 gr_set_current_canvas(dest_canv);
463                 gr_bitmap_fullscr(&source_bm);
464                 gr_set_current_canvas(save_canv);
465                 gr_free_sub_canvas(dest_canv);
466         } else
467                 gr_bm_ubitblt(rw, rh, rdx, rdy, 0, 0, &source_bm, &grd_curcanv->cv_bitmap);
468
469         RobBufCount++;
470         RobBufCount %= RobBufLimit;
471 #endif
472 }
473
474
475 //returns 1 if frame updated ok
476 int RotateRobot()
477 {
478         int err;
479
480 #ifdef BUFFER_MOVIE
481         if (PlayingBuf)
482         {
483                 ShowRobotBuffer();
484                 return 1;
485         }
486 #endif
487
488         err = MVE_rmStepMovie();
489
490         if (err == MVE_ERR_EOF)     //end of movie, so reset
491         {
492 #ifdef BUFFER_MOVIE
493                 PlayingBuf = 1;
494                 RobBufLimit = RobBufCount;
495                 RobBufCount = 0;
496                 //RobBufTime=timer_get_approx_seconds();
497                 return 1;
498 #else
499                 reset_movie_file(RoboFile);
500                 if (MVE_rmPrepMovie(RoboFile, MenuHires?280:140, MenuHires?200:80, 0))
501                 {
502                         Int3();
503                         return 0;
504                 }
505 #endif
506         }
507         else if (err) {
508                 Int3();
509                 return 0;
510         }
511
512         return 1;
513 }
514
515
516 void FreeRoboBuffer(int n)
517 {
518         // frees the 64k frame buffers, starting with n and then working down
519
520 #ifndef BUFFER_MOVIE
521         n++; // kill warning
522         return;
523 #else
524         int i;
525
526         for (i = n; i >= 0; i--)
527                 d_free(RoboBuffer[i]);
528 #endif
529 }
530
531
532 void DeInitRobotMovie(void)
533 {
534 #ifdef BUFFER_MOVIE
535         RobBufCount = 0;
536         PlayingBuf = 0;
537 #endif
538
539         MVE_rmEndMovie();
540
541         FreeRoboBuffer(49);
542
543         close_movie_file(RoboFile); // Close Movie File
544         RoboFile = NULL;
545 }
546
547
548 int InitRobotMovie(char *filename)
549 {
550 #ifdef BUFFER_MOVIE
551         int i;
552
553         RobBufCount = 0;
554         PlayingBuf = 0;
555         RobBufLimit = 0;
556 #endif
557
558         if (FindArg("-nomovies"))
559                 return 0;
560
561         con_printf(CON_DEBUG, "RoboFile=%s\n", filename);
562
563 #ifdef BUFFER_MOVIE
564
565         for (i = 0; i < 50; i++)
566         {
567                 if (MenuHires)
568                         RoboBuffer[i] = d_malloc(65000L);
569                 else
570                         RoboBuffer[i] = d_malloc(17000L);
571
572                 if (RoboBuffer[i] == NULL)
573                 {
574                         con_printf(CON_URGENT, "ROBOERROR: Could't allocate frame %d!\n", i);
575                         if (i)
576                                 FreeRoboBuffer(i - 1);
577                         return 0;
578                 }
579         }
580 #endif
581
582         MVE_sndInit(-1);        //tell movies to play no sound for robots
583
584         MVE_memCallbacks(MPlayAlloc, MPlayFree);
585         MVE_ioCallbacks(FileRead);
586
587         RoboFile = open_movie_file(filename, 1);
588
589         if (!RoboFile)
590         {
591                 FreeRoboBuffer(49);
592                 con_printf(CON_URGENT, "Can't open movie <%s>: %s\n", filename, PHYSFS_getLastError());
593                 return MOVIE_NOT_PLAYED;
594         }
595
596         MVE_palCallbacks(MovieSetPalette);
597         MVE_sfCallbacks(MovieShowFrame);
598
599         if (MVE_rmPrepMovie((void *)RoboFile, MenuHires?280:140, MenuHires?200:80, 0)) {
600                 Int3();
601                 FreeRoboBuffer(49);
602                 return 0;
603         }
604
605         MVE_getVideoSpec(&MVESpec);
606
607 #ifdef BUFFER_MOVIE
608         RoboFilePos = SDL_RWseek(RoboFile, 0L, SEEK_CUR);
609         con_printf(CON_DEBUG, "RoboFilePos=%d!\n", RoboFilePos);
610 #endif
611
612         return 1;
613 }
614
615
616 /*
617  *              Subtitle system code
618  */
619
620 char *subtitle_raw_data;
621
622
623 //search for next field following whitespace 
624 char *next_field (char *p)
625 {
626         while (*p && !isspace(*p))
627                 p++;
628
629         if (!*p)
630                 return NULL;
631
632         while (*p && isspace(*p))
633                 p++;
634
635         if (!*p)
636                 return NULL;
637
638         return p;
639 }
640
641
642 int init_subtitles(char *filename)
643 {
644         CFILE *ifile;
645         int size,read_count;
646         char *p;
647         int have_binary = 0;
648
649         Num_subtitles = 0;
650
651         if (! FindArg("-subtitles"))
652                 return 0;
653
654         ifile = cfopen(filename,"rb");          //try text version
655
656         if (!ifile) {                                                           //no text version, try binary version
657                 char filename2[FILENAME_LEN];
658                 change_filename_extension(filename2, filename, ".TXB");
659                 ifile = cfopen(filename2,"rb");
660                 if (!ifile)
661                         return 0;
662                 have_binary = 1;
663         }
664
665         size = cfilelength(ifile);
666
667         MALLOC (subtitle_raw_data, char, size+1);
668
669         read_count = cfread(subtitle_raw_data, 1, size, ifile);
670
671         cfclose(ifile);
672
673         subtitle_raw_data[size] = 0;
674
675         if (read_count != size) {
676                 d_free(subtitle_raw_data);
677                 return 0;
678         }
679
680         p = subtitle_raw_data;
681
682         while (p && p < subtitle_raw_data+size) {
683                 char *endp;
684
685                 endp = strchr(p,'\n'); 
686                 if (endp) {
687                         if (endp[-1] == '\r')
688                                 endp[-1] = 0;           //handle 0d0a pair
689                         *endp = 0;                      //string termintor
690                 }
691
692                 if (have_binary)
693                         decode_text_line(p);
694
695                 if (*p != ';') {
696                         Subtitles[Num_subtitles].first_frame = atoi(p);
697                         p = next_field(p); if (!p) continue;
698                         Subtitles[Num_subtitles].last_frame = atoi(p);
699                         p = next_field(p); if (!p) continue;
700                         Subtitles[Num_subtitles].msg = p;
701
702                         Assert(Num_subtitles==0 || Subtitles[Num_subtitles].first_frame >= Subtitles[Num_subtitles-1].first_frame);
703                         Assert(Subtitles[Num_subtitles].last_frame >= Subtitles[Num_subtitles].first_frame);
704
705                         Num_subtitles++;
706                 }
707
708                 p = endp+1;
709
710         }
711
712         return 1;
713 }
714
715
716 void close_subtitles()
717 {
718         if (subtitle_raw_data)
719                 d_free(subtitle_raw_data);
720         subtitle_raw_data = NULL;
721         Num_subtitles = 0;
722 }
723
724
725 //draw the subtitles for this frame
726 void draw_subtitles(int frame_num)
727 {
728         static int active_subtitles[MAX_ACTIVE_SUBTITLES];
729         static int num_active_subtitles,next_subtitle,line_spacing;
730         int t,y;
731         int must_erase=0;
732
733         if (frame_num == 0) {
734                 num_active_subtitles = 0;
735                 next_subtitle = 0;
736                 gr_set_curfont( GAME_FONT );
737                 line_spacing = grd_curcanv->cv_font->ft_h + (grd_curcanv->cv_font->ft_h >> 2);
738                 gr_set_fontcolor(255,-1);
739         }
740
741         //get rid of any subtitles that have expired
742         for (t=0;t<num_active_subtitles;)
743                 if (frame_num > Subtitles[active_subtitles[t]].last_frame) {
744                         int t2;
745                         for (t2=t;t2<num_active_subtitles-1;t2++)
746                                 active_subtitles[t2] = active_subtitles[t2+1];
747                         num_active_subtitles--;
748                         must_erase = 1;
749                 }
750                 else
751                         t++;
752
753         //get any subtitles new for this frame 
754         while (next_subtitle < Num_subtitles && frame_num >= Subtitles[next_subtitle].first_frame) {
755                 if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES)
756                         Error("Too many active subtitles!");
757                 active_subtitles[num_active_subtitles++] = next_subtitle;
758                 next_subtitle++;
759         }
760
761         //find y coordinate for first line of subtitles
762         y = grd_curcanv->cv_bitmap.bm_h-((line_spacing+1)*MAX_ACTIVE_SUBTITLES+2);
763
764         //erase old subtitles if necessary
765         if (must_erase) {
766                 gr_setcolor(0);
767                 gr_rect(0,y,grd_curcanv->cv_bitmap.bm_w-1,grd_curcanv->cv_bitmap.bm_h-1);
768         }
769
770         //now draw the current subtitles
771         for (t=0;t<num_active_subtitles;t++)
772                 if (active_subtitles[t] != -1) {
773                         gr_string(0x8000,y,Subtitles[active_subtitles[t]].msg);
774                         y += line_spacing+1;
775                 }
776 }
777
778
779 //find the specified movie library, and read in list of movies in it
780 static int init_movie_lib(char *filename)
781 {
782         //note: this based on cfile_init_hogfile()
783
784         char pathname[PATH_MAX];
785
786         if (!PHYSFSX_getRealPath(filename, pathname))
787                 return 0;
788
789         return PHYSFS_mount(pathname, "movies", 1);
790 }
791
792
793 void close_movie(char *movielib, int is_robots)
794 {
795         int high_res;
796         char filename[FILENAME_LEN];
797         char pathname[PATH_MAX];
798
799         if (is_robots)
800                 high_res = MenuHiresAvailable;
801         else
802                 high_res = MovieHires.intval;
803
804         sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
805
806         if (!PHYSFSX_getRealPath(filename, pathname) ||
807                 !PHYSFS_removeFromSearchPath(pathname))
808         {
809                 con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
810                 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
811
812                 if (!PHYSFSX_getRealPath(filename, pathname) ||
813                         !PHYSFS_removeFromSearchPath(pathname))
814                         con_printf(CON_URGENT, "Can't close movielib <%s>: %s\n", filename, PHYSFS_getLastError());
815         }
816 }
817
818 void close_movies()
819 {
820         int i, is_robots;
821
822         for (i = 0 ; i < N_BUILTIN_MOVIE_LIBS ; i++)
823         {
824                 if (!strnicmp(movielib_files[i], "robot", 5))
825                         is_robots = 1;
826                 else
827                         is_robots = 0;
828
829                 close_movie(movielib_files[i], is_robots);
830         }
831 }
832
833
834 void init_movie(char *movielib, int is_robots, int required)
835 {
836         int high_res;
837         char filename[FILENAME_LEN];
838
839         //for robots, load highres versions if highres menus set
840         if (is_robots)
841                 high_res = MenuHiresAvailable;
842         else
843                 high_res = MovieHires.intval;
844
845         sprintf(filename, "%s-%s.mvl", movielib, high_res?"h":"l");
846
847         if (!init_movie_lib(filename))
848         {
849                 if (required)
850                         con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
851
852                 sprintf(filename, "%s-%s.mvl", movielib, high_res?"l":"h");
853
854                 if (!init_movie_lib(filename))
855                         if (required)
856                                 con_printf(CON_URGENT, "Can't open movielib <%s>: %s\n", filename, PHYSFS_getLastError());
857         }
858 }
859
860
861 //find and initialize the movie libraries
862 void init_movies()
863 {
864         int i;
865         int is_robots;
866
867         if (FindArg("-nomovies"))
868                 return;
869
870         for (i=0;i<N_BUILTIN_MOVIE_LIBS;i++) {
871
872                 if (!strnicmp(movielib_files[i],"robot",5))
873                         is_robots = 1;
874                 else
875                         is_robots = 0;
876
877                 init_movie(movielib_files[i], is_robots, 1);
878         }
879
880         atexit(close_movies);
881 }
882
883
884 void close_extra_robot_movie(void)
885 {
886         if (strlen(movielib_files[EXTRA_ROBOT_LIB]))
887                 close_movie(movielib_files[EXTRA_ROBOT_LIB], 1);
888 }
889
890 void init_extra_robot_movie(char *movielib)
891 {
892         if (FindArg("-nomovies"))
893                 return;
894
895         close_extra_robot_movie();
896         init_movie(movielib, 1, 0);
897         strcpy(movielib_files[EXTRA_ROBOT_LIB], movielib);
898         atexit(close_extra_robot_movie);
899 }
900
901
902 //returns file handle
903 static SDL_RWops *open_movie_file(const char *filename, int must_have)
904 {
905         char moviepath[PATH_MAX];
906
907         strcpy(moviepath, "movies/");
908         strcat(moviepath, filename);
909
910         PHYSFSEXT_locateCorrectCase(moviepath);
911
912         return PHYSFSRWOPS_openRead(moviepath);
913 }