Commit b3ed6283 authored by Gerhard Stein's avatar Gerhard Stein

Cleanups

parent ab2e6292
......@@ -27,17 +27,12 @@
#ifndef CRINGBUFFER_H_
#define CRINGBUFFER_H_
// TODO: It is more of a container. We should rename that to Ring or RingComposite or similar.
template <typename T>
class RingBuffer {
public:
RingBuffer():
mp_start(nullptr),
mp_cur(nullptr),
mp_end(nullptr),
m_size(0)
RingBuffer()
{}
~RingBuffer()
......@@ -64,17 +59,21 @@ public:
/**
* Allocates memory for the Ring buffer
*/
bool reserve(unsigned int size)
bool resize(const unsigned int size)
{
if(!empty())
{
// TODO: throw exception here! This must never happen!
clear();
return false;
}
m_size = size;
if(m_size == 0)
{
return false;
}
mp_cur = new T[m_size];
mp_start = mp_cur;
mp_end = mp_start + m_size;
......@@ -88,7 +87,11 @@ public:
*/
void clear()
{
delete [] mp_start;
if(mp_start)
{
delete [] mp_start;
}
mp_start = nullptr;
m_size = 0;
}
......@@ -193,8 +196,10 @@ public:
private:
T *mp_start, *mp_cur, *mp_end;
unsigned int m_size;
T *mp_start = nullptr;
T *mp_cur = nullptr;
T *mp_end = nullptr;
unsigned int m_size = 0;
};
#endif /* CRINGBUFFER_H_ */
......@@ -31,6 +31,7 @@ void CHUD::createHUDBlit()
{
mHUDBlit.createRGBSurface(mRenderRect);
mHUDBlit.makeBlitCompatible();
mHUDBlit.fillRGB(0,0,0);
}
void CHUD::setup(const int id)
......
......@@ -340,7 +340,6 @@ bool CMapPlayGalaxy::operator<<(CSaveGameController &savedGame)
// Load the Background Music
gMusicPlayer.stop();
if( !galaxy::loadLevelMusic(level) )
{
gLogging.textOut("Warning: The music cannot be played. Check that all the files have been correctly copied!");
......
......@@ -494,7 +494,9 @@ bool CExeFile::unpackAudioInterval( RingBuffer<IMFChunkType> &imfData,
word data_size;
if (*imfDataPtr == 0) // Is the IMF file of Type-0?
{
data_size = emb_file_data_size;
}
else
{
data_size = *((word*) (void*) imfDataPtr);
......@@ -503,10 +505,12 @@ bool CExeFile::unpackAudioInterval( RingBuffer<IMFChunkType> &imfData,
if(!imfData.empty())
{
imfData.clear();
}
const word imf_chunks = data_size/sizeof(IMFChunkType);
imfData.reserve(imf_chunks);
imfData.resize(imf_chunks);
memcpy(imfData.getStartPtr(), imfDataPtr, data_size);
return true;
}
......@@ -650,7 +654,9 @@ bool CExeFile::readCompressedAudiointoMemory(RingBuffer<IMFChunkType> &imfData,
uint32_t audiofilecompsize;
std::string init_audiofilename = "AUDIO.CK" + itoa(episode);
std::string audiofilename = getResourceFilename( init_audiofilename, gKeenFiles.gameDir, true, false);
const std::string audiofilename = getResourceFilename( init_audiofilename,
gKeenFiles.gameDir,
true, false);
if( audiofilename == "" )
return false;
......@@ -665,13 +671,16 @@ bool CExeFile::readCompressedAudiointoMemory(RingBuffer<IMFChunkType> &imfData,
// create memory so we can store the Audio.ck there and use it later for extraction
AudioCompFileData.resize(audiofilecompsize);
AudioFile.read((char*) &(AudioCompFileData.front()), audiofilecompsize);
AudioFile.read((char*) &(AudioCompFileData.front()),
audiofilecompsize);
AudioFile.close();
std::string audiohedfile = gKeenFiles.audioHedFilename;
if(!audiohedfile.empty())
{
audiohedfile = getResourceFilename( audiohedfile, gKeenFiles.gameDir, false, false);
}
// The musiched is just one part of the AUDIOHED. It's not a separate file.
// Open the AUDIOHED so we know where to mp_IMF_Data decompress
......@@ -685,7 +694,8 @@ bool CExeFile::readCompressedAudiointoMemory(RingBuffer<IMFChunkType> &imfData,
bool CExeFile::loadMusicTrack(RingBuffer<IMFChunkType> &imfData, const int track) const
bool CExeFile::loadMusicTrack(RingBuffer<IMFChunkType> &imfData,
const int track) const
{
// Now get the proper music slot reading the assignment table.
std::vector<uint8_t> AudioCompFileData;
......
......@@ -58,7 +58,7 @@ bool CIMFPlayer::loadMusicFromFile(const std::string& filename)
m_IMF_Data.clear();
const word imf_chunks = (data_size/sizeof(IMFChunkType));
m_IMF_Data.reserve(imf_chunks);
m_IMF_Data.resize(imf_chunks);
if( imf_chunks != fread( m_IMF_Data.getStartPtr(), sizeof(IMFChunkType), imf_chunks, fp ) )
{
......@@ -77,8 +77,10 @@ bool CIMFPlayer::loadMusicFromFile(const std::string& filename)
bool CIMFPlayer::loadMusicTrack(const int track)
{
if( m_IMF_Data.empty() )
if( !m_IMF_Data.empty() )
{
m_IMF_Data.clear();
}
if(!gKeenFiles.exeFile.loadMusicTrack(m_IMF_Data, track))
{
......@@ -230,9 +232,14 @@ int locImfMusPos = 0;
bool loadIMFFile(const std::string &fname)
{
if(locIMFPlayer.loadMusicFromFile(fname))
if(locIMFPlayer.playing())
{
locIMFPlayer.close(false);
Mix_HaltMusic();
}
if(locIMFPlayer.loadMusicFromFile(fname))
{
locIMFPlayer.open(false);
Mix_HookMusic(imfMusicPlayer, &locImfMusPos);
Mix_HookMusicFinished(musicFinished);
......@@ -245,9 +252,14 @@ bool loadIMFFile(const std::string &fname)
bool loadIMFTrack(const int track)
{
if(locIMFPlayer.loadMusicTrack(track))
if(locIMFPlayer.playing())
{
locIMFPlayer.close(false);
Mix_HaltMusic();
}
if(locIMFPlayer.loadMusicTrack(track))
{
locIMFPlayer.open(false);
Mix_HookMusic(imfMusicPlayer, &locImfMusPos);
Mix_HookMusicFinished(musicFinished);
......@@ -286,6 +298,7 @@ void musicFinished()
void unhookAll()
{
locImfMusPos = 0;
Mix_HookMusic(nullptr, &locImfMusPos);
Mix_HookMusicFinished(nullptr);
}
......
......@@ -23,17 +23,17 @@ bool CMusic::loadTrack(const int track)
{
//return false;
// TODO: This still crashes IMF Music has issues
gSound.pauseAudio();
//gSound.pauseAudio();
gLogging.textOut("Load track number " + itoa(track) + "");
if(loadIMFTrack(track))
{
gSound.resumeAudio();
//gSound.resumeAudio();
return true;
}
gSound.resumeAudio();
//gSound.resumeAudio();
return false;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment