Creating time keeping for the game using c++ threads instead of relying on GTK

This commit is contained in:
Bernardo Magri
2025-03-10 10:49:24 +00:00
parent 33733b9ec3
commit a2f3c89599
2 changed files with 36 additions and 14 deletions

View File

@@ -12,6 +12,20 @@ MineField::MineField(int cols, int rows, int mines): m_rows(rows),
}
}
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));
auto now = std::chrono::system_clock::now();
const auto duration = now - start;
std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
m_time += duration.count();
timerSignal.emit(m_time);
start = std::chrono::system_clock::now();
}
}
void MineField::initBombs(int x, int y) {
@@ -28,6 +42,11 @@ void MineField::initBombs(int x, int y) {
m_cells.at(position)->isBomb = true;
--remainingMines;
}
//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
}
bool MineField::openCell(int x, int y) {