-
Notifications
You must be signed in to change notification settings - Fork 2
/
sfml_pause_overlay.cpp
138 lines (123 loc) · 4.42 KB
/
sfml_pause_overlay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "sfml_pause_overlay.h"
#include "sfml_resources.h"
#include "sfml_button.h"
#include "sfml_window_manager.h"
#include <iostream>
#include <cassert>
sfml_pause_overlay::sfml_pause_overlay()
: m_window{ sfml_window_manager::get().get_window() },
m_font{ sfml_resources::get().get_default_font() }
{
m_header.setFont(m_font);
m_header.setCharacterSize(40);
m_header.setString("PAUSED");
sf::RectangleShape &b1_s = m_button1.get_shape();
b1_s.setFillColor(sf::Color(53,234,151));
m_button1.set_size(250, 75);
m_button1.set_string("CONTINUE");
sf::RectangleShape &b2_s = m_button2.get_shape();
b2_s.setFillColor(sf::Color(53,234,151));
m_button2.set_size(250, 75);
m_button2.set_string("SAVE");
sf::RectangleShape &b3_s = m_button3.get_shape();
b3_s.setFillColor(sf::Color(53,234,151));
m_button3.set_size(250, 75);
m_button3.set_string("QUIT");
#if(SFML_VERSION_MINOR > 3)
m_header.setFillColor(sf::Color(51, 51, 51));
m_header.setOutlineColor(sf::Color(41,180,116));
m_header.setOutlineThickness(3);
m_bg_rect.setFillColor(sf::Color(80, 140, 80, 200));
#else
//Only relevant for Travis
m_header.setColor(sf::Color::Green);
#endif
}
void sfml_pause_overlay::exec() //!OCLINT high cyclomatic complexity
{
assert(active(game_state::paused));
sf::Event event;
while (m_window.pollEvent(event))
{
sf::View view = m_window.getDefaultView();
switch (event.type) //!OCLINT too few branches, please fix
{
case sf::Event::Closed:
close();
break;
case sf::Event::Resized:
sfml_window_manager::get().update();
view.setSize(static_cast<float>(m_window.getSize().x),
static_cast<float>(m_window.getSize().y));
m_window.setView(view);
break;
case sf::Event::MouseButtonPressed:
if (m_button1.is_clicked(event, m_window))
close(game_state::playing);
if (m_button2.is_clicked(event, m_window))
close(game_state::saving);
if (m_button3.is_clicked(event, m_window))
close(game_state::menuscreen);
break;
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::C ||
event.key.code == sf::Keyboard::Escape) {
close(game_state::playing);
}
if(event.key.code == sf::Keyboard::Q){
close(game_state::menuscreen);
}
if(event.key.code == sf::Keyboard::S){
close(game_state::saving);
}
break;
default:
sfml_window_manager::get().process();
break;
}
}
set_positions();
draw_objects();
m_window.display();
}
void sfml_pause_overlay::draw_objects() {
m_window.draw(m_bg_rect);
m_window.draw(m_header);
m_window.draw(m_button1.get_shape());
m_window.draw(m_button1.get_text());
m_window.draw(m_button2.get_shape());
m_window.draw(m_button2.get_text());
m_window.draw(m_button3.get_shape());
m_window.draw(m_button3.get_text());
}
void sfml_pause_overlay::set_positions() {
//Header
m_header.setPosition((m_window.getSize().x / 2) - m_header.getGlobalBounds().width/2,
m_window.getView().getCenter().y-(m_window.getSize().y/2)+
(m_window.getSize().y/568)*80);
m_header.setPosition(m_window.mapPixelToCoords(
sf::Vector2i(m_header.getPosition())));
//Button 1
sf::Vector2f b1_pos(m_window.mapPixelToCoords(sf::Vector2i(
(m_window.getSize().x / 2),
(m_window.getSize().y/568)*220)));
m_button1.set_pos(b1_pos.x, b1_pos.y);
//Button 2
sf::Vector2f b2_pos(m_window.mapPixelToCoords(sf::Vector2i(
(m_window.getSize().x / 2),
(m_window.getSize().y/568)*330)));
m_button2.set_pos(b2_pos.x, b2_pos.y);
//Button 3
sf::Vector2f b3_pos(m_window.mapPixelToCoords(sf::Vector2i(
(m_window.getSize().x / 2),
(m_window.getSize().y/568)*440)));
m_button3.set_pos(b3_pos.x, b3_pos.y);
m_bg_rect.setPosition(m_window.mapPixelToCoords(sf::Vector2i(0, 0)));
m_bg_rect.setSize(m_window.getView().getSize());
}
void sfml_pause_overlay::close(game_state s) {
sfml_window_manager::get().set_state(s);
}
void sfml_pause_overlay::close() {
m_window.close();
}