]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cl_video.c
replaced sbarpic_t with cachepic_t in sbar code (saves a bit of memory)
[xonotic/darkplaces.git] / cl_video.c
index 70053f1b557fe2aae4387ad54ed8dfa9c7a3931b..e74f27094cc27c685c2b0362c270ce8f65490e19 100644 (file)
@@ -9,19 +9,10 @@ static int  cl_videobmask;
 static int  cl_videogmask;
 static int     cl_videobytesperpixel;
 
+static int cl_activevideos;
 static clvideo_t videoarray[ MAXCLVIDEOS ];
-static mempool_t *cl_videomempool;
 static rtexturepool_t *cl_videotexturepool;
 
-static clvideo_t *FindUnusedVid( void )
-{
-       int i;
-       for( i = 1 ; i < MAXCLVIDEOS ; i++ )
-               if( videoarray[ i ].state == CLVIDEO_UNUSED )
-                       return &videoarray[ i ];
-       return NULL;
-}
-
 static qboolean OpenStream( clvideo_t * video )
 {
        char *errorstring;
@@ -44,8 +35,8 @@ static void SuspendVideo( clvideo_t * video )
        // free the image data
        Mem_Free( video->imagedata );
        // if we are in firstframe mode, also close the stream
-       if( video->state == CLVIDEO_FIRSTFRAME ) 
-               dpvsimpledecode_close( video->stream ); 
+       if( video->state == CLVIDEO_FIRSTFRAME )
+               dpvsimpledecode_close( video->stream );
 }
 
 static qboolean WakeVideo( clvideo_t * video )
@@ -59,10 +50,10 @@ static qboolean WakeVideo( clvideo_t * video )
                        video->state = CLVIDEO_UNUSED;
                        return false;
                }
-               
-       video->imagedata = Mem_Alloc( cl_videomempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
-       video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, 
-               video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );    
+
+       video->imagedata = Mem_Alloc( cl_mempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
+       video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
+               video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
 
        // update starttime
        video->starttime += realtime - video->lasttime;
@@ -70,12 +61,34 @@ static qboolean WakeVideo( clvideo_t * video )
        return true;
 }
 
-static clvideo_t* OpenVideo( clvideo_t *video, char *filename, char *name, int owner )
+clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner, qboolean cinematic )
 {
+       int i;
+       clvideo_t *video;
+
+       if (cinematic)
+       {
+               video = videoarray;
+               i = 0;
+       }
+       else
+       {
+               for (i = 1, video = videoarray; i < cl_activevideos;i++, video++)
+                       if (videoarray[i].state == CLVIDEO_UNUSED)
+                               break;
+               if (i == MAXCLVIDEOS)
+               {
+                       Con_Printf( "unable to open video \"%s\" - video limit reached\n", filename );
+                       return NULL;
+               }
+       }
+
        strncpy( video->filename, filename, MAX_QPATH );
        video->ownertag = owner;
+
        if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
                return NULL;
+
        strncpy( video->cpif.name, name, MAX_QPATH );
 
        if( !OpenStream( video ) )
@@ -88,36 +101,25 @@ static clvideo_t* OpenVideo( clvideo_t *video, char *filename, char *name, int o
 
        video->cpif.width = dpvsimpledecode_getwidth( video->stream );
        video->cpif.height = dpvsimpledecode_getheight( video->stream );
-       video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, 
-               video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
+       video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
 
-    video->imagedata = Mem_Alloc( cl_videomempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
+       video->imagedata = Mem_Alloc( cl_mempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel );
 
+       // expand the active range to include the new entry
+       cl_activevideos = max(cl_activevideos, i + 1);
        return video;
 }
 
-clvideo_t* CL_OpenVideo( char *filename, char *name, int owner )
-{
-       clvideo_t *video;
-
-       video = FindUnusedVid();
-       if( !video ) {
-               Con_Printf( "unable to open video \"%s\" - video limit reached\n", filename );
-               return NULL;
-       }
-       return OpenVideo( video, filename, name, owner );
-}
-
-clvideo_t* CL_GetVideo( char *name )
+clvideo_t* CL_GetVideo( const char *name )
 {
        int i;
        clvideo_t *video;
 
-       for( i = 0 ; i < MAXCLVIDEOS ; i++ )
-               if( videoarray[ i ].state != CLVIDEO_UNUSED 
+       for( i = 0 ; i < cl_activevideos ; i++ )
+               if( videoarray[ i ].state != CLVIDEO_UNUSED
                        &&      !strcmp( videoarray[ i ].cpif.name , name ) )
                        break;
-       if( i == MAXCLVIDEOS )
+       if( i == cl_activevideos )
                return NULL;
        video = &videoarray[ i ];
 
@@ -125,7 +127,7 @@ clvideo_t* CL_GetVideo( char *name )
        {
                if( !WakeVideo( video ) )
                        return NULL;
-               else if( video->state == CLVIDEO_RESETONWAKEUP ) 
+               else if( video->state == CLVIDEO_RESETONWAKEUP )
                        video->framenum = -1;
        }
 
@@ -149,7 +151,7 @@ void CL_RestartVideo( clvideo_t *video )
 {
        if( !video )
                return;
-    
+
        video->starttime = video->lasttime = realtime;
        video->framenum = -1;
 
@@ -186,9 +188,9 @@ static void VideoFrame( clvideo_t *video )
        if( video->framenum < destframe ) {
                do {
                        video->framenum++;
-                       if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask, 
-                               cl_videogmask, cl_videobmask, cl_videobytesperpixel, 
-                               cl_videobytesperpixel * video->cpif.width ) 
+                       if( dpvsimpledecode_video( video->stream, video->imagedata, cl_videormask,
+                               cl_videogmask, cl_videobmask, cl_videobytesperpixel,
+                               cl_videobytesperpixel * video->cpif.width )
                                ) { // finished?
                                CL_RestartVideo( video );
                                if( video->state == CLVIDEO_PLAY )
@@ -196,8 +198,8 @@ static void VideoFrame( clvideo_t *video )
                                return;
                        }
                } while( video->framenum < destframe );
-               R_UpdateTexture( video->cpif.tex, video->imagedata );
-       }                                       
+               R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata );
+       }
 }
 
 void CL_VideoFrame( void ) // update all videos
@@ -205,35 +207,36 @@ void CL_VideoFrame( void ) // update all videos
        int i;
        clvideo_t *video;
 
-       for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; video++, i++ )
+       for( video = videoarray, i = 0 ; i < cl_activevideos ; video++, i++ )
                if( video->state != CLVIDEO_UNUSED && !video->suspended )
                {
                        if( realtime - video->lasttime > CLTHRESHOLD )
                                SuspendVideo( video );
                        else if( video->state == CLVIDEO_PAUSE )
                                video->starttime = realtime - video->framenum * video->framerate;
-                       else 
+                       else
                                VideoFrame( video );
                }
 
        if( videoarray->state == CLVIDEO_FIRSTFRAME )
                CL_VideoStop();
