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-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 09:48:55 +00:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2020-06-28 19:38:25 +00:00
|
|
|
void RunScreenJob(JobDesc *jobs, int count, CompletionFunc completion, bool clearbefore)
|
2020-06-27 09:48:55 +00:00
|
|
|
{
|
2020-06-28 19:38:25 +00:00
|
|
|
// Release all jobs from the garbage collector - the code as it is cannot deal with them getting collected.
|
|
|
|
for (int i = 0; i < count; i++)
|
2020-06-28 12:42:31 +00:00
|
|
|
{
|
2020-06-28 19:38:25 +00:00
|
|
|
jobs[i].job->Release();
|
2020-06-28 12:42:31 +00:00
|
|
|
}
|
2020-06-28 19:38:25 +00:00
|
|
|
bool skipped = false;
|
|
|
|
for (int i = 0; i < count; i++)
|
2020-06-27 09:48:55 +00:00
|
|
|
{
|
2020-06-28 19:38:25 +00:00
|
|
|
auto job = jobs[i];
|
|
|
|
if (job.job != nullptr && (!skipped || !job.ignoreifskipped))
|
2020-06-27 09:48:55 +00:00
|
|
|
{
|
2020-06-28 19:38:25 +00:00
|
|
|
skipped = false;
|
|
|
|
if (clearbefore)
|
|
|
|
{
|
|
|
|
twod->ClearScreen();
|
|
|
|
videoNextPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto startTime = I_nsTime();
|
|
|
|
|
|
|
|
// Input later needs to be event based so that these screens can do more than be skipped.
|
|
|
|
inputState.ClearAllInput();
|
2020-07-01 10:55:32 +00:00
|
|
|
|
|
|
|
float screenfade = job.job->fadestyle & DScreenJob::fadein ? 0.f : 1.f;
|
|
|
|
|
2020-06-28 19:38:25 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
auto now = I_nsTime();
|
|
|
|
handleevents();
|
|
|
|
bool skiprequest = inputState.CheckAllInput();
|
2020-07-01 10:55:32 +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);
|
|
|
|
twod->SetScreenFade(screenfade);
|
2020-07-03 07:59:24 +00:00
|
|
|
job.job->fadestate = DScreenJob::fadein;
|
2020-07-01 10:55:32 +00:00
|
|
|
}
|
2020-07-03 07:59:24 +00:00
|
|
|
else job.job->fadestate = DScreenJob::visible;
|
2020-07-01 10:55:32 +00:00
|
|
|
job.job->SetClock(clock);
|
|
|
|
int state = job.job->Frame(clock, skiprequest);
|
|
|
|
startTime -= job.job->GetClock() - clock;
|
|
|
|
// Must lock before displaying.
|
|
|
|
if (state < 1 && job.job->fadestyle & DScreenJob::fadeout)
|
|
|
|
twod->Lock();
|
|
|
|
|
2020-06-28 19:38:25 +00:00
|
|
|
videoNextPage();
|
|
|
|
if (state < 1)
|
|
|
|
{
|
2020-07-01 10:55:32 +00:00
|
|
|
if (job.job->fadestyle & DScreenJob::fadeout)
|
|
|
|
{
|
2020-07-03 07:59:24 +00:00
|
|
|
job.job->fadestate = DScreenJob::fadeout;
|
2020-07-01 10:55:32 +00:00
|
|
|
startTime = now;
|
|
|
|
float screenfade2;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
now = I_nsTime();
|
|
|
|
auto clock = now - startTime;
|
|
|
|
float ms = (clock / 1'000'000) / job.job->fadetime;
|
|
|
|
screenfade2 = clamp(screenfade - ms, 0.f, 1.f);
|
|
|
|
twod->SetScreenFade(screenfade2);
|
|
|
|
if (screenfade2 <= 0.f) twod->Unlock(); // must unlock before displaying.
|
|
|
|
videoNextPage();
|
|
|
|
|
|
|
|
} while (screenfade2 > 0.f);
|
|
|
|
}
|
2020-06-28 19:38:25 +00:00
|
|
|
skipped = state < 0;
|
|
|
|
job.job->Destroy();
|
|
|
|
job.job->ObjectFlags |= OF_YesReallyDelete;
|
|
|
|
delete job.job;
|
2020-07-01 10:55:32 +00:00
|
|
|
twod->SetScreenFade(1);
|
2020-06-28 19:38:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-06-27 09:48:55 +00:00
|
|
|
}
|
2020-06-28 19:38:25 +00:00
|
|
|
if (job.postAction) job.postAction();
|
2020-06-27 09:48:55 +00:00
|
|
|
}
|
2020-06-28 19:38:25 +00:00
|
|
|
if (completion) completion(false);
|
2020-06-27 09:48:55 +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 lastframe = -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");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (totalclock < ototalclock - 1)
|
|
|
|
{
|
|
|
|
twod->ClearScreen();
|
|
|
|
DrawTexture(twod, animtex.GetFrame(), 0, 0, DTA_FullscreenEx, 3, DTA_Masked, false, TAG_DONE);
|
|
|
|
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-06-28 19:38:25 +00:00
|
|
|
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-01 20:27:38 +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
|
|
|
|
}
|
|
|
|
else if (!memcmp(id, "Interplay MVE file", 18))
|
|
|
|
{
|
|
|
|
// todo
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|