1010#include < span>
1111#include < spdlog/spdlog.h>
1212
13- struct TetrisApplication : public Application {
13+ /* *
14+ * @brief This struct inherits from Application and implements the game's functionality. Instantiate this struct
15+ * and call run() on it to start the main game loop.
16+ */
17+ struct TetrisApplication final : public Application {
1418private:
1519 using TetrionHeaders = std::vector<Recording::TetrionHeader>;
1620
@@ -26,44 +30,135 @@ struct TetrisApplication : public Application {
2630 static constexpr int width = 800 ;
2731 static constexpr int height = 600 ;
2832
33+ /* *
34+ * @brief Constructs a TetrisApplication instance.
35+ * @param command_line_arguments The command line arguments passed to the executable.
36+ */
2937 explicit TetrisApplication (CommandLineArguments command_line_arguments);
3038
3139protected:
40+ /* *
41+ * @brief Calls the update() functions on all elements of m_inputs.
42+ */
3243 void update_inputs ();
3344 void late_update_inputs ();
45+
46+ /* *
47+ * @brief Calls the update() functions on all elements of m_tetrions.
48+ */
3449 void update_tetrions ();
50+
51+ /* *
52+ * @brief Updates all inputs and all tetrions.
53+ */
3554 void update () override ;
55+
56+ /* *
57+ * @brief Renders the contents of the window.
58+ */
3659 void render () const override ;
3760
3861private:
62+ /* *
63+ * @brief Creates an input from given controls.
64+ * @param controls The controls (e.g. keyboard bindings) to apply for this control.
65+ * @param associated_tetrion The tetrion that is targeted by this input.
66+ * @param on_event_callback Callback that is invoked when an input event happens.
67+ * @return The input.
68+ */
3969 [[nodiscard]] std::unique_ptr<Input>
4070 create_input (Controls controls, Tetrion* associated_tetrion, Input::OnEventCallback on_event_callback);
4171
4272 [[nodiscard]] static std::unique_ptr<Input> create_replay_input (
73+ /* *
74+ * @brief Creates an input that replays a recorded game.
75+ * @param tetrion_index The index of the tetrion that is targeted by this input.
76+ * @param recording_reader A pointer to the RecordingReader that holds the recorded game.
77+ * @param associated_tetrion The tetrion that is targeted by this input.
78+ * @param on_event_callback Callback that is invoked when an input event happens.
79+ * @return The input.
80+ */
81+ [[nodiscard]] static std::unique_ptr<Input> create_recording_input (
4382 u8 tetrion_index,
4483 RecordingReader* constrecording_reader,
4584 Tetrion *constassociated_tetrion,
4685 Input::OnEventCallback on_event_callback
4786 );
4887
88+ /* *
89+ * @brief Creates a callback that can be passed to create_input() or create_recording_input(). If a game is being
90+ * recorded, the returned callback stores all events in the associated RecordingWriter (m_recording_writer).
91+ * Otherwise, a callback that does nothing is returned.
92+ * @param tetrion_index The index of the tetrion that shall be recorded.
93+ * @return The callback.
94+ */
4995 [[nodiscard]] Input::OnEventCallback create_on_event_callback (int tetrion_index);
5096
97+ /* *
98+ * @brief Tries to load the settings from settings.json and stores them into m_settings. Applies default
99+ * values upon failure.
100+ */
51101 void try_load_settings ();
52102
103+ /* *
104+ * @brief Returns true if the path to a recorded game was specified as command line argument, false otherwise.
105+ * @return Whether a replay shall be replayed.
106+ */
53107 [[nodiscard]] bool is_replay_mode () const ;
54108
109+ /* *
110+ * @brief Returns true if the current game shall be recorded, false otherwise. The game shall be recorded if it's
111+ * not replaying a recorded game.
112+ * @return Whether the game shall be recorded.
113+ */
55114 [[nodiscard]] bool game_is_recorded () const ;
56115
116+ /* *
117+ * @brief Returns the random seed to be used for a given tetrion. The choice is based on whether a recorded
118+ * game is being replayed (then the seed of the recording is used) or not (then the passed common_seed is used).
119+ * @param tetrion_index The index of the tetrion this seed shall be used for.
120+ * @param common_seed The common random seed to be used if the application is not replaying a recorded game.
121+ * @return The seed for the tetrion.
122+ */
57123 [[nodiscard]] Random::Seed seed_for_tetrion (u8 tetrion_index, Random::Seed common_seed) const ;
58124
125+ /* *
126+ * @brief Returns the starting level for a given tetrion. The choice is based on whether a recorded game is
127+ * being replayed (then the starting level of the recording is used) or not (then the starting level is taken
128+ * from the command line arguments (maybe its default value).
129+ * @param tetrion_index The index of the tetrion this starting level shall be used for.
130+ * @return The starting level.
131+ */
59132 [[nodiscard]] auto starting_level_for_tetrion (u8 tetrion_index) const
60133 -> decltype(CommandLineArguments::starting_level);
61134
135+ /* *
136+ * @brief Creates the TetrionHeader instances to be used when a game shall be recorded. This data is part of the
137+ * header of the recording.
138+ * @param seeds The seeds of all tetrions.
139+ * @return The tetrion headers for all tetrions.
140+ */
62141 [[nodiscard]] TetrionHeaders create_tetrion_headers (std::span<const Random::Seed> seeds) const ;
63142
143+ /* *
144+ * @brief Creates the subdirectory for recorded games (if necessary) and instantiates a RecordingWriter to start
145+ * recording the game. The output filename is based on the current date and time.
146+ * @param tetrion_headers The tetrion headers of all tetrions.
147+ * @return The RecordingWriter.
148+ */
64149 [[nodiscard]] static std::unique_ptr<RecordingWriter> create_recording_writer (TetrionHeaders tetrion_headers);
65150
151+ /* *
152+ * @brief Creates seeds for a given number of tetrions.
153+ * @param num_tetrions The number of tetrions.
154+ * @return The seeds for all tetrions.
155+ */
66156 [[nodiscard]] std::vector<Random::Seed> create_seeds (u8 num_tetrions) const ;
67157
158+ /* *
159+ * @brief Returns an tl::optional containing a pointer to the object managed by m_recording_writer if present.
160+ * Otherwise returns an empty tl::optional.
161+ * @return The pointer to the RecordingWriter as an tl::optional.
162+ */
68163 [[nodiscard]] tl::optional<RecordingWriter*> recording_writer_optional ();
69164};
0 commit comments