]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cl_video.c
removed cl_particles_explosions_smoke cvar and code
[xonotic/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_num_videos;
13 static clvideo_t cl_videos[ 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( cl_videos[ i ].state == CLVIDEO_UNUSED )
21                         return &cl_videos[ 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( cls.permanentmempool, 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, TEXF_ALWAYSPRECACHE, 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         strlcpy( video->filename, filename, sizeof(video->filename) );
76         video->ownertag = owner;
77         if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
78                 return NULL;
79         strlcpy( video->cpif.name, name, sizeof(video->cpif.name) );
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, TEXF_ALWAYSPRECACHE, NULL );
93
94     video->imagedata = Mem_Alloc( cls.permanentmempool, 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_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1);
112         return video;
113 }
114
115 static clvideo_t* CL_GetVideoBySlot( int slot )
116 {
117         clvideo_t *video = &cl_videos[ slot ];
118
119         if( video->suspended )
120         {
121                 if( !WakeVideo( video ) )
122                         return NULL;
123                 else if( video->state == CLVIDEO_RESETONWAKEUP )
124                         video->framenum = -1;
125         }
126
127         video->lasttime = realtime;
128
129         return video;
130 }
131
132 clvideo_t *CL_GetVideoByName( const char *name )
133 {
134         int i;
135
136         for( i = 0 ; i < cl_num_videos ; i++ )
137                 if( cl_videos[ i ].state != CLVIDEO_UNUSED
138                         &&      !strcmp( cl_videos[ i ].cpif.name , name ) )
139                         break;
140         if( i != cl_num_videos )
141                 return CL_GetVideoBySlot( i );
142         else
143                 return NULL;
144 }
145
146 void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
147 {
148         if( !video )
149                 return;
150
151         video->lasttime = realtime;
152         video->state = state;
153         if( state == CLVIDEO_FIRSTFRAME )
154                 CL_RestartVideo( video );
155 }
156
157 void CL_RestartVideo( clvideo_t *video )
158 {
159         if( !video )
160                 return;
161
162         video->starttime = video->lasttime = realtime;
163         video->framenum = -1;
164
165         dpvsimpledecode_close( video->stream );
166         if( !OpenStream( video ) )
167                 video->state = CLVIDEO_UNUSED;
168 }
169
170 void CL_CloseVideo( clvideo_t * video )
171 {
172         if( !video || video->state == CLVIDEO_UNUSED )
173                 return;
174
175         if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
176                 dpvsimpledecode_close( video->stream );
177         if( !video->suspended ) {
178                 Mem_Free( video->imagedata );
179                 R_FreeTexture( video->cpif.tex );
180         }
181
182         video->state = CLVIDEO_UNUSED;
183 }
184
185 static void VideoFrame( clvideo_t *video )
186 {
187         int destframe;
188
189         if( video->state == CLVIDEO_FIRSTFRAME )
190                 destframe = 0;
191         else
192                 destframe = (int)((realtime - video->starttime) * video->framerate);
193         if( destframe < 0 )
194                 destframe = 0;
195         if( video->framenum < destframe ) {
196                 do {
197                         video->framenum++;
198                         if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
199                                 cl_videogmask, cl_videobmask, cl_videobytesperpixel,
200                                 cl_videobytesperpixel * video->cpif.width )
201                                 ) { // finished?
202                                 CL_RestartVideo( video );
203                                 if( video->state == CLVIDEO_PLAY )
204                                                 video->state = CLVIDEO_FIRSTFRAME;
205                                 return;
206                         }
207                 } while( video->framenum < destframe );
208                 R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, video->cpif.width, video->cpif.height );
209         }
210 }
211
212 void CL_VideoFrame( void ) // update all videos
213 {
214         int i;
215         clvideo_t *video;
216
217         if (!cl_num_videos)
218                 return;
219
220         for( video = cl_videos, i = 0 ; i < cl_num_videos ; video++, i++ )
221                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
222                 {
223                         if( realtime - video->lasttime > CLTHRESHOLD )
224                                 SuspendVideo( video );
225                         else if( video->state == CLVIDEO_PAUSE )
226                                 video->starttime = realtime - video->framenum * video->framerate;
227                         else
228                                 VideoFrame( video );
229                 }
230
231         if( cl_videos->state == CLVIDEO_FIRSTFRAME )
232                 CL_VideoStop();
233
234         // reduce range to exclude unnecessary entries
235         while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED)
236                 cl_num_videos--;
237 }
238
239 void CL_Video_Shutdown( void )
240 {
241         int i;
242         for( i = 0 ; i < cl_num_videos ; i++ )
243                 CL_CloseVideo( &cl_videos[ i ] );
244 }
245
246 void CL_PurgeOwner( int owner )
247 {
248         int i;
249         for( i = 0 ; i < cl_num_videos ; i++ )
250                 if( cl_videos[ i ].ownertag == owner )
251                         CL_CloseVideo( &cl_videos[ i ] );
252 }
253
254 int cl_videoplaying = false; // old, but still supported
255
256 void CL_DrawVideo(void)
257 {
258         if (cl_videoplaying)
259                 DrawQ_Pic(0, 0, &CL_GetVideoBySlot( 0 )->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0);
260 }
261
262 void CL_VideoStart(char *filename)
263 {
264         Host_StartVideo();
265
266         if( cl_videos->state != CLVIDEO_UNUSED )
267                 CL_CloseVideo( cl_videos );
268         if( !OpenVideo( cl_videos, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) )
269                 return;
270         // expand the active range to include the new entry
271         cl_num_videos = max(cl_num_videos, 1);
272
273         cl_videoplaying = true;
274
275         CL_SetVideoState( cl_videos, CLVIDEO_PLAY );
276         CL_RestartVideo( cl_videos );
277 }
278
279 void CL_Video_KeyEvent( int key, int ascii, qboolean down ) 
280 {
281         // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting..
282         if( !down ) {
283                 if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) {
284                         CL_VideoStop();
285                 }
286         }
287 }
288
289 void CL_VideoStop(void)
290 {
291         cl_videoplaying = false;
292
293         CL_CloseVideo( cl_videos );
294 }
295
296 static void CL_PlayVideo_f(void)
297 {
298         char name[MAX_QPATH];
299
300         Host_StartVideo();
301
302         if (Cmd_Argc() != 2)
303         {
304                 Con_Print("usage: playvideo <videoname>\nplays video named video/<videoname>.dpv\n");
305                 return;
306         }
307
308         sprintf(name, "video/%s.dpv", Cmd_Argv(1));
309         CL_VideoStart(name);
310 }
311
312 static void CL_StopVideo_f(void)
313 {
314         CL_VideoStop();
315 }
316
317 static void cl_video_start( void )
318 {
319         int i;
320         clvideo_t *video;
321
322         cl_videotexturepool = R_AllocTexturePool();
323
324         for( video = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ )
325                 if( video->state != CLVIDEO_UNUSED && !video->suspended )
326                         video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
327                                 video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE, NULL );
328 }
329
330 static void cl_video_shutdown( void )
331 {
332         R_FreeTexturePool( &cl_videotexturepool );
333 }
334
335 static void cl_video_newmap( void )
336 {
337 }
338
339 void CL_Video_Init( void )
340 {
341         cl_num_videos = 0;
342         cl_videobytesperpixel = 4;
343         cl_videormask = BigLong(0xFF000000);
344         cl_videogmask = BigLong(0x00FF0000);
345         cl_videobmask = BigLong(0x0000FF00);
346
347         Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
348         Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
349
350         R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );
351 }
352