Skip to content

Commit e71b6b1

Browse files
committed
[OPTIMIZE]: Reducing calls to sf::draw()
Performances issue
1 parent 1863572 commit e71b6b1

File tree

6 files changed

+132
-104
lines changed

6 files changed

+132
-104
lines changed

conf/default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"graphics": {
99
"width": 750,
1010
"height": 750,
11-
"frame-rate": 60
11+
"frame-rate": 100
1212
},
1313
"grid": {
1414
"rows": 25,

src/app.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ constexpr int WINDOW_DEFAULT_WIDTH{ 640 };
2626

2727
std::map<sf::Keyboard::Key, App::ACTION> _bindings;
2828
bool need_cleaning{ false };
29+
bool need_render{ false };
2930

3031
/*****************************************************************************/
3132
App&
@@ -98,7 +99,7 @@ App::update(void) noexcept
9899
case Event::MouseEntered:
99100
case Event::MouseMoved: {
100101
if (nullptr != _cell_cur)
101-
_cell_cur->remState(Cell::SELECTED);
102+
_cell_cur->remState(ICell::SELECTED);
102103

103104
auto mousePos{ Mouse::getPosition(*_window) };
104105

@@ -109,17 +110,17 @@ App::update(void) noexcept
109110
nullptr == _cell_cur)
110111
break;
111112

112-
_cell_cur->addState(Cell::SELECTED);
113+
_cell_cur->addState(ICell::SELECTED);
113114

114115
if (locked_click &&
115-
!(_cell_cur->getState() & (Cell::START_CELL | Cell::END_CELL))) {
116-
_cell_cur->addState(Cell::WALL);
116+
!(_cell_cur->getState() & (ICell::START_CELL | ICell::END_CELL))) {
117+
_cell_cur->addState(ICell::WALL);
117118
}
118119

119120
} break;
120121
case Event::MouseLeft: {
121122
if (nullptr != _cell_cur) {
122-
_cell_cur->remState(Cell::SELECTED);
123+
_cell_cur->remState(ICell::SELECTED);
123124
_cell_cur = nullptr;
124125
}
125126
} break;
@@ -142,30 +143,30 @@ App::update(void) noexcept
142143
switch (event.mouseButton.button) {
143144
case Mouse::Button::Left: {
144145
locked_click = false;
145-
if (!_cell_cur->hasState(Cell::START_CELL | Cell::END_CELL))
146-
_cell_cur->addState(Cell::WALL);
146+
if (!_cell_cur->hasState(ICell::START_CELL | ICell::END_CELL))
147+
_cell_cur->addState(ICell::WALL);
147148

148149
} break;
149150
case Mouse::Button::Right: {
150-
if (_cell_cur->hasState(Cell::WALL))
151+
if (_cell_cur->hasState(ICell::WALL))
151152
break;
152153

153154
if (_cell_cur == _cell_start) {
154-
_cell_cur->remState(Cell::START_CELL);
155+
_cell_cur->remState(ICell::START_CELL);
155156
_cell_start = nullptr;
156157
} else if (_cell_cur == _cell_end) {
157-
_cell_cur->remState(Cell::END_CELL);
158+
_cell_cur->remState(ICell::END_CELL);
158159
_cell_end = nullptr;
159160
} else if (nullptr == _cell_start) {
160161
_cell_start = _cell_cur;
161-
_cell_start->addState(Cell::START_CELL);
162+
_cell_start->addState(ICell::START_CELL);
162163
} else if (nullptr == _cell_end) {
163164
_cell_end = _cell_cur;
164-
_cell_end->addState(Cell::END_CELL);
165+
_cell_end->addState(ICell::END_CELL);
165166
} else {
166-
_cell_end->remState(Cell::END_CELL);
167+
_cell_end->remState(ICell::END_CELL);
167168
_cell_end = _cell_cur;
168-
_cell_end->addState(Cell::END_CELL);
169+
_cell_end->addState(ICell::END_CELL);
169170
}
170171
} break;
171172
default:
@@ -188,6 +189,15 @@ void
188189
App::render(void) noexcept
189190
{
190191
_window->clear();
192+
/*
193+
if (nullptr != _cell_cur) {
194+
auto cursor{ sf::RectangleShape() };
195+
cursor.setOutlineColor(Color::Green);
196+
cursor.setOutlineThickness(-3.f);
197+
_window->draw(cursor);
198+
}
199+
*/
200+
191201
_window->draw(*_grid);
192202
_window->display();
193203
}

src/app.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ class App
7070
std::unique_ptr<ui::Grid> _grid;
7171
std::unique_ptr<astar::Impl> _analyzer;
7272

73-
ui::Cell* _cell_start{ nullptr };
74-
ui::Cell* _cell_end{ nullptr };
75-
ui::Cell* _cell_cur{ nullptr };
73+
PathCell* _cell_start{ nullptr };
74+
PathCell* _cell_end{ nullptr };
75+
PathCell* _cell_cur{ nullptr };
7676

7777
std::string _what;
7878
std::string _conf_fileName;

src/grid.cpp

Lines changed: 74 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,58 @@ ICell::ICell(uint x, uint y) noexcept
2222
{}
2323

2424
/*****************************************************************************/
25-
void
25+
bool
2626
ICell::clear(void) noexcept
2727
{
28-
if (EMPTY != _state)
29-
_changed = true;
30-
_state = EMPTY;
28+
if (EMPTY != _state) {
29+
_state = EMPTY;
30+
return true;
31+
}
32+
return false;
3133
}
3234

3335
/*****************************************************************************/
34-
void
36+
bool
3537
ICell::clean(void) noexcept
3638
{
37-
if (_state & PATH)
38-
_changed = true;
39-
_state &= ~PATH;
39+
if (_state & PATH) {
40+
_state &= ~PATH;
41+
return true;
42+
}
43+
return false;
4044
}
4145

4246
/*****************************************************************************/
43-
void
47+
bool
4448
ICell::setState(int st) noexcept
4549
{
4650
if (st != _state) {
4751
_state = st;
48-
_changed = true;
52+
return true;
4953
}
54+
return false;
5055
}
5156

5257
/*****************************************************************************/
53-
void
58+
bool
5459
ICell::addState(State st) noexcept
5560
{
5661
if (!(_state & st)) {
5762
_state |= st;
58-
_changed = true;
63+
return true;
5964
}
65+
return false;
6066
}
6167

6268
/*****************************************************************************/
63-
void
69+
bool
6470
ICell::remState(State st) noexcept
6571
{
6672
if (_state & st) {
6773
_state &= ~st;
68-
_changed = true;
74+
return true;
6975
}
76+
return false;
7077
}
7178

7279
/*****************************************************************************/
@@ -76,21 +83,23 @@ PathCell::PathCell(uint x, uint y, PathCell* parent)
7683
{}
7784

7885
/*****************************************************************************/
79-
void
86+
bool
8087
PathCell::clean(void) noexcept
8188
{
82-
ICell::clean();
8389
_G = _H = 0;
8490
_parent = nullptr;
91+
92+
return ICell::clean();
8593
}
8694

8795
/*****************************************************************************/
88-
void
96+
bool
8997
PathCell::clear(void) noexcept
9098
{
91-
ICell::clear();
9299
_G = _H = 0;
93100
_parent = nullptr;
101+
102+
return ICell::clear();
94103
}
95104

96105
/*****************************************************************************/
@@ -101,60 +110,70 @@ PathCell::getScore() const noexcept
101110
}
102111

