2014-07-01 16:44:01 +00:00
|
|
|
#include "stdafx.h"
|
2015-07-02 03:17:14 +00:00
|
|
|
#include "MessageManager.h"
|
2014-07-01 16:44:01 +00:00
|
|
|
#include "Movie.h"
|
|
|
|
#include "Console.h"
|
2015-07-02 03:17:14 +00:00
|
|
|
#include "../Utilities/FolderUtilities.h"
|
2014-07-01 16:44:01 +00:00
|
|
|
|
|
|
|
Movie* Movie::Instance = new Movie();
|
|
|
|
|
|
|
|
void Movie::PushState(uint8_t port)
|
|
|
|
{
|
|
|
|
if(_counter[port] > 0) {
|
|
|
|
uint16_t data = _lastState[port] << 8 | _counter[port];
|
|
|
|
_data.PortData[port].push_back(data);
|
|
|
|
|
|
|
|
_lastState[port] = 0;
|
|
|
|
_counter[port] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Movie::RecordState(uint8_t port, uint8_t state)
|
|
|
|
{
|
|
|
|
if(_recording) {
|
|
|
|
if(_lastState[port] != state || _counter[port] == 0) {
|
|
|
|
if(_counter[port] != 0) {
|
|
|
|
PushState(port);
|
|
|
|
}
|
|
|
|
_lastState[port] = state;
|
|
|
|
_counter[port] = 1;
|
|
|
|
} else {
|
|
|
|
_counter[port]++;
|
|
|
|
|
|
|
|
if(_counter[port] == 255) {
|
|
|
|
PushState(port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t Movie::GetState(uint8_t port)
|
|
|
|
{
|
|
|
|
uint16_t data = --_data.PortData[port][_readPosition[port]];
|
|
|
|
if((data & 0xFF) == 0) {
|
|
|
|
_readPosition[port]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_readPosition[port] >= _data.DataSize[port]) {
|
|
|
|
//End of movie file
|
2015-07-02 03:17:14 +00:00
|
|
|
MessageManager::DisplayMessage(L"Movie ended.");
|
2014-07-01 16:44:01 +00:00
|
|
|
_playing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (data >> 8);
|
|
|
|
}
|
|
|
|
|
2014-07-01 22:05:54 +00:00
|
|
|
void Movie::Reset()
|
|
|
|
{
|
|
|
|
_startState.clear();
|
|
|
|
_startState.seekg(0, ios::beg);
|
|
|
|
_startState.seekp(0, ios::beg);
|
|
|
|
|
|
|
|
memset(_readPosition, 0, 4 * sizeof(uint32_t));
|
|
|
|
memset(_counter, 0, 4);
|
|
|
|
memset(_lastState, 0, 4);
|
|
|
|
_data = MovieData();
|
|
|
|
|
|
|
|
_recording = false;
|
|
|
|
_playing = false;
|
|
|
|
}
|
|
|
|
|
2014-07-01 16:44:01 +00:00
|
|
|
void Movie::StartRecording(wstring filename, bool reset)
|
|
|
|
{
|
|
|
|
_file = ofstream(filename, ios::out | ios::binary);
|
|
|
|
|
|
|
|
if(_file) {
|
2014-07-01 22:05:54 +00:00
|
|
|
Console::Pause();
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
|
2014-07-01 16:44:01 +00:00
|
|
|
if(reset) {
|
|
|
|
Console::Reset();
|
|
|
|
} else {
|
|
|
|
Console::SaveState(_startState);
|
|
|
|
}
|
|
|
|
|
|
|
|
_recording = true;
|
2014-07-01 22:05:54 +00:00
|
|
|
|
|
|
|
Console::Resume();
|
2014-07-06 23:54:47 +00:00
|
|
|
|
2015-07-02 03:17:14 +00:00
|
|
|
MessageManager::DisplayMessage(L"Recording...");
|
2014-07-01 16:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Movie::StopAll()
|
|
|
|
{
|
|
|
|
if(_recording) {
|
2014-07-01 22:05:54 +00:00
|
|
|
_recording = false;
|
2014-07-01 16:44:01 +00:00
|
|
|
for(int i = 0; i < 4; i++) {
|
|
|
|
PushState(i);
|
|
|
|
}
|
|
|
|
_data.Save(_file, _startState);
|
|
|
|
}
|
|
|
|
_playing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Movie::PlayMovie(wstring filename)
|
|
|
|
{
|
2014-07-01 22:05:54 +00:00
|
|
|
StopAll();
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
if(_data.Load(filename, _startState)) {
|
|
|
|
Console::Pause();
|
|
|
|
if(_startState.tellp() > 0) {
|
|
|
|
//Restore state if one was present in the movie
|
|
|
|
Console::LoadState(_startState);
|
|
|
|
} else {
|
|
|
|
Console::Reset();
|
2014-07-01 16:44:01 +00:00
|
|
|
}
|
2014-07-01 22:05:54 +00:00
|
|
|
_playing = true;
|
|
|
|
Console::Resume();
|
2015-07-02 03:17:14 +00:00
|
|
|
MessageManager::DisplayMessage(L"Playing movie: " + FolderUtilities::GetFilename(filename, true));
|
2014-07-01 16:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Movie::Record(wstring filename, bool reset)
|
|
|
|
{
|
|
|
|
Instance->StartRecording(filename, reset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Movie::Play(wstring filename)
|
|
|
|
{
|
|
|
|
Instance->PlayMovie(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Movie::Stop()
|
|
|
|
{
|
|
|
|
Instance->StopAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Movie::Playing()
|
|
|
|
{
|
|
|
|
return Instance->_playing;
|
2014-07-09 22:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Movie::Recording()
|
|
|
|
{
|
|
|
|
return Instance->_recording;
|
2014-07-01 16:44:01 +00:00
|
|
|
}
|