Minesweeper2  V1.1.1 Game in C++ by Górka Mateusz
Menu Class Reference

Mange a menu GUI on the begin of game. More...

#include <Menu.hpp>

+ Inheritance diagram for Menu:

Public Member Functions

 Menu ()
 Construct a new Menu object. More...
 
 ~Menu ()=default
 Destroy the Menu object (default) More...
 
void draw (sf::RenderTarget &target, sf::RenderStates states) const override
 Draw method. More...
 
void update ()
 Update text values to display. More...
 
bool click (const sf::RenderWindow &window)
 Click manage on menu GUI. More...
 
void output (int &argc, char **&argv)
 Create argv and argc based on GUI menu. More...
 
void change (unsigned short &val, const short int change, const short int minval, const short int maxval)
 Change value. More...
 

Static Public Member Functions

static void handling (int &argc, char **&argv)
 Menu GUI handling. More...
 
static void clean (const int argc, char **&argv)
 Delete argv table. More...
 
static bool inside (const sf::Vector2i &pos, const sf::Vector2i &coo, const sf::Vector2i &size)
 Is position pos is in the rectangle of coordinates coo and sizes size More...
 
static bool insideX (const sf::Vector2i &pos, const sf::Vector2i &coo, const sf::Vector2i &size)
 Is pos.x is bigger than coo.x and smoller coo.x + size.x. More...
 

Private Attributes

sf::Font font
 Font. More...
 
sf::Texture menu_texture
 Menu GUI background texture. More...
 
sf::Sprite menu_bg
 Menu GUI background spirte. More...
 
sf::Text width_txt
 Text to display width of the board. More...
 
sf::Text height_txt
 Text to display height of the board. More...
 
sf::Text mines_txt
 Text to display number mines on the board. More...
 
sf::Text hints_txt
 Text to display number mines on the board. More...
 
unsigned short int out_width {DEFAULT_X_SIZE}
 Width of window. More...
 
unsigned short int out_height {DEFAULT_Y_SIZE}
 Height of window. More...
 
unsigned short int out_mines {DEFAULT_MINE}
 Number of mines on the window. More...
 
bool allowHints {false}
 Is hints will be allowed. More...
 

Detailed Description

Mange a menu GUI on the begin of game.

Constructor & Destructor Documentation

◆ Menu()

Menu::Menu ( )

Construct a new Menu object.

  • Load menu GUI background texture
  • Init spirte of menu background
  • Load font
  • Texts init
11  {
14 
16  menu_bg.setTexture( menu_texture );
17  menu_bg.setPosition( 0, 0 );
18 
20  font = LoadFontFromResource("CONSOLA");
21  //IDEA font from Display::font ???
22 
24  width_txt.setFont( font );
25  width_txt.setCharacterSize( MENU_SIZE_VAL );
26  width_txt.setColor( Color::White );
27  width_txt.setPosition( MENU_X_VALUES , MENU_Y_WIDTH );
28 
29  height_txt.setFont( font );
30  height_txt.setCharacterSize( MENU_SIZE_VAL );
31  height_txt.setColor( Color::White );
32  height_txt.setPosition( MENU_X_VALUES , MENU_Y_HEIGHT );
33 
34  mines_txt.setFont( font );
35  mines_txt.setCharacterSize( MENU_SIZE_VAL );
36  mines_txt.setColor( Color::White );
37  mines_txt.setPosition( MENU_X_VALUES , MENU_Y_MINES );
38 
39  hints_txt.setFont( font );
40  hints_txt.setCharacterSize( MENU_SIZE_HINTTXT );
41  hints_txt.setColor( Color::White );
42  hints_txt.setPosition( MENU_XY_HINTTXT );
43 }

◆ ~Menu()

Menu::~Menu ( )
default

Destroy the Menu object (default)

Member Function Documentation

◆ draw()

void Menu::draw ( sf::RenderTarget &  target,
sf::RenderStates  states 
) const
override

Draw method.

Parameters
target-
states-
114  {
115  target.draw( menu_bg );
116  target.draw( width_txt );
117  target.draw( height_txt );
118  target.draw( mines_txt );
119  target.draw( hints_txt );
120 }

◆ update()

void Menu::update ( )

Update text values to display.

  • Update width txt
  • Update height txt
  • Update mines txt
  • Update hints state
