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