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 clvideo_t videoarray[ MAXCLVIDEOS ];
13 static rtexturepool_t *cl_videotexturepool;
15 static clvideo_t *FindUnusedVid( void )
18 for( i = 1 ; i < MAXCLVIDEOS ; i++ )
19 if( videoarray[ i ].state == CLVIDEO_UNUSED )
20 return &videoarray[ i ];
24 static qboolean OpenStream( clvideo_t * video )
27 video->stream = dpvsimpledecode_open( video->filename, &errorstring);
30 Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
36 static void SuspendVideo( clvideo_t * video )
38 if( video->suspended )
40 video->suspended = true;
42 R_FreeTexture( video->cpif.tex );
43 // free the image data
44 Mem_Free( video->imagedata );
45 // if we are in firstframe mode, also close the stream
46 if( video->state == CLVIDEO_FIRSTFRAME )
47 dpvsimpledecode_close( video->stream );
50 static qboolean WakeVideo( clvideo_t * video )
52 if( !video->suspended )
54 video->suspended = false;
56 if( video->state == CLVIDEO_FIRSTFRAME )
57 if( !OpenStream( video ) ) {
58 video->state = CLVIDEO_UNUSED;
62 video->imagedata = Mem_Alloc( cl_mempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
63 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
64 video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
67 video->starttime += realtime - video->lasttime;
72 static clvideo_t* OpenVideo( clvideo_t *video, char *filename, char *name, int owner )
74 strncpy( video->filename, filename, MAX_QPATH );
75 video->ownertag = owner;
76 if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
78 strncpy( video->cpif.name, name, MAX_QPATH );
80 if( !OpenStream( video ) )
83 video->state = CLVIDEO_FIRSTFRAME;
85 video->framerate = dpvsimpledecode_getframerate( video->stream );
86 video->lasttime = realtime;
88 video->cpif.width = dpvsimpledecode_getwidth( video->stream );
89 video->cpif.height = dpvsimpledecode_getheight( video->stream );
90 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
91 video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
93 video->imagedata = Mem_Alloc( cl_mempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
98 clvideo_t* CL_OpenVideo( char *filename, char *name, int owner )
102 video = FindUnusedVid();
104 Con_Printf( "unable to open video \"%s\" - video limit reached\n", filename );
107 return OpenVideo( video, filename, name, owner );
110 clvideo_t* CL_GetVideo( char *name )
115 for( i = 0 ; i < MAXCLVIDEOS ; i++ )
116 if( videoarray[ i ].state != CLVIDEO_UNUSED
117 && !strcmp( videoarray[ i ].cpif.name , name ) )
119 if( i == MAXCLVIDEOS )
121 video = &videoarray[ i ];
123 if( video->suspended )
125 if( !WakeVideo( video ) )
127 else if( video->state == CLVIDEO_RESETONWAKEUP )
128 video->framenum = -1;
131 video->lasttime = realtime;
136 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
141 video->lasttime = realtime;
142 video->state = state;
143 if( state == CLVIDEO_FIRSTFRAME )
144 CL_RestartVideo( video );
147 void CL_RestartVideo( clvideo_t *video )
152 video->starttime = video->lasttime = realtime;
153 video->framenum = -1;
155 dpvsimpledecode_close( video->stream );
156 if( !OpenStream( video ) )
157 video->state = CLVIDEO_UNUSED;
160 void CL_CloseVideo( clvideo_t * video )
162 if( !video || video->state == CLVIDEO_UNUSED )
165 if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
166 dpvsimpledecode_close( video->stream );
167 if( !video->suspended ) {
168 Mem_Free( video->imagedata );
169 R_FreeTexture( video->cpif.tex );
172 video->state = CLVIDEO_UNUSED;
175 static void VideoFrame( clvideo_t *video )
179 if( video->state == CLVIDEO_FIRSTFRAME )
182 destframe = (realtime - video->starttime) * video->framerate;
185 if( video->framenum < destframe ) {
188 if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
189 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
190 cl_videobytesperpixel * video->cpif.width )
192 CL_RestartVideo( video );
193 if( video->state == CLVIDEO_PLAY )
194 video->state = CLVIDEO_FIRSTFRAME;
197 } while( video->framenum < destframe );
198 R_UpdateTexture( video->cpif.tex, video->imagedata );
202 void CL_VideoFrame( void ) // update all videos
207 for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; video++, i++ )
208 if( video->state != CLVIDEO_UNUSED && !video->suspended )
210 if( realtime - video->lasttime > CLTHRESHOLD )
211 SuspendVideo( video );
212 else if( video->state == CLVIDEO_PAUSE )
213 video->starttime = realtime - video->framenum * video->framerate;
218 if( videoarray->state == CLVIDEO_FIRSTFRAME )
222 void CL_Video_Shutdown( void )
225 for( i = 0 ; i < MAXCLVIDEOS ; i++ )
226 CL_CloseVideo( &videoarray[ i ] );
229 void CL_PurgeOwner( int owner )
232 for( i = 0 ; i < MAXCLVIDEOS ; i++ )
233 if( videoarray[ i ].ownertag == owner )
234 CL_CloseVideo( &videoarray[ i ] );
237 int cl_videoplaying = false; // old, but still supported
239 void CL_DrawVideo(void)
242 DrawQ_Pic(0, 0, videoarray->cpif.name, vid.conwidth, vid.conheight, 1, 1, 1, 1, 0);
245 void CL_VideoStart(char *filename)
249 if( videoarray->state != CLVIDEO_UNUSED )
250 CL_CloseVideo( videoarray );
251 if( !OpenVideo( videoarray, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) )
254 cl_videoplaying = true;
256 CL_SetVideoState( videoarray, CLVIDEO_PLAY );
257 CL_RestartVideo( videoarray );
260 void CL_VideoStop(void)
262 cl_videoplaying = false;
264 CL_CloseVideo( videoarray );
267 static void CL_PlayVideo_f(void)
273 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
277 sprintf(name, "video/%s.dpv", Cmd_Argv(1));
281 static void CL_StopVideo_f(void)
286 static void cl_video_start( void )
291 cl_videotexturepool = R_AllocTexturePool();
293 for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; i++, video++ )
294 if( video->state != CLVIDEO_UNUSED && !video->suspended )
295 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
296 video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
299 static void cl_video_shutdown( void )
301 R_FreeTexturePool( &cl_videotexturepool );
304 static void cl_video_newmap( void )
308 void CL_Video_Init( void )
310 cl_videobytesperpixel = 4;
311 cl_videormask = BigLong(0xFF000000);
312 cl_videogmask = BigLong(0x00FF0000);
313 cl_videobmask = BigLong(0x0000FF00);
315 Cmd_AddCommand( "playvideo", CL_PlayVideo_f );
316 Cmd_AddCommand( "stopvideo", CL_StopVideo_f );
318 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );