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