From 8afb29b6805bf25708ced898d382aa678de14e3a Mon Sep 17 00:00:00 2001 From: Bernardo Magri Date: Mon, 10 Mar 2025 18:55:45 +0000 Subject: [PATCH] Changing the clocklabel to work with the timerThread signal * src/minefield.hpp: --- src/minefield.cpp | 6 ++++-- src/minefield.hpp | 2 +- src/window.cpp | 25 ++++++++++++++----------- src/window.hpp | 2 +- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/minefield.cpp b/src/minefield.cpp index f854b76..aa1275c 100644 --- a/src/minefield.cpp +++ b/src/minefield.cpp @@ -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(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(); } } diff --git a/src/minefield.hpp b/src/minefield.hpp index 6aa7526..f621957 100644 --- a/src/minefield.hpp +++ b/src/minefield.hpp @@ -53,5 +53,5 @@ public: sigc::signal remainingFlagsSignal; sigc::signal gameWonSignal; sigc::signal gameOverSignal; - sigc::signal timerSignal; + sigc::signal timerSignal; }; diff --git a/src/window.cpp b/src/window.cpp index d6b595c..a4941c3 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -163,20 +163,22 @@ void MainWindow::gameOver() { //std::cout << "Signal gameOver emmited\n"; } -bool MainWindow::updateClockLabel() +void MainWindow::updateClockLabel(size_t time) { - ++m_elapsedTime; + //++m_elapsedTime; - int deciseconds = m_elapsedTime % 10; - int seconds = (m_elapsedTime / 10) % 60; - int minutes = (m_elapsedTime /600) % 60; + //int deciseconds = m_elapsedTime % 10; + //int seconds = (m_elapsedTime / 10) % 60; + //int minutes = (m_elapsedTime /600) % 60; - Glib::ustring msg = Glib::ustring::compose("Elapsed time: %1:%2.%3", \ - Glib::ustring::format(std::setfill(L'0'), std::setw(2), minutes), \ - Glib::ustring::format(std::setfill(L'0'), std::setw(2), seconds), \ - Glib::ustring::format(std::setfill(L'0'), std::setw(1), deciseconds)); + Glib::ustring msg = Glib::ustring::compose("Elapsed time: %1", time); + + // Glib::ustring msg = Glib::ustring::compose("Elapsed time: %1:%2.%3", \ + // Glib::ustring::format(std::setfill(L'0'), std::setw(2), minutes), \ + // Glib::ustring::format(std::setfill(L'0'), std::setw(2), seconds), \ + // Glib::ustring::format(std::setfill(L'0'), std::setw(1), deciseconds)); clockLabel.set_label(msg); - return true; + //return true; } MainWindow::MainWindow() @@ -273,9 +275,10 @@ MainWindow::MainWindow() //optionButton.set_icon_name("open-menu"); + field.timerSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::updateClockLabel))); //if (clockSignalConn.connected()) clockSignalConn.disconnect(); //elapsedTime = 0; - clockSignalConn = Glib::signal_timeout().connect(sigc::mem_fun(*this, &MainWindow::updateClockLabel), 100); + //clockSignalConn = Glib::signal_timeout().connect(sigc::mem_fun(*this, &MainWindow::updateClockLabel), 100); //} //create the minefield //field = new MineField(COLS, MINES); diff --git a/src/window.hpp b/src/window.hpp index f558ce5..eb7a5fb 100644 --- a/src/window.hpp +++ b/src/window.hpp @@ -32,7 +32,7 @@ class MainWindow : public Gtk::Window void updateCell(int x, int y); void openBombs(); void updateFlagsLabel(int flags); - bool updateClockLabel(); + void updateClockLabel(size_t time); void gameWon(); void gameOver(); sigc::connection clockSignalConn;