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_activevideos;
13 static clvideo_t videoarray[ MAXCLVIDEOS ];
14 static rtexturepool_t *cl_videotexturepool;
16 static clvideo_t *FindUnusedVid( void )
19 for( i = 1 ; i < MAXCLVIDEOS ; i++ )
20 if( videoarray[ i ].state == CLVIDEO_UNUSED )
21 return &videoarray[ 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( cl_mempool, 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, 0, NULL );
68 video->starttime += realtime - video->lasttime;
73 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner )
75 strncpy( video->filename, filename, MAX_QPATH );
76 video->ownertag = owner;
77 if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
79 strncpy( video->cpif.name, name, MAX_QPATH );
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, 0, NULL );
94 video->imagedata = Mem_Alloc( cl_mempool, 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_activevideos = max(cl_activevideos, (int)(video - videoarray) + 1);
115 clvideo_t* CL_GetVideo( const char *name )
120 for( i = 0 ; i < cl_activevideos ; i++ )
121 if( videoarray[ i ].state != CLVIDEO_UNUSED
122 && !strcmp( videoarray[ i ].cpif.name , name ) )
124 if( i == cl_activevideos )
126 video = &videoarray[ i ];
128 if( video->suspended )
130 if( !WakeVideo( video ) )
132 else if( video->state == CLVIDEO_RESETONWAKEUP )
133 video->framenum = -1;
136 video->lasttime = realtime;
141 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
146 video->lasttime = realtime;
147 video->state = state;
148 if( state == CLVIDEO_FIRSTFRAME )
149 CL_RestartVideo( video );
152 void CL_RestartVideo( clvideo_t *video )
157 video->starttime = video->lasttime = realtime;
158 video->framenum = -1;
160 dpvsimpledecode_close( video->stream );
161 if( !OpenStream( video ) )
162 video->state = CLVIDEO_UNUSED;
165 void CL_CloseVideo( clvideo_t * video )
167 if( !video || video->state == CLVIDEO_UNUSED )
170 if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
171 dpvsimpledecode_close( video->stream );
172 if( !video->suspended ) {
173 Mem_Free( video->imagedata );
174 R_FreeTexture( video->cpif.tex );
177 video->state = CLVIDEO_UNUSED;
180 static void VideoFrame( clvideo_t *video )
184 if( video->state == CLVIDEO_FIRSTFRAME )
187 destframe = (realtime - video->starttime) * video->framerate;
190 if( video->framenum < destframe ) {
193 if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
194 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
195 cl_videobytesperpixel * video->cpif.width )
197 CL_RestartVideo( video );
198 if( video->state == CLVIDEO_PLAY )
199 video->state = CLVIDEO_FIRSTFRAME;
202 } while( video->framenum < destframe );
203 R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata );
207 void CL_VideoFrame( void ) // update all videos
212 if (!cl_activevideos)
215 for( video = videoarray, i = 0 ; i < cl_activevideos ; video++, i++ )
216 if( video->state != CLVIDEO_UNUSED && !video->suspended )
218 if( realtime - video->lasttime > CLTHRESHOLD )
219 SuspendVideo( video );
220 else if( video->state == CLVIDEO_PAUSE )
221 video->starttime = realtime - video->framenum * video->framerate;
226 if( videoarray->state == CLVIDEO_FIRSTFRAME )
229 // reduce range to exclude unnecessary entries
230 while (cl_activevideos > 0 && videoarray[cl_activevideos-1].state == CLVIDEO_UNUSED)
234 void CL_Video_Shutdown( void )
237 for( i = 0 ; i < cl_activevideos ; i++ )
238 CL_CloseVideo( &videoarray[ i ] );
241 void CL_PurgeOwner( int owner )
244 for( i = 0 ; i < cl_activevideos ; i++ )
245 if( videoarray[ i ].ownertag == owner )
246 CL_CloseVideo( &videoarray[ i ] );
249 int cl_videoplaying = false; // old, but still supported
251 void CL_DrawVideo(void)
254 DrawQ_Pic(0, 0, &videoarray->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0);
257 void CL_VideoStart(char *filename)
261 if( videoarray->state != CLVIDEO_UNUSED )
262 CL_CloseVideo( videoarray );
263 if( !OpenVideo( videoarray, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) )
265 // expand the active range to include the new entry
266 cl_activevideos = max(cl_activevideos, 1);
268 cl_videoplaying = true;
270 CL_SetVideoState( videoarray, CLVIDEO_PLAY );
271 CL_RestartVideo( videoarray );
274 void CL_VideoStop(void)
276 cl_videoplaying = false;
278 CL_CloseVideo( videoarray );
281 static void CL_PlayVideo_f(void)
283 char name[MAX_QPATH];
289 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
293 sprintf(name, "video/%s.dpv", Cmd_Argv(1));
297 static void CL_StopVideo_f(void)
302 static void cl_video_start( void )
307 cl_videotexturepool = R_AllocTexturePool();
309 for( video = videoarray, i = 0 ; i < cl_activevideos ; i++, video++ )
310 if( video->state != CLVIDEO_UNUSED && !video->suspended )
311 video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
312 video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
315 static void cl_video_shutdown( void )
317 R_FreeTexturePool( &cl_videotexturepool );
320 static void cl_video_newmap( void )
324 void CL_Video_Init( void )
327 cl_videobytesperpixel = 4;
328 cl_videormask = BigLong(0xFF000000);
329 cl_videogmask = BigLong(0x00FF0000);
330 cl_videobmask = BigLong(0x0000FF00);
332 Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
333 Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
335 R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );