From 751e12f495e4604ada39c1dbcb440e5d07a4b8e7 Mon Sep 17 00:00:00 2001 From: Bernardo Magri Date: Sat, 8 Mar 2025 10:17:26 +0000 Subject: [PATCH] Fixing the clock and adding new signals --- src/minefield.cpp | 1 - src/window.cpp | 67 +++++++++++++++++++++++++---------------------- src/window.hpp | 20 +++++++------- 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/minefield.cpp b/src/minefield.cpp index 3174e51..5d9163b 100644 --- a/src/minefield.cpp +++ b/src/minefield.cpp @@ -31,7 +31,6 @@ void MineField::initBombs(int x, int y) { } bool MineField::openCell(int x, int y) { - if(isBomb(x, y)) { m_exploded = true; gameOverSignal.emit(); diff --git a/src/window.cpp b/src/window.cpp index e70a15b..e3bc001 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,4 +1,6 @@ #include "window.hpp" +#include "gdkmm/texture.h" +#include "sigc++/functors/mem_fun.h" //} @@ -19,7 +21,7 @@ void MainWindow::OnCellRightClick(int n_press, double n_x, double n_y, int index field.toggleFlag(x, y); if(field.isFlagged(x, y)) { auto imgflag = Gtk::make_managed(); - imgflag->set(m_pixbufFlag); + imgflag->set(m_textureFlag); buttons.at(pos)->set_child(*imgflag); buttons.at(pos)->set_active(true); } @@ -35,7 +37,6 @@ void MainWindow::updateFlagsLabel(int flags) { Glib::ustring msg = Glib::ustring::compose("Remaining flags: %1", flags); flagLabel.set_label(msg); } - // void MainWindow::OnNewButtonClick() { // newGame = true; // gameOver = false; @@ -67,14 +68,13 @@ void MainWindow::OnCellClick(int x, int y) { if(field.isFlagged(x, y)) { buttons.at(x + y * field.getRows())->set_active(true); } - else if(field.isBomb(x, y)) { - openBombs(); - } - else { + else { field.openCell(x, y); + if(field.isBomb(x, y)) { + openBombs(); + } } -} - +} void MainWindow::openBombs() { for(int i=0; i < field.getCols() * field.getRows(); i++) { @@ -86,12 +86,12 @@ void MainWindow::openBombs() { if(field.isBomb(x, y)) { if(field.isFlagged(x, y)) { auto imgFlagBomb = std::make_shared(); - imgFlagBomb->set(m_pixbufFlagBomb); + imgFlagBomb->set(m_textureFlagBomb); buttons.at(i)->set_child(*imgFlagBomb); } else { auto imgBomb = std::make_shared(); - imgBomb->set(m_pixbufBomb); + imgBomb->set(m_textureBomb); buttons.at(i)->set_child(*imgBomb); } buttons.at(i)->set_active(true); @@ -158,31 +158,31 @@ void MainWindow::updateCell(int x, int y) { // } // return true; // } +void MainWindow::gameOver() { + clockSignalConn.disconnect(); + std::cout << "Signal gameOver emmited\n"; +} +bool MainWindow::updateClockLabel() +{ + ++m_elapsedTime; -// bool MainWindow::UpdateClockLabel() -// { -// if(gameOver) return false; - -// elapsedTime++; + int deciseconds = m_elapsedTime % 10; + int seconds = (m_elapsedTime / 10) % 60; + int minutes = (m_elapsedTime /600) % 60; -// int deciseconds = elapsedTime % 10; -// int seconds = (elapsedTime / 10) % 60; -// int minutes = (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)); -// clockLabel.set_label(msg); - -// return true; -// } + 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; +} MainWindow::MainWindow() { // ApplyStyles(); // Load the CSS file - elapsedTime = 0; + m_elapsedTime = 0; newGame = true; set_title("MineSweeper"); set_default_size(400, 400); @@ -222,9 +222,9 @@ MainWindow::MainWindow() //TODO check if it's okay to mix std::shared_ptr with Gdk::ptr - m_pixbufBomb = Gdk::Pixbuf::create_from_resource("/minesweeper/bomb-solid"); - m_pixbufFlag = Gdk::Pixbuf::create_from_resource("/minesweeper/flag-solid"); - m_pixbufFlagBomb = Gdk::Pixbuf::create_from_resource("/minesweeper/flag-bomb"); + m_textureBomb = Gdk::Texture::create_from_resource("/minesweeper/bomb-solid"); + m_textureFlag = Gdk::Texture::create_from_resource("/minesweeper/flag-solid"); + m_textureFlagBomb = Gdk::Texture::create_from_resource("/minesweeper/flag-bomb"); // bombPix.set_from_resource("/minesweeper/bomb-solid"); @@ -266,12 +266,17 @@ MainWindow::MainWindow() field.openCellSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::updateCell))); field.remainingFlagsSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::updateFlagsLabel))); + field.gameOverSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::gameOver))); //newGameButton.set_label("New"); //newGameButton.add_css_class("suggested-action"); //newGameButton.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::OnNewButtonClick)); //optionButton.set_icon_name("open-menu"); + //if (clockSignalConn.connected()) clockSignalConn.disconnect(); + //elapsedTime = 0; + 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 072f850..f558ce5 100644 --- a/src/window.hpp +++ b/src/window.hpp @@ -1,13 +1,13 @@ #pragma once #include "minefield.hpp" -#include "gdkmm/pixbuf.h" #include #include #include #include #include -#include +#include +#include #define PROJECT_NAME "minesweeper" @@ -24,18 +24,20 @@ class MainWindow : public Gtk::Window Gtk::Label flagLabel; Gtk::Label clockLabel; MineField field {16, 16, 40}; - int elapsedTime; + int m_elapsedTime; bool newGame; - std::shared_ptr m_pixbufBomb; - std::shared_ptr m_pixbufFlag; - std::shared_ptr m_pixbufFlagBomb; + std::shared_ptr m_textureBomb; + std::shared_ptr m_textureFlag; + std::shared_ptr m_textureFlagBomb; void updateCell(int x, int y); void openBombs(); void updateFlagsLabel(int flags); - -// sigc::connection clockConn; + bool updateClockLabel(); + void gameWon(); + void gameOver(); + sigc::connection clockSignalConn; // void OpenNearCells(int index); -// void Explode(); +// void Explode();xo // bool AllCellsOpened(); public: