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

Care about display on the interface. More...

#include <Display.hpp>

Public Member Functions

 Display ()=default
 Construct a new Display object (default constructor) More...
 
void config (const Vector2D &size)
 Config a Display class. More...
 
 ~Display ()
 Destroy the Display object. More...
 
void changeButt (const bool mode)
 Change a view of mode button. More...
 
void board (sf::RenderTarget &target, const Board *const board) const
 Display board on the window. More...
 
void draw (sf::RenderTarget &target) const
 Draw a interface of the game. More...
 
void drawStart (sf::RenderTarget &target) const
 Draw start button on interface. More...
 
void stopwatch (unsigned int seconds)
 Update a value of stopwatch time. More...
 
void mineCounter (short signed int mine)
 Update value of mine on the display. More...
 
void hideHint ()
 Hide a hint. More...
 

Public Attributes

Vector2DhintPos {NULL}
 Position of hint, NULL if no hint to display. More...
 

Static Private Member Functions

static TField *const pointer (const Field &field)
 Field code to element on Templete array. More...
 

Private Attributes

sf::Texture flag_texture
 Click mode flag texture. More...
 
sf::Texture click_texture
 Click mode "click" texture. More...
 
sf::Sprite mode_butt
 Mode button spirte. More...
 
sf::Font font
 Font. More...
 
sf::Texture start_texture
 Mine counter counter backgroun texture. More...
 
sf::Sprite start_butt
 Mine counter counter background. More...
 
sf::Texture mine_texture
 Mine counter counter backgroun texture. More...
 
sf::Sprite mine_bg
 Mine counter counter background. More...
 
sf::Text mine_txt
 Mine counter counter text. More...
 
sf::Texture stopwatch_texture
 Stopwatch background textrue. More...
 
sf::Sprite stopwatch_bg
 Stopwatch background. More...
 
sf::Text stopwatch_txt
 Stopwatch text. More...
 

Static Private Attributes

static TFieldTemplate [NUM_FIELD_VIEW] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}
 Templates of Field view. More...
 

Detailed Description

Care about display on the interface.

Constructor & Destructor Documentation

◆ Display()

Display::Display ( )
default

Construct a new Display object (default constructor)

◆ ~Display()

Display::~Display ( )

Destroy the Display object.

  • Delete Template array
81  {
83  for( short int i=0; i<NUM_FIELD_VIEW; ++i )
84  if( Display::Template[ i ] != NULL )
85  delete Display::Template[ i ];
86 }

Member Function Documentation

◆ pointer()

TField *const Display::pointer ( const Field field)
staticprivate

Field code to element on Templete array.

Parameters
field- value of field
Returns
short int - id in the Templete array.
  • Find the code of field

< id of field

  • First time create new TField
  • Return pointer
14  {
16  short int id;
17  if( field.flagged() ) id = 10;
18  else if( field.covered() ) id = 11;
19  else id = field.val();
20 
22  if( Display::Template[ id ] == NULL )
23  Display::Template[ id ] = new TField( id );
24 
26  return Display::Template[ id ];
27 }

◆ config()

void Display::config ( const Vector2D size)

Config a Display class.

  • Load button textures
  • Set positions and default texture of mode button
  • Load font
  • Score counter text config
  • Set position and texture of mine counter
  • Stopwatch text config
  • Set position and texture of stopwatch
  • Load start button
30  {
34 
36  mode_butt.setPosition( (size.x*FIELD_SIZE - MODE_BUTT_W)/2, MODE_BUTT_Y );
37  mode_butt.setTexture( this->click_texture );
38 
40  font = LoadFontFromResource("CONSOLA");
41 
43  mine_txt.setFont( font );
44  mine_txt.setPosition( size.x*FIELD_SIZE - COUNTER_X, COUNTER_Y );
45  mine_txt.setCharacterSize( COUNTER_FONT );
46  mine_txt.setFillColor( sf::Color::White );
47  mine_txt.setStyle( sf::Text::Bold) ;
48 
51 
52  mine_bg.setTexture( mine_texture );
53  mine_bg.setPosition( size.x*FIELD_SIZE - COUNTER_X_BG, COUNTER_Y_BG );
54 
56  stopwatch_txt.setFont( font );
57  stopwatch_txt.setPosition( STOPWATCH_X, STOPWATCH_Y );
58  stopwatch_txt.setCharacterSize( STOPWATCH_FONT );
59  stopwatch_txt.setFillColor( sf::Color::White );
60 
63 
64  stopwatch_bg.setTexture( stopwatch_texture );
66 
69 
70  start_butt.setTexture( start_texture );
71  start_butt.setPosition( (size.x*FIELD_SIZE)/2 + START_X, START_Y );
72 }

◆ changeButt()

void Display::changeButt ( const bool  mode)

Change a view of mode button.

Postcondition
Change texture in butt_mode
75  {
76  if( mode ) this->mode_butt.setTexture( this->flag_texture );
77  else this->mode_butt.setTexture( this->click_texture );
78 }

◆ board()

void Display::board ( sf::RenderTarget &  target,
const Board *const  board 
) const

Display board on the window.

Parameters
target- window target
board- game board

Draw every field

121  {
122  Vector2D pos;
124 
125  if( ! board->created() ){
126  for( pos.y = 0; pos.y < board->h(); ++pos.y ){
127  for( pos.x = 0; pos.x < board->w(); ++pos.x ){
128  pointer->set( pos );
129  target.draw( *pointer );
130  }
131  }
132  }
133  else {
135 
136  if( hintPos ){
137  for( pos.y = 0; pos.y < board->h(); ++pos.y )
138  for( pos.x = 0; pos.x < board->w(); ++pos.x ){
139  pointer = Display::pointer( (*board)(pos) );
140 
141  if( pos == *hintPos ) pointer->hint();
142 
143  pointer->set( pos );
144  target.draw( *pointer );
145 
146  if( pos == *hintPos ) pointer->normal();
147  }
148  }
149  else {
150  for( pos.y = 0; pos.y < board->h(); ++pos.y )
151  for( pos.x = 0; pos.x < board->w(); ++pos.x ){
152  pointer = Display::pointer( (*board)(pos) );
153  pointer->set( pos );
154  target.draw( *pointer );
155  }
156  }
157  }
158 }

◆ draw()

void Display::draw ( sf::RenderTarget &  target) const

Draw a interface of the game.

Parameters
target- window target
  • Mode button draw
  • Score counter draw
  • Stopwatch draw
106  {
108  target.draw( mode_butt );
109 
111  target.draw( mine_bg );
112  target.draw( mine_txt );
113 
115  target.draw( stopwatch_bg );
116  target.draw( stopwatch_txt );
117 
118 }

◆ drawStart()

void Display::drawStart ( sf::RenderTarget &  target) const
inline

Draw start button on interface.

Parameters
target-
102  { target.draw( start_butt ); }

◆ stopwatch()

void Display::stopwatch ( unsigned int  seconds)

Update a value of stopwatch time.

Parameters
seconds-
89  {
90  stringstream strm;
91  if( seconds / 60 < 10 ) strm << '0';
92  strm << seconds / 60 << ':';
93  if( seconds % 60 < 10 ) strm << '0';
94  strm << seconds % 60;
95  stopwatch_txt.setString( strm.str() );
96 }

◆ mineCounter()

void Display::mineCounter ( short signed int  mine)

Update value of mine on the display.

Parameters
mine-
99  {
100  stringstream strm;
101  strm << mine;
102  mine_txt.setString( strm.str() );
103 }

◆ hideHint()

void Display::hideHint ( )
inline

Hide a hint.

120  { if( hintPos ){ delete hintPos; hintPos = NULL; } }

Member Data Documentation

◆ Template

TField * Display::Template = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}
staticprivate

Templates of Field view.

Default: array of NULL.

id name
0 empty
1-8 values
9 mine
10 flag
11 covered
See also
NUM_FIELD_VIEW, TField

Declaration od Template[]

◆ flag_texture

sf::Texture Display::flag_texture
private

Click mode flag texture.

◆ click_texture

sf::Texture Display::click_texture
private

Click mode "click" texture.

◆ mode_butt

sf::Sprite Display::mode_butt
private

Mode button spirte.

◆ font

sf::Font Display::font
private

Font.

◆ start_texture

sf::Texture Display::start_texture
private

Mine counter counter backgroun texture.

◆ start_butt

sf::Sprite Display::start_butt
private

Mine counter counter background.

◆ mine_texture

sf::Texture Display::mine_texture
private

Mine counter counter backgroun texture.

◆ mine_bg

sf::Sprite Display::mine_bg
private

Mine counter counter background.

◆ mine_txt

sf::Text Display::mine_txt
private

Mine counter counter text.

◆ stopwatch_texture

sf::Texture Display::stopwatch_texture
private

Stopwatch background textrue.

◆ stopwatch_bg

sf::Sprite Display::stopwatch_bg
private

Stopwatch background.

◆ stopwatch_txt

sf::Text Display::stopwatch_txt
private

Stopwatch text.

◆ hintPos

Vector2D* Display::hintPos {NULL}

Position of hint, NULL if no hint to display.


