make infobar height configurable
[divverent/darkplaces.git] / cl_video.c
1
2 #include "quakedef.h"
3 #include "cl_dyntexture.h"
4 #include "cl_video.h"
5 #include "dpvsimpledecode.h"
6
7 // VorteX: JAM video module used by Blood Omnicide
8 //#define USEJAM
9 #ifdef USEJAM
10   #include "jamdecode.c"
11 #endif
12
13 // cvars
14 cvar_t cl_video_subtitles = {CVAR_SAVE, "cl_video_subtitles", "0", "show subtitles for videos (if they are presented)"};
15 cvar_t cl_video_subtitles_lines = {CVAR_SAVE, "cl_video_subtitles_lines", "4", "how many lines to occupy for subtitles"};
16 cvar_t cl_video_subtitles_textsize = {CVAR_SAVE, "cl_video_subtitles_textsize", "16", "textsize for subtitles"};
17 cvar_t cl_video_scale = {CVAR_SAVE, "cl_video_scale", "1", "scale of video, 1 = fullscreen, 0.75 - 3/4 of screen etc."};
18 cvar_t cl_video_scale_vpos = {CVAR_SAVE, "cl_video_scale_vpos", "0", "vertial align of scaled video, -1 is top, 1 is bottom"};
19 cvar_t cl_video_stipple = {CVAR_SAVE, "cl_video_stipple", "0", "draw interlacing-like effect on videos, similar to scr_stipple but static and used only with video playing."};
20 cvar_t cl_video_brightness = {CVAR_SAVE, "cl_video_brightness", "1", "brightness of video, 1 = fullbright, 0.75 - 3/4 etc."};
21 cvar_t cl_video_keepaspectratio = {CVAR_SAVE, "cl_video_keepaspectratio", "0", "keeps aspect ratio of fullscreen videos, leaving black color on unfilled areas, a value of 2 let video to be stretched horizontally with to & bottom being sliced out"};
22 cvar_t cl_video_fadein = {CVAR_SAVE, "cl_video_fadein", "0", "fading-from-black effect once video is started, in seconds"};
23 cvar_t cl_video_fadeout = {CVAR_SAVE, "cl_video_fadeout", "0", "fading-to-black effect once video is ended, in seconds"};
24
25 // constants (and semi-constants)
26 static int  cl_videormask;
27 static int  cl_videobmask;
28 static int  cl_videogmask;
29 static int      cl_videobytesperpixel;
30
31 static int cl_num_videos;
32 static clvideo_t cl_videos[ MAXCLVIDEOS ];
33 static rtexturepool_t *cl_videotexturepool;
34
35 static clvideo_t *FindUnusedVid( void )
36 {
37         int i;
38         for( i = 1 ; i < MAXCLVIDEOS ; i++ )
39                 if( cl_videos[ i ].state == CLVIDEO_UNUSED )
40                         return &cl_videos[ i ];
41         return NULL;
42 }
43
44 static qboolean OpenStream( clvideo_t * video )
45 {
46         const char *errorstring;
47         video->stream = dpvsimpledecode_open( video, video->filename, &errorstring);
48         if (!video->stream )
49         {
50 #ifdef USEJAM
51                 video->stream = jam_open( video, video->filename, &errorstring);
52                 if (video->stream)
53                         return true;
54 #endif
55                 Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
56                 return false;
57         }
58         return true;
59 }
60
61 static void VideoUpdateCallback(rtexture_t *rt, void *data)
62 {
63         clvideo_t *video = (clvideo_t *) data;
64         R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, video->cpif.width, video->cpif.height );
65 }
66
67 static void LinkVideoTexture( clvideo_t *video )
68 {
69         video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, video->cpif.width, video->cpif.height, NULL, TEXTYPE_BGRA, TEXF_PERSISTENT | TEXF_CLAMP, -1, NULL );
70         R_MakeTextureDynamic( video->cpif.tex, VideoUpdateCallback, video );
71         CL_LinkDynTexture( video->cpif.name, video->cpif.tex );
72 }
73
74 static void UnlinkVideoTexture( clvideo_t *video )
75 {
76         CL_UnlinkDynTexture( video->cpif.name );
77         // free the texture
78         R_FreeTexture( video->cpif.tex );
79         video->cpif.tex = NULL;
80         // free the image data
81         Mem_Free( video->imagedata );
82 }
83
84 static void SuspendVideo( clvideo_t * video )
85 {
86         if (video->suspended)
87                 return;
88         video->suspended = true;
89         UnlinkVideoTexture(video);
90         // if we are in firstframe mode, also close the stream
91         if (video->state == CLVIDEO_FIRSTFRAME)
92                 video->close(video->stream);
93 }
94
95 static qboolean WakeVideo( clvideo_t * video )
96 {
97         if( !video->suspended )
98                 return true;
99         video->suspended = false;
100
101         if( video->state == CLVIDEO_FIRSTFRAME )
102                 if( !OpenStream( video ) ) {
103                         video->state = CLVIDEO_UNUSED;
104                         return false;
105                 }
106
107         video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
108         LinkVideoTexture( video );
109
110         // update starttime
111         video->starttime += realtime - video->lasttime;
112
113         return true;
114 }
115
116 static void LoadSubtitles( clvideo_t *video, const char *subtitlesfile )
117 {
118         char *subtitle_text;
119         const char *data;
120         float subtime, sublen;
121         int numsubs = 0;
122
123         if (gamemode == GAME_BLOODOMNICIDE)
124         {
125                 char overridename[MAX_QPATH];
126                 cvar_t *langcvar;
127
128                 langcvar = Cvar_FindVar("language");
129                 subtitle_text = NULL;
130                 if (langcvar)
131                 {
132                         dpsnprintf(overridename, sizeof(overridename), "script/locale/%s/%s", langcvar->string, subtitlesfile);
133                         subtitle_text = (char *)FS_LoadFile(overridename, cls.permanentmempool, false, NULL);
134                 }
135                 if (!subtitle_text)
136                         subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
137         }
138         else
139         {
140                 subtitle_text = (char *)FS_LoadFile(subtitlesfile, cls.permanentmempool, false, NULL);
141         }
142         if (!subtitle_text)
143         {
144                 Con_DPrintf( "LoadSubtitles: can't open subtitle file '%s'!\n", subtitlesfile );
145                 return;
146         }
147
148         // parse subtitle_text
149         // line is: x y "text" where
150         //    x - start time
151         //    y - seconds last (if 0 - last thru next sub, if negative - last to next sub - this amount of seconds)
152
153         data = subtitle_text;
154         for (;;)
155         {
156                 if (!COM_ParseToken_QuakeC(&data, false))
157                         break;
158                 subtime = atof( com_token );
159                 if (!COM_ParseToken_QuakeC(&data, false))
160                         break;
161                 sublen = atof( com_token );
162                 if (!COM_ParseToken_QuakeC(&data, false))
163                         break;
164                 if (!com_token[0])
165                         continue;
166                 // check limits
167                 if (video->subtitles == CLVIDEO_MAX_SUBTITLES)
168                 {
169                         Con_Printf("WARNING: CLVIDEO_MAX_SUBTITLES = %i reached when reading subtitles from '%s'\n", CLVIDEO_MAX_SUBTITLES, subtitlesfile);
170                         break;  
171                 }
172                 // add a sub
173                 video->subtitle_text[numsubs] = (char *) Mem_Alloc(cls.permanentmempool, strlen(com_token) + 1);
174                 memcpy(video->subtitle_text[numsubs], com_token, strlen(com_token) + 1);
175                 video->subtitle_start[numsubs] = subtime;
176                 video->subtitle_end[numsubs] = sublen;
177                 if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
178                 {
179                         if (video->subtitle_end[numsubs-1] <= 0)
180                                 video->subtitle_end[numsubs-1] = max(video->subtitle_start[numsubs-1], video->subtitle_start[numsubs] + video->subtitle_end[numsubs-1]);
181                         else
182                                 video->subtitle_end[numsubs-1] = min(video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1], video->subtitle_start[numsubs]);
183                 }
184                 numsubs++;
185                 // todo: check timing for consistency?
186         }
187         if (numsubs > 0) // make true len for prev sub, autofix overlapping subtitles
188         {
189                 if (video->subtitle_end[numsubs-1] <= 0)
190                         video->subtitle_end[numsubs-1] = 99999999; // fixme: make it end when video ends?
191                 else
192                         video->subtitle_end[numsubs-1] = video->subtitle_start[numsubs-1] + video->subtitle_end[numsubs-1];
193         }
194         Z_Free( subtitle_text );
195         video->subtitles = numsubs;
196 /*
197         Con_Printf( "video->subtitles: %i\n", video->subtitles );
198         for (numsubs = 0; numsubs < video->subtitles; numsubs++)
199                 Con_Printf( "  %03.2f %03.2f : %s\n", video->subtitle_start[numsubs], video->subtitle_end[numsubs], video->subtitle_text[numsubs] );
200 */
201 }
202
203 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner, const char *subtitlesfile )
204 {
205         strlcpy( video->filename, filename, sizeof(video->filename) );
206         video->ownertag = owner;
207         if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
208                 return NULL;
209         strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
210
211         if( !OpenStream( video ) )
212                 return NULL;
213
214         video->state = CLVIDEO_FIRSTFRAME;
215         video->framenum = -1;
216         video->framerate = video->getframerate( video->stream );
217         video->lasttime = realtime;
218         video->subtitles = 0;
219
220         video->cpif.width = video->getwidth( video->stream );
221         video->cpif.height = video->getheight( video->stream );
222         video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
223         LinkVideoTexture( video );
224
225         // VorteX: load simple subtitle_text file
226         if (subtitlesfile[0])
227                 LoadSubtitles( video, subtitlesfile );
228
229         return video;
230 }
231
232 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner, const char *subtitlesfile )
233 {
234         clvideo_t *video;
235         // sanity check
236         if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) {
237                 Con_DPrintf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
238                 return NULL;
239         }
240
241         video = FindUnusedVid();
242         if( !video ) {
243                 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
244                 return NULL;
245         }
246         video = OpenVideo( video, filename, name, owner, subtitlesfile );
247         // expand the active range to include the new entry
248         if (video) {
249                 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
250         }
251         return video;
252 }
253
254 static clvideo_t* CL_GetVideoBySlot( int slot )
255 {
256         clvideo_t *video = &cl_videos[ slot ];
257
258         if( video->suspended )
259         {
260                 if( !WakeVideo( video ) )
261                         return NULL;
262                 else if( video->state == CLVIDEO_RESETONWAKEUP )
263                         video->framenum = -1;
264         }
265
266         video->lasttime = realtime;
267
268         return video;
269 }
270
271 clvideo_t *CL_GetVideoByName( const char *name )
272 {
273         int i;
274
275         for( i = 0 ; i < cl_num_videos ; i++ )
276                 if( cl_videos[ i ].state != CLVIDEO_UNUSED
277                         &&      !strcmp( cl_videos[ i ].cpif.name , name ) )
278                         break;
279         if( i != cl_num_videos )
280                 return CL_GetVideoBySlot( i );
281         else
282                 return NULL;
283 }
284
285 void CL_SetVideoState(clvideo_t *video, clvideostate_t state)
286 {
287         if (!video)
288                 return;
289
290         video->lasttime = realtime;
291         video->state = state;
292         if (state == CLVIDEO_FIRSTFRAME)
293                 CL_RestartVideo(video);
294 }
295
296 void CL_RestartVideo(clvideo_t *video)
297 {
298         if (!video)
299                 return;
300
301         // reset time
302         video->starttime = video->lasttime = realtime;
303         video->framenum = -1;
304
305         // reopen stream
306         video->close(video->stream);
307         if (!OpenStream(video))
308                 video->state = CLVIDEO_UNUSED;
309 }
310
311 // close video
312 void CL_CloseVideo(clvideo_t * video)
313 {
314         int i;
315
316         if (!video || video->state == CLVIDEO_UNUSED)
317                 return;
318
319         // close stream
320         if (!video->suspended || video->state != CLVIDEO_FIRSTFRAME)
321                 video->close(video->stream);
322         // unlink texture
323         if (!video->suspended)
324                 UnlinkVideoTexture(video);
325         // purge subtitles
326         if (video->subtitles)
327         {
328                 for (i = 0; i < video->subtitles; i++)
329                         Z_Free( video->subtitle_text[i] );
330                 video->subtitles = 0;
331         }
332         video->state = CLVIDEO_UNUSED;
333 }
334
335 // update all videos
336 void CL_Video_Frame(void) 
337 {
338         clvideo_t *video;
339         int destframe;
340         int i;
341
342         if (!cl_num_videos)
343                 return;
344         for (video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++)
345         {
346                 if (video->state != CLVIDEO_UNUSED && !video->suspended)
347                 {
348                         if (realtime - video->lasttime > CLTHRESHOLD)
349                         {
350                                 SuspendVideo(video);
351                                 continue;
352                         }
353                         if (video->state == CLVIDEO_PAUSE)
354                         {
355                                 video->starttime = realtime - video->framenum * video->framerate;
356                                 continue;
357                         }
358                         // read video frame from stream if time has come
359                         if (video->state == CLVIDEO_FIRSTFRAME )
360                                 destframe = 0;
361                         else
362                                 destframe = (int)((realtime - video->starttime) * video->framerate);
363                         if (destframe < 0)
364                                 destframe = 0;
365                         if (video->framenum < destframe)
366                         {
367                                 do {
368                                         video->framenum++;
369                                         if (video->decodeframe(video->stream, video->imagedata, cl_videormask, cl_videogmask, cl_videobmask, cl_videobytesperpixel, cl_videobytesperpixel * video->cpif.width))
370                                         { 
371                                                 // finished?
372                                                 CL_RestartVideo(video);
373                                                 if (video->state == CLVIDEO_PLAY)
374                                                         video->state = CLVIDEO_FIRSTFRAME;
375                                                 return;
376                                         }
377                                 } while(video->framenum < destframe);
378                                 R_MarkDirtyTexture(video->cpif.tex);
379                         }
380                 }
381         }
382
383         // stop main video
384         if (cl_videos->state == CLVIDEO_FIRSTFRAME)
385                 CL_VideoStop();
386
387         // reduce range to exclude unnecessary entries
388         while(cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
389                 cl_num_videos--;
390 }
391
392 void CL_Video_Shutdown( void )
393 {
394         int i;
395         for (i = 0 ; i < cl_num_videos ; i++)
396                 CL_CloseVideo(&cl_videos[ i ]);
397 }
398
399 void CL_PurgeOwner( int owner )
400 {
401         int i;
402
403         for (i = 0 ; i < cl_num_videos ; i++)
404                 if (cl_videos[i].ownertag == owner)
405                         CL_CloseVideo(&cl_videos[i]);
406 }
407
408 typedef struct
409 {
410         dp_font_t *font;
411         float x;
412         float y;
413         float width;
414         float height;
415         float alignment; // 0 = left, 0.5 = center, 1 = right
416         float fontsize;
417         float textalpha;
418 }
419 cl_video_subtitle_info_t;
420
421 float CL_DrawVideo_WordWidthFunc(void *passthrough, const char *w, size_t *length, float maxWidth)
422 {
423         cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
424
425         if(w == NULL)
426                 return si->fontsize * si->font->maxwidth;
427         if(maxWidth >= 0)
428                 return DrawQ_TextWidth_UntilWidth(w, length, si->fontsize, si->fontsize, false, si->font, -maxWidth); // -maxWidth: we want at least one char
429         else if(maxWidth == -1)
430                 return DrawQ_TextWidth(w, *length, si->fontsize, si->fontsize, false, si->font);
431         else
432                 return 0;
433 }
434
435 int CL_DrawVideo_DisplaySubtitleLine(void *passthrough, const char *line, size_t length, float width, qboolean isContinuation)
436 {
437         cl_video_subtitle_info_t *si = (cl_video_subtitle_info_t *) passthrough;
438
439         int x = (int) (si->x + (si->width - width) * si->alignment);
440         if (length > 0)
441                 DrawQ_String(x, si->y, line, length, si->fontsize, si->fontsize, 1.0, 1.0, 1.0, si->textalpha, 0, NULL, false, si->font);
442         si->y += si->fontsize;
443         return 1;
444 }
445
446 int cl_videoplaying = false; // old, but still supported
447
448 void CL_DrawVideo(void)
449 {
450         clvideo_t *video;
451         float videotime, px, py, sx, sy, st[8], b;
452         cl_video_subtitle_info_t si;
453         int i;
454
455         if (!cl_videoplaying)
456                 return;
457
458         video = CL_GetVideoBySlot( 0 );
459
460         // fix cvars
461         if (cl_video_scale.value <= 0 || cl_video_scale.value > 1)
462                 Cvar_SetValueQuick( &cl_video_scale, 1);
463         if (cl_video_brightness.value <= 0 || cl_video_brightness.value > 10)
464                 Cvar_SetValueQuick( &cl_video_brightness, 1);
465
466         // calc video proportions
467         px = 0;
468         py = 0;
469         sx = vid_conwidth.integer;
470         sy = vid_conheight.integer;
471         st[0] = 0.0; st[1] = 0.0; 
472         st[2] = 1.0; st[3] = 0.0; 
473         st[4] = 0.0; st[5] = 1.0; 
474         st[6] = 1.0; st[7] = 1.0; 
475         if (cl_video_keepaspectratio.integer)
476         {
477                 float a = ((float)video->cpif.width / (float)video->cpif.height) / ((float)vid.width / (float)vid.height);
478                 if (cl_video_keepaspectratio.integer >= 2)
479                 {
480                         // clip instead of scale
481                         if (a < 1.0) // clip horizontally
482                         {
483                                 st[1] = st[3] = (1 - a)*0.5;
484                                 st[5] = st[7] = 1 - (1 - a)*0.5;
485                         }
486                         else if (a > 1.0) // clip vertically
487                         {
488                                 st[0] = st[4] = (1 - 1/a)*0.5;
489                                 st[2] = st[6] = (1/a)*0.5;
490                         }
491                 }
492                 else if (a < 1.0) // scale horizontally
493                 {
494                         px += sx * (1 - a) * 0.5;
495                         sx *= a;
496                 }
497                 else if (a > 1.0) // scale vertically
498                 {
499                         a = 1 / a;
500                         py += sy * (1 - a);
501                         sy *= a;
502                 }
503         }
504
505         if (cl_video_scale.value != 1)
506         {
507                 px += sx * (1 - cl_video_scale.value) * 0.5;
508                 py += sy * (1 - cl_video_scale.value) * ((bound(-1, cl_video_scale_vpos.value, 1) + 1) / 2);
509                 sx *= cl_video_scale.value;
510                 sy *= cl_video_scale.value;
511         }
512
513         // calc brightness for fadein and fadeout effects
514         b = cl_video_brightness.value;
515         if (cl_video_fadein.value && (realtime - video->starttime) < cl_video_fadein.value)
516                 b = pow((realtime - video->starttime)/cl_video_fadein.value, 2);
517         else if (cl_video_fadeout.value && ((video->starttime + video->framenum * video->framerate) - realtime) < cl_video_fadeout.value)
518                 b = pow(((video->starttime + video->framenum * video->framerate) - realtime)/cl_video_fadeout.value, 2);
519
520         // draw black bg in case stipple is active or video is scaled
521         if (cl_video_stipple.integer || px != 0 || py != 0 || sx != vid_conwidth.integer || sy != vid_conheight.integer)
522                 DrawQ_Fill(0, 0, vid_conwidth.integer, vid_conheight.integer, 0, 0, 0, 1, 0);
523
524         // enable video-only polygon stipple (of global stipple is not active)
525         if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
526         {
527                 GLubyte stipple[128];
528                 int i, s, width, parts;
529         
530                 s = cl_video_stipple.integer;
531                 parts = (s & 007);
532                 width = (s & 070) >> 3;
533                 qglEnable(GL_POLYGON_STIPPLE);CHECKGLERROR // 0x0B42
534                 for(i = 0; i < 128; ++i)
535                 {
536                         int line = i/4;
537                         stipple[i] = ((line >> width) & ((1 << parts) - 1)) ? 0x00 : 0xFF;
538                 }
539                 qglPolygonStipple(stipple);CHECKGLERROR
540         }
541
542         // draw video
543         DrawQ_SuperPic(px, py, &video->cpif, sx, sy, st[0], st[1], b, b, b, 1, st[2], st[3], b, b, b, 1, st[4], st[5], b, b, b, 1, st[6], st[7], b, b, b, 1, 0);
544
545         // disable video-only stipple
546         if (qglPolygonStipple && !scr_stipple.integer && cl_video_stipple.integer)
547                 qglDisable(GL_POLYGON_STIPPLE);CHECKGLERROR
548
549         // VorteX: draw subtitle_text
550         if (!video->subtitles || !cl_video_subtitles.integer)
551                 return;
552
553         // find current subtitle
554         videotime = realtime - video->starttime;
555         for (i = 0; i < video->subtitles; i++)
556         {
557                 if (videotime >= video->subtitle_start[i] && videotime <= video->subtitle_end[i])
558                 {
559                         // found, draw it
560                         si.font = FONT_NOTIFY;
561                         si.x = vid_conwidth.integer * 0.1;
562                         si.y = vid_conheight.integer - (max(1, cl_video_subtitles_lines.value) * cl_video_subtitles_textsize.value);
563                         si.width = vid_conwidth.integer * 0.8;
564                         si.height = max(1, cl_video_subtitles_lines.integer) * cl_video_subtitles_textsize.value;
565                         si.alignment = 0.5;
566                         si.fontsize = cl_video_subtitles_textsize.value;
567                         si.textalpha = min(1, (videotime - video->subtitle_start[i])/0.5) * min(1, ((video->subtitle_end[i] - videotime)/0.3)); // fade in and fade out
568                         COM_Wordwrap(video->subtitle_text[i], strlen(video->subtitle_text[i]), 0, si.width, CL_DrawVideo_WordWidthFunc, &si, CL_DrawVideo_DisplaySubtitleLine, &si);
569                         break;
570                 }
571         }
572 }
573
574 void CL_VideoStart(char *filename, const char *subtitlesfile)
575 {
576         Host_StartVideo();
577
578         if( cl_videos->state != CLVIDEO_UNUSED )
579                 CL_CloseVideo( cl_videos );
580         // already contains video/
581         if( !OpenVideo( cl_videos, filename, va( CLDYNTEXTUREPREFIX "%s", filename ), 0, subtitlesfile ) )
582                 return;
583         // expand the active range to include the new entry
584         cl_num_videos = max(cl_num_videos, 1);
585
586         cl_videoplaying = true;
587
588         CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
589         CL_RestartVideo( cl_videos );
590 }
591
592 void CL_Video_KeyEvent( int key, int ascii, qboolean down ) 
593 {
594         // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
595         if( !down ) {
596                 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
597                         CL_VideoStop();
598                 }
599         }
600 }
601
602 void CL_VideoStop(void)
603 {
604         cl_videoplaying = false;
605
606         CL_CloseVideo( cl_videos );
607 }
608
609 static void CL_PlayVideo_f(void)
610 {
611         char name[MAX_QPATH], subtitlesfile[MAX_QPATH];
612         const char *extension;
613
614         Host_StartVideo();
615
616         if (Cmd_Argc() < 2)
617         {
618                 Con_Print("usage: playvideo <videoname> [custom_subtitles_file]\nplays video named video/<videoname>.dpv\nif custom subtitles file is not presented\nit tries video/<videoname>.sub");
619                 return;
620         }
621
622         extension = FS_FileExtension(Cmd_Argv(1));
623         if (extension[0])
624                 dpsnprintf(name, sizeof(name), "video/%s", Cmd_Argv(1));
625         else
626                 dpsnprintf(name, sizeof(name), "video/%s.dpv", Cmd_Argv(1));
627         if ( Cmd_Argc() > 2)
628                 CL_VideoStart(name, Cmd_Argv(2));
629         else
630         {
631                 dpsnprintf(subtitlesfile, sizeof(subtitlesfile), "video/%s.dpsubs", Cmd_Argv(1));
632                 CL_VideoStart(name, subtitlesfile);
633         }
634 }
635
636 static void CL_StopVideo_f(void)
637 {
638         CL_VideoStop();
639 }
640
641 static void cl_video_start( void )
642 {
643         int i;
644         clvideo_t *video;
645
646         cl_videotexturepool = R_AllocTexturePool();
647
648         for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
649                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
650                         LinkVideoTexture( video );
651 }
652
653 static void cl_video_shutdown( void )
654 {
655         int i;
656         clvideo_t *video;
657
658         for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
659                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
660                         SuspendVideo( video );
661         R_FreeTexturePool( &cl_videotexturepool );
662 }
663
664 static void cl_video_newmap( void )
665 {
666 }
667
668 void CL_Video_Init( void )
669 {
670         union
671         {
672                 unsigned char b[4];
673                 unsigned int i;
674         }
675         bgra;
676
677         cl_num_videos = 0;
678         cl_videobytesperpixel = 4;
679
680         // set masks in an endian-independent way (as they really represent bytes)
681         bgra.i = 0;bgra.b[0] = 0xFF;cl_videobmask = bgra.i;
682         bgra.i = 0;bgra.b[1] = 0xFF;cl_videogmask = bgra.i;
683         bgra.i = 0;bgra.b[2] = 0xFF;cl_videormask = bgra.i;
684
685         Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
686         Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
687
688         Cvar_RegisterVariable(&cl_video_subtitles);
689         Cvar_RegisterVariable(&cl_video_subtitles_lines);
690         Cvar_RegisterVariable(&cl_video_subtitles_textsize);
691         Cvar_RegisterVariable(&cl_video_scale);
692         Cvar_RegisterVariable(&cl_video_scale_vpos);
693         Cvar_RegisterVariable(&cl_video_brightness);
694         Cvar_RegisterVariable(&cl_video_stipple);
695         Cvar_RegisterVariable(&cl_video_keepaspectratio);
696         Cvar_RegisterVariable(&cl_video_fadein);
697         Cvar_RegisterVariable(&cl_video_fadeout);
698
699         R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap, NULL, NULL );
700 }