+
+       // reduce range to exclude unnecessary entries
+       while (cl_activevideos > 0 && videoarray[cl_activevideos-1].state == CLVIDEO_UNUSED)
+               cl_activevideos--;
 }
 
 void CL_Video_Shutdown( void )
 {
        int i;
-       for( i = 0 ; i < MAXCLVIDEOS ; i++ )
+       for( i = 0 ; i < cl_activevideos ; i++ )
                CL_CloseVideo( &videoarray[ i ] );
-
-       R_FreeTexturePool( &cl_videotexturepool );
-       Mem_FreePool( &cl_videomempool );
 }
 
 void CL_PurgeOwner( int owner )
 {
        int i;
-       for( i = 0 ; i < MAXCLVIDEOS ; i++ )
+       for( i = 0 ; i < cl_activevideos ; i++ )
                if( videoarray[ i ].ownertag == owner )
                        CL_CloseVideo( &videoarray[ i ] );
 }
@@ -243,14 +246,16 @@ int cl_videoplaying = false; // old, but still supported
 void CL_DrawVideo(void)
 {
        if (cl_videoplaying)
-               DrawQ_Pic(0, 0, videoarray->cpif.name, vid.conwidth, vid.conheight, 1, 1, 1, 1, 0);
+               DrawQ_Pic(0, 0, &videoarray->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0);
 }
 
 void CL_VideoStart(char *filename)
 {
+       Host_StartVideo();
+
        if( videoarray->state != CLVIDEO_UNUSED )
                CL_CloseVideo( videoarray );
-       if( !OpenVideo( videoarray, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) )
+       if( !CL_OpenVideo( filename, va( CLVIDEOPREFIX "%s", filename ), 0, true ) )
                return;
 
        cl_videoplaying = true;
@@ -268,7 +273,9 @@ void CL_VideoStop(void)
 
 static void CL_PlayVideo_f(void)
 {
-       char name[1024];
+       char name[MAX_QPATH];
+
+       Host_StartVideo();
 
        if (Cmd_Argc() != 2)
        {
@@ -285,16 +292,39 @@ static void CL_StopVideo_f(void)
        CL_VideoStop();
 }
 
+static void cl_video_start( void )
+{
+       int i;
+       clvideo_t *video;
+
+       cl_videotexturepool = R_AllocTexturePool();
+
+       for( video = videoarray, i = 0 ; i < cl_activevideos ; i++, video++ )
+               if( video->state != CLVIDEO_UNUSED && !video->suspended )
+                       video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name,
+                               video->cpif.width, video->cpif.height, NULL, TEXTYPE_RGBA, 0, NULL );
+}
+
+static void cl_video_shutdown( void )
+{
+       R_FreeTexturePool( &cl_videotexturepool );
+}
+
+static void cl_video_newmap( void )
+{
+}
+
 void CL_Video_Init( void )
 {
+       cl_activevideos = 0;
        cl_videobytesperpixel = 4;
        cl_videormask = BigLong(0xFF000000);
        cl_videogmask = BigLong(0x00FF0000);
        cl_videobmask = BigLong(0x0000FF00);
 
-       cl_videomempool = Mem_AllocPool( "CL_Video", 0, NULL );
-       cl_videotexturepool = R_AllocTexturePool();
+       Cmd_AddCommand( "playvideo", CL_PlayVideo_f, "play a .dpv video file" );
+       Cmd_AddCommand( "stopvideo", CL_StopVideo_f, "stop playing a .dpv video file" );
 
-       Cmd_AddCommand( "playvideo", CL_PlayVideo_f );
-       Cmd_AddCommand( "stopvideo", CL_StopVideo_f );
+       R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );
 }
+