Changing the clocklabel to work with the timerThread signal

* src/minefield.hpp:
This commit is contained in:
Bernardo Magri
2025-03-10 18:55:45 +00:00
parent efc523c0c9
commit 8afb29b680
4 changed files with 20 additions and 15 deletions
+4 -2
View File
@@ -17,7 +17,7 @@ void MineField::timerTick() {
auto start = std::chrono::system_clock::now();
while((m_exploded == false) && (m_gameWon == false)) {
std::this_thread::sleep_for(std::chrono::milliseconds(200));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto now = std::chrono::system_clock::now();
const auto duration = now - start;
std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
@@ -47,12 +47,13 @@ void MineField::initBombs(int x, int y) {
//init the timer to zero and start the timer thread
m_time = 0;
timerThread = std::thread(&MineField::timerTick, this);
timerThread.detach(); //not sure if this is okay (better to call join() when I set the condition to stop the thread)
//timerThread.detach(); //not sure if this is okay (better to call join() when I set the condition to stop the thread)
}
bool MineField::openCell(int x, int y) {
if(isBomb(x, y)) {
m_exploded = true;
timerThread.join();
gameOverSignal.emit();
return false;
}
@@ -120,6 +121,7 @@ void MineField::setOpenCell(int x, int y) {
openCellSignal.emit(x, y);
if((++m_openCells == (m_cols * m_rows - m_totalMines)) && (m_exploded == false)) {
m_gameWon = true;
timerThread.join();
gameWonSignal.emit();
}
}