4 #include "dpvsimpledecode.h"
6 // constants (and semi-constants)
7 static int cl_videormask;
8 static int cl_videobmask;
9 static int cl_videogmask;
10 static int cl_videobytesperpixel;
12 static int cl_num_videos;
13 static clvideo_t cl_videos[ MAXCLVIDEOS ];
14 static rtexturepool_t *cl_videotexturepool;
16 static clvideo_t *FindUnusedVid( void )
19 for( i = 1 ; i < MAXCLVIDEOS ; i++ )
20 if( cl_videos[ i ].state == CLVIDEO_UNUSED )
21 return &cl_videos[ i ];
25 static qboolean OpenStream( clvideo_t * video )
28 video->stream = dpvsimpledecode_open( video->filename, &errorstring);
31 Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
37 static void SuspendVideo( clvideo_t * video )
39 if( video->suspended )
41 video->suspended = true;
43 R_FreeTexture( video->cpif.tex );
44 // free the image data
45 Mem_Free( video->imagedata );
46 // if we are in firstframe mode, also close the stream
47 if( video->state == CLVIDEO_FIRSTFRAME )
48 dpvsimpledecode_close( video->stream );
51 static qboolean WakeVideo( clvideo_t * video )
53 if( !video->suspended )
55 video->suspended = false;
57 if( video->state == CLVIDEO_FIRSTFRAME )
58 if( !OpenStream( video ) ) {
59 video->state = CLVIDEO_UNUSED;
63 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
64 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
65 video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE, NULL );
68 video->starttime += realtime - video->lasttime;
73 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner )
75 strlcpy( video->filename, filename, sizeof(video->filename) );
76 video->ownertag = owner;
77 if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
79 strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
81 if( !OpenStream( video ) )
84 video->state = CLVIDEO_FIRSTFRAME;
86 video->framerate = dpvsimpledecode_getframerate( video->stream );
87 video->lasttime = realtime;
89 video->cpif.width = dpvsimpledecode_getwidth( video->stream );
90 video->cpif.height = dpvsimpledecode_getheight( video->stream );
91 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
92 video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE, NULL );
94 video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
99 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner )
103 video = FindUnusedVid();
105 Con_Printf( "unable to open video \"%s\" - video limit reached\n", filename );
108 video = OpenVideo( video, filename, name, owner );
109 // expand the active range to include the new entry
111 cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
115 static clvideo_t* CL_GetVideoBySlot( int slot )
117 clvideo_t *video = &cl_videos[ slot ];
119 if( video->suspended )
121 if( !WakeVideo( video ) )
123 else if( video->state == CLVIDEO_RESETONWAKEUP )
124 video->framenum = -1;
127 video->lasttime = realtime;
132 clvideo_t *CL_GetVideoByName( const char *name )
136 for( i = 0 ; i < cl_num_videos ; i++ )
137 if( cl_videos[ i ].state != CLVIDEO_UNUSED
138 && !strcmp( cl_videos[ i ].cpif.name , name ) )
140 if( i != cl_num_videos )
141 return CL_GetVideoBySlot( i );
146 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
151 video->lasttime = realtime;
152 video->state = state;
153 if( state == CLVIDEO_FIRSTFRAME )
154 CL_RestartVideo( video );
157 void CL_RestartVideo( clvideo_t *video )
162 video->starttime = video->lasttime = realtime;
163 video->framenum = -1;
165 dpvsimpledecode_close( video->stream );
166 if( !OpenStream( video ) )
167 video->state = CLVIDEO_UNUSED;
170 void CL_CloseVideo( clvideo_t * video )
172 if( !video || video->state == CLVIDEO_UNUSED )
175 if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
176 dpvsimpledecode_close( video->stream );
177 if( !video->suspended ) {
178 Mem_Free( video->imagedata );
179 R_FreeTexture( video->cpif.tex );
182 video->state = CLVIDEO_UNUSED;
185 static void VideoFrame( clvideo_t *video )
189 if( video->state == CLVIDEO_FIRSTFRAME )
192 destframe = (int)((realtime - video->starttime) * video->framerate);
195 if( video->framenum < destframe ) {
198 if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
199 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
200 cl_videobytesperpixel * video->cpif.width )
202 CL_RestartVideo( video );
203 if( video->state == CLVIDEO_PLAY )
204 video->state = CLVIDEO_FIRSTFRAME;
207 } while( video->framenum < destframe );
208 R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, video->cpif.width, video->cpif.height );
212 void CL_VideoFrame( void ) // update all videos
220 for( video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++ )
221 if( video->state != CLVIDEO_UNUSED && !video->suspended )
223 if( realtime - video->lasttime > CLTHRESHOLD )
224 SuspendVideo( video );
225 else if( video->state == CLVIDEO_PAUSE )
226 video->starttime = realtime - video->framenum * video->framerate;
231 if( cl_videos->state == CLVIDEO_FIRSTFRAME )
234 // reduce range to exclude unnecessary entries
235 while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
239 void CL_Video_Shutdown( void )
242 for( i = 0 ; i < cl_num_videos ; i++ )
243 CL_CloseVideo( &cl_videos[ i ] );
246 void CL_PurgeOwner( int owner )
249 for( i = 0 ; i < cl_num_videos ; i++ )
250 if( cl_videos[ i ].ownertag == owner )
251 CL_CloseVideo( &cl_videos[ i ] );
254 int cl_videoplaying = false; // old, but still supported
256 void CL_DrawVideo(void)
259 DrawQ_Pic(0, 0, &CL_GetVideoBySlot( 0 )->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0);
262 void CL_VideoStart(char *filename)
266 if( cl_videos->state != CLVIDEO_UNUSED )
267 CL_CloseVideo( cl_videos );
268 if( !OpenVideo( cl_videos, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) )
270 // expand the active range to include the new entry
271 cl_num_videos = max(cl_num_videos, 1);
273 cl_videoplaying = true;
275 CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
276 CL_RestartVideo( cl_videos );
279 void CL_Video_KeyEvent( int key, int ascii, qboolean down )
281 // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
283 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
289 void CL_VideoStop(void)
291 cl_videoplaying = false;
293 CL_CloseVideo( cl_videos );
296 static void CL_PlayVideo_f(void)
298 char name[MAX_QPATH];
304 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
308 sprintf(name, "video/%s.dpv", Cmd_Argv(1));
312 static void CL_StopVideo_f(void)
317 static void cl_video_start( void )
322 cl_videotexturepool = R_AllocTexturePool();
324 for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
325 if( video->state != CLVIDEO_UNUSED && !video->suspended )
326 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
327 video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE, NULL );
330 static void cl_video_shutdown( void )
332 R_FreeTexturePool( &cl_videotexturepool );
335 static void cl_video_newmap( void )
339 void CL_Video_Init( void )
342 cl_videobytesperpixel = 4;
343 cl_videormask = BigLong(0xFF000000);
344 cl_videogmask = BigLong(0x00FF0000);
345 cl_videobmask = BigLong(0x0000FF00);
347 Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
348 Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
350 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );