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 "movie.h"
|
|
|
|
#include "cdrom.h"
|
|
|
|
#include "light.h"
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include "baselayer.h"
|
|
|
|
#include "typedefs.h"
|
|
|
|
#include "keyboard.h"
|
2019-11-24 12:59:36 +00:00
|
|
|
#include "cache1d.h"
|
2019-12-05 21:18:34 +00:00
|
|
|
#include "fx_man.h"
|
|
|
|
#include "sound.h"
|
|
|
|
#include "mutex.h"
|
2019-08-26 03:59:14 +00:00
|
|
|
|
2019-11-22 23:11:37 +00:00
|
|
|
BEGIN_PS_NS
|
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
void ServeSample(const char** ptr, uint32_t* length);
|
|
|
|
|
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
|
|
|
uint32_t lSoundBytesRead = 0;
|
|
|
|
uint32_t lSoundBytesUsed = 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];
|
2019-12-05 21:18:34 +00:00
|
|
|
static mutex_t mutex;
|
2019-10-12 21:09:55 +00:00
|
|
|
|
2019-11-24 12:59:36 +00:00
|
|
|
int ReadFrame(FileReader &fp)
|
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;
|
|
|
|
uint8_t palette[768];
|
|
|
|
|
|
|
|
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:
|
|
|
|
{
|
2019-11-24 12:59:36 +00:00
|
|
|
fp.Read(palette, sizeof(palette));
|
|
|
|
fp.Read(&var_1C, sizeof(var_1C));
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2019-11-03 04:00:08 +00:00
|
|
|
for (auto &c : palette)
|
|
|
|
c <<= 2;
|
|
|
|
|
2019-10-12 21:09:55 +00:00
|
|
|
paletteSetColorTable(ANIMPAL, palette);
|
2019-10-12 22:27:12 +00:00
|
|
|
videoSetPalette(0, ANIMPAL, 2+8);
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
memset(CurFrame, overscanindex, 4); //sizeof(CurFrame));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case kFrameSound:
|
|
|
|
{
|
2019-11-24 09:03:19 +00:00
|
|
|
Printf("Reading sound block size %d...\n", nSize);
|
2019-12-05 21:18:34 +00:00
|
|
|
|
|
|
|
if (lSoundBytesRead - lSoundBytesUsed >= kSampleRate)
|
|
|
|
{
|
|
|
|
DebugOut("SOUND BUF FULL!\n");
|
2019-11-24 12:59:36 +00:00
|
|
|
fp.Seek(nSize, FileReader::SeekCur);
|
2019-12-05 21:18:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mutex_lock(&mutex);
|
|
|
|
|
|
|
|
int nRead = fread((char*)bankbuf + bankptr, 1, nSize, fp);
|
|
|
|
|
|
|
|
lSoundBytesRead += nRead;
|
|
|
|
bankptr += nSize;
|
|
|
|
|
|
|
|
assert(nSize == nRead);
|
|
|
|
assert(bankptr <= kSampleRate);
|
|
|
|
|
|
|
|
if (bankptr >= kSampleRate) {
|
|
|
|
bankptr -= kSampleRate; // loop back to start
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
|
2019-08-31 07:47:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case kFrameImage:
|
|
|
|
{
|
2019-11-24 09:03:19 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:54:31 +00:00
|
|
|
tileInvalidate(kMovieTile, -1, -1);
|
|
|
|
|
2019-08-31 07:47:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kFrameDone:
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-26 03:59:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
void ServeSample(const char** ptr, uint32_t* length)
|
2019-08-26 03:59:14 +00:00
|
|
|
{
|
2019-12-05 21:18:34 +00:00
|
|
|
mutex_lock(&mutex);
|
|
|
|
|
|
|
|
*ptr = (char*)bankbuf + banktail;
|
|
|
|
*length = kSampleSize;
|
|
|
|
|
|
|
|
banktail += kSampleSize;
|
|
|
|
if (banktail >= kSampleRate) {
|
|
|
|
banktail -= kSampleRate; // rotate back to start
|
|
|
|
}
|
|
|
|
|
|
|
|
lSoundBytesUsed += kSampleSize;
|
|
|
|
bServedSample = true;
|
|
|
|
|
|
|
|
mutex_unlock(&mutex);
|
|
|
|
}
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
void PlayMovie(const char* fileName)
|
|
|
|
{
|
2019-11-24 12:59:36 +00:00
|
|
|
char buffer[256];
|
2019-12-05 21:18:34 +00:00
|
|
|
int bDoFade = kTrue;
|
|
|
|
int hFx = -1;
|
2019-11-24 12:59:36 +00:00
|
|
|
|
|
|
|
if (bNoCDCheck)
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
|
|
|
sprintf(buffer, "C:\\PS\\%s", fileName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char driveLetter = GetCDDriveLetter();
|
|
|
|
if (!driveLetter) {
|
|
|
|
driveLetter = 'C';
|
|
|
|
}
|
|
|
|
sprintf(buffer, "%c:%s", driveLetter, fileName);
|
|
|
|
}
|
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
FILE* fp = fopen(buffer, "rb");
|
2019-08-31 07:47:15 +00:00
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2019-11-24 09:03:19 +00:00
|
|
|
Printf("Can't open movie file '%s' on CD-ROM\n", buffer);
|
2019-08-31 07:47:15 +00:00
|
|
|
fp = fopen(fileName, "rb");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2019-11-24 09:03:19 +00:00
|
|
|
Printf("Can't open movie file on hard drive\n");
|
2019-08-31 07:47:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 12:59:36 +00:00
|
|
|
#else
|
|
|
|
auto fp = kopenFileReader(fileName, 0);
|
|
|
|
if (!fp.isOpen())
|
|
|
|
{
|
|
|
|
Printf("Unable to open %s\n", fileName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2019-08-31 07:47:15 +00:00
|
|
|
|
2019-11-24 12:59:36 +00:00
|
|
|
tileLoad(kMovieTile);
|
|
|
|
CurFrame = TileFiles.tileMakeWritable(kMovieTile);
|
|
|
|
|
|
|
|
|
|
|
|
fp.Read(lh, sizeof(lh));
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
// sound stuff
|
2019-12-05 21:18:34 +00:00
|
|
|
mutex_init(&mutex);
|
|
|
|
bankptr = 0;
|
|
|
|
banktail = 0;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
// clear keys
|
2019-11-24 12:59:36 +00:00
|
|
|
inputState.keyFlushChars();
|
|
|
|
inputState.ClearAllKeyStatus();
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
if (bDoFade) {
|
|
|
|
StartFadeIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
int angle = 1536;
|
|
|
|
int z = 0;
|
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
videoSetPalette(0, ANIMPAL, 2 + 8);
|
2019-10-12 21:09:55 +00:00
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
// Read a frame in first
|
2019-08-31 07:47:15 +00:00
|
|
|
if (ReadFrame(fp))
|
|
|
|
{
|
2019-12-05 21:18:34 +00:00
|
|
|
// start audio playback
|
|
|
|
hFx = FX_StartDemandFeedPlayback(ServeSample, kSampleRate, 0, gMusicVolume, gMusicVolume, gMusicVolume, FX_MUSIC_PRIORITY, fix16_one, -1);
|
|
|
|
|
2019-11-24 12:59:36 +00:00
|
|
|
while (!inputState.keyBufferWaiting())
|
2019-08-31 07:47:15 +00:00
|
|
|
{
|
2019-12-05 21:18:34 +00:00
|
|
|
HandleAsync();
|
|
|
|
|
|
|
|
// audio is king for sync - if the backend doesn't need any more samples yet,
|
|
|
|
// don't process any more movie file data.
|
|
|
|
if (!bServedSample) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bServedSample = false;
|
2019-08-31 07:47:15 +00:00
|
|
|
|
|
|
|
if (z < 65536) { // Zoom - normal zoom is 65536.
|
|
|
|
z += 2048;
|
|
|
|
}
|
|
|
|
if (angle != 0) {
|
|
|
|
angle += 16;
|
|
|
|
if (angle == 2048) {
|
|
|
|
angle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 11:49:12 +00:00
|
|
|
videoClearViewableArea(blackcol);
|
2019-08-31 07:47:15 +00:00
|
|
|
rotatesprite(160 << 16, 100 << 16, z, angle, kMovieTile, 0, 1, 2, 0, 0, xdim - 1, ydim - 1);
|
|
|
|
|
|
|
|
if (bDoFade) {
|
|
|
|
bDoFade = DoFadeIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
videoNextPage();
|
|
|
|
|
|
|
|
if (ReadFrame(fp) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 21:18:34 +00:00
|
|
|
if (hFx > 0) {
|
|
|
|
FX_StopSound(hFx);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2019-11-22 23:11:37 +00:00
|
|
|
END_PS_NS
|