Fixing the clock and adding new signals

This commit is contained in:
Bernardo Magri
2025-03-08 10:17:26 +00:00
parent 052690fede
commit 751e12f495
3 changed files with 47 additions and 41 deletions

View File

@@ -31,7 +31,6 @@ void MineField::initBombs(int x, int y) {
} }
bool MineField::openCell(int x, int y) { bool MineField::openCell(int x, int y) {
if(isBomb(x, y)) { if(isBomb(x, y)) {
m_exploded = true; m_exploded = true;
gameOverSignal.emit(); gameOverSignal.emit();

View File

@@ -1,4 +1,6 @@
#include "window.hpp" #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); field.toggleFlag(x, y);
if(field.isFlagged(x, y)) { if(field.isFlagged(x, y)) {
auto imgflag = Gtk::make_managed<Gtk::Image>(); auto imgflag = Gtk::make_managed<Gtk::Image>();
imgflag->set(m_pixbufFlag); imgflag->set(m_textureFlag);
buttons.at(pos)->set_child(*imgflag); buttons.at(pos)->set_child(*imgflag);
buttons.at(pos)->set_active(true); 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); Glib::ustring msg = Glib::ustring::compose("Remaining flags: %1", flags);
flagLabel.set_label(msg); flagLabel.set_label(msg);
} }
// void MainWindow::OnNewButtonClick() { // void MainWindow::OnNewButtonClick() {
// newGame = true; // newGame = true;
// gameOver = false; // gameOver = false;
@@ -67,15 +68,14 @@ void MainWindow::OnCellClick(int x, int y) {
if(field.isFlagged(x, y)) { if(field.isFlagged(x, y)) {
buttons.at(x + y * field.getRows())->set_active(true); buttons.at(x + y * field.getRows())->set_active(true);
} }
else if(field.isBomb(x, y)) { else {
openBombs();
}
else {
field.openCell(x, y); field.openCell(x, y);
if(field.isBomb(x, y)) {
openBombs();
}
} }
} }
void MainWindow::openBombs() { void MainWindow::openBombs() {
for(int i=0; i < field.getCols() * field.getRows(); i++) { for(int i=0; i < field.getCols() * field.getRows(); i++) {
int x = i % field.getCols(); int x = i % field.getCols();
@@ -86,12 +86,12 @@ void MainWindow::openBombs() {
if(field.isBomb(x, y)) { if(field.isBomb(x, y)) {
if(field.isFlagged(x, y)) { if(field.isFlagged(x, y)) {
auto imgFlagBomb = std::make_shared<Gtk::Image>(); auto imgFlagBomb = std::make_shared<Gtk::Image>();
imgFlagBomb->set(m_pixbufFlagBomb); imgFlagBomb->set(m_textureFlagBomb);
buttons.at(i)->set_child(*imgFlagBomb); buttons.at(i)->set_child(*imgFlagBomb);
} }
else { else {
auto imgBomb = std::make_shared<Gtk::Image>(); auto imgBomb = std::make_shared<Gtk::Image>();
imgBomb->set(m_pixbufBomb); imgBomb->set(m_textureBomb);
buttons.at(i)->set_child(*imgBomb); buttons.at(i)->set_child(*imgBomb);
} }
buttons.at(i)->set_active(true); buttons.at(i)->set_active(true);
@@ -158,31 +158,31 @@ void MainWindow::updateCell(int x, int y) {
// } // }
// return true; // return true;
// } // }
void MainWindow::gameOver() {
clockSignalConn.disconnect();
std::cout << "Signal gameOver emmited\n";
}
bool MainWindow::updateClockLabel()
{
++m_elapsedTime;
// bool MainWindow::UpdateClockLabel() int deciseconds = m_elapsedTime % 10;
// { int seconds = (m_elapsedTime / 10) % 60;
// if(gameOver) return false; int minutes = (m_elapsedTime /600) % 60;
// elapsedTime++; Glib::ustring msg = Glib::ustring::compose("Elapsed time: %1:%2.%3", \
Glib::ustring::format(std::setfill(L'0'), std::setw(2), minutes), \
// int deciseconds = elapsedTime % 10; Glib::ustring::format(std::setfill(L'0'), std::setw(2), seconds), \
// int seconds = (elapsedTime / 10) % 60; Glib::ustring::format(std::setfill(L'0'), std::setw(1), deciseconds));
// int minutes = (elapsedTime /600) % 60; 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() MainWindow::MainWindow()
{ {
// ApplyStyles(); // Load the CSS file // ApplyStyles(); // Load the CSS file
elapsedTime = 0; m_elapsedTime = 0;
newGame = true; newGame = true;
set_title("MineSweeper"); set_title("MineSweeper");
set_default_size(400, 400); 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 //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_textureBomb = Gdk::Texture::create_from_resource("/minesweeper/bomb-solid");
m_pixbufFlag = Gdk::Pixbuf::create_from_resource("/minesweeper/flag-solid"); m_textureFlag = Gdk::Texture::create_from_resource("/minesweeper/flag-solid");
m_pixbufFlagBomb = Gdk::Pixbuf::create_from_resource("/minesweeper/flag-bomb"); m_textureFlagBomb = Gdk::Texture::create_from_resource("/minesweeper/flag-bomb");
// bombPix.set_from_resource("/minesweeper/bomb-solid"); // 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.openCellSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::updateCell)));
field.remainingFlagsSignal.connect(sigc::bind(sigc::mem_fun(*this, &MainWindow::updateFlagsLabel))); 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.set_label("New");
//newGameButton.add_css_class("suggested-action"); //newGameButton.add_css_class("suggested-action");
//newGameButton.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::OnNewButtonClick)); //newGameButton.signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::OnNewButtonClick));
//optionButton.set_icon_name("open-menu"); //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 //create the minefield
//field = new MineField(COLS, MINES); //field = new MineField(COLS, MINES);

View File

@@ -1,13 +1,13 @@
#pragma once #pragma once
#include "minefield.hpp" #include "minefield.hpp"
#include "gdkmm/pixbuf.h"
#include <memory> #include <memory>
#include <gtkmm.h> #include <gtkmm.h>
#include <glibmm.h> #include <glibmm.h>
#include <gdkmm.h> #include <gdkmm.h>
#include <sigc++/sigc++.h> #include <sigc++/sigc++.h>
#include <gtkmm-4.0/gtkmm/gestureclick.h> #include <iomanip>
#include <iostream>
#define PROJECT_NAME "minesweeper" #define PROJECT_NAME "minesweeper"
@@ -24,18 +24,20 @@ class MainWindow : public Gtk::Window
Gtk::Label flagLabel; Gtk::Label flagLabel;
Gtk::Label clockLabel; Gtk::Label clockLabel;
MineField field {16, 16, 40}; MineField field {16, 16, 40};
int elapsedTime; int m_elapsedTime;
bool newGame; bool newGame;
std::shared_ptr<Gdk::Pixbuf> m_pixbufBomb; std::shared_ptr<Gdk::Texture> m_textureBomb;
std::shared_ptr<Gdk::Pixbuf> m_pixbufFlag; std::shared_ptr<Gdk::Texture> m_textureFlag;
std::shared_ptr<Gdk::Pixbuf> m_pixbufFlagBomb; std::shared_ptr<Gdk::Texture> m_textureFlagBomb;
void updateCell(int x, int y); void updateCell(int x, int y);
void openBombs(); void openBombs();
void updateFlagsLabel(int flags); void updateFlagsLabel(int flags);
bool updateClockLabel();
// sigc::connection clockConn; void gameWon();
void gameOver();
sigc::connection clockSignalConn;
// void OpenNearCells(int index); // void OpenNearCells(int index);
// void Explode(); // void Explode();xo
// bool AllCellsOpened(); // bool AllCellsOpened();
public: public: