]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_video.c
another C99 lazy variable fixed
[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 clvideo_t videoarray[ MAXCLVIDEOS ];
13 static mempool_t *cl_videomempool;
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_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 );    
66
67         // update starttime
68         video->starttime += realtime - video->lasttime;
69
70         return true;
71 }
72
73 static clvideo_t* OpenVideo( clvideo_t *video, char *filename, 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_videomempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
95
96         return video;
97 }
98
99 clvideo_t* CL_OpenVideo( char *filename, 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         return OpenVideo( video, filename, name, owner );
109 }
110
111 clvideo_t* CL_GetVideo( char *name )
112 {
113         int i;
114         clvideo_t *video;
115
116         for( i = 0 ; i < MAXCLVIDEOS ; i++ )
117                 if( videoarray[ i ].state != CLVIDEO_UNUSED 
118                         &&      !strcmp( videoarray[ i ].cpif.name , name ) )
119                         break;
120         if( i == MAXCLVIDEOS )
121                 return NULL;
122         video = &videoarray[ i ];
123
124         if( video->suspended )
125         {
126                 if( !WakeVideo( video ) )
127                         return NULL;
128                 else if( video->state == CLVIDEO_RESETONWAKEUP ) 
129                         video->framenum = -1;
130         }
131
132         video->lasttime = realtime;
133
134         return video;
135 }
136
137 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
138 {
139         if( !video )
140                 return;
141
142         video->lasttime = realtime;
143         video->state = state;
144         if( state == CLVIDEO_FIRSTFRAME )
145                 CL_RestartVideo( video );
146 }
147
148 void CL_RestartVideo( clvideo_t *video )
149 {
150         if( !video )
151                 return;
152     
153         video->starttime = video->lasttime = realtime;
154         video->framenum = -1;
155
156         dpvsimpledecode_close( video->stream );
157         if( !OpenStream( video ) )
158                 video->state = CLVIDEO_UNUSED;
159 }
160
161 void CL_CloseVideo( clvideo_t * video )
162 {
163         if( !video || video->state == CLVIDEO_UNUSED )
164                 return;
165
166         if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
167                 dpvsimpledecode_close( video->stream );
168         if( !video->suspended ) {
169                 Mem_Free( video->imagedata );
170                 R_FreeTexture( video->cpif.tex );
171         }
172
173         video->state = CLVIDEO_UNUSED;
174 }
175
176 static void VideoFrame( clvideo_t *video )
177 {
178         int destframe;
179
180         if( video->state == CLVIDEO_FIRSTFRAME )
181                 destframe = 0;
182         else
183                 destframe = (realtime - video->starttime) * video->framerate;
184         if( destframe < 0 )
185                 destframe = 0;
186         if( video->framenum < destframe ) {
187                 do {
188                         video->framenum++;
189                         if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask, 
190                                 cl_videogmask, cl_videobmask, cl_videobytesperpixel, 
191                                 cl_videobytesperpixel * video->cpif.width ) 
192                                 ) { // finished?
193                                 CL_RestartVideo( video );
194                                 if( video->state == CLVIDEO_PLAY )
195                                                 video->state = CLVIDEO_FIRSTFRAME;
196                                 return;
197                         }
198                 } while( video->framenum < destframe );
199                 R_UpdateTexture( video->cpif.tex, video->imagedata );
200         }                                       
201 }
202
203 void CL_VideoFrame( void ) // update all videos
204 {
205         int i;
206         clvideo_t *video;
207
208         for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; video++, i++ )
209                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
210                 {
211                         if( realtime - video->lasttime > CLTHRESHOLD )
212                                 SuspendVideo( video );
213                         else if( video->state == CLVIDEO_PAUSE )
214                                 video->starttime = realtime - video->framenum * video->framerate;
215                         else 
216                                 VideoFrame( video );
217                 }
218
219         if( videoarray->state == CLVIDEO_FIRSTFRAME )
220                 CL_VideoStop();
221 }
222
223 void CL_Video_Shutdown( void )
224 {
225         int i;
226         for( i = 0 ; i < MAXCLVIDEOS ; i++ )
227                 CL_CloseVideo( &videoarray[ i ] );
228
229         Mem_FreePool( &cl_videomempool );
230 }
231
232 void CL_PurgeOwner( int owner )
233 {
234         int i;
235         for( i = 0 ; i < MAXCLVIDEOS ; i++ )
236                 if( videoarray[ i ].ownertag == owner )
237                         CL_CloseVideo( &videoarray[ i ] );
238 }
239
240 int cl_videoplaying = false; // old, but still supported
241
242 void CL_DrawVideo(void)
243 {
244         if (cl_videoplaying)
245                 DrawQ_Pic(0, 0, videoarray->cpif.name, vid.conwidth, vid.conheight, 1, 1, 1, 1, 0);
246 }
247
248 void CL_VideoStart(char *filename)
249 {
250         if( videoarray->state != CLVIDEO_UNUSED )
251                 CL_CloseVideo( videoarray );
252         if( !OpenVideo( videoarray, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) )
253                 return;
254
255         cl_videoplaying = true;
256
257         CL_SetVideoState( videoarray, CLVIDEO_PLAY );
258         CL_RestartVideo( videoarray );
259 }
260
261 void CL_VideoStop(void)
262 {
263         cl_videoplaying = false;
264
265         CL_CloseVideo( videoarray );
266 }
267
268 static void CL_PlayVideo_f(void)
269 {
270         char name[1024];
271
272         if (Cmd_Argc() != 2)
273         {
274                 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
275                 return;
276         }
277
278         sprintf(name, "video/%s.dpv", Cmd_Argv(1));
279         CL_VideoStart(name);
280 }
281
282 static void CL_StopVideo_f(void)
283 {
284         CL_VideoStop();
285 }
286
287 static void cl_video_start( void )
288 {
289         int i;
290         clvideo_t *video;
291
292         cl_videotexturepool = R_AllocTexturePool();
293
294         for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; i++, video++ )
295                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
296                         video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, 
297                                 video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
298 }
299
300 static void cl_video_shutdown( void )
301 {
302         R_FreeTexturePool( &cl_videotexturepool );
303 }
304
305 static void cl_video_newmap( void )
306 {
307 }
308
309 void CL_Video_Init( void )
310 {
311         cl_videobytesperpixel = 4;
312         cl_videormask = BigLong(0xFF000000);
313         cl_videogmask = BigLong(0x00FF0000);
314         cl_videobmask = BigLong(0x0000FF00);
315
316         Cmd_AddCommand( "playvideo", CL_PlayVideo_f );
317         Cmd_AddCommand( "stopvideo", CL_StopVideo_f );
318         
319         cl_videomempool = Mem_AllocPool( "CL_Video", 0, NULL );
320
321         R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );
322 }
323