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, clvideo_t *video) {
39 R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, video->cpif.width, video->cpif.height );
42 static void LinkVideoTexture( clvideo_t *video ) {
43 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
44 video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE, NULL );
45 R_MakeTextureDynamic( video->cpif.tex, VideoUpdateCallback, video );
46 CL_LinkDynTexture( video->cpif.name, video->cpif.tex );
49 static void UnlinkVideoTexture( clvideo_t *video ) {
50 CL_UnlinkDynTexture( video->cpif.name );
52 R_FreeTexture( video->cpif.tex );
53 // free the image data
54 Mem_Free( video->imagedata );
57 static void SuspendVideo( clvideo_t * video )
59 if( video->suspended )
61 video->suspended = true;
62 UnlinkVideoTexture( video );
63 // if we are in firstframe mode, also close the stream
64 if( video->state == CLVIDEO_FIRSTFRAME )
65 dpvsimpledecode_close( video->stream );
68 static qboolean WakeVideo( clvideo_t * video )
70 if( !video->suspended )
72 video->suspended = false;
74 if( video->state == CLVIDEO_FIRSTFRAME )
75 if( !OpenStream( video ) ) {
76 video->state = CLVIDEO_UNUSED;
80 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
81 LinkVideoTexture( video );
84 video->starttime += realtime - video->lasttime;
89 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner )
91 strlcpy( video->filename, filename, sizeof(video->filename) );
92 video->ownertag = owner;
93 if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
95 strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
97 if( !OpenStream( video ) )
100 video->state = CLVIDEO_FIRSTFRAME;
101 video->framenum = -1;
102 video->framerate = dpvsimpledecode_getframerate( video->stream );
103 video->lasttime = realtime;
105 video->cpif.width = dpvsimpledecode_getwidth( video->stream );
106 video->cpif.height = dpvsimpledecode_getheight( video->stream );
107 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
108 LinkVideoTexture( video );
113 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner )
117 if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) {
118 if( developer.integer > 0 ) {
119 Con_Printf( "CL_OpenVideo: Bad video texture name '%s'!\n", name );
124 video = FindUnusedVid();
126 Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename );
129 video = OpenVideo( video, filename, name, owner );
130 // expand the active range to include the new entry
132 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
137 static clvideo_t* CL_GetVideoBySlot( int slot )
139 clvideo_t *video = &cl_videos[ slot ];
141 if( video->suspended )
143 if( !WakeVideo( video ) )
145 else if( video->state == CLVIDEO_RESETONWAKEUP )
146 video->framenum = -1;
149 video->lasttime = realtime;
154 clvideo_t *CL_GetVideoByName( const char *name )
158 for( i = 0 ; i < cl_num_videos ; i++ )
159 if( cl_videos[ i ].state != CLVIDEO_UNUSED
160 && !strcmp( cl_videos[ i ].cpif.name , name ) )
162 if( i != cl_num_videos )
163 return CL_GetVideoBySlot( i );
168 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
173 video->lasttime = realtime;
174 video->state = state;
175 if( state == CLVIDEO_FIRSTFRAME )
176 CL_RestartVideo( video );
179 void CL_RestartVideo( clvideo_t *video )
184 video->starttime = video->lasttime = realtime;
185 video->framenum = -1;
187 dpvsimpledecode_close( video->stream );
188 if( !OpenStream( video ) )
189 video->state = CLVIDEO_UNUSED;
192 void CL_CloseVideo( clvideo_t * video )
194 if( !video || video->state == CLVIDEO_UNUSED )
197 if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
198 dpvsimpledecode_close( video->stream );
199 if( !video->suspended ) {
200 UnlinkVideoTexture( video );
203 video->state = CLVIDEO_UNUSED;
206 static void VideoFrame( clvideo_t *video )
210 if( video->state == CLVIDEO_FIRSTFRAME )
213 destframe = (int)((realtime - video->starttime) * video->framerate);
216 if( video->framenum < destframe ) {
219 if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
220 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
221 cl_videobytesperpixel * video->cpif.width )
223 CL_RestartVideo( video );
224 if( video->state == CLVIDEO_PLAY )
225 video->state = CLVIDEO_FIRSTFRAME;
228 } while( video->framenum < destframe );
229 R_MarkDirtyTexture( video->cpif.tex );
233 void CL_Video_Frame( void ) // update all videos
241 for( video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++ )
242 if( video->state != CLVIDEO_UNUSED && !video->suspended )
244 if( realtime - video->lasttime > CLTHRESHOLD )
245 SuspendVideo( video );
246 else if( video->state == CLVIDEO_PAUSE )
247 video->starttime = realtime - video->framenum * video->framerate;
252 if( cl_videos->state == CLVIDEO_FIRSTFRAME )
255 // reduce range to exclude unnecessary entries
256 while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
260 void CL_Video_Shutdown( void )
263 for( i = 0 ; i < cl_num_videos ; i++ )
264 CL_CloseVideo( &cl_videos[ i ] );
267 void CL_PurgeOwner( int owner )
270 for( i = 0 ; i < cl_num_videos ; i++ )
271 if( cl_videos[ i ].ownertag == owner )
272 CL_CloseVideo( &cl_videos[ i ] );
275 int cl_videoplaying = false; // old, but still supported
277 void CL_DrawVideo(void)
280 DrawQ_Pic(0, 0, &CL_GetVideoBySlot( 0 )->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0);
283 void CL_VideoStart(char *filename)
287 if( cl_videos->state != CLVIDEO_UNUSED )
288 CL_CloseVideo( cl_videos );
289 // already contains video/
290 if( !OpenVideo( cl_videos, filename, va( CLDYNTEXTUREPREFIX "%s", filename ), 0 ) )
292 // expand the active range to include the new entry
293 cl_num_videos = max(cl_num_videos, 1);
295 cl_videoplaying = true;
297 CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
298 CL_RestartVideo( cl_videos );
301 void CL_Video_KeyEvent( int key, int ascii, qboolean down )
303 // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
305 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
311 void CL_VideoStop(void)
313 cl_videoplaying = false;
315 CL_CloseVideo( cl_videos );
318 static void CL_PlayVideo_f(void)
320 char name[MAX_QPATH];
326 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
330 sprintf(name, "video/%s.dpv", Cmd_Argv(1));
334 static void CL_StopVideo_f(void)
339 static void cl_video_start( void )
344 cl_videotexturepool = R_AllocTexturePool();
346 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
347 if( video->state != CLVIDEO_UNUSED && !video->suspended )
348 LinkVideoTexture( video );
351 static void cl_video_shutdown( void )
353 // TODO: unlink video textures?
354 R_FreeTexturePool( &cl_videotexturepool );
357 static void cl_video_newmap( void )
361 void CL_Video_Init( void )
364 cl_videobytesperpixel = 4;
365 cl_videormask = BigLong(0xFF000000);
366 cl_videogmask = BigLong(0x00FF0000);
367 cl_videobmask = BigLong(0x0000FF00);
369 Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
370 Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
372 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );