Skip to content

Commit 401cb1f

Browse files
committed
fix target fps:
always simulate at 60 fps, an exception is, when a replay says, it uses another simulation frequency (which is not part of the standard, since this is stored in the additional information section of the recording format) - we simulate 60 times per second, even if the display / render frequency may be less or more - allow faster simulation frequencies per se, since later we might add a speedup, alias play speed for recordings or similar. - since in recordings we store the simulation index it is crucial to know which simulation_frequency we used, but it's not an additional field, so that we don#t break backwards compatibility, for that I added the additional information format earlier
1 parent cee53ca commit 401cb1f

File tree

9 files changed

+35
-11
lines changed

9 files changed

+35
-11
lines changed

src/game/game.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ Game::Game(
88
ServiceProvider* const service_provider,
99
const std::shared_ptr<input::GameInput>& input,
1010
const tetrion::StartingParameters& starting_parameters,
11+
u32 simulation_frequency,
1112
const ui::Layout& layout,
1213
bool is_top_level
1314
)
1415
: ui::Widget{ layout, ui::WidgetType::Component, is_top_level },
15-
m_clock_source{ std::make_unique<LocalClock>(starting_parameters.target_fps) },
16+
m_clock_source{ std::make_unique<LocalClock>(simulation_frequency) },
1617
m_input{ input } {
1718

1819

src/game/game.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct Game : public ui::Widget {
2121
ServiceProvider* service_provider,
2222
const std::shared_ptr<input::GameInput>& input,
2323
const tetrion::StartingParameters& starting_parameters,
24+
u32 simulation_frequency,
2425
const ui::Layout& layout,
2526
bool is_top_level
2627
);

src/helper/constants.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ namespace constants {
1515
constexpr auto program_name = StaticString{ STRINGIFY(OOPETRIS_NAME) };
1616
constexpr auto author = StaticString{ STRINGIFY(OOPETRIS_AUTHOR) };
1717
constexpr auto version = StaticString{ STRINGIFY(OOPETRIS_VERSION) };
18-
constexpr auto music_change_level = 30;
18+
constexpr u32 music_change_level = 30;
1919
constexpr auto recordings_directory = "recordings";
20+
constexpr u32 simulation_frequency = 60;
2021

2122
#if not defined(AUDIO_QUALITY)
2223
#define AUDIO_QUALITY 0 // NOLINT(cppcoreguidelines-macro-usage)

src/input/input_creator.cpp

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

22

33
#include "input_creator.hpp"
4+
#include "additional_information.hpp"
45
#include "helper/command_line_arguments.hpp"
56
#include "helper/date.hpp"
67
#include "helper/errors.hpp"
@@ -44,7 +45,8 @@ namespace {
4445
} // namespace
4546

4647

47-
[[nodiscard]] std::vector<input::AdditionalInfo> input::get_game_parameters_for_replay(
48+
[[nodiscard]] std::pair<std::vector<input::AdditionalInfo>, recorder::AdditionalInformation>
49+
input::get_game_parameters_for_replay(
4850
ServiceProvider* const service_provider,
4951
const std::filesystem::path& recording_path
5052
) {
@@ -67,6 +69,7 @@ namespace {
6769

6870
const auto tetrion_headers = recording_reader->tetrion_headers();
6971

72+
7073
result.reserve(tetrion_headers.size());
7174

7275
for (u8 tetrion_index = 0; tetrion_index < static_cast<u8>(tetrion_headers.size()); ++tetrion_index) {
@@ -87,7 +90,7 @@ namespace {
8790
}
8891

8992

90-
return result;
93+
return { result, recording_reader->information() };
9194
}
9295

9396

src/input/input_creator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace input {
3636

3737
using AdditionalInfo = std::tuple<std::shared_ptr<input::GameInput>, tetrion::StartingParameters>;
3838

39-
[[nodiscard]] std::vector<input::AdditionalInfo>
39+
[[nodiscard]] std::pair<std::vector<input::AdditionalInfo>, recorder::AdditionalInformation>
4040
get_game_parameters_for_replay(ServiceProvider* service_provider, const std::filesystem::path& recording_path);
4141

4242
[[nodiscard]] helper::expected<input::AdditionalInfo, std::string> get_single_player_game_parameters(

src/recordings/additional_information.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,18 @@ void recorder::AdditionalInformation::add_value(const std::string& key, const In
470470

471471
helper::optional<recorder::InformationValue> recorder::AdditionalInformation::get(const std::string& key) const {
472472

473-
if (not m_values.contains(key)) {
473+
if (not has(key)) {
474474
return helper::nullopt;
475475
}
476476

477477
return m_values.at(key);
478478
}
479479

480+
[[nodiscard]] bool recorder::AdditionalInformation::has(const std::string& key) const {
481+
return m_values.contains(key);
482+
}
483+
484+
480485
[[nodiscard]] helper::expected<std::vector<char>, std::string> recorder::AdditionalInformation::to_bytes() const {
481486
auto bytes = std::vector<char>{};
482487

src/recordings/additional_information.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ namespace recorder {
149149

150150
[[nodiscard]] helper::optional<InformationValue> get(const std::string& key) const;
151151

152+
[[nodiscard]] bool has(const std::string& key) const;
153+
152154
template<typename T>
153155
[[nodiscard]] helper::optional<T> get_if(const std::string& key) const {
154156

155-
if (not m_values.contains(key)) {
157+
if (not has(key)) {
156158
return helper::nullopt;
157159
}
158160

src/scenes/replay_game/replay_game.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace scenes {
1616
)
1717
: Scene{ service_provider, layout } {
1818

19-
auto parameters = input::get_game_parameters_for_replay(service_provider, recording_path);
19+
auto [parameters, information] = input::get_game_parameters_for_replay(service_provider, recording_path);
2020

2121

2222
std::vector<ui::Layout> layouts{};
@@ -35,11 +35,18 @@ namespace scenes {
3535
throw std::runtime_error("At the moment only replays from up to two players are supported");
3636
}
3737

38+
u32 simulation_frequency = constants::simulation_frequency;
39+
if (const auto stored_simulation_frequency = information.get_if<u32>("simulation_frequency");
40+
stored_simulation_frequency.has_value()) {
41+
simulation_frequency = stored_simulation_frequency.value();
42+
}
43+
44+
3845
for (decltype(parameters.size()) i = 0; i < parameters.size(); ++i) {
3946
auto [input, starting_parameters] = std::move(parameters.at(i));
4047

4148
m_games.emplace_back(std::make_unique<Game>(
42-
service_provider, std::move(input), starting_parameters, layouts.at(i), false
49+
service_provider, std::move(input), starting_parameters, simulation_frequency, layouts.at(i), false
4350
));
4451
}
4552

src/scenes/single_player_game/single_player_game.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ namespace scenes {
1818
: Scene{ service_provider, layout } {
1919

2020
const auto date = date::ISO8601Date::now();
21+
const auto simulation_frequency = constants::simulation_frequency;
22+
2123

2224
recorder::AdditionalInformation additional_information{};
25+
additional_information.add<u32>("simulation_frequency", simulation_frequency);
2326
additional_information.add("mode", "single_player");
2427
additional_information.add("platform", std::string{ magic_enum::enum_name(utils::get_platform()) });
2528
additional_information.add("date", date.value());
2629
//TODO(Totto): add more information, if logged in
2730

28-
2931
auto result =
3032
input::get_single_player_game_parameters(service_provider, std::move(additional_information), date);
3133

@@ -38,7 +40,9 @@ namespace scenes {
3840

3941
auto [input, starting_parameters] = result.value();
4042

41-
m_game = std::make_unique<Game>(service_provider, std::move(input), starting_parameters, layout, true);
43+
m_game = std::make_unique<Game>(
44+
service_provider, std::move(input), starting_parameters, simulation_frequency, layout, true
45+
);
4246

4347

4448
#if defined(_HAVE_DISCORD_SDK)

0 commit comments

Comments
 (0)