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 = 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_ALWAYSPRECACHE | TEXF_PERSISTENT, 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 if( developer.integer > 0 ) {
120 Con_Printf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
125 video = FindUnusedVid();
127 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
130 video = OpenVideo( video, filename, name, owner );
131 // expand the active range to include the new entry
133 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
138 static clvideo_t* CL_GetVideoBySlot( int slot )
140 clvideo_t *video = &cl_videos[ slot ];
142 if( video->suspended )
144 if( !WakeVideo( video ) )
146 else if( video->state == CLVIDEO_RESETONWAKEUP )
147 video->framenum = -1;
150 video->lasttime = realtime;
155 clvideo_t *CL_GetVideoByName( const char *name )
159 for( i = 0 ; i < cl_num_videos ; i++ )
160 if( cl_videos[ i ].state != CLVIDEO_UNUSED
161 && !strcmp( cl_videos[ i ].cpif.name , name ) )
163 if( i != cl_num_videos )
164 return CL_GetVideoBySlot( i );
169 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
174 video->lasttime = realtime;
175 video->state = state;
176 if( state == CLVIDEO_FIRSTFRAME )
177 CL_RestartVideo( video );
180 void CL_RestartVideo( clvideo_t *video )
185 video->starttime = video->lasttime = realtime;
186 video->framenum = -1;
188 dpvsimpledecode_close( video->stream );
189 if( !OpenStream( video ) )
190 video->state = CLVIDEO_UNUSED;
193 void CL_CloseVideo( clvideo_t * video )
195 if( !video || video->state == CLVIDEO_UNUSED )
198 if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
199 dpvsimpledecode_close( video->stream );
200 if( !video->suspended ) {
201 UnlinkVideoTexture( video );
204 video->state = CLVIDEO_UNUSED;
207 static void VideoFrame( clvideo_t *video )
211 if( video->state == CLVIDEO_FIRSTFRAME )
214 destframe = (int)((realtime - video->starttime) * video->framerate);
217 if( video->framenum < destframe ) {
220 if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
221 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
222 cl_videobytesperpixel * video->cpif.width )
224 CL_RestartVideo( video );
225 if( video->state == CLVIDEO_PLAY )
226 video->state = CLVIDEO_FIRSTFRAME;
229 } while( video->framenum < destframe );
230 R_MarkDirtyTexture( video->cpif.tex );
234 void CL_Video_Frame( void ) // update all videos
242 for( video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++ )
243 if( video->state != CLVIDEO_UNUSED && !video->suspended )
245 if( realtime - video->lasttime > CLTHRESHOLD )
246 SuspendVideo( video );
247 else if( video->state == CLVIDEO_PAUSE )
248 video->starttime = realtime - video->framenum * video->framerate;
253 if( cl_videos->state == CLVIDEO_FIRSTFRAME )
256 // reduce range to exclude unnecessary entries
257 while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
261 void CL_Video_Shutdown( void )
264 for( i = 0 ; i < cl_num_videos ; i++ )
265 CL_CloseVideo( &cl_videos[ i ] );
268 void CL_PurgeOwner( int owner )
271 for( i = 0 ; i < cl_num_videos ; i++ )
272 if( cl_videos[ i ].ownertag == owner )
273 CL_CloseVideo( &cl_videos[ i ] );
276 int cl_videoplaying = false; // old, but still supported
278 void CL_DrawVideo(void)
281 DrawQ_Pic(0, 0, &CL_GetVideoBySlot( 0 )->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0);
284 void CL_VideoStart(char *filename)
288 if( cl_videos->state != CLVIDEO_UNUSED )
289 CL_CloseVideo( cl_videos );
290 // already contains video/
291 if( !OpenVideo( cl_videos, filename, va( CLDYNTEXTUREPREFIX "%s", filename ), 0 ) )
293 // expand the active range to include the new entry
294 cl_num_videos = max(cl_num_videos, 1);
296 cl_videoplaying = true;
298 CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
299 CL_RestartVideo( cl_videos );
302 void CL_Video_KeyEvent( int key, int ascii, qboolean down )
304 // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
306 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
312 void CL_VideoStop(void)
314 cl_videoplaying = false;
316 CL_CloseVideo( cl_videos );
319 static void CL_PlayVideo_f(void)
321 char name[MAX_QPATH];
327 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
331 sprintf(name, "video/%s.dpv", Cmd_Argv(1));
335 static void CL_StopVideo_f(void)
340 static void cl_video_start( void )
345 cl_videotexturepool = R_AllocTexturePool();
347 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
348 if( video->state != CLVIDEO_UNUSED && !video->suspended )
349 LinkVideoTexture( video );
352 static void cl_video_shutdown( void )
354 // TODO: unlink video textures?
355 R_FreeTexturePool( &cl_videotexturepool );
358 static void cl_video_newmap( void )
362 void CL_Video_Init( void )
372 cl_videobytesperpixel = 4;
374 // set masks in an endian-independent way (as they really represent bytes)
375 bgra.i = 0;bgra.b[0] = 0xFF;cl_videobmask = bgra.i;
376 bgra.i = 0;bgra.b[1] = 0xFF;cl_videogmask = bgra.i;
377 bgra.i = 0;bgra.b[2] = 0xFF;cl_videormask = bgra.i;
379 Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
380 Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
382 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );