]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_video.c
unmerge OpenVideo code, back to the proper multilayer system it was
[divverent/darkplaces.git] / cl_video.c
1
2 #include "quakedef.h"
3 #include "cl_video.h"
4 #include "dpvsimpledecode.h"
5
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;
11
12 static int cl_activevideos;
13 static clvideo_t videoarray[ MAXCLVIDEOS ];
14 static rtexturepool_t *cl_videotexturepool;
15
16 static clvideo_t *FindUnusedVid( void )
17 {
18         int i;
19         for( i = 1 ; i < MAXCLVIDEOS ; i++ )
20                 if( videoarray[ i ].state == CLVIDEO_UNUSED )
21                         return &videoarray[ i ];
22         return NULL;
23 }
24
25 static qboolean OpenStream( clvideo_t * video )
26 {
27         char *errorstring;
28         video->stream = dpvsimpledecode_open( video->filename, &errorstring);
29         if (!video->stream )
30         {
31                 Con_Printf("unable to open \"%s\", error: %s\n", video->filename, errorstring);
32                 return false;
33         }
34         return true;
35 }
36
37 static void SuspendVideo( clvideo_t * video )
38 {
39         if( video->suspended )
40                 return;
41         video->suspended = true;
42         // free the texture
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 );
49 }
50
51 static qboolean WakeVideo( clvideo_t * video )
52 {
53         if( !video->suspended )
54                 return true;
55         video->suspended = false;
56
57         if( video->state == CLVIDEO_FIRSTFRAME )
58                 if( !OpenStream( video ) ) {
59                         video->state = CLVIDEO_UNUSED;
60                         return false;
61                 }
62
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 );
66
67         // update starttime
68         video->starttime += realtime - video->lasttime;
69
70         return true;
71 }
72
73 static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner )
74 {
75         strncpy( video->filename, filename, MAX_QPATH );
76         video->ownertag = owner;
77         if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
78                 return NULL;
79         strncpy( video->cpif.name, name, MAX_QPATH );
80
81         if( !OpenStream( video ) )
82                 return NULL;
83
84         video->state = CLVIDEO_FIRSTFRAME;
85         video->framenum = -1;
86         video->framerate = dpvsimpledecode_getframerate( video->stream );
87         video->lasttime = realtime;
88
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 );
93
94     video->imagedata = Mem_Alloc( cl_mempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
95
96         return video;
97 }
98
99 clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner )
100 {
101         clvideo_t *video;
102
103         video = FindUnusedVid();
104         if( !video ) {
105                 Con_Printf( "unable to open video \"%s\" - video limit reached\n", filename );
106                 return NULL;
107         }
108         video = OpenVideo( video, filename, name, owner );
109         // expand the active range to include the new entry
110         if (video)
111                 cl_activevideos = max(cl_activevideos, (int)(video - videoarray) + 1);
112         return video;
113 }
114
115 clvideo_t* CL_GetVideo( const char *name )
116 {
117         int i;
118         clvideo_t *video;
119
120         for( i = 0 ; i < cl_activevideos ; i++ )
121                 if( videoarray[ i ].state != CLVIDEO_UNUSED
122                         &&      !strcmp( videoarray[ i ].cpif.name , name ) )
123                         break;
124         if( i == cl_activevideos )
125                 return NULL;
126         video = &videoarray[ i ];
127
128         if( video->suspended )
129         {
130                 if( !WakeVideo( video ) )
131                         return NULL;
132                 else if( video->state == CLVIDEO_RESETONWAKEUP )
133                         video->framenum = -1;
134         }
135
136         video->lasttime = realtime;
137
138         return video;
139 }
140
141 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
142 {
143         if( !video )
144                 return;
145
146         video->lasttime = realtime;
147         video->state = state;
148         if( state == CLVIDEO_FIRSTFRAME )
149                 CL_RestartVideo( video );
150 }
151
152 void CL_RestartVideo( clvideo_t *video )
153 {
154         if( !video )
155                 return;
156
157         video->starttime = video->lasttime = realtime;
158         video->framenum = -1;
159
160         dpvsimpledecode_close( video->stream );
161         if( !OpenStream( video ) )
162                 video->state = CLVIDEO_UNUSED;
163 }
164
165 void CL_CloseVideo( clvideo_t * video )
166 {
167         if( !video || video->state == CLVIDEO_UNUSED )
168                 return;
169
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 );
175         }
176
177         video->state = CLVIDEO_UNUSED;
178 }
179
180 static void VideoFrame( clvideo_t *video )
181 {
182         int destframe;
183
184         if( video->state == CLVIDEO_FIRSTFRAME )
185                 destframe = 0;
186         else
187                 destframe = (realtime - video->starttime) * video->framerate;
188         if( destframe < 0 )
189                 destframe = 0;
190         if( video->framenum < destframe ) {
191                 do {
192                         video->framenum++;
193                         if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
194                                 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
195                                 cl_videobytesperpixel * video->cpif.width )
196                                 ) { // finished?
197                                 CL_RestartVideo( video );
198                                 if( video->state == CLVIDEO_PLAY )
199                                                 video->state = CLVIDEO_FIRSTFRAME;
200                                 return;
201                         }
202                 } while( video->framenum < destframe );
203                 R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata );
204         }
205 }
206
207 void CL_VideoFrame( void ) // update all videos
208 {
209         int i;
210         clvideo_t *video;
211
212         if (!cl_activevideos)
213                 return;
214
215         for( video = videoarray, i = 0 ; i < cl_activevideos ; video++, i++ )
216                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
217                 {
218                         if( realtime - video->lasttime > CLTHRESHOLD )
219                                 SuspendVideo( video );
220                         else if( video->state == CLVIDEO_PAUSE )
221                                 video->starttime = realtime - video->framenum * video->framerate;
222                         else
223                                 VideoFrame( video );
224                 }
225
226         if( videoarray->state == CLVIDEO_FIRSTFRAME )
227                 CL_VideoStop();
228
229         // reduce range to exclude unnecessary entries
230         while (cl_activevideos > 0 && videoarray[cl_activevideos-1].state == CLVIDEO_UNUSED)
231                 cl_activevideos--;
232 }
233
234 void CL_Video_Shutdown( void )
235 {
236         int i;
237         for( i = 0 ; i < cl_activevideos ; i++ )
238                 CL_CloseVideo( &videoarray[ i ] );
239 }
240
241 void CL_PurgeOwner( int owner )
242 {
243         int i;
244         for( i = 0 ; i < cl_activevideos ; i++ )
245                 if( videoarray[ i ].ownertag == owner )
246                         CL_CloseVideo( &videoarray[ i ] );
247 }
248
249 int cl_videoplaying = false; // old, but still supported
250
251 void CL_DrawVideo(void)
252 {
253         if (cl_videoplaying)
254                 DrawQ_Pic(0, 0, &videoarray->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0);
255 }
256
257 void CL_VideoStart(char *filename)
258 {
259         Host_StartVideo();
260
261         if( videoarray->state != CLVIDEO_UNUSED )
262                 CL_CloseVideo( videoarray );
263         if( !OpenVideo( videoarray, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) )
264                 return;
265         // expand the active range to include the new entry
266         cl_activevideos = max(cl_activevideos, 1);
267
268         cl_videoplaying = true;
269
270         CL_SetVideoState( videoarray, CLVIDEO_PLAY );
271         CL_RestartVideo( videoarray );
272 }
273
274 void CL_VideoStop(void)
275 {
276         cl_videoplaying = false;
277
278         CL_CloseVideo( videoarray );
279 }
280
281 static void CL_PlayVideo_f(void)
282 {
283         char name[MAX_QPATH];
284
285         Host_StartVideo();
286
287         if (Cmd_Argc() != 2)
288         {
289                 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
290                 return;
291         }
292
293         sprintf(name, "video/%s.dpv", Cmd_Argv(1));
294         CL_VideoStart(name);
295 }
296
297 static void CL_StopVideo_f(void)
298 {
299         CL_VideoStop();
300 }
301
302 static void cl_video_start( void )
303 {
304         int i;
305         clvideo_t *video;
306
307         cl_videotexturepool = R_AllocTexturePool();
308
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 );
313 }
314
315 static void cl_video_shutdown( void )
316 {
317         R_FreeTexturePool( &cl_videotexturepool );
318 }
319
320 static void cl_video_newmap( void )
321 {
322 }
323
324 void CL_Video_Init( void )
325 {
326         cl_activevideos = 0;
327         cl_videobytesperpixel = 4;
328         cl_videormask = BigLong(0xFF000000);
329         cl_videogmask = BigLong(0x00FF0000);
330         cl_videobmask = BigLong(0x0000FF00);
331
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" );
334
335         R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );
336 }
337