228  {
229  stringstream strm;
230  string s;
231 
233  strm << out_width << ' '; strm >> s;
234  width_txt.setString( s );
235 
237  strm << out_height << ' '; strm >> s;
238  height_txt.setString( s );
239 
241  strm << out_mines << ' '; strm >> s;
242  mines_txt.setString( s );
243 
245  if( allowHints ) hints_txt.setString("on");
246  else hints_txt.setString("off");
247 }

◆ handling()

void Menu::handling ( int &  argc,
char **&  argv 
)
static

Menu GUI handling.

Postcondition
Parameters
argc- size of argv[] (out)
argv- values of init argument to game (out)
  1. Create window and GUI object
  2. GUI loop
  3. Make w new argv listx`
46  {
47 
49  RenderWindow window(
50  VideoMode( GUI_MENU_W, GUI_MENU_H ),
51  "Minesweeper | maatiug",
52  Style::Close | Style::Titlebar );
53  Event event;
54 
55  Menu menu;
56 
58  bool change = true;
59 
60  while( window.isOpen() ){
61 
62  //* Process events
63  while( window.pollEvent(event) ){
64 
65  // Close window: exit
66  if (event.type == Event::Closed)
67  window.close();
68 
69  // Buttons
70  if( ! change ){
71  if( Mouse::isButtonPressed( Mouse::Left ) ){
72 
73  if( menu.click( window ) )
74  window.close();
75 
76  sf::sleep( milliseconds(SLEEP_CLICK) );
77  }
78 
79  change = true;
80  }
81  }
82 
83  //* Display
84  if( change ){
85 
86  menu.update();
87 
88  // Clear screen
89  window.clear();
90  // Draw the string
91  window.draw( menu );
92  // Update the window
93  window.display();
94 
95  change = false;
96  }
97 
98  sf::sleep( milliseconds( SLEEP_LOOP ) );
99 
100  }
101 
103  menu.output( argc, argv );
104 
105 }

◆ click()

bool Menu::click ( const sf::RenderWindow &  window)

Click manage on menu GUI.

Parameters
window- window
175  {
176 
177  Vector2i mouse_pos = Mouse::getPosition(window);
178 
179  // Buttons of sizes
180  const Vector2i butt_sizes = Vector2i( MENU_W_SIZES, MENU_H_SIZES );
181 
182  //width
183  if( mouse_pos.y > MENU_Y_WIDTH && mouse_pos.y < MENU_Y_WIDTH+MENU_H_PM ){
184  if ( mouse_pos.x > MENU_X_2PLUS && mouse_pos.x < MENU_X_2PLUS + MENU_W_PM ) change( out_width, 5, 7, 100 );
185  else if( mouse_pos.x > MENU_X_PLUS && mouse_pos.x < MENU_X_PLUS + MENU_W_PM ) change( out_width, 1, 7, 100 );
186  else if( mouse_pos.x > MENU_X_2MINUS && mouse_pos.x < MENU_X_2MINUS + MENU_W_PM ) change( out_width, -5, 7, 100 );
187  else if( mouse_pos.x > MENU_X_MINUS && mouse_pos.x < MENU_X_MINUS + MENU_W_PM ) change( out_width, -1, 7, 100 );
188  }
189  //height
190  else if( mouse_pos.y > MENU_Y_HEIGHT && mouse_pos.y < MENU_Y_HEIGHT + MENU_H_PM ){
191  if ( mouse_pos.x > MENU_X_2PLUS && mouse_pos.x < MENU_X_2PLUS + MENU_W_PM ) change( out_height, 5, 1, 100 );
192  else if( mouse_pos.x > MENU_X_PLUS && mouse_pos.x < MENU_X_PLUS + MENU_W_PM ) change( out_height, 1, 1, 100 );
193  else if( mouse_pos.x > MENU_X_2MINUS && mouse_pos.x < MENU_X_2MINUS + MENU_W_PM ) change( out_height, -5, 1, 100 );
194  else if( mouse_pos.x > MENU_X_MINUS && mouse_pos.x < MENU_X_MINUS + MENU_W_PM ) change( out_height, -1, 1, 100 );
195  }
196  //mines
197  else if( mouse_pos.y > MENU_Y_MINES && mouse_pos.y < MENU_Y_MINES + MENU_H_PM ){
198  if ( mouse_pos.x > MENU_X_2PLUS_B && mouse_pos.x < MENU_X_2PLUS_B + MENU_W_PM ) change( out_mines, 5, 0, 1000 );
199  else if( mouse_pos.x > MENU_X_PLUS_B && mouse_pos.x < MENU_X_PLUS_B + MENU_W_PM ) change( out_mines, 1, 0, 1000 );
200  else if( mouse_pos.x > MENU_X_2MINUS && mouse_pos.x < MENU_X_2MINUS + MENU_W_PM ) change( out_mines, -5, 0, 1000 );
201  else if( mouse_pos.x > MENU_X_MINUS && mouse_pos.x < MENU_X_MINUS + MENU_W_PM ) change( out_mines, -1, 0, 1000 );
202  }
203  //small
204  else if( Menu::inside( mouse_pos, Vector2i( MENU_X_SMALL, MENU_Y_SIZES ), butt_sizes ) ){
206  }
207  //big
208  else if( Menu::inside( mouse_pos, Vector2i( MENU_X_BIG, MENU_Y_SIZES ), butt_sizes ) ){
210  }
211  //huge
212  else if( Menu::inside( mouse_pos, Vector2i( MENU_X_HUGE, MENU_Y_SIZES ), butt_sizes ) ){
214  }
215  // start button
216  else if( Menu::inside( mouse_pos, Vector2i( MENU_XY_START ), Vector2i( MENU_WH_START ) ) ){
217  return true;
218  }
219  // hints button
220  else if( Menu::inside( mouse_pos, Vector2i( MENU_XY_HINT ), Vector2i( MENU_WH_HINT ) ) ){
222  }
223 
224  return false;
225 }

◆ output()

void Menu::output ( int &  argc,
char **&  argv 
)

Create argv and argc based on GUI menu.

Parameters
argc- [out] length of argument list
argv- [out] argument list (array)
  • Clean existing argv
  • Create new argv
123  {
124 
126  Menu::clean( argc, argv );
127 
129  stringstream strm;
130 
131  //hints
132  argc = (allowHints) ? 8 : 7;
133  argv = new char*[ argc ];
134 
135  //empty
136  argv[0] = new char;
137  argv[0][0] = '\0';
138 
139  //width
140  argv[1] = new char[3];
141  strm << "-w\0" << ' ';
142  strm >> argv[1];
143 
144  argv[2] = new char[3];
145  strm << out_width << ' ';
146  strm >> argv[2];
147 
148  //height
149  argv[3] = new char[3];
150  strm << "-h\0" << ' ';
151  strm >> argv[3];
152 
153  argv[4] = new char[3];
154  strm << out_height << ' ';
155  strm >> argv[4];
156 
157  //mines
158  argv[5] = new char[3];
159  strm << "-m\0" << ' ';
160  strm >> argv[5];
161 
162  argv[6] = new char[4];
163  strm << out_mines << ' ';
164  strm >> argv[6];
165 
166  //hints
167  if( allowHints ){
168  argv[7] = new char[4];
169  strm << "-H\0" << ' ';
170  strm >> argv[7];
171  }
172 }

◆ change()

void Menu::change ( unsigned short &  val,
const short int  change,
const short int  minval,
const short int  maxval 
)

Change value.

Parameters
val- [out] changed values
change- change
minval- min value of val
maxval- max value of val
250  {
251  if( val + change > minval && val + change < maxval ) val += change;
252 
253  if( out_mines + 9 > out_width * out_height )
255 }

◆ clean()

void Menu::clean ( const int  argc,
char **&  argv 
)
static

Delete argv table.

Parameters
argc- size of argv[]
argv- char** array of arrays to delete
108  {
109  delete[] argv[0];
110  delete[] argv;
111 }

◆ inside()

bool Menu::inside ( const sf::Vector2i &  pos,
const sf::Vector2i &  coo,
const sf::Vector2i &  size 
)
static

Is position pos is in the rectangle of coordinates coo and sizes size

  • coo is a bottom-left corner of rectangle
  • [0,0] pos is a top-left corner of window
    Parameters
    pos- position
    coo- cooridnates of rectangle
    size- sizes of rectangle
258  {
259  if( pos.y > coo.y && pos.y < coo.y + size.y )
260  if( pos.x > coo.x && pos.x < coo.x + size.x )
261  return true;
262  return false;
263 }

◆ insideX()

static bool Menu::insideX ( const sf::Vector2i &  pos,
const sf::Vector2i &  coo,
const sf::Vector2i &  size 
)
inlinestatic

Is pos.x is bigger than coo.x and smoller coo.x + size.x.

Parameters
pos- position
coo- coordinates of 'line'
size- size of 'line'
Returns
true -
false -
119  { return pos.x > coo.x && pos.x < coo.x + size.x; }

Member Data Documentation

◆ font

sf::Font Menu::font
private

Font.

◆ menu_texture

sf::Texture Menu::menu_texture
private

Menu GUI background texture.

◆ menu_bg

sf::Sprite Menu::menu_bg
private

Menu GUI background spirte.

◆ width_txt

sf::Text Menu::width_txt
private

Text to display width of the board.

◆ height_txt

sf::Text Menu::height_txt
private

Text to display height of the board.

◆ mines_txt

sf::Text Menu::mines_txt
private

Text to display number mines on the board.

◆ hints_txt

sf::Text Menu::hints_txt
private

Text to display number mines on the board.

◆ out_width

unsigned short int Menu::out_width {DEFAULT_X_SIZE}
private

Width of window.

◆ out_height

unsigned short int Menu::out_height {DEFAULT_Y_SIZE}
private

Height of window.

◆ out_mines

unsigned short int Menu::out_mines {DEFAULT_MINE}
private

Number of mines on the window.

◆ allowHints

bool Menu::allowHints {false}
private

Is hints will be allowed.


The documentation for this class was generated from the following files:
Menu::font
sf::Font font
Font.
Definition: Menu.hpp:12
MENU_SIZE_HINTTXT
#define MENU_SIZE_HINTTXT
GUI Menu size of font hints state.
Definition: Const.hpp:95
MENU_X_MINUS
#define MENU_X_MINUS
GUI Menu X of button '-'.
Definition: Const.hpp:77
Menu::output
void output(int &argc, char **&argv)
Create argv and argc based on GUI menu.
Definition: Menu.cpp:123
MENU_XY_HINT
#define MENU_XY_HINT
GUI Menu X, Y position of hints button.
Definition: Const.hpp:92
MENU_X_SMALL
#define MENU_X_SMALL
GUI Menu X of button 'small'.
Definition: Const.hpp:70
GUI_MENU_W
#define GUI_MENU_W
Width of GUI menu window.
Definition: Const.hpp:67
Menu::click
bool click(const sf::RenderWindow &window)
Click manage on menu GUI.
Definition: Menu.cpp:175
Menu::out_mines
unsigned short int out_mines
Number of mines on the window.
Definition: Menu.hpp:24
Menu::clean
static void clean(const int argc, char **&argv)
Delete argv table.
Definition: Menu.cpp:108
MENU_X_PLUS
#define MENU_X_PLUS
GUI Menu X of button '+'.
Definition: Const.hpp:79
GUI_MENU_H
#define GUI_MENU_H
Height of GUI menu window.
Definition: Const.hpp:68
SMALL_WIDTH
#define SMALL_WIDTH
Width of small board.
Definition: Const.hpp:54
MENU_Y_WIDTH
#define MENU_Y_WIDTH
GUI Menu Y of text of width.
Definition: Const.hpp:88
Menu
Mange a menu GUI on the begin of game.
Definition: Menu.hpp:10
MENU_SIZE_VAL
#define MENU_SIZE_VAL
GUI Menu size of font size of width, height and mines values.
Definition: Const.hpp:86
MENU_X_BIG
#define MENU_X_BIG
GUI Menu X of button 'big'.
Definition: Const.hpp:71
MENU_H_SIZES
#define MENU_H_SIZES
GUI Menu height of sizes buttons.
Definition: Const.hpp:75
LoadFontFromResource
sf::Font LoadFontFromResource(const char *const name)
Create a new sf::Font and load it from resoruces.
Definition: Func.cpp:28
HUGE_WIDTH
#define HUGE_WIDTH
Width of huge board.
Definition: Const.hpp:62
MENU_Y_HEIGHT
#define MENU_Y_HEIGHT
GUI Menu Y of text of height.
Definition: Const.hpp:89
Menu::width_txt
sf::Text width_txt
Text to display width of the board.
Definition: Menu.hpp:17
MENU_Y_SIZES
#define MENU_Y_SIZES
GUI Menu Y of sizes buttons.
Definition: Const.hpp:73
MENU_X_2PLUS_B
#define MENU_X_2PLUS_B
GUI Menu X of button '++' for number of mines.
Definition: Const.hpp:82
Menu::change
void change(unsigned short &val, const short int change, const short int minval, const short int maxval)
Change value.
Definition: Menu.cpp:250
HUGE_MINES
#define HUGE_MINES
Mines of huge board.
Definition: Const.hpp:64
BIG_HEIGHT
#define BIG_HEIGHT
Height of big board.
Definition: Const.hpp:59
LoadTextureFromResource
sf::Texture LoadTextureFromResource(const char *const name)
Creates a new sf::Texture and loads it with Texture data from a resource (.rc) file https://github....
Definition: Func.cpp:6
BIG_MINES
#define BIG_MINES
Mines of big board.
Definition: Const.hpp:60
Menu::out_height
unsigned short int out_height
Height of window.
Definition: Menu.hpp:23
SLEEP_LOOP
#define SLEEP_LOOP
Sleep time of game loop.
Definition: Const.hpp:45
Menu::mines_txt
sf::Text mines_txt
Text to display number mines on the board.
Definition: Menu.hpp:19
Menu::menu_bg
sf::Sprite menu_bg
Menu GUI background spirte.
Definition: Menu.hpp:15
Menu::update
void update()
Update text values to display.
Definition: Menu.cpp:228
MENU_X_VALUES
#define MENU_X_VALUES
GUI Menu X of text values width, height, number of mines.
Definition: Const.hpp:87
Menu::allowHints
bool allowHints
Is hints will be allowed.
Definition: Menu.hpp:25
Menu::height_txt
sf::Text height_txt
Text to display height of the board.
Definition: Menu.hpp:18
BIG_WIDTH
#define BIG_WIDTH
Width of big board.
Definition: Const.hpp:58
Menu::out_width
unsigned short int out_width
Width of window.
Definition: Menu.hpp:22
Menu::hints_txt
sf::Text hints_txt
Text to display number mines on the board.
Definition: Menu.hpp:20
Menu::menu_texture
sf::Texture menu_texture
Menu GUI background texture.
Definition: Menu.hpp:14
MENU_WH_START
#define MENU_WH_START
MENU_W_SIZES
#define MENU_W_SIZES
GUI Menu width of sizes buttons.
Definition: Const.hpp:74
MENU_X_2PLUS
#define MENU_X_2PLUS
GUI Menu X of button '++'.
Definition: Const.hpp:80
SLEEP_CLICK
#define SLEEP_CLICK
Sleep time after click.
Definition: Const.hpp:46
MENU_X_HUGE
#define MENU_X_HUGE
GUI Menu X of button 'huge'.
Definition: Const.hpp:72
SMALL_MINES
#define SMALL_MINES
Mines of small board.
Definition: Const.hpp:56
HUGE_HEIGHT
#define HUGE_HEIGHT
Height of huge board.
Definition: Const.hpp:63
MENU_H_PM
#define MENU_H_PM
GUI Menu height of buttons -, –, +, ++.
Definition: Const.hpp:84
MENU_W_PM
#define MENU_W_PM
GUI Menu width of buttons -, –, +, ++.
Definition: Const.hpp:83
MENU_WH_HINT
#define MENU_WH_HINT
GUI Menu width, height of hints button.
Definition: Const.hpp:93
MENU_XY_HINTTXT
#define MENU_XY_HINTTXT
GUI Menu position of off/on txt on hits button.
Definition: Const.hpp:94
MENU_Y_MINES
#define MENU_Y_MINES
GUI Menu Y of text of number of mines.
Definition: Const.hpp:90
SMALL_HEIGHT
#define SMALL_HEIGHT
Height of small board.
Definition: Const.hpp:55
MENU_X_PLUS_B
#define MENU_X_PLUS_B
GUI Menu X of button '+' for number of mines.
Definition: Const.hpp:81
MENU_X_2MINUS
#define MENU_X_2MINUS
GUI Menu X of button '–'.
Definition: Const.hpp:78
Menu::inside
static bool inside(const sf::Vector2i &pos, const sf::Vector2i &coo, const sf::Vector2i &size)
Is position pos is in the rectangle of coordinates coo and sizes size
Definition: Menu.cpp:258
MENU_XY_START
#define MENU_XY_START
GUI Menu X, Y position of start button.
Definition: Const.hpp:97