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 mempool_t *cl_videomempool;
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_videomempool, 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, char *filename, 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_videomempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
99 clvideo_t* CL_OpenVideo( char *filename, char *name, int owner )
103 video = FindUnusedVid();
105 Con_Printf( "unable to open video \"%s\" - video limit reached\n", filename );
108 return OpenVideo( video, filename, name, owner );
111 clvideo_t* CL_GetVideo( char *name )
116 for( i = 0 ; i < MAXCLVIDEOS ; i++ )
117 if( videoarray[ i ].state != CLVIDEO_UNUSED
118 && !strcmp( videoarray[ i ].cpif.name , name ) )
120 if( i == MAXCLVIDEOS )
122 video = &videoarray[ i ];
124 if( video->suspended )
125 if( !WakeVideo( video ) )
127 else if( video->state == CLVIDEO_RESETONWAKEUP )
128 video->framenum = -1;
130 video->lasttime = realtime;
135 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
140 video->lasttime = realtime;
141 video->state = state;
142 if( state == CLVIDEO_FIRSTFRAME )
143 CL_RestartVideo( video );
146 void CL_RestartVideo( clvideo_t *video )
151 video->starttime = video->lasttime = realtime;
152 video->framenum = -1;
154 dpvsimpledecode_close( video->stream );
155 if( !OpenStream( video ) )
156 video->state = CLVIDEO_UNUSED;
159 void CL_CloseVideo( clvideo_t * video )
161 if( !video || video->state == CLVIDEO_UNUSED )
164 if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
165 dpvsimpledecode_close( video->stream );
166 if( !video->suspended ) {
167 Mem_Free( video->imagedata );
168 R_FreeTexture( video->cpif.tex );
171 video->state = CLVIDEO_UNUSED;
174 static void VideoFrame( clvideo_t *video )
178 if( video->state == CLVIDEO_FIRSTFRAME )
181 destframe = (realtime - video->starttime) * video->framerate;
184 if( video->framenum < destframe ) {
187 if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
188 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
189 cl_videobytesperpixel * video->cpif.width )
191 CL_RestartVideo( video );
192 if( video->state == CLVIDEO_PLAY )
193 video->state = CLVIDEO_FIRSTFRAME;
196 } while( video->framenum < destframe );
197 R_UpdateTexture( video->cpif.tex, video->imagedata );
201 void CL_VideoFrame( void ) // update all videos
206 for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; video++, i++ )
207 if( video->state != CLVIDEO_UNUSED && !video->suspended )
208 if( realtime - video->lasttime > CLTHRESHOLD )
209 SuspendVideo( video );
210 else if( video->state == CLVIDEO_PAUSE )
211 video->starttime = realtime + video->framenum * video->framerate;
215 if( videoarray->state == CLVIDEO_FIRSTFRAME )
219 void CL_Video_Shutdown( void )
222 for( i = 0 ; i < MAXCLVIDEOS ; i++ )
223 CL_CloseVideo( &videoarray[ i ] );
225 R_FreeTexturePool( &cl_videotexturepool );
226 Mem_FreePool( &cl_videomempool );
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)
247 if( videoarray->state != CLVIDEO_UNUSED )
248 CL_CloseVideo( videoarray );
249 if( !OpenVideo( videoarray, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) )
252 cl_videoplaying = true;
254 CL_SetVideoState( videoarray, CLVIDEO_PLAY );
255 CL_RestartVideo( videoarray );
258 void CL_VideoStop(void)
260 cl_videoplaying = false;
262 CL_CloseVideo( videoarray );
265 static void CL_PlayVideo_f(void)
271 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
275 sprintf(name, "video/%s.dpv", Cmd_Argv(1));
279 static void CL_StopVideo_f(void)
284 void CL_Video_Init( void )
286 cl_videobytesperpixel = 4;
287 cl_videormask = BigLong(0xFF000000);
288 cl_videogmask = BigLong(0x00FF0000);
289 cl_videobmask = BigLong(0x0000FF00);
291 cl_videomempool = Mem_AllocPool( "CL_Video", 0, NULL );
292 cl_videotexturepool = R_AllocTexturePool();
294 Cmd_AddCommand( "playvideo", CL_PlayVideo_f );
295 Cmd_AddCommand( "stopvideo", CL_StopVideo_f );