3 #include "cl_dyntexture.h"
5 #include "dpvsimpledecode.h"
7 // constants (and semi-constants)
8 static int cl_videormask;
9 static int cl_videobmask;
10 static int cl_videogmask;
11 static int cl_videobytesperpixel;
13 static int cl_num_videos;
14 static clvideo_t cl_videos[ MAXCLVIDEOS ];
15 static rtexturepool_t *cl_videotexturepool;
17 static clvideo_t *FindUnusedVid( void )
20 for( i = 1 ; i < MAXCLVIDEOS ; i++ )
21 if( cl_videos[ i ].state == CLVIDEO_UNUSED )
22 return &cl_videos[ i ];
26 static qboolean OpenStream( clvideo_t * video )
29 video->stream = dpvsimpledecode_open( video->filename, &errorstring);
32 Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
38 static void VideoUpdateCallback(rtexture_t *rt, void *data) {
39 clvideo_t *video = (clvideo_t *) data;
40 R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, video->cpif.width, video->cpif.height );
43 static void LinkVideoTexture( clvideo_t *video ) {
44 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
45 video->cpif.width, video->cpif.height, NULL, TEXTYPE_BGRA, TEXF_PERSISTENT | TEXF_ALLOWUPDATES, NULL );
46 R_MakeTextureDynamic( video->cpif.tex, VideoUpdateCallback, video );
47 CL_LinkDynTexture( video->cpif.name, video->cpif.tex );
50 static void UnlinkVideoTexture( clvideo_t *video ) {
51 CL_UnlinkDynTexture( video->cpif.name );
53 R_FreeTexture( video->cpif.tex );
54 // free the image data
55 Mem_Free( video->imagedata );
58 static void SuspendVideo( clvideo_t * video )
60 if( video->suspended )
62 video->suspended = true;
63 UnlinkVideoTexture( video );
64 // if we are in firstframe mode, also close the stream
65 if( video->state == CLVIDEO_FIRSTFRAME )
66 dpvsimpledecode_close( video->stream );
69 static qboolean WakeVideo( clvideo_t * video )
71 if( !video->suspended )
73 video->suspended = false;
75 if( video->state == CLVIDEO_FIRSTFRAME )
76 if( !OpenStream( video ) ) {
77 video->state = CLVIDEO_UNUSED;
81 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
82 LinkVideoTexture( video );
85 video->starttime += realtime - video->lasttime;
90 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner )
92 strlcpy( video->filename, filename, sizeof(video->filename) );
93 video->ownertag = owner;
94 if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
96 strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
98 if( !OpenStream( video ) )
101 video->state = CLVIDEO_FIRSTFRAME;
102 video->framenum = -1;
103 video->framerate = dpvsimpledecode_getframerate( video->stream );
104 video->lasttime = realtime;
106 video->cpif.width = dpvsimpledecode_getwidth( video->stream );
107 video->cpif.height = dpvsimpledecode_getheight( video->stream );
108 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
109 LinkVideoTexture( video );
114 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner )
118 if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) {
119 Con_DPrintf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
123 video = FindUnusedVid();
125 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
128 video = OpenVideo( video, filename, name, owner );
129 // expand the active range to include the new entry
131 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
136 static clvideo_t* CL_GetVideoBySlot( int slot )
138 clvideo_t *video = &cl_videos[ slot ];
140 if( video->suspended )
142 if( !WakeVideo( video ) )
144 else if( video->state == CLVIDEO_RESETONWAKEUP )
145 video->framenum = -1;
148 video->lasttime = realtime;
153 clvideo_t *CL_GetVideoByName( const char *name )
157 for( i = 0 ; i < cl_num_videos ; i++ )
158 if( cl_videos[ i ].state != CLVIDEO_UNUSED
159 && !strcmp( cl_videos[ i ].cpif.name , name ) )
161 if( i != cl_num_videos )
162 return CL_GetVideoBySlot( i );
167 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
172 video->lasttime = realtime;
173 video->state = state;
174 if( state == CLVIDEO_FIRSTFRAME )
175 CL_RestartVideo( video );
178 void CL_RestartVideo( clvideo_t *video )
183 video->starttime = video->lasttime = realtime;
184 video->framenum = -1;
186 dpvsimpledecode_close( video->stream );
187 if( !OpenStream( video ) )
188 video->state = CLVIDEO_UNUSED;
191 void CL_CloseVideo( clvideo_t * video )
193 if( !video || video->state == CLVIDEO_UNUSED )
196 if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
197 dpvsimpledecode_close( video->stream );
198 if( !video->suspended ) {
199 UnlinkVideoTexture( video );
202 video->state = CLVIDEO_UNUSED;
205 static void VideoFrame( clvideo_t *video )
209 if( video->state == CLVIDEO_FIRSTFRAME )
212 destframe = (int)((realtime - video->starttime) * video->framerate);
215 if( video->framenum < destframe ) {
218 if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
219 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
220 cl_videobytesperpixel * video->cpif.width )
222 CL_RestartVideo( video );
223 if( video->state == CLVIDEO_PLAY )
224 video->state = CLVIDEO_FIRSTFRAME;
227 } while( video->framenum < destframe );
228 R_MarkDirtyTexture( video->cpif.tex );
232 void CL_Video_Frame( void ) // update all videos
240 for( video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++ )
241 if( video->state != CLVIDEO_UNUSED && !video->suspended )
243 if( realtime - video->lasttime > CLTHRESHOLD )
244 SuspendVideo( video );
245 else if( video->state == CLVIDEO_PAUSE )
246 video->starttime = realtime - video->framenum * video->framerate;
251 if( cl_videos->state == CLVIDEO_FIRSTFRAME )
254 // reduce range to exclude unnecessary entries
255 while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
259 void CL_Video_Shutdown( void )
262 for( i = 0 ; i < cl_num_videos ; i++ )
263 CL_CloseVideo( &cl_videos[ i ] );
266 void CL_PurgeOwner( int owner )
269 for( i = 0 ; i < cl_num_videos ; i++ )
270 if( cl_videos[ i ].ownertag == owner )
271 CL_CloseVideo( &cl_videos[ i ] );
274 int cl_videoplaying = false; // old, but still supported
276 void CL_DrawVideo(void)
279 DrawQ_Pic(0, 0, &CL_GetVideoBySlot( 0 )->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0);
282 void CL_VideoStart(char *filename)
286 if( cl_videos->state != CLVIDEO_UNUSED )
287 CL_CloseVideo( cl_videos );
288 // already contains video/
289 if( !OpenVideo( cl_videos, filename, va( CLDYNTEXTUREPREFIX "%s", filename ), 0 ) )
291 // expand the active range to include the new entry
292 cl_num_videos = max(cl_num_videos, 1);
294 cl_videoplaying = true;
296 CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
297 CL_RestartVideo( cl_videos );
300 void CL_Video_KeyEvent( int key, int ascii, qboolean down )
302 // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
304 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
310 void CL_VideoStop(void)
312 cl_videoplaying = false;
314 CL_CloseVideo( cl_videos );
317 static void CL_PlayVideo_f(void)
319 char name[MAX_QPATH];
325 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
329 dpsnprintf(name, sizeof(name), "video/%s.dpv", Cmd_Argv(1));
333 static void CL_StopVideo_f(void)
338 static void cl_video_start( void )
343 cl_videotexturepool = R_AllocTexturePool();
345 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
346 if( video->state != CLVIDEO_UNUSED && !video->suspended )
347 LinkVideoTexture( video );
350 static void cl_video_shutdown( void )
352 // TODO: unlink video textures?
353 R_FreeTexturePool( &cl_videotexturepool );
356 static void cl_video_newmap( void )
360 void CL_Video_Init( void )
370 cl_videobytesperpixel = 4;
372 // set masks in an endian-independent way (as they really represent bytes)
373 bgra.i = 0;bgra.b[0] = 0xFF;cl_videobmask = bgra.i;
374 bgra.i = 0;bgra.b[1] = 0xFF;cl_videogmask = bgra.i;
375 bgra.i = 0;bgra.b[2] = 0xFF;cl_videormask = bgra.i;
377 Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
378 Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
380 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );