2020-06-27 09:48:55 +00:00
/*
* * screenjob . cpp
* *
* * Generic asynchronous screen display
* *
* * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* * Copyright 2020 Christoph Oelckers
* * All rights reserved .
* *
* * Redistribution and use in source and binary forms , with or without
* * modification , are permitted provided that the following conditions
* * are met :
* *
* * 1. Redistributions of source code must retain the above copyright
* * notice , this list of conditions and the following disclaimer .
* * 2. Redistributions in binary form must reproduce the above copyright
* * notice , this list of conditions and the following disclaimer in the
* * documentation and / or other materials provided with the distribution .
* * 3. The name of the author may not be used to endorse or promote products
* * derived from this software without specific prior written permission .
* *
* * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ` ` AS IS ' ' AND ANY EXPRESS OR
* * IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
* * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED .
* * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT , INDIRECT ,
* * INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT
* * NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* * DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* * THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
* * THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
* * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* *
*/
2020-06-20 07:46:41 +00:00
# include "types.h"
# include "build.h"
# include "screenjob.h"
2020-06-27 09:48:55 +00:00
# include "i_time.h"
# include "v_2ddrawer.h"
2020-06-27 22:32:28 +00:00
# include "animlib.h"
# include "v_draw.h"
# include "s_soundinternal.h"
# include "animtexture.h"
2020-07-19 10:48:31 +00:00
# include "gamestate.h"
2020-07-21 22:42:50 +00:00
# include "menu.h"
2020-07-26 16:02:24 +00:00
# include "raze_sound.h"
2020-07-23 20:26:07 +00:00
# include "movie/playmve.h"
2020-06-20 07:46:41 +00:00
2020-06-27 09:48:55 +00:00
IMPLEMENT_CLASS ( DScreenJob , true , false )
2020-06-28 20:17:27 +00:00
IMPLEMENT_CLASS ( DImageScreen , true , false )
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
int DImageScreen : : Frame ( uint64_t clock , bool skiprequest )
{
if ( tilenum > 0 )
{
tex = tileGetTexture ( tilenum , true ) ;
}
if ( ! tex ) return 0 ;
int span = int ( clock / 1'000'000 ) ;
twod - > ClearScreen ( ) ;
2020-07-01 10:55:32 +00:00
DrawTexture ( twod , tex , 0 , 0 , DTA_FullscreenEx , 3 , DTA_LegacyRenderStyle , STYLE_Normal , TAG_DONE ) ;
2020-06-28 20:17:27 +00:00
// Only end after having faded out.
2020-07-01 10:55:32 +00:00
return skiprequest ? - 1 : 1 ;
2020-06-28 20:17:27 +00:00
}
2020-06-27 22:32:28 +00:00
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
2020-06-27 09:48:55 +00:00
2020-06-27 22:32:28 +00:00
class DAnmPlayer : public DScreenJob
2020-06-20 07:46:41 +00:00
{
2020-06-27 22:32:28 +00:00
// This doesn't need its own class type
anim_t anim ;
TArray < uint8_t > buffer ;
int numframes = 0 ;
int curframe = 1 ;
int frametime = 0 ;
int ototalclock = 0 ;
AnimTextures animtex ;
const AnimSound * animSnd ;
const int * frameTicks ;
public :
bool isvalid ( ) { return numframes > 0 ; }
DAnmPlayer ( FileReader & fr , const AnimSound * ans , const int * frameticks )
: animSnd ( ans ) , frameTicks ( frameticks )
2020-06-20 07:46:41 +00:00
{
2020-06-27 22:32:28 +00:00
buffer = fr . ReadPadded ( 1 ) ;
fr . Close ( ) ;
if ( ANIM_LoadAnim ( & anim , buffer . Data ( ) , buffer . Size ( ) - 1 ) < 0 )
2020-06-20 07:46:41 +00:00
{
2020-06-27 22:32:28 +00:00
return ;
}
numframes = ANIM_NumFrames ( & anim ) ;
animtex . SetSize ( AnimTexture : : Paletted , 320 , 200 ) ;
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
int Frame ( uint64_t clock , bool skiprequest ) override
{
int totalclock = int ( clock * 120 / 1'000'000'000 ) ;
if ( curframe > 4 & & totalclock > frametime + 60 )
{
Printf ( " WARNING: slowdown in video playback, aborting \n " ) ;
2020-07-26 16:02:24 +00:00
soundEngine - > StopAllChannels ( ) ;
2020-06-27 22:32:28 +00:00
return - 1 ;
}
if ( totalclock < ototalclock - 1 )
{
twod - > ClearScreen ( ) ;
DrawTexture ( twod , animtex . GetFrame ( ) , 0 , 0 , DTA_FullscreenEx , 3 , DTA_Masked , false , TAG_DONE ) ;
2020-07-26 16:02:24 +00:00
if ( skiprequest ) soundEngine - > StopAllChannels ( ) ;
2020-06-27 22:32:28 +00:00
return skiprequest ? - 1 : 1 ;
}
animtex . SetFrame ( ANIM_GetPalette ( & anim ) , ANIM_DrawFrame ( & anim , curframe ) ) ;
frametime = totalclock ;
twod - > ClearScreen ( ) ;
DrawTexture ( twod , animtex . GetFrame ( ) , 0 , 0 , DTA_FullscreenEx , 3 , DTA_Masked , false , TAG_DONE ) ;
int delay = 20 ;
if ( frameTicks )
{
if ( curframe = = 0 ) delay = frameTicks [ 0 ] ;
else if ( curframe < numframes - 1 ) delay = frameTicks [ 1 ] ;
else delay = frameTicks [ 2 ] ;
}
ototalclock + = delay ;
for ( int i = 0 ; animSnd [ i ] . framenum > = 0 ; i + + )
{
if ( animSnd [ i ] . framenum = = curframe )
2020-06-20 07:46:41 +00:00
{
2020-06-27 22:32:28 +00:00
int sound = animSnd [ i ] . soundnum ;
if ( sound = = - 1 )
soundEngine - > StopAllChannels ( ) ;
else
2020-06-28 12:42:31 +00:00
soundEngine - > StartSound ( SOURCE_None , nullptr , nullptr , CHAN_AUTO , CHANF_UI , sound , 1.f , ATTN_NONE ) ;
2020-06-20 07:46:41 +00:00
}
}
2020-06-27 22:32:28 +00:00
curframe + + ;
2020-06-28 19:38:25 +00:00
if ( skiprequest ) soundEngine - > StopAllChannels ( ) ;
2020-06-27 22:32:28 +00:00
return skiprequest ? - 1 : curframe < numframes ? 1 : 0 ;
}
void OnDestroy ( ) override
{
buffer . Reset ( ) ;
animtex . Clean ( ) ;
}
} ;
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
2020-07-23 20:26:07 +00:00
class DMvePlayer : public DScreenJob
{
InterplayDecoder decoder ;
bool failed = false ;
public :
bool isvalid ( ) { return ! failed ; }
DMvePlayer ( FileReader & fr )
{
failed = ! decoder . Open ( fr ) ;
}
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
int Frame ( uint64_t clock , bool skiprequest ) override
{
if ( failed ) return - 1 ;
bool playon = decoder . RunFrame ( clock ) ;
twod - > ClearScreen ( ) ;
DrawTexture ( twod , decoder . animTex ( ) . GetFrame ( ) , 0 , 0 , DTA_FullscreenEx , 3 , TAG_DONE ) ;
2020-07-24 17:05:34 +00:00
return skiprequest ? - 1 : playon ? 1 : 0 ;
2020-07-23 20:26:07 +00:00
}
void OnDestroy ( ) override
{
decoder . Close ( ) ;
}
} ;
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
DScreenJob * PlayVideo ( const char * filename , const AnimSound * ans , const int * frameticks )
2020-06-27 22:32:28 +00:00
{
2020-07-01 20:27:38 +00:00
auto nothing = [ ] ( ) - > DScreenJob * { return Create < DScreenJob > ( ) ; } ;
2020-06-28 19:38:25 +00:00
if ( ! filename )
2020-06-27 22:32:28 +00:00
{
2020-07-01 20:27:38 +00:00
return nothing ( ) ;
2020-06-27 22:32:28 +00:00
}
auto fr = fileSystem . OpenFileReader ( filename ) ;
if ( ! fr . isOpen ( ) )
{
Printf ( " %s: Unable to open video \n " , filename ) ;
2020-07-01 20:27:38 +00:00
return nothing ( ) ;
2020-06-27 22:32:28 +00:00
}
char id [ 20 ] = { } ;
fr . Read ( & id , 20 ) ;
fr . Seek ( - 20 , FileReader : : SeekCur ) ;
if ( ! memcmp ( id , " LPF " , 4 ) )
{
auto anm = Create < DAnmPlayer > ( fr , ans , frameticks ) ;
if ( ! anm - > isvalid ( ) )
{
Printf ( " %s: invalid ANM file. \n " , filename ) ;
anm - > Destroy ( ) ;
2020-07-23 20:26:07 +00:00
return nothing ( ) ;
2020-06-27 22:32:28 +00:00
}
2020-06-28 19:38:25 +00:00
return anm ;
2020-06-27 22:32:28 +00:00
}
else if ( ! memcmp ( id , " SMK2 " , 4 ) )
{
// todo
}
2020-07-23 20:26:07 +00:00
else if ( ! memcmp ( id , " Interplay MVE File " , 18 ) )
2020-06-27 22:32:28 +00:00
{
2020-07-23 20:26:07 +00:00
auto anm = Create < DMvePlayer > ( fr ) ;
if ( ! anm - > isvalid ( ) )
{
anm - > Destroy ( ) ;
return nothing ( ) ;
}
return anm ;
2020-06-27 22:32:28 +00:00
}
// add more formats here.
else
{
Printf ( " %s: Unknown video format \n " , filename ) ;
2020-06-20 07:46:41 +00:00
}
2020-07-01 20:27:38 +00:00
return nothing ( ) ;
2020-06-20 07:46:41 +00:00
}
2020-06-27 22:32:28 +00:00
2020-07-19 09:57:00 +00:00
//---------------------------------------------------------------------------
//
//
//
//---------------------------------------------------------------------------
class ScreenJobRunner
{
enum
{
State_Clear ,
State_Run ,
State_Fadeout
} ;
TArray < JobDesc > jobs ;
CompletionFunc completion ;
int index = - 1 ;
float screenfade ;
bool clearbefore ;
2020-07-19 10:11:51 +00:00
uint64_t startTime = - 1 ;
2020-07-21 22:42:50 +00:00
uint64_t lastTime = - 1 ;
2020-07-19 09:57:00 +00:00
int actionState ;
int terminateState ;
public :
ScreenJobRunner ( JobDesc * jobs_ , int count , CompletionFunc completion_ , bool clearbefore_ )
: completion ( std : : move ( completion_ ) ) , clearbefore ( clearbefore_ )
{
jobs . Resize ( count ) ;
memcpy ( jobs . Data ( ) , jobs_ , count * sizeof ( JobDesc ) ) ;
// Release all jobs from the garbage collector - the code as it is cannot deal with them getting collected. This should be removed later once the GC is working.
for ( int i = 0 ; i < count ; i + + )
{
jobs [ i ] . job - > Release ( ) ;
}
AdvanceJob ( false ) ;
}
2020-07-19 10:48:31 +00:00
~ ScreenJobRunner ( )
{
DeleteJobs ( ) ;
}
void DeleteJobs ( )
{
for ( auto & job : jobs )
{
job . job - > Destroy ( ) ;
job . job - > ObjectFlags | = OF_YesReallyDelete ;
delete job . job ;
}
jobs . Clear ( ) ;
}
2020-07-19 09:57:00 +00:00
void AdvanceJob ( bool skip )
{
2020-07-24 18:11:50 +00:00
if ( index > = 0 & & jobs [ index ] . postAction ) jobs [ index ] . postAction ( ) ;
2020-07-19 09:57:00 +00:00
index + + ;
while ( index < jobs . Size ( ) & & ( jobs [ index ] . job = = nullptr | | ( skip & & jobs [ index ] . ignoreifskipped ) ) ) index + + ;
actionState = clearbefore ? State_Clear : State_Run ;
if ( index < jobs . Size ( ) ) screenfade = jobs [ index ] . job - > fadestyle & DScreenJob : : fadein ? 0.f : 1.f ;
2020-07-19 10:11:51 +00:00
startTime = - 1 ;
2020-07-26 17:55:06 +00:00
inputState . ClearAllInput ( ) ;
2020-07-19 09:57:00 +00:00
}
int DisplayFrame ( )
{
auto & job = jobs [ index ] ;
auto now = I_nsTime ( ) ;
bool skiprequest = inputState . CheckAllInput ( ) ;
2020-07-19 10:11:51 +00:00
if ( startTime = = - 1 ) startTime = now ;
2020-07-21 22:42:50 +00:00
if ( M_Active ( ) )
{
startTime + = now - lastTime ;
}
lastTime = now ;
2020-07-19 09:57:00 +00:00
auto clock = now - startTime ;
if ( screenfade < 1.f )
{
float ms = ( clock / 1'000'000 ) / job . job - > fadetime ;
screenfade = clamp ( ms , 0.f , 1.f ) ;
2020-07-21 22:42:50 +00:00
if ( ! M_Active ( ) ) twod - > SetScreenFade ( screenfade ) ;
2020-07-19 09:57:00 +00:00
job . job - > fadestate = DScreenJob : : fadein ;
}
else job . job - > fadestate = DScreenJob : : visible ;
job . job - > SetClock ( clock ) ;
int state = job . job - > Frame ( clock , skiprequest ) ;
startTime - = job . job - > GetClock ( ) - clock ;
return state ;
}
int FadeoutFrame ( )
{
auto now = I_nsTime ( ) ;
2020-07-21 22:42:50 +00:00
if ( M_Active ( ) )
{
startTime + = now - lastTime ;
}
lastTime = now ;
2020-07-19 09:57:00 +00:00
auto clock = now - startTime ;
float ms = ( clock / 1'000'000 ) / jobs [ index ] . job - > fadetime ;
float screenfade2 = clamp ( screenfade - ms , 0.f , 1.f ) ;
2020-07-21 22:42:50 +00:00
if ( ! M_Active ( ) ) twod - > SetScreenFade ( screenfade2 ) ;
2020-07-19 09:57:00 +00:00
if ( screenfade2 < = 0.f )
{
twod - > Unlock ( ) ; // must unlock before displaying.
return 0 ;
}
return 1 ;
}
bool RunFrame ( )
{
if ( index > = jobs . Size ( ) )
{
2020-07-19 10:48:31 +00:00
DeleteJobs ( ) ;
2020-07-19 09:57:00 +00:00
twod - > SetScreenFade ( 1 ) ;
if ( completion ) completion ( false ) ;
return false ;
}
handleevents ( ) ;
if ( actionState = = State_Clear )
{
actionState = State_Run ;
twod - > ClearScreen ( ) ;
}
else if ( actionState = = State_Run )
{
terminateState = DisplayFrame ( ) ;
if ( terminateState < 1 )
{
// Must lock before displaying.
if ( jobs [ index ] . job - > fadestyle & DScreenJob : : fadeout )
{
twod - > Lock ( ) ;
startTime = I_nsTime ( ) ;
jobs [ index ] . job - > fadestate = DScreenJob : : fadeout ;
actionState = State_Fadeout ;
}
else
{
AdvanceJob ( terminateState < 0 ) ;
}
}
}
else if ( actionState = = State_Fadeout )
{
int ended = FadeoutFrame ( ) ;
if ( ended < 1 )
{
AdvanceJob ( terminateState < 0 ) ;
}
}
return true ;
}
} ;
2020-07-19 10:48:31 +00:00
ScreenJobRunner * runner ;
2020-07-21 22:42:50 +00:00
void RunScreenJob ( JobDesc * jobs , int count , CompletionFunc completion , bool clearbefore , bool blockingui )
2020-07-19 09:57:00 +00:00
{
2020-07-19 10:48:31 +00:00
assert ( completion ! = nullptr ) ;
if ( count )
2020-07-19 09:57:00 +00:00
{
2020-07-19 10:48:31 +00:00
runner = new ScreenJobRunner ( jobs , count , completion , clearbefore ) ;
2020-07-21 22:42:50 +00:00
gamestate = blockingui ? GS_INTRO : GS_INTERMISSION ;
2020-07-19 09:57:00 +00:00
}
}
2020-07-19 10:48:31 +00:00
void DeleteScreenJob ( )
2020-07-19 09:57:00 +00:00
{
2020-07-19 10:48:31 +00:00
if ( runner )
2020-07-19 09:57:00 +00:00
{
2020-07-19 10:48:31 +00:00
delete runner ;
runner = nullptr ;
2020-07-19 09:57:00 +00:00
}
2020-07-19 10:48:31 +00:00
}
2020-07-19 09:57:00 +00:00
2020-07-19 10:48:31 +00:00
void RunScreenJobFrame ( )
{
// we cannot recover from this because we have no completion callback to call.
if ( ! runner ) I_Error ( " Trying to run a non-existent screen job " ) ;
auto res = runner - > RunFrame ( ) ;
2020-07-26 10:43:32 +00:00
if ( ! res )
{
assert ( gamestate ! = GS_INTERMISSION & & gamestate ! = GS_INTRO ) ;
DeleteScreenJob ( ) ;
}
2020-07-19 09:57:00 +00:00
}