2019-11-20 16:21:32 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Copyright (C) 2010-2019 EDuke32 developers and contributors
|
|
|
|
Copyright (C) 2019 sirlemonhead, Nuke.YKT
|
|
|
|
This file is part of PCExhumed.
|
|
|
|
PCExhumed is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License version 2
|
|
|
|
as published by the Free Software Foundation.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
2019-11-22 23:11:37 +00:00
|
|
|
#include "ns.h"
|
2019-08-26 03:59:14 +00:00
|
|
|
#include "engine.h"
|
|
|
|
#include "exhumed.h"
|
|
|
|
#include "names.h"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include "baselayer.h"
|
2020-01-02 18:56:57 +00:00
|
|
|
#include "c_bind.h"
|
2019-12-05 21:18:34 +00:00
|
|
|
#include "sound.h"
|
2019-12-31 18:25:49 +00:00
|
|
|
#include "v_2ddrawer.h"
|
2020-04-12 05:46:53 +00:00
|
|
|
#include "animtexture.h"
|
2020-08-20 15:52:56 +00:00
|
|
|
#include "s_music.h"
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-11-22 23:11:37 +00:00
|
|
|
BEGIN_PS_NS
|
|
|
|
|
2019-08-26 03:59:14 +00:00
|
|
|
enum {
|
2019-08-31 07:47:15 +00:00
|
|
|
kFramePalette = 0,
|
|
|
|
kFrameSound,
|
|
|
|
kFrameImage,
|
|
|
|
kFrameDone
|
2019-08-26 03:59:14 +00:00
|
|
|
};
|
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
#define kSampleRate 22050
|
|
|
|
#define kSampleSize 2205
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
uint8_t bankbuf[kSampleRate];
|
|
|
|
uint32_t bankptr = 0;
|
|
|
|
uint32_t banktail = 0;
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
uint8_t lh[32] = { 0 };
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
static uint8_t* CurFrame = NULL;
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
bool bServedSample = false;
|
2019-10-12 21:09:55 +00:00
|
|
|
palette_t moviepal[256];
|
2020-08-20 15:52:56 +00:00
|
|
|
const int numAudioBlocks = 20;
|
|
|
|
int sfxnum = -1;
|
|
|
|
|
|
|
|
struct AudioData
|
|
|
|
{
|
|
|
|
int16_t samples[2205 * numAudioBlocks]; // must be a multiple of the stream buffer size
|
|
|
|
int nWrite;
|
|
|
|
int nRead;
|
|
|
|
};
|
|
|
|
|
|
|
|
SoundStream* stream;
|
|
|
|
AudioData audio;
|
|
|
|
bool bAudioStarted;
|
|
|
|
|
|
|
|
SoundHandle shandle;
|
2019-10-12 21:09:55 +00:00
|
|
|
|
2020-04-12 05:46:53 +00:00
|
|
|
int ReadFrame(FileReader &fp, uint8_t *palette)
|
2019-08-26 03:59:14 +00:00
|
|
|
{
|
2019-08-31 07:47:15 +00:00
|
|
|
static int nFrame = 0;
|
2019-11-24 09:03:19 +00:00
|
|
|
Printf("Reading frame %d...\n", nFrame);
|
2019-08-31 07:47:15 +00:00
|
|
|
nFrame++;
|
|
|
|
|
|
|
|
uint8_t nType;
|
|
|
|
uint8_t var_1C;
|
|
|
|
int nSize;
|
2019-08-31 09:08:38 +00:00
|
|
|
uint16_t yOffset;
|
2019-08-31 07:47:15 +00:00
|
|
|
uint8_t xOffset;
|
|
|
|
uint8_t nPixels;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2019-11-24 12:59:36 +00:00
|
|
|
if (fp.Read(&nType, sizeof(nType)) == 0) {
|
2019-08-31 07:47:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-24 12:59:36 +00:00
|
|
|
fp.Read(&nSize, sizeof(nSize));
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
nType--;
|
|
|
|
if (nType > 3) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (nType)
|
|
|
|
{
|
|
|
|
case kFramePalette:
|
|
|
|
{
|
2020-04-12 05:46:53 +00:00
|
|
|
fp.Read(palette, 768);
|
2019-11-24 12:59:36 +00:00
|
|
|
fp.Read(&var_1C, sizeof(var_1C));
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-04-12 05:46:53 +00:00
|
|
|
for (unsigned i = 0; i < 768;i++)
|
|
|
|
palette[i] <<= 2;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
memset(CurFrame, overscanindex, 4); //sizeof(CurFrame));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case kFrameSound:
|
|
|
|
{
|
2020-08-20 15:52:56 +00:00
|
|
|
auto sfxx = soundEngine->GetSounds();
|
|
|
|
auto buffer = fp.Read(nSize);
|
|
|
|
assert(buffer.Size() == 2205);
|
|
|
|
auto wbuffer = audio.samples + audio.nWrite * 2205;
|
|
|
|
for (int i = 0; i < 2205; i++)
|
2019-12-05 21:18:34 +00:00
|
|
|
{
|
2020-08-20 15:52:56 +00:00
|
|
|
wbuffer[i] = (buffer[i] - 128) << 8;
|
2019-12-05 21:18:34 +00:00
|
|
|
}
|
2020-08-20 15:52:56 +00:00
|
|
|
audio.nWrite++;
|
|
|
|
if (audio.nWrite >= numAudioBlocks) audio.nWrite = 0;
|
2019-08-31 07:47:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case kFrameImage:
|
|
|
|
{
|
2020-03-01 16:22:31 +00:00
|
|
|
//Printf("Reading image block size %d...\n", nSize);
|
2019-08-31 07:47:15 +00:00
|
|
|
if (nSize == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *pFrame = CurFrame;
|
|
|
|
|
2019-11-24 12:59:36 +00:00
|
|
|
int nRead = fp.Read(&yOffset, sizeof(yOffset));
|
2019-08-31 07:47:15 +00:00
|
|
|
nSize -= nRead;
|
|
|
|
|
|
|
|
pFrame += yOffset * 200; // row position
|
|
|
|
|
|
|
|
while (nSize > 0)
|
|
|
|
{
|
2019-11-24 12:59:36 +00:00
|
|
|
fp.Read(&xOffset, sizeof(xOffset));
|
|
|
|
fp.Read(&nPixels, sizeof(nPixels));
|
2019-08-31 07:47:15 +00:00
|
|
|
nSize -= 2;
|
|
|
|
|
|
|
|
pFrame += xOffset;
|
|
|
|
|
|
|
|
if (nPixels)
|
|
|
|
{
|
2019-11-24 12:59:36 +00:00
|
|
|
int nRead = fp.Read(pFrame, nPixels);
|
2019-08-31 07:47:15 +00:00
|
|
|
pFrame += nRead;
|
|
|
|
nSize -= nRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kFrameDone:
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 03:59:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 14:35:54 +00:00
|
|
|
#if 0
|
2020-02-04 21:45:10 +00:00
|
|
|
static void ServeSample(const char** ptr, uint32_t* length)
|
2019-08-26 03:59:14 +00:00
|
|
|
{
|
2019-12-25 23:21:04 +00:00
|
|
|
//mutex_lock(&mutex);
|
2019-12-05 21:18:34 +00:00
|
|
|
|
|
|
|
*ptr = (char*)bankbuf + banktail;
|
|
|
|
*length = kSampleSize;
|
|
|
|
|
|
|
|
banktail += kSampleSize;
|
|
|
|
if (banktail >= kSampleRate) {
|
|
|
|
banktail -= kSampleRate; // rotate back to start
|
|
|
|
}
|
|
|
|
|
|
|
|
lSoundBytesUsed += kSampleSize;
|
|
|
|
bServedSample = true;
|
|
|
|
|
2019-12-25 23:21:04 +00:00
|
|
|
//mutex_unlock(&mutex);
|
2019-12-05 21:18:34 +00:00
|
|
|
}
|
2020-07-23 14:35:54 +00:00
|
|
|
#endif
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 15:52:56 +00:00
|
|
|
static bool StreamCallbackFunc(SoundStream* stream, void* buff, int len, void* userdata)
|
|
|
|
{
|
|
|
|
memcpy(buff, &audio.samples[audio.nRead], len);
|
|
|
|
audio.nRead += len / 2;
|
|
|
|
if (audio.nRead >= countof(audio.samples)) audio.nRead = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
void PlayMovie(const char* fileName)
|
|
|
|
{
|
2020-04-12 05:46:53 +00:00
|
|
|
uint8_t palette[768];
|
2020-03-01 17:32:43 +00:00
|
|
|
TArray<uint8_t> f(64000, true);
|
|
|
|
CurFrame = f.Data();
|
2020-02-29 11:55:12 +00:00
|
|
|
|
2020-08-18 08:28:19 +00:00
|
|
|
int bDoFade = true;
|
2020-04-11 21:54:33 +00:00
|
|
|
auto fp = fileSystem.OpenFileReader(fileName);
|
2019-11-24 12:59:36 +00:00
|
|
|
if (!fp.isOpen())
|
|
|
|
{
|
|
|
|
Printf("Unable to open %s\n", fileName);
|
|
|
|
return;
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2019-11-24 12:59:36 +00:00
|
|
|
fp.Read(lh, sizeof(lh));
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
// sound stuff
|
2019-12-05 21:18:34 +00:00
|
|
|
bankptr = 0;
|
|
|
|
banktail = 0;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
// clear keys
|
2020-01-01 10:35:47 +00:00
|
|
|
inputState.ClearAllInput();
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 15:52:56 +00:00
|
|
|
bAudioStarted = false;
|
2019-08-31 07:47:15 +00:00
|
|
|
if (bDoFade) {
|
|
|
|
StartFadeIn();
|
|
|
|
}
|
2020-08-20 15:52:56 +00:00
|
|
|
memset(audio.samples, 0, sizeof(audio.samples));
|
|
|
|
audio.nWrite = 5; // play 5 blocks (i.e. half a second) of silence to get ahead of the stream. For this video it isn't necessary to sync it perfectly.
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 15:52:56 +00:00
|
|
|
double angle = 1536;
|
|
|
|
double z = 0;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-04-12 05:46:53 +00:00
|
|
|
AnimTextures animtex;
|
2020-06-14 18:27:13 +00:00
|
|
|
animtex.SetSize(AnimTexture::Paletted, 200, 320);
|
2019-10-12 21:09:55 +00:00
|
|
|
|
2020-08-20 15:52:56 +00:00
|
|
|
auto sfxx = soundEngine->GetSounds();
|
|
|
|
|
|
|
|
if (!bAudioStarted)
|
|
|
|
{
|
|
|
|
// start audio playback
|
|
|
|
stream = S_CreateCustomStream(4410, 22050, 1, StreamCallbackFunc, nullptr);
|
|
|
|
bAudioStarted = true;
|
|
|
|
}
|
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
// Read a frame in first
|
2020-04-12 05:46:53 +00:00
|
|
|
if (ReadFrame(fp, palette))
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
2020-03-01 17:32:43 +00:00
|
|
|
int fn = 0;
|
2020-08-20 15:52:56 +00:00
|
|
|
int ototalclock = totalclock + 12;
|
|
|
|
int lastclock = totalclock;
|
|
|
|
while (true)// !inputState.keyBufferWaiting())
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
2019-12-05 21:18:34 +00:00
|
|
|
HandleAsync();
|
|
|
|
|
2019-08-31 07:47:15 +00:00
|
|
|
if (z < 65536) { // Zoom - normal zoom is 65536.
|
2020-08-20 15:52:56 +00:00
|
|
|
z += 2048 * (totalclock - lastclock) / 8.;
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
2020-08-20 15:52:56 +00:00
|
|
|
if (z > 65536) z = 65536;
|
2019-08-31 07:47:15 +00:00
|
|
|
if (angle != 0) {
|
2020-08-20 15:52:56 +00:00
|
|
|
angle += 16. * (totalclock-lastclock) / 8. ;
|
|
|
|
if (angle >= 2048) {
|
2019-08-31 07:47:15 +00:00
|
|
|
angle = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-08-20 15:52:56 +00:00
|
|
|
lastclock = totalclock;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-03-01 17:32:43 +00:00
|
|
|
// I have no idea why this needs double buffering now.
|
|
|
|
fn ^= 1;
|
2020-04-12 05:46:53 +00:00
|
|
|
animtex.SetFrame(palette, CurFrame);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2020-08-20 15:52:56 +00:00
|
|
|
|
|
|
|
rotatesprite(160 << 16, 100 << 16, int(z), int(angle+512), -1, 0, 1, RS_AUTO | RS_YFLIP, 0, 0, xdim - 1, ydim - 1, animtex.GetFrame());
|
|
|
|
|
|
|
|
#if 0
|
2019-08-31 07:47:15 +00:00
|
|
|
if (bDoFade) {
|
|
|
|
bDoFade = DoFadeIn();
|
|
|
|
}
|
2020-08-20 15:52:56 +00:00
|
|
|
#endif
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
videoNextPage();
|
|
|
|
|
2020-08-20 15:52:56 +00:00
|
|
|
if (totalclock >= ototalclock)
|
|
|
|
{
|
|
|
|
ototalclock += 12;
|
|
|
|
if (ReadFrame(fp, palette) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 15:52:56 +00:00
|
|
|
S_StopCustomStream(stream);
|
2019-11-24 12:59:36 +00:00
|
|
|
if (inputState.keyBufferWaiting()) {
|
|
|
|
inputState.keyGetChar();
|
2019-08-31 07:47:15 +00:00
|
|
|
}
|
2019-08-26 03:59:14 +00:00
|
|
|
}
|
2020-07-23 14:35:54 +00:00
|
|
|
|
2019-11-22 23:11:37 +00:00
|
|
|
END_PS_NS
|