Bladeren bron

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

Bernardo Magri 10 maanden geleden
bovenliggende
commit
a2f3c89599
2 gewijzigde bestanden met toevoegingen van 36 en 14 verwijderingen
  1. 19 0
      src/minefield.cpp
  2. 17 14
      src/minefield.hpp

+ 19 - 0
src/minefield.cpp

@@ -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) {

+ 17 - 14
src/minefield.hpp

@@ -1,12 +1,13 @@
 #pragma once
 
-// #include <emmintrin.h>
 #include <sigc++/signal.h>
 #include <utility>
 #include <vector>
 #include <cstdlib>     
 #include <ctime>
 #include <memory>
+#include <thread>
+#include <chrono>
 
 struct Cell {
   bool isFlagged;
@@ -17,19 +18,20 @@ struct Cell {
 };
 
 class MineField {
-
   std::vector<std::shared_ptr<Cell>> m_cells;
-  int  m_rows;
-  int  m_cols;
-  int  m_totalMines;
-  int  m_remainingFlags;
-  int  m_openCells;
-  bool m_exploded;
+  int    m_rows;
+  int    m_cols;
+  int    m_totalMines;
+  int    m_remainingFlags;
+  int    m_openCells;
+  bool   m_exploded;
+  bool   m_gameWon;
+  size_t m_time;
+  std::thread timerThread;
   void computeBombsNearby(int x, int y);
   void openNeighboorhood(int x, int y);
   void setOpenCell(int x, int y);
-  //bint vecToPosition(int x, int y) {return (x + y * m_rows); };
-  //std::pair<int, int> positionToVec(int pos) {return std::make_pair(pos % m_cols, pos / m_cols); };
+  void timerTick();
   
 public:
   MineField(int cols, int rows, int mines);
@@ -47,8 +49,9 @@ public:
   int  getTotalMines() {return m_totalMines; };
   void startNewGame(int cols, int rows, int mines);
 
-  sigc::signal<void(int, int)> openCellSignal;
-  sigc::signal<void(int)> remainingFlagsSignal;
-  sigc::signal<void(void)> gameWonSignal;
-  sigc::signal<void(void)> gameOverSignal;
+  sigc::signal<void(int, int)>     openCellSignal;
+  sigc::signal<void(int)>          remainingFlagsSignal;
+  sigc::signal<void(void)>         gameWonSignal;
+  sigc::signal<void(void)>         gameOverSignal;
+  sigc::signal<void(unsigned int)> timerSignal;
 };