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