Quellcode durchsuchen

Fixing the clock and adding new signals

Bernardo Magri vor 10 Monaten
Ursprung
Commit
751e12f495
3 geänderte Dateien mit 49 neuen und 43 gelöschten Zeilen
  1. 0 1
      src/minefield.cpp
  2. 38 33
      src/window.cpp
  3. 11 9
      src/window.hpp

+ 0 - 1
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();

+ 38 - 33
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<Gtk::Image>();
-      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<Gtk::Image>();
-	imgFlagBomb->set(m_pixbufFlagBomb);
+	imgFlagBomb->set(m_textureFlagBomb);
 	buttons.at(i)->set_child(*imgFlagBomb);
       }
       else {
 	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_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()
-// {
-//   if(gameOver)  return false;
-  
-//   elapsedTime++;
-
-//   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;
-// }
+bool MainWindow::updateClockLabel()
+{
+  ++m_elapsedTime;
+
+  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));
+  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);
 

+ 11 - 9
src/window.hpp

@@ -1,13 +1,13 @@
 #pragma once
 
 #include "minefield.hpp"
-#include "gdkmm/pixbuf.h"
 #include <memory>
 #include <gtkmm.h>
 #include <glibmm.h>
 #include <gdkmm.h>
 #include <sigc++/sigc++.h>
-#include <gtkmm-4.0/gtkmm/gestureclick.h>
+#include <iomanip>
+#include <iostream>
 
 #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<Gdk::Pixbuf> m_pixbufBomb;
-  std::shared_ptr<Gdk::Pixbuf> m_pixbufFlag;
-  std::shared_ptr<Gdk::Pixbuf> m_pixbufFlagBomb;
+  std::shared_ptr<Gdk::Texture> m_textureBomb;
+  std::shared_ptr<Gdk::Texture> m_textureFlag;
+  std::shared_ptr<Gdk::Texture> 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: