-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI.cpp
155 lines (106 loc) · 4.3 KB
/
UI.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "UI.h"
#include "constants.h"
#include "Renderer/Window.h"
UI::UI() {
healthBar = nullptr;
fps = nullptr;
healthBarPos = sf::Vector2f(Constants::UI::HealthBar::X, Constants::UI::HealthBar::Y);
bombBarPos = sf::Vector2f(Constants::UI::BombBar::X, Constants::UI::BombBar::Y);
}
void UI::drawHealthBar(int newHealth) {
static int health = -1;
static int newLenght = 0;
uint lenght = Constants::UI::HealthBar::LENGHT;
SpriteData sd = Sprite::instance()->getSpriteData("UI_HealtBar");
if(healthBar == nullptr) {
/** ryzowanie lewej czesci belki **/
sprite = Sprite::instance()->getSprite("UI_HealtBarLeft");
sprite.SetPosition(healthBarPos.x, healthBarPos.y);
Window::instance()->getRW()->Draw(sprite);
/** rysowanie srodkowej czesci belki **/
sprite = Sprite::instance()->getSprite("UI_HealtBar");
for(uint i = 0; i < lenght; ++i) {
sprite.SetPosition(healthBarPos.x + i + 2, healthBarPos.y);
Window::instance()->getRW()->Draw(sprite);
}
/** rysowanie prawej czesci belki **/
sprite = Sprite::instance()->getSprite("UI_HealtBarRight");
sprite.SetPosition(healthBarPos.x + lenght + 2, healthBarPos.y);
Window::instance()->getRW()->Draw(sprite);
/** koniec rysowania **/
newHealthBarImg = Window::instance()->getRW()->Capture();
newHealthBarImg.SetSmooth(false);
healthBar = new sf::Sprite(newHealthBarImg);
healthBarAlpha = new sf::Sprite(newHealthBarImg);
healthBar->SetCenter(0, sd.dimensions.y);
healthBarAlpha->SetCenter(0, sd.dimensions.y);
healthBar->SetPosition(healthBarPos);
healthBarAlpha->SetPosition(healthBarPos);
healthBar->SetSubRect( sf::IntRect(healthBarPos.x,
healthBarPos.y - sd.dimensions.y,
healthBarPos.x + lenght,
healthBarPos.y) );
healthBarAlpha->SetSubRect( sf::IntRect(healthBarPos.x,
healthBarPos.y - sd.dimensions.y,
healthBarPos.x + lenght + 4,
healthBarPos.y) );
healthBarAlpha->SetColor( sf::Color(255, 255, 255, Constants::UI::HealthBar::ALPHA) );
}
if(newHealth > -1 && newHealth < Constants::Player::MAX_HEALTH + 1 && health != newHealth ) {
newLenght = lenght / Constants::Player::MAX_HEALTH * newHealth;
healthBar->SetSubRect( sf::IntRect(healthBarPos.x,
healthBarPos.y - sd.dimensions.y,
healthBarPos.x + newLenght + 2,
healthBarPos.y) );
health = newHealth;
}
Window::instance()->getRW()->Draw(*healthBarAlpha);
Window::instance()->getRW()->Draw(*healthBar);
/** rysowanie prawej czesci belki **/
sprite = Sprite::instance()->getSprite("UI_HealtBarRight");
sprite.SetPosition(healthBarPos.x + newLenght + 2, healthBarPos.y);
Window::instance()->getRW()->Draw(sprite);
}
void UI::drawBombBar(int bombAmount) {
if(bombAmount < -1) bombAmount = 0;
else if(bombAmount > Constants::Bomb::MAX_AMOUNT + 1) bombAmount = Constants::Bomb::MAX_AMOUNT;
int i = 0, xTmp;
SpriteData sd = Sprite::instance()->getSpriteData("Bomb");
sprite = Sprite::instance()->getSprite("Bomb");
while(i < bombAmount) {
xTmp = bombBarPos.x + i * sd.dimensions.x + i * Constants::UI::BombBar::OFFSET();
sprite.SetPosition(xTmp, bombBarPos.y);
Window::instance()->getRW()->Draw(sprite);
++i;
}
sprite.SetColor( sf::Color(255, 255, 255, Constants::UI::BombBar::ALPHA) );
while(i < Constants::Bomb::MAX_AMOUNT) {
xTmp = bombBarPos.x + i * sd.dimensions.x + i * Constants::UI::BombBar::OFFSET();
sprite.SetPosition(xTmp, bombBarPos.y);
Window::instance()->getRW()->Draw(sprite);
++i;
}
}
void UI::drawFPS(float dt) {
static float time = 0;
static float frames = 0;
static float nFPS = 0;
if(fps == nullptr) {
fps = new sf::String;
fps->SetPosition(Constants::UI::FPS::X, Constants::UI::FPS::Y);
fps->SetColor(sf::Color::White);
fps->SetSize(Constants::UI::FPS::SIZE);
fps->SetText("FPS");
}
time += dt;
++frames;
if(time >= 1.0f) {
nFPS = frames / time;
frames = 0;
time = 0.0f;
std::ostringstream ss;
ss << nFPS;
fps->SetText(ss.str());
}
Window::instance()->getRW()->Draw(*fps);
}