summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Jones <admin@danieljon.es>2020-06-12 00:51:51 +0930
committerDaniel Jones <admin@danieljon.es>2020-06-12 00:51:51 +0930
commit9439f3beb0d6fe8632b793b7957dc6cb512117ff (patch)
tree43c152b987998c4798b863af63cc8b2b7290395b
parentcdb56f714a3d3f6423ed3379d822a0a4cff8ab01 (diff)
downloadfoxminesweeper-9439f3beb0d6fe8632b793b7957dc6cb512117ff.tar.gz
foxminesweeper-9439f3beb0d6fe8632b793b7957dc6cb512117ff.zip
added timer
-rw-r--r--MainWindow.cpp33
-rw-r--r--MainWindow.h6
2 files changed, 37 insertions, 2 deletions
diff --git a/MainWindow.cpp b/MainWindow.cpp
index fa1efa8..063b22b 100644
--- a/MainWindow.cpp
+++ b/MainWindow.cpp
@@ -25,6 +25,7 @@ FXDEFMAP(MainWindow) MainWindow_Map[]=
FXMAPFUNC(SEL_COMMAND, MainWindow::UI_Tile, MainWindow::on_Tile_Click),
FXMAPFUNC(SEL_COMMAND, MainWindow::UI_New, MainWindow::on_New_Click),
FXMAPFUNC(SEL_RIGHTBUTTONPRESS, MainWindow::UI_Tile, MainWindow::on_Tile_Right_Click),
+ FXMAPFUNC(SEL_TIMEOUT, MainWindow::UI_Timer_Tick, MainWindow::on_Timer_Tick),
};
FXIMPLEMENT(MainWindow, FXMainWindow, MainWindow_Map, ARRAYNUMBER(MainWindow_Map))
@@ -47,6 +48,15 @@ MainWindow::~MainWindow()
delete board;
}
+long
+MainWindow::on_Timer_Tick(FXObject *sender, FXSelector sel, void *data)
+{
+ seconds++;
+ time_label->setText(FXStringVal(seconds));
+ getApp()->addTimeout(this, UI_Timer_Tick, 1000, nullptr);
+ return 1;
+}
+
void
MainWindow::create()
{
@@ -75,6 +85,7 @@ MainWindow::create_ui()
// Label above the canvas
new FXLabel(canvasFrame,"foxminesweeper", NULL, JUSTIFY_CENTER_X|LAYOUT_FILL_X);
+ time_label = new FXLabel(canvasFrame, "0", nullptr, LAYOUT_CENTER_X);
// Horizontal divider line
new FXHorizontalSeparator(canvasFrame, SEPARATOR_GROOVE|LAYOUT_FILL_X);
@@ -109,6 +120,8 @@ MainWindow::create_ui()
void
MainWindow::new_game(int width, int height, int minecount)
{
+ seconds = 0;
+ ticking = false;
if (board)
delete board;
puts("starting new game..");
@@ -196,6 +209,11 @@ MainWindow::draw_buttons()
long
MainWindow::on_Tile_Click(FXObject *sender, FXSelector sel, void *data)
{
+ if (!ticking)
+ {
+ app->addTimeout(this, UI_Timer_Tick, 1000);
+ ticking = true;
+ }
if (game_over == true)
return 1;
int x = 0, y = 0;
@@ -216,20 +234,26 @@ MainWindow::on_Tile_Click(FXObject *sender, FXSelector sel, void *data)
if (!board->is_game_running() && !board->is_game_won())
{
/* lost */
+ app->removeTimeout(this, UI_Timer_Tick);
puts("you lose the game");
- FXMessageBox *msgbox = new FXMessageBox(app, "Game Over", "You lost", nullptr, FX::MBOX_OK);
+ std::string message = "You lost in: " + std::to_string(seconds) + " seconds";
+ FXMessageBox *msgbox = new FXMessageBox(app, "Game Over", FXString(message.c_str()), nullptr, FX::MBOX_OK);
msgbox->create();
msgbox->show();
game_over = true;
+ ticking = false;
}
else if (!board->is_game_running() && board->is_game_won())
{
/* won */
+ app->removeTimeout(this, UI_Timer_Tick);
puts("you won the game");
- FXMessageBox *msgbox = new FXMessageBox(app, "Game Over", "You win", nullptr, FX::MBOX_OK);
+ std::string message = "You won in: " + std::to_string(seconds) + " seconds";
+ FXMessageBox *msgbox = new FXMessageBox(app, "Game Over", FXString(message.c_str()), nullptr, FX::MBOX_OK);
msgbox->create();
msgbox->show();
game_over = true;
+ ticking = false;
}
button->setFrameStyle(0);
draw_buttons();
@@ -239,6 +263,11 @@ MainWindow::on_Tile_Click(FXObject *sender, FXSelector sel, void *data)
long
MainWindow::on_Tile_Right_Click(FXObject *sender, FXSelector sel, void *data)
{
+ if (!ticking)
+ {
+ app->addTimeout(this, UI_Timer_Tick, 1000);
+ ticking = true;
+ }
if (game_over == true)
return 1;
int x = 0, y = 0;
diff --git a/MainWindow.h b/MainWindow.h
index 0cc0841..29e998c 100644
--- a/MainWindow.h
+++ b/MainWindow.h
@@ -20,6 +20,7 @@
#include <FXScrollArea.h>
#include <FXMessageBox.h>
#include <algorithm>
+#include <string>
#include "Board.h"
#include "icons.h"
@@ -37,12 +38,14 @@ class MainWindow : public FXMainWindow
enum {
UI_Tile = FXMainWindow::ID_LAST,
UI_New,
+ UI_Timer_Tick,
};
/* Event handlers */
long on_Tile_Click(FXObject *sender, FXSelector sel, void *data);
long on_New_Click(FXObject *sender, FXSelector sel, void *data);
long on_Tile_Right_Click(FXObject *sender, FXSelector sel, void *data);
+ long on_Timer_Tick(FXObject *sender, FXSelector sel, void *data);
FXApp *get_app(){ return app; };
@@ -55,6 +58,8 @@ class MainWindow : public FXMainWindow
void new_game(int width, int height, int minecount);
void draw_buttons();
bool game_over;
+ long unsigned int seconds;
+ bool ticking;
FXHorizontalFrame *contents; // Content frame
FXVerticalFrame *canvasFrame; // Canvas frame
FXVerticalFrame *buttonFrame; // Button frame
@@ -62,6 +67,7 @@ class MainWindow : public FXMainWindow
FXApp *app;
Board *board;
FXMatrix *matrix;
+ FXLabel *time_label;
std::vector<std::shared_ptr<FXButton>> tile_buttons;
/* icons */
FXIcon *bomb_icon;