Skip to content

Commit a2531a9

Browse files
committed
cleanup: de-duplicate code for layout generation
1 parent f543241 commit a2531a9

File tree

5 files changed

+45
-31
lines changed

5 files changed

+45
-31
lines changed

src/game/layout.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
#include "./layout.hpp"
3+
4+
std::vector<ui::Layout> game::get_layouts_for(u32 players, const ui::Layout& layout) {
5+
6+
std::vector<ui::Layout> layouts{};
7+
layouts.reserve(players);
8+
9+
if (players == 0) {
10+
throw std::runtime_error("An empty recording file isn't supported");
11+
} else if (players == 1) { // NOLINT(readability-else-after-return,llvm-else-after-return)
12+
layouts.push_back(ui::RelativeLayout{ layout, 0.02, 0.01, 0.96, 0.98 });
13+
} else if (players == 2) {
14+
layouts.push_back(ui::RelativeLayout{ layout, 0.02, 0.01, 0.46, 0.98 });
15+
layouts.push_back(ui::RelativeLayout{ layout, 0.52, 0.01, 0.46, 0.98 });
16+
} else {
17+
18+
//TODO(Totto): support bigger layouts than just 2
19+
throw std::runtime_error("At the moment only replays from up to two players are supported");
20+
}
21+
22+
return layouts;
23+
}

src/game/layout.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
#pragma once
4+
5+
#include <core/helper/types.hpp>
6+
7+
#include "helper/windows.hpp"
8+
#include "ui/layout.hpp"
9+
10+
11+
namespace game {
12+
OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] std::vector<ui::Layout>
13+
get_layouts_for(u32 players, const ui::Layout& layout);
14+
15+
}

src/game/meson.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ graphics_src_files += files(
99
'graphic_helpers.hpp',
1010
'grid.cpp',
1111
'grid.hpp',
12+
'layout.cpp',
13+
'layout.hpp',
14+
1215
'rotation.cpp',
1316
'rotation.hpp',
1417
'simulated_tetrion.cpp',

src/graphics/video_renderer.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22

33
#include "video_renderer.hpp"
4+
#include "game/layout.hpp"
45

56
#include <fmt/format.h>
67

@@ -33,25 +34,10 @@ void VideoRenderer::initialize_games(const std::filesystem::path& recording_path
3334
shapes::URect{ { 0, 0 }, m_size }
3435
};
3536

36-
std::vector<ui::Layout> layouts{};
37-
layouts.reserve(parameters.size());
38-
39-
if (parameters.empty()) {
40-
throw std::runtime_error("An empty recording file isn't supported");
41-
} else if (parameters.size() == 1) { // NOLINT(readability-else-after-return,llvm-else-after-return)
42-
layouts.push_back(ui::RelativeLayout{ layout, 0.02, 0.01, 0.96, 0.98 });
43-
} else if (parameters.size() == 2) {
44-
layouts.push_back(ui::RelativeLayout{ layout, 0.02, 0.01, 0.46, 0.98 });
45-
layouts.push_back(ui::RelativeLayout{ layout, 0.52, 0.01, 0.46, 0.98 });
46-
} else {
47-
48-
//TODO(Totto): support bigger layouts than just 2
49-
throw std::runtime_error("At the moment only replays from up to two players are supported");
50-
}
37+
std::vector<ui::Layout> layouts = game::get_layouts_for(parameters.size(), layout);
5138

5239
m_clock = std::make_shared<ManualClock>();
5340

54-
5541
for (decltype(parameters.size()) i = 0; i < parameters.size(); ++i) {
5642
auto [input, starting_parameters] = std::move(parameters.at(i));
5743

src/scenes/replay_game/replay_game.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "replay_game.hpp"
22
#include "../single_player_game/game_over.hpp"
33
#include "../single_player_game/pause.hpp"
4+
#include "game/layout.hpp"
45
#include "helper/constants.hpp"
56
#include "helper/graphic_utils.hpp"
67
#include "helper/music_utils.hpp"
@@ -21,21 +22,7 @@ namespace scenes {
2122
auto [parameters, information] = input::get_game_parameters_for_replay(service_provider, recording_path);
2223

2324

24-
std::vector<ui::Layout> layouts{};
25-
layouts.reserve(parameters.size());
26-
27-
if (parameters.empty()) {
28-
throw std::runtime_error("An empty recording file isn't supported");
29-
} else if (parameters.size() == 1) { // NOLINT(readability-else-after-return,llvm-else-after-return)
30-
layouts.push_back(ui::RelativeLayout{ layout, 0.02, 0.01, 0.96, 0.98 });
31-
} else if (parameters.size() == 2) {
32-
layouts.push_back(ui::RelativeLayout{ layout, 0.02, 0.01, 0.46, 0.98 });
33-
layouts.push_back(ui::RelativeLayout{ layout, 0.52, 0.01, 0.46, 0.98 });
34-
} else {
35-
36-
//TODO(Totto): support bigger layouts than just 2
37-
throw std::runtime_error("At the moment only replays from up to two players are supported");
38-
}
25+
std::vector<ui::Layout> layouts = game::get_layouts_for(parameters.size(), layout);
3926

4027
u32 simulation_frequency = constants::simulation_frequency;
4128
if (const auto stored_simulation_frequency = information.get_if<u32>("simulation_frequency");

0 commit comments

Comments
 (0)