X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=cl_video.c;h=9ac887c9069fa2de872770dc4c5acd43ccf12ff9;hb=5090f6da276d673f6ee0b149ee2f83ef82f62902;hp=d3804da616df4f61d45f72a99dd1491feaf90fc2;hpb=afe94889d0a86a7372936cf870bfe2951d449cb8;p=xonotic%2Fdarkplaces.git diff --git a/cl_video.c b/cl_video.c index d3804da6..9ac887c9 100644 --- a/cl_video.c +++ b/cl_video.c @@ -1,5 +1,6 @@ #include "quakedef.h" +#include "cl_dyntexture.h" #include "cl_video.h" #include "dpvsimpledecode.h" @@ -9,16 +10,16 @@ static int cl_videobmask; static int cl_videogmask; static int cl_videobytesperpixel; -static clvideo_t videoarray[ MAXCLVIDEOS ]; -static mempool_t *cl_videomempool; +static int cl_num_videos; +static clvideo_t cl_videos[ MAXCLVIDEOS ]; 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 ]; + if( cl_videos[ i ].state == CLVIDEO_UNUSED ) + return &cl_videos[ i ]; return NULL; } @@ -34,18 +35,35 @@ static qboolean OpenStream( clvideo_t * video ) return true; } +static void VideoUpdateCallback(rtexture_t *rt, void *data) { + clvideo_t *video = data; + R_UpdateTexture( video->cpif.tex, (unsigned char *)video->imagedata, 0, 0, video->cpif.width, video->cpif.height ); +} + +static void LinkVideoTexture( clvideo_t *video ) { + video->cpif.tex = R_LoadTexture2D( cl_videotexturepool, video->cpif.name, + video->cpif.width, video->cpif.height, NULL, TEXTYPE_BGRA, TEXF_ALWAYSPRECACHE | TEXF_PERSISTENT, NULL ); + R_MakeTextureDynamic( video->cpif.tex, VideoUpdateCallback, video ); + CL_LinkDynTexture( video->cpif.name, video->cpif.tex ); +} + +static void UnlinkVideoTexture( clvideo_t *video ) { + CL_UnlinkDynTexture( video->cpif.name ); + // free the texture + R_FreeTexture( video->cpif.tex ); + // free the image data + Mem_Free( video->imagedata ); +} + static void SuspendVideo( clvideo_t * video ) { if( video->suspended ) return; video->suspended = true; - // free the texture - R_FreeTexture( video->cpif.tex ); - // free the image data - Mem_Free( video->imagedata ); + UnlinkVideoTexture( video ); // 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 ) @@ -53,28 +71,29 @@ 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; 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( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel ); + LinkVideoTexture( video ); // update starttime video->starttime += realtime - video->lasttime; + return true; } -static clvideo_t* OpenVideo( clvideo_t *video, char *filename, char *name, int owner ) +static clvideo_t* OpenVideo( clvideo_t *video, const char *filename, const char *name, int owner ) { - strncpy( video->filename, filename, MAX_QPATH ); + strlcpy( video->filename, filename, sizeof(video->filename) ); 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; + strlcpy( video->cpif.name, name, sizeof(video->cpif.name) ); if( !OpenStream( video ) ) return NULL; @@ -86,93 +105,89 @@ 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->imagedata = Mem_Alloc( cl_videomempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel ); + video->imagedata = Mem_Alloc( cls.permanentmempool, video->cpif.width * video->cpif.height * cl_videobytesperpixel ); + LinkVideoTexture( video ); return video; } -clvideo_t* CL_OpenVideo( char *filename, char *name, int owner ) +clvideo_t* CL_OpenVideo( const char *filename, const char *name, int owner ) { clvideo_t *video; + // sanity check + if( !name || !*name || strncmp( name, CLVIDEOPREFIX, sizeof( CLVIDEOPREFIX ) - 1 ) != 0 ) { + if( developer.integer > 0 ) { + Con_Printf( "CL_OpenVideo: Bad video texture name '%s'!\n", name ); + } + return NULL; + } video = FindUnusedVid(); if( !video ) { - Con_Printf( "unable to open video \"%s\" - video limit reached\n", filename ); + Con_Printf( "CL_OpenVideo: unable to open video \"%s\" - video limit reached\n", filename ); return NULL; } - return OpenVideo( video, filename, name, owner ); + video = OpenVideo( video, filename, name, owner ); + // expand the active range to include the new entry + if (video) { + cl_num_videos = max(cl_num_videos, (int)(video - cl_videos) + 1); + } + return video; } -clvideo_t* CL_GetVideo( char *name ) +static clvideo_t* CL_GetVideoBySlot( int slot ) { - int i; - clvideo_t *video; - - for( i = 0 ; i < MAXCLVIDEOS ; i++ ) - if( videoarray[ i ].state != CLVIDEO_UNUSED - && !strcmp( videoarray[ i ].cpif.name , name ) ) - break; - if( i == MAXCLVIDEOS ) - return NULL; - video = &videoarray[ i ]; + clvideo_t *video = &cl_videos[ slot ]; 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 ) +clvideo_t *CL_GetVideoByName( const char *name ) { - if( !video ) - return; - - video->starttime = video->lasttime = realtime; - video->framenum = -1; - video->state = CLVIDEO_PLAY; -} - -void CL_LoopVideo( clvideo_t * video ) -{ - if( !video ) - return; + int i; - video->starttime = video->lasttime = realtime; - video->framenum = -1; - video->state = CLVIDEO_LOOP; + for( i = 0 ; i < cl_num_videos ; i++ ) + if( cl_videos[ i ].state != CLVIDEO_UNUSED + && !strcmp( cl_videos[ i ].cpif.name , name ) ) + break; + if( i != cl_num_videos ) + return CL_GetVideoBySlot( i ); + else + return NULL; } -void CL_PauseVideo( clvideo_t * video ) +void CL_SetVideoState( clvideo_t *video, clvideostate_t state ) { 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 ) { if( !video ) return; - + 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 +195,13 @@ 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 ); + UnlinkVideoTexture( video ); } + + video->state = CLVIDEO_UNUSED; } static void VideoFrame( clvideo_t *video ) @@ -197,62 +211,66 @@ static void VideoFrame( clvideo_t *video ) if( video->state == CLVIDEO_FIRSTFRAME ) destframe = 0; else - destframe = (realtime - video->starttime) * video->framerate; + destframe = (int)((realtime - video->starttime) * video->framerate); if( destframe < 0 ) destframe = 0; 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? - 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; } } while( video->framenum < destframe ); - R_UpdateTexture( video->cpif.tex, video->imagedata ); - } + R_MarkDirtyTexture( video->cpif.tex ); + } } -void CL_VideoFrame( void ) // update all videos +void CL_Video_Frame( void ) // update all videos { int i; clvideo_t *video; - for( video = videoarray, i = 0 ; i < MAXCLVIDEOS ; video++, i++ ) + if (!cl_num_videos) + return; + + for( video = cl_videos, i = 0 ; i < cl_num_videos ; 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 + video->starttime = realtime - video->framenum * video->framerate; + else VideoFrame( video ); + } - if( videoarray->state == CLVIDEO_FIRSTFRAME ) + if( cl_videos->state == CLVIDEO_FIRSTFRAME ) CL_VideoStop(); + + // reduce range to exclude unnecessary entries + while (cl_num_videos > 0 && cl_videos[cl_num_videos-1].state == CLVIDEO_UNUSED) + cl_num_videos--; } void CL_Video_Shutdown( void ) { int i; - for( i = 0 ; i < MAXCLVIDEOS ; i++ ) - CL_CloseVideo( &videoarray[ i ] ); - - R_FreeTexturePool( &cl_videotexturepool ); - Mem_FreePool( &cl_videomempool ); + for( i = 0 ; i < cl_num_videos ; i++ ) + CL_CloseVideo( &cl_videos[ i ] ); } void CL_PurgeOwner( int owner ) { int i; - for( i = 0 ; i < MAXCLVIDEOS ; i++ ) - if( videoarray[ i ].ownertag == owner ) - CL_CloseVideo( &videoarray[ i ] ); + for( i = 0 ; i < cl_num_videos ; i++ ) + if( cl_videos[ i ].ownertag == owner ) + CL_CloseVideo( &cl_videos[ i ] ); } int cl_videoplaying = false; // old, but still supported @@ -260,31 +278,49 @@ 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, &CL_GetVideoBySlot( 0 )->cpif, vid_conwidth.integer, vid_conheight.integer, 1, 1, 1, 1, 0); } void CL_VideoStart(char *filename) { - if( videoarray->state != CLVIDEO_UNUSED ) - CL_CloseVideo( videoarray ); - if( !OpenVideo( videoarray, filename, filename, 0 ) ) + Host_StartVideo(); + + if( cl_videos->state != CLVIDEO_UNUSED ) + CL_CloseVideo( cl_videos ); + // already contains video/ + if( !OpenVideo( cl_videos, filename, va( CLDYNTEXTUREPREFIX "%s", filename ), 0 ) ) return; + // expand the active range to include the new entry + cl_num_videos = max(cl_num_videos, 1); cl_videoplaying = true; - CL_StartVideo( videoarray ); + CL_SetVideoState( cl_videos, CLVIDEO_PLAY ); + CL_RestartVideo( cl_videos ); +} + +void CL_Video_KeyEvent( int key, int ascii, qboolean down ) +{ + // only react to up events, to allow the user to delay the abortion point if it suddenly becomes interesting.. + if( !down ) { + if( key == K_ESCAPE || key == K_ENTER || key == K_SPACE ) { + CL_VideoStop(); + } + } } void CL_VideoStop(void) { cl_videoplaying = false; - CL_CloseVideo( videoarray ); + CL_CloseVideo( cl_videos ); } static void CL_PlayVideo_f(void) { - char name[1024]; + char name[MAX_QPATH]; + + Host_StartVideo(); if (Cmd_Argc() != 2) { @@ -292,7 +328,7 @@ static void CL_PlayVideo_f(void) return; } - sprintf(name, "video/%s.dpv", Cmd_Argv(1)); + dpsnprintf(name, sizeof(name), "video/%s.dpv", Cmd_Argv(1)); CL_VideoStart(name); } @@ -301,16 +337,48 @@ 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 = cl_videos, i = 0 ; i < cl_num_videos ; i++, video++ ) + if( video->state != CLVIDEO_UNUSED && !video->suspended ) + LinkVideoTexture( video ); +} + +static void cl_video_shutdown( void ) +{ + // TODO: unlink video textures? + R_FreeTexturePool( &cl_videotexturepool ); +} + +static void cl_video_newmap( void ) +{ +} + void CL_Video_Init( void ) { + union + { + unsigned char b[4]; + unsigned int i; + } + bgra; + + cl_num_videos = 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(); + // set masks in an endian-independent way (as they really represent bytes) + bgra.i = 0;bgra.b[0] = 0xFF;cl_videobmask = bgra.i; + bgra.i = 0;bgra.b[1] = 0xFF;cl_videogmask = bgra.i; + bgra.i = 0;bgra.b[2] = 0xFF;cl_videormask = bgra.i; + + 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 ); } +