The documentation for this class was generated from the following files:
TField::hint
void hint()
Set a hint style.
Definition: TField.hpp:38
Display::board
void board(sf::RenderTarget &target, const Board *const board) const
Display board on the window.
Definition: Display.cpp:121
COUNTER_X_BG
#define COUNTER_X_BG
x position of mine counter background (from top-left corner)
Definition: Const.hpp:34
TField
Templete of field view.
Definition: TField.hpp:13
Vector2D_t< short int >
Display::mode_butt
sf::Sprite mode_butt
Mode button spirte.
Definition: Display.hpp:31
Field::covered
bool covered() const
Is field covered? (is covered)
Definition: Field.hpp:126
Display::mine_txt
sf::Text mine_txt
Mine counter counter text.
Definition: Display.hpp:40
COUNTER_Y
#define COUNTER_Y
y position of mine counter text (from top-left corner)
Definition: Const.hpp:37
Display::mine_bg
sf::Sprite mine_bg
Mine counter counter background.
Definition: Display.hpp:39
Display::mine_texture
sf::Texture mine_texture
Mine counter counter backgroun texture.
Definition: Display.hpp:38
Display::stopwatch_bg
sf::Sprite stopwatch_bg
Stopwatch background.
Definition: Display.hpp:43
STOPWATCH_X_BG
#define STOPWATCH_X_BG
x position of stopwatch background (from top-right corner)
Definition: Const.hpp:27
Display::hintPos
Vector2D * hintPos
Position of hint, NULL if no hint to display.
Definition: Display.hpp:49
COUNTER_FONT
#define COUNTER_FONT
size of stopwatch font [px]
Definition: Const.hpp:38
LoadFontFromResource
sf::Font LoadFontFromResource(const char *const name)
Create a new sf::Font and load it from resoruces.
Definition: Func.cpp:28
TField::set
void set(const Vector2D &pos)
Set position.
Definition: TField.hpp:32
Display::start_texture
sf::Texture start_texture
Mine counter counter backgroun texture.
Definition: Display.hpp:35
Display::flag_texture
sf::Texture flag_texture
Click mode flag texture.
Definition: Display.hpp:29
Display::click_texture
sf::Texture click_texture
Click mode "click" texture.
Definition: Display.hpp:30
Vector2D_t::x
T x
X coordinate of vectior.
Definition: Vector2D.hpp:11
Display::stopwatch_txt
sf::Text stopwatch_txt
Stopwatch text.
Definition: Display.hpp:44
Display::font
sf::Font font
Font.
Definition: Display.hpp:33
COUNTER_Y_BG
#define COUNTER_Y_BG
y position of mine counter background (from top-left corner)
Definition: Const.hpp:35
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
Field::flagged
bool flagged() const
Is field flagged? ** flagged field is covered! **.
Definition: Field.hpp:109
STOPWATCH_Y
#define STOPWATCH_Y
y position of stopwatch text (from top-right corner)
Definition: Const.hpp:30
NUM_FIELD_VIEW
#define NUM_FIELD_VIEW
Number of fields views.
Definition: Const.hpp:51
Display::stopwatch_texture
sf::Texture stopwatch_texture
Stopwatch background textrue.
Definition: Display.hpp:42
Display::start_butt
sf::Sprite start_butt
Mine counter counter background.
Definition: Display.hpp:36
Display::pointer
static TField *const pointer(const Field &field)
Field code to element on Templete array.
Definition: Display.cpp:14
START_Y
#define START_Y
y position of start button (from top edge
Definition: Const.hpp:42
STOPWATCH_Y_BG
#define STOPWATCH_Y_BG
y position of stopwatch background (from top-right corner)
Definition: Const.hpp:28
Field::val
uint8_t val() const
Return value of field.
Definition: Field.hpp:72
MODE_BUTT_W
#define MODE_BUTT_W
Width of mode button.
Definition: Const.hpp:23
FIELD_SIZE
#define FIELD_SIZE
Sizes of field on the board.
Definition: Const.hpp:19
COUNTER_X
#define COUNTER_X
x position of mine counter text (from top-left corner)
Definition: Const.hpp:36
TField::normal
void normal()
Remove hint style.
Definition: TField.hpp:44
STOPWATCH_X
#define STOPWATCH_X
x position of stopwatch text (from top-right corner)
Definition: Const.hpp:29
Vector2D_t::y
T y
Y coordinate of vectior.
Definition: Vector2D.hpp:12
STOPWATCH_FONT
#define STOPWATCH_FONT
size of stopwatch font [px]
Definition: Const.hpp:31
Display::Template
static TField * Template[NUM_FIELD_VIEW]
Templates of Field view.
Definition: Display.hpp:26
Field
Field of the board.
Definition: Field.hpp:26
MODE_BUTT_Y
#define MODE_BUTT_Y
Y position of mode button (x is variable)
Definition: Const.hpp:24
START_X
#define START_X
x position of start button (from center point)
Definition: Const.hpp:41