Convert KidVid file I/O to C++ ifstreams.

This commit is contained in:
Stephen Anthony 2022-09-02 10:14:14 -02:30
parent bbb87af60f
commit 162921b9f3
2 changed files with 25 additions and 31 deletions

View File

@ -15,8 +15,6 @@
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================ //============================================================================
#include <cstdlib>
#include "Event.hxx" #include "Event.hxx"
#include "KidVid.hxx" #include "KidVid.hxx"
@ -36,13 +34,6 @@ KidVid::KidVid(Jack jack, const Event& event, const System& system,
myEnabled = false; myEnabled = false;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
KidVid::~KidVid()
{
closeSampleFiles();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void KidVid::write(DigitalPin pin, bool value) void KidVid::write(DigitalPin pin, bool value)
{ {
@ -139,9 +130,9 @@ void KidVid::update()
void KidVid::openSampleFiles() void KidVid::openSampleFiles()
{ {
#ifdef KID_TAPE #ifdef KID_TAPE
static constexpr char* const fileNames[6] = { static constexpr const char* fileNames[6] = {
"kvs3.wav", "kvs1.wav", "kvs2.wav", "KVS3.WAV", "KVS1.WAV", "KVS2.WAV",
"kvb3.wav", "kvb1.wav", "kvb2.wav" "KVB3.WAV", "KVB1.WAV", "KVB2.WAV"
}; };
static constexpr uInt32 startSong[6] = { static constexpr uInt32 startSong[6] = {
44 + 38, 44 + 38,
@ -157,17 +148,17 @@ void KidVid::openSampleFiles()
int i = myGame == Smurfs ? myTape - 1 : myTape + 2; int i = myGame == Smurfs ? myTape - 1 : myTape + 2;
if(myTape == 4) i = 3; if(myTape == 4) i = 3;
mySampleFile = fopen((myBaseDir + fileNames[i]).c_str(), "rb"); mySampleFile.open(myBaseDir + fileNames[i], std::ios::binary);
if(mySampleFile != nullptr) if(mySampleFile.is_open())
{ {
cerr << "opened file: " << fileNames[i] << endl; cerr << "opened file: " << fileNames[i] << endl;
mySharedSampleFile = fopen((myBaseDir + "kvshared.wav").c_str(), "rb"); mySharedSampleFile.open(myBaseDir + "KVSHARED.WAV", std::ios::binary);
if(mySharedSampleFile == nullptr) if(!mySharedSampleFile.is_open())
fclose(mySampleFile); mySampleFile.close();
else else
{ {
cerr << "opened file: " << "kvshared.wav" << endl; cerr << "opened file: " << "kvshared.wav" << endl;
fseek(mySampleFile, 45, SEEK_SET); mySampleFile.seekg(45);
myFilesOpened = true; myFilesOpened = true;
} }
} }
@ -182,12 +173,12 @@ cerr << "opened file: " << "kvshared.wav" << endl;
void KidVid::closeSampleFiles() void KidVid::closeSampleFiles()
{ {
#ifdef KID_TAPE #ifdef KID_TAPE
if(myFilesOpened) if(mySampleFile.is_open())
{ mySampleFile.close();
fclose(mySampleFile); if(mySharedSampleFile.is_open())
fclose(mySharedSampleFile); mySharedSampleFile.close();
myFilesOpened = false;
} myFilesOpened = false;
#endif #endif
} }
@ -204,7 +195,10 @@ void KidVid::setNextSong()
mySharedData = (temp < 10); mySharedData = (temp < 10);
mySongCounter = ourSongStart[temp+1] - ourSongStart[temp]; mySongCounter = ourSongStart[temp+1] - ourSongStart[temp];
fseek(mySharedData ? mySharedSampleFile : mySampleFile, ourSongStart[temp], SEEK_SET); if(mySharedData)
mySharedSampleFile.seekg(ourSongStart[temp]);
else
mySampleFile.seekg(ourSongStart[temp]);
++myFilePointer; ++myFilePointer;
myTapeBusy = true; myTapeBusy = true;
@ -238,7 +232,9 @@ void KidVid::getNextSampleByte()
myTapeBusy = (mySongCounter > 262 * 48) || !myBeep; myTapeBusy = (mySongCounter > 262 * 48) || !myBeep;
#ifdef KID_TAPE #ifdef KID_TAPE
mySampleByte = myFilesOpened ? getc(mySharedData ? mySharedSampleFile : mySampleFile) : 0x80; mySampleByte = myFilesOpened
? (mySharedData ? mySharedSampleFile.get() : mySampleFile.get())
: 0x80;
#endif #endif
if(!myBeep && (mySongCounter == 0)) if(!myBeep && (mySongCounter == 0))

View File

@ -20,8 +20,6 @@
//#define KID_TAPE //#define KID_TAPE
#include <cstdio>
class Event; class Event;
#include "bspf.hxx" #include "bspf.hxx"
@ -51,7 +49,7 @@ class KidVid : public Controller
*/ */
KidVid(Jack jack, const Event& event, const System& system, KidVid(Jack jack, const Event& event, const System& system,
const string& baseDir, const string& romMd5); const string& baseDir, const string& romMd5);
~KidVid() override; ~KidVid() override = default;
public: public:
/** /**
@ -103,8 +101,8 @@ class KidVid : public Controller
string myBaseDir; string myBaseDir;
#ifdef KID_TAPE #ifdef KID_TAPE
// The file handles for the WAV files // The file streams for the WAV files
FILE *mySampleFile{nullptr}, *mySharedSampleFile{nullptr}; std::ifstream mySampleFile, mySharedSampleFile;
// Indicates if sample files have been successfully opened // Indicates if sample files have been successfully opened
bool myFilesOpened{false}; bool myFilesOpened{false};