Skip to content

Commit

Permalink
fixed opengl related crash on windows, fixed temp file error
Browse files Browse the repository at this point in the history
  • Loading branch information
nebomuk committed Jan 7, 2018
1 parent 34affc7 commit 5ae418c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
int main(int argc, char ** argv)
{
QApplication app( argc, argv );

// crashes when trying to use ANGLE and opengl driver might not be available
#ifdef Q_OS_WIN
QCoreApplication::setAttribute(Qt::AA_UseSoftwareOpenGL);
#endif

app.setQuitOnLastWindowClosed(false);

app.setApplicationName("tasteroids");
Expand Down
60 changes: 34 additions & 26 deletions src/soundengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,35 @@
SoundEngine::SoundEngine(QObject * parent) : QObject(parent)
{
loopDuplicateCount_ = 4;
copyFilesToTempDir_ = false;
}

void SoundEngine::loadSounds(const QStringList& fileNames)
void SoundEngine::loadSounds(const QStringList& filePaths)
{
for(QString sourceFileName : fileNames)
for(QString sourceFilePath : filePaths)
{
QString fileName = QFileInfo(sourceFileName).fileName(); // returns file name without path
QString fileName = QFileInfo(sourceFilePath).fileName();

// copy the files to the temp directory
QFile::copy(sourceFileName,QDir::tempPath() + "/tmp_" + fileName);
QString tempFileName(QDir::tempPath() + "/tmp_" + fileName);

// save the absolute file names in order to delete them in the dtor
tempFileNames_ << tempFileName;
if(copyFilesToTempDir_)
{
QFile::copy(sourceFilePath,QDir::tempPath() + "/tmp_" + fileName);
sourceFilePath = QDir::tempPath() + "/tmp_" + fileName;
// save the absolute file names in order to delete them in the dtor
tempFileNames_ << sourceFilePath;
}

if(fileName.startsWith("loop", Qt::CaseInsensitive))
{
for(int i = 0; i< loopDuplicateCount_; ++i)
{
ISound * snd = new Sound();
snd->setFile(qPrintable(QDir::toNativeSeparators(tempFileName))); // required for SDL, on windows C:/path becomes C:\path
ISound * snd = new Sound();
snd->setFile(qPrintable(QDir::toNativeSeparators(sourceFilePath))); // required for SDL, on windows C:/path becomes C:\path
soundPlayers_.insert(fileName+ '_' + QString::number(i),snd); // loop_sound.wav_0, loop_sound.wav_1 etc
}
}
ISound * snd = new Sound();
snd->setFile(qPrintable(QDir::toNativeSeparators(tempFileName)));

ISound * snd = new Sound();
snd->setFile(qPrintable(QDir::toNativeSeparators(sourceFilePath)));
soundPlayers_.insert(fileName,snd);
}

Expand Down Expand Up @@ -75,23 +78,28 @@ void SoundEngine::stopAll()
}
}

void SoundEngine::removeTemporaryFiles()
{
bool tempRemoveFailed = false;
for(QString file : tempFileNames_)
{
QDir dir;
if(!dir.remove(file))
tempRemoveFailed = true;
}
if(tempRemoveFailed)
{
QMessageBox::warning(NULL,tr("Could not delete temporary files"),
tr("One ore more temporary sound files could not be removed."
"You can clean up the temp_* files yourself in ") + QDir::tempPath());
}
}

SoundEngine::~SoundEngine()
{
// if the application crashes here, check for multiple deletes (and multiple inserts of the same new'ed value)
qDeleteAll(soundPlayers_);

// destroy the temporary files that have been created before
bool tempRemoveFailed = false;
for(QString file : tempFileNames_)
{
QDir dir;
if(!dir.remove(file))
tempRemoveFailed = true;
}
if(tempRemoveFailed)
{
QMessageBox::warning(NULL,tr("Could not delete temporary files"),
tr("One ore more temporary sound files could not be removed."
"You can clean up the temp_* files yourself in ") + QDir::tempPath());
}
removeTemporaryFiles();
}
4 changes: 3 additions & 1 deletion src/soundengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ public slots:
// copys the files in soundFiles into the system's temp directory
// and makes them available to the SoundEngine with their names (without path)
// this is necessary for resource files, because the SDL Sound backend doesn't support Qt-resources
void loadSounds(const QStringList& soundFiles);
void loadSounds(const QStringList& filePaths);

private:
QHash<QString,ISound*> soundPlayers_;
QStringList tempFileNames_;
int loopDuplicateCount_;
bool copyFilesToTempDir_;
void removeTemporaryFiles();
};

#endif // SOUNDENGINE_H

0 comments on commit 5ae418c

Please sign in to comment.