]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cl_video.c
slight optimization to CL_RelinkNetworkEntities to scan entities faster
[xonotic/darkplaces.git] / cl_video.c
index d3804da616df4f61d45f72a99dd1491feaf90fc2..cd34f6f07f2ddeb560543e009c67faa7de512865 100644 (file)
@@ -53,7 +53,7 @@ static qboolean WakeVideo( clvideo_t * video )
        if( !video->suspended )
                return true;
        video->suspended = false;
-       
+
        if( video->state == CLVIDEO_FIRSTFRAME )
                if( !OpenStream( video ) ) {
                        video->state = CLVIDEO_UNUSED;
@@ -66,6 +66,7 @@ static qboolean WakeVideo( clvideo_t * video )
 
        // update starttime
        video->starttime += realtime - video->lasttime;
+
        return true;
 }
 
@@ -73,8 +74,9 @@ static clvideo_t* OpenVideo( clvideo_t *video, char *filename, char *name, int o
 {
        strncpy( video->filename, filename, MAX_QPATH );
        video->ownertag = owner;
-       strncpy( video->cpif.name, CLVIDEOPREFIX, MAX_QPATH );
-       strncat( video->cpif.name, name, MAX_QPATH - sizeof( CLVIDEOPREFIX ) );
+       if( strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) )
+               return NULL;
+       strncpy( video->cpif.name, name, MAX_QPATH );
 
        if( !OpenStream( video ) )
                return NULL;
@@ -120,40 +122,27 @@ clvideo_t* CL_GetVideo( char *name )
        video = &videoarray[ i ];
 
        if( video->suspended )
+       {
                if( !WakeVideo( video ) )
                        return NULL;
+               else if( video->state == CLVIDEO_RESETONWAKEUP ) 
+                       video->framenum = -1;
+       }
+
        video->lasttime = realtime;
 
        return video;
 }
 
-void CL_StartVideo( clvideo_t * video )
-{
-       if( !video )
-               return;
-
-       video->starttime = video->lasttime = realtime;
-       video->framenum = -1;
-       video->state = CLVIDEO_PLAY;
-}
-
-void CL_LoopVideo( clvideo_t * video )
+void CL_SetVideoState( clvideo_t *video, clvideostate_t state )
 {
        if( !video )
                return;
 
-       video->starttime = video->lasttime = realtime;
-       video->framenum = -1;
-       video->state = CLVIDEO_LOOP;
-}
-
-void CL_PauseVideo( clvideo_t * video )
-{
-       if( !video )
-               return;
-
-       video->state = CLVIDEO_PAUSE;
        video->lasttime = realtime;
+       video->state = state;
+       if( state == CLVIDEO_FIRSTFRAME )
+               CL_RestartVideo( video );
 }
 
 void CL_RestartVideo( clvideo_t *video )
@@ -163,16 +152,10 @@ void CL_RestartVideo( clvideo_t *video )
     
        video->starttime = video->lasttime = realtime;
        video->framenum = -1;
-}
 
-void CL_StopVideo( clvideo_t * video )
-{
-       if( !video )
-               return;
-
-       video->lasttime = realtime;
-       video->framenum = -1;
-       video->state = CLVIDEO_FIRSTFRAME;
+       dpvsimpledecode_close( video->stream );
+       if( !OpenStream( video ) )
+               video->state = CLVIDEO_UNUSED;
 }
 
 void CL_CloseVideo( clvideo_t * video )
@@ -180,14 +163,14 @@ void CL_CloseVideo( clvideo_t * video )
        if( !video || video->state == CLVIDEO_UNUSED )
                return;
 
-       video->state = CLVIDEO_UNUSED;
-       
        if( !video->suspended || video->state != CLVIDEO_FIRSTFRAME )
                dpvsimpledecode_close( video->stream );
        if( !video->suspended ) {
                Mem_Free( video->imagedata );
                R_FreeTexture( video->cpif.tex );
        }
+
+       video->state = CLVIDEO_UNUSED;
 }
 
 static void VideoFrame( clvideo_t *video )
@@ -207,10 +190,8 @@ static void VideoFrame( clvideo_t *video )
                                cl_videogmask, cl_videobmask, cl_videobytesperpixel, 
                                cl_videobytesperpixel * video->cpif.width ) 
                                ) { // finished?
-                               video->framenum = -1;
-                               if( video->state == CLVIDEO_LOOP )
-                                               video->starttime = realtime;
-                               else if( video->state == CLVIDEO_PLAY )
+                               CL_RestartVideo( video );
+                               if( video->state == CLVIDEO_PLAY )
                                                video->state = CLVIDEO_FIRSTFRAME;
                                return;
                        }
@@ -226,12 +207,14 @@ void CL_VideoFrame( void ) // update all videos
 
        for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; 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;
+                               video->starttime = realtime - video->framenum * video->framerate;
                        else 
                                VideoFrame( video );
+               }
 
        if( videoarray->state == CLVIDEO_FIRSTFRAME )
                CL_VideoStop();
@@ -243,7 +226,6 @@ void CL_Video_Shutdown( void )
        for( i = 0 ; i < MAXCLVIDEOS ; i++ )
                CL_CloseVideo( &videoarray[ i ] );
 
-       R_FreeTexturePool( &cl_videotexturepool );
        Mem_FreePool( &cl_videomempool );
 }
 
@@ -267,12 +249,13 @@ void CL_VideoStart(char *filename)
 {
        if( videoarray->state != CLVIDEO_UNUSED )
                CL_CloseVideo( videoarray );
-       if( !OpenVideo( videoarray, filename, filename, 0 ) )
+       if( !OpenVideo( videoarray, filename, va( CLVIDEOPREFIX "%s", filename ), 0 ) )
                return;
 
        cl_videoplaying = true;
 
-       CL_StartVideo( videoarray );
+       CL_SetVideoState( videoarray, CLVIDEO_PLAY );
+       CL_RestartVideo( videoarray );
 }
 
 void CL_VideoStop(void)
@@ -301,6 +284,28 @@ 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 < MAXCLVIDEOS ; 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_videobytesperpixel = 4;
@@ -308,9 +313,11 @@ void CL_Video_Init( void )
        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 );
        Cmd_AddCommand( "stopvideo", CL_StopVideo_f );
+       
+       cl_videomempool = Mem_AllocPool( "CL_Video", 0, NULL );
+
+       R_RegisterModule( "CL_Video", cl_video_start, cl_video_shutdown, cl_video_newmap );
 }
+