Skip to content

Commit

Permalink
avoid play soud for every cell cleaned on recursive clean
Browse files Browse the repository at this point in the history
This affects performance a lot
  • Loading branch information
Bollos00 committed Jan 11, 2024
1 parent 1260179 commit d1004ef
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
12 changes: 8 additions & 4 deletions src/libreminesgameengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void LibreMinesGameEngine::vResetPrincipalMatrix()
principalMatrix.clear();
}

bool LibreMinesGameEngine::bCleanCell(const uchar _X, const uchar _Y)
bool LibreMinesGameEngine::bCleanCell(const uchar _X, const uchar _Y, const bool recursive)
{
if(principalMatrix[_X][_Y].flagState == FlagState::NoFlag &&
principalMatrix[_X][_Y].value == CellValue::MINE)
Expand All @@ -274,7 +274,7 @@ bool LibreMinesGameEngine::bCleanCell(const uchar _X, const uchar _Y)
{
// Unlock the cell
principalMatrix[_X][_Y].isHidden = false;
Q_EMIT SIGNAL_showCell(_X, _Y);
Q_EMIT SIGNAL_showCell(_X, _Y, recursive);

// If the state of the cell is CellValue::ZERO, unlock all neighbor cells
if(principalMatrix[_X][_Y].value == CellValue::ZERO)
Expand Down Expand Up @@ -555,7 +555,7 @@ void LibreMinesGameEngine::SLOT_cleanCell(const uchar _X, const uchar _Y)
SLOT_startTimer();
bFirst = false;
}
bCleanCell(_X, _Y);
bCleanCell(_X, _Y, false);
}

void LibreMinesGameEngine::SLOT_changeFlagState(const uchar _X, const uchar _Y)
Expand Down Expand Up @@ -614,6 +614,8 @@ void LibreMinesGameEngine::SLOT_startTimer()

void LibreMinesGameEngine::SLOT_cleanNeighborCells(const uchar _X, const uchar _Y)
{

bool recursive = false;
// Clean all hided and unflaged neighbor flags
for(short i=_X-1; i<=_X+1; i++)
{
Expand All @@ -629,8 +631,10 @@ void LibreMinesGameEngine::SLOT_cleanNeighborCells(const uchar _X, const uchar _

if(cell.isHidden && cell.flagState == FlagState::NoFlag)
{
if(!bCleanCell(i, j))
if(!bCleanCell(i, j, recursive))
return;

recursive = true;
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/libreminesgameengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ class LibreMinesGameEngine: public QObject

private:
void vResetPrincipalMatrix();
bool bCleanCell(const uchar _X, const uchar _Y);

/**
* @brief Clear the cell in the #_X, #_Y position.
* Create the condition of game lost if the cell is a MINE.
* Clear all neighbor cells if the cell is ZERO
*
* @param recursive - Indicates this cell is being cleared along with another cell.
*/
bool bCleanCell(const uchar _X, const uchar _Y, const bool recursive=true);
void vGameLost(const uchar _X, const uchar _Y);
void vGameWon();

Expand Down Expand Up @@ -92,7 +100,7 @@ class LibreMinesGameEngine: public QObject
bool bGameActive;

Q_SIGNALS:
void SIGNAL_showCell(const uchar _X, const uchar _Y);
void SIGNAL_showCell(const uchar _X, const uchar _Y, const bool recursive);
void SIGNAL_endGameScore(LibreMinesScore score,
int iCorrectFlags,
int iWrongFlags,
Expand Down
8 changes: 5 additions & 3 deletions src/libreminesgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ void LibreMinesGui::SLOT_onCellLabelClicked(const QMouseEvent *const e)

}

void LibreMinesGui::SLOT_showCell(const uchar _X, const uchar _Y)
void LibreMinesGui::SLOT_showCell(const uchar _X, const uchar _Y, const bool recursive)
{
principalMatrix[_X][_Y].button->hide();

Expand All @@ -1180,7 +1180,10 @@ void LibreMinesGui::SLOT_showCell(const uchar _X, const uchar _Y)
}


Q_EMIT(sound->SIGNAL_releaseCell());
if(!recursive)
{
Q_EMIT(sound->SIGNAL_releaseCell());
}
}

void LibreMinesGui::SLOT_endGameScore(LibreMinesScore score,
Expand Down Expand Up @@ -1963,5 +1966,4 @@ void LibreMinesGui::vUpdatePreferences()
}

sound->setVolume(preferences->optionSoundVolume());
sound->setMuted(preferences->optionSoundVolume() == 0);
}
2 changes: 1 addition & 1 deletion src/libreminesgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private Q_SLOTS:
void SLOT_onCellLabelReleased(const QMouseEvent*const e);
void SLOT_onCellLabelClicked(const QMouseEvent*const e);

void SLOT_showCell(const uchar _X, const uchar _Y);
void SLOT_showCell(const uchar _X, const uchar _Y, const bool recursive);
void SLOT_endGameScore(LibreMinesScore score,
int iCorrectFlags,
int iWrongFlags,
Expand Down
13 changes: 2 additions & 11 deletions src/soundeffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

SoundEffects::SoundEffects(QObject* parent) :
QObject( parent ),
soundEffects( {&soundGameBegin, &soundGameWon, &soundGameLost, &soundClockTick,
soundEffects( {&soundGameBegin, &soundGameWon, &soundGameLost,
&soundKeyboardControllMove, &soundReleaseCell, &soundFlagCell} )

{
soundGameBegin.setSource(QUrl("qrc:/sound_effects/clock_tick.wav"));
soundGameWon.setSource(QUrl("qrc:/sound_effects/game_won.wav"));
soundGameLost.setSource(QUrl("qrc:/sound_effects/game_lost.wav"));
soundClockTick.setSource(QUrl("qrc:/sound_effects/clock_tick.wav"));

soundKeyboardControllMove.setSource(QUrl("qrc:/sound_effects/move.wav"));
soundReleaseCell.setSource(QUrl("qrc:/sound_effects/release_cell.wav"));
Expand All @@ -23,8 +22,6 @@ SoundEffects::SoundEffects(QObject* parent) :
connect(this, &SoundEffects::SIGNAL_gameLost,
&soundGameLost, &QSoundEffect::play);

connect(this, &SoundEffects::SIGNAL_clockTick,
&soundClockTick, &QSoundEffect::play);

connect(this, &SoundEffects::SIGNAL_keyboardControllerMove,
&soundKeyboardControllMove, &QSoundEffect::play);
Expand All @@ -41,13 +38,7 @@ void SoundEffects::setVolume(const int vol)
for(QSoundEffect* sound : soundEffects)
{
sound->setVolume(vol/100.f);
sound->setMuted(vol == 0);
}
}

void SoundEffects::setMuted(const bool mute)
{
for(QSoundEffect* sound : soundEffects)
{
sound->setMuted(mute);
}
}
3 changes: 0 additions & 3 deletions src/soundeffects.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ class SoundEffects : public QObject
SoundEffects(QObject* parent = nullptr);

void setVolume(const int vol);
void setMuted(const bool mute);

Q_SIGNALS:

void SIGNAL_gameBegin();
void SIGNAL_gameWon();
void SIGNAL_gameLost();
void SIGNAL_clockTick();

void SIGNAL_keyboardControllerMove();
void SIGNAL_releaseCell();
Expand All @@ -29,7 +27,6 @@ class SoundEffects : public QObject
QSoundEffect soundGameBegin;
QSoundEffect soundGameWon;
QSoundEffect soundGameLost;
QSoundEffect soundClockTick;

QSoundEffect soundKeyboardControllMove;
QSoundEffect soundReleaseCell;
Expand Down

0 comments on commit d1004ef

Please sign in to comment.