103112
/*****************************************************************************/
104-
Cell::Cell(uint x, uint y) noexcept
105-
: PathCell(x, y)
113+
Grid::Grid() noexcept
114+
: Graph<PathCell>()
115+
, _vertexes{ _width * _height * 4, Vertex() }
106116
{}
107117

108118
/*****************************************************************************/
109119
void
110-
Cell::update(void) noexcept
120+
Grid::draw(sf::RenderTarget& target, sf::RenderStates states) const
111121
{
112-
if (!_changed)
113-
return;
122+
static double cell_width{ 0 };
123+
static double cell_height{ 0 };
114124

115-
setOutlineColor(Color::Black);
116-
setOutlineThickness(-1.f);
125+
bool updt{ false };
126+
const auto new_width{ target.getSize().x / static_cast<double>(_width) };
127+
const auto new_height{ target.getSize().y / static_cast<double>(_height) };
117128

118-
if (EMPTY == _state) {
119-
setFillColor(Color(200, 200, 200, 250));
120-
return;
129+
if (new_width != cell_width || new_height != cell_height) {
130+
cell_width = new_width;
131+
cell_height = new_height;
132+
updt = true;
121133
}
122134

123-
if (_state & (START_CELL | END_CELL))
124-
setFillColor(Color(234, 24, 24));
125-
else if (_state & WALL)
126-
setFillColor(Color::Black);
127-
else if (_state & PATH)
128-
setFillColor(Color(32, 32, 228));
135+
for (uint i{ 0 }; i < _width; ++i)
136+
for (uint j{ 0 }; j < _height; ++j) {
137+
if (updt)
138+
updateCell(i,
139+
j,
140+
Vector2f(cell_width * i, cell_height * j),
141+
Vector2f(cell_width, cell_height));
129142

130-
if (_state & SELECTED) {
131-
setOutlineColor(Color::Green);
132-
setOutlineThickness(-3.f);
133-
}
143+
updateCellStyle(i, j);
144+
}
134145

135-
_changed = false;
146+
target.draw(_vertexes.data(), std::size(_vertexes), sf::Quads, states);
136147
}
137148

138-
/*****************************************************************************/
139-
Grid::Grid() noexcept
140-
: Graph<ui::Cell>()
141-
{}
142-
143149
/*****************************************************************************/
144150
void
145-
Grid::draw(sf::RenderTarget& target, sf::RenderStates states) const
151+
Grid::updateCellStyle(uint i, uint j) const noexcept
146152
{
147-
const auto cell_width{ target.getSize().x / static_cast<double>(_width) };
148-
const auto cell_height{ target.getSize().y / static_cast<double>(_height) };
153+
auto st{ _grid[i + j * _width].getState() };
154+
auto idx{ (i + j * _width) * 4 };
155+
sf::Color color{ 200, 200, 200, 250 };
156+
157+
if (st & (ICell::START_CELL | ICell::END_CELL)) {
158+
color = Color(220, 20, 20, 250);
159+
} else if (st & ICell::PATH) {
160+
color = Color(32, 32, 228);
161+
} else if (st & ICell::WALL) {
162+
color = Color::Black;
163+
}
149164

150-
for (size_t i{ 0 }; i < _width; ++i)
151-
for (size_t j{ 0 }; j < _height; ++j) {
152-
auto& cell{ _grid[i + j * _width] };
165+
for (auto it{ 0 }; it < 4; ++it)
166+
_vertexes[idx + it].color = color;
167+
}
153168

154-
cell.update();
155-
cell.setSize(Vector2f(cell_width, cell_height));
156-
cell.setPosition(Vector2f(cell_width * i, cell_height * j));
169+
/*****************************************************************************/
170+
void
171+
Grid::updateCell(uint i, uint j, Vector2f pos, Vector2f size) const noexcept
172+
{
173+
auto idx{ (i + j * _width) * 4 };
157174

158-
target.draw(cell, states);
159-
}
175+
_vertexes[idx].position = { pos.x, pos.y };
176+
_vertexes[idx + 1].position = { pos.x + size.x, pos.y };
177+
_vertexes[idx + 2].position = { pos.x + size.x, pos.y + size.y };
178+
_vertexes[idx + 3].position = { pos.x, pos.y + size.y };
160179
}

0 commit comments

Comments
 (0)