diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml new file mode 100644 index 00000000..1179a60d --- /dev/null +++ b/.github/workflows/docs.yml @@ -0,0 +1,21 @@ +name: Doxygen Generation + +on: + push: + branches: ["main"] + pull_request: + branches: ["main"] + workflow_dispatch: + +jobs: + generate-docs: + runs-on: ubuntu-22.04 + permissions: + contents: write + steps: + - uses: DenverCoder1/doxygen-github-pages-action@v1.2.0 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + branch: gh-pages + folder: docs + config_file: Doxyfile \ No newline at end of file diff --git a/.gitignore b/.gitignore index 2ddb38d7..073f82c4 100644 --- a/.gitignore +++ b/.gitignore @@ -86,3 +86,4 @@ cmake-build-* recordings/ /build-* /toolchains/ +docs/ diff --git a/CMakeLists.txt b/CMakeLists.txt index dc37e276..81e934b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,8 +57,8 @@ add_executable(oopetris src/random.hpp src/recording.hpp src/input_event.hpp - src/utils.hpp - src/utils.cpp + src/utils/utils.hpp + src/utils/utils.cpp src/command_line_arguments.hpp src/controls.hpp src/mino_stack.cpp diff --git a/Doxyfile b/Doxyfile new file mode 100644 index 00000000..cbf60674 --- /dev/null +++ b/Doxyfile @@ -0,0 +1,393 @@ +# Doxyfile 1.9.4 + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- +DOXYFILE_ENCODING = UTF-8 +PROJECT_NAME = "OOpetris" +PROJECT_NUMBER = +PROJECT_BRIEF = +PROJECT_LOGO = +OUTPUT_DIRECTORY = +CREATE_SUBDIRS = NO +CREATE_SUBDIRS_LEVEL = 8 +ALLOW_UNICODE_NAMES = NO +OUTPUT_LANGUAGE = English +BRIEF_MEMBER_DESC = YES +REPEAT_BRIEF = YES +ABBREVIATE_BRIEF = "The $name class" \ + "The $name widget" \ + "The $name file" \ + is \ + provides \ + specifies \ + contains \ + represents \ + a \ + an \ + the +ALWAYS_DETAILED_SEC = NO +INLINE_INHERITED_MEMB = NO +FULL_PATH_NAMES = YES +STRIP_FROM_PATH = +STRIP_FROM_INC_PATH = +SHORT_NAMES = NO +JAVADOC_AUTOBRIEF = NO +JAVADOC_BANNER = NO +QT_AUTOBRIEF = NO +MULTILINE_CPP_IS_BRIEF = NO +PYTHON_DOCSTRING = YES +INHERIT_DOCS = YES +SEPARATE_MEMBER_PAGES = NO +TAB_SIZE = 4 +ALIASES = +OPTIMIZE_OUTPUT_FOR_C = NO +OPTIMIZE_OUTPUT_JAVA = NO +OPTIMIZE_FOR_FORTRAN = NO +OPTIMIZE_OUTPUT_VHDL = NO +OPTIMIZE_OUTPUT_SLICE = NO +EXTENSION_MAPPING = +MARKDOWN_SUPPORT = YES +TOC_INCLUDE_HEADINGS = 5 +AUTOLINK_SUPPORT = YES +BUILTIN_STL_SUPPORT = NO +CPP_CLI_SUPPORT = NO +SIP_SUPPORT = NO +IDL_PROPERTY_SUPPORT = YES +DISTRIBUTE_GROUP_DOC = NO +GROUP_NESTED_COMPOUNDS = NO +SUBGROUPING = YES +INLINE_GROUPED_CLASSES = NO +INLINE_SIMPLE_STRUCTS = NO +TYPEDEF_HIDES_STRUCT = NO +LOOKUP_CACHE_SIZE = 0 +NUM_PROC_THREADS = 1 +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- +EXTRACT_ALL = NO +EXTRACT_PRIVATE = NO +EXTRACT_PRIV_VIRTUAL = NO +EXTRACT_PACKAGE = NO +EXTRACT_STATIC = NO +EXTRACT_LOCAL_CLASSES = YES +EXTRACT_LOCAL_METHODS = NO +EXTRACT_ANON_NSPACES = NO +RESOLVE_UNNAMED_PARAMS = YES +HIDE_UNDOC_MEMBERS = NO +HIDE_UNDOC_CLASSES = NO +HIDE_FRIEND_COMPOUNDS = NO +HIDE_IN_BODY_DOCS = NO +INTERNAL_DOCS = NO +CASE_SENSE_NAMES = YES +HIDE_SCOPE_NAMES = NO +HIDE_COMPOUND_REFERENCE= NO +SHOW_HEADERFILE = YES +SHOW_INCLUDE_FILES = YES +SHOW_GROUPED_MEMB_INC = NO +FORCE_LOCAL_INCLUDES = NO +INLINE_INFO = YES +SORT_MEMBER_DOCS = YES +SORT_BRIEF_DOCS = NO +SORT_MEMBERS_CTORS_1ST = NO +SORT_GROUP_NAMES = NO +SORT_BY_SCOPE_NAME = NO +STRICT_PROTO_MATCHING = NO +GENERATE_TODOLIST = YES +GENERATE_TESTLIST = YES +GENERATE_BUGLIST = YES +GENERATE_DEPRECATEDLIST= YES +ENABLED_SECTIONS = +MAX_INITIALIZER_LINES = 30 +SHOW_USED_FILES = YES +SHOW_FILES = YES +SHOW_NAMESPACES = YES +FILE_VERSION_FILTER = +LAYOUT_FILE = +CITE_BIB_FILES = +#--------------------------------------------------------------------------- +# Configuration options related to warning and progress messages +#--------------------------------------------------------------------------- +QUIET = NO +WARNINGS = YES +WARN_IF_UNDOCUMENTED = YES +WARN_IF_DOC_ERROR = YES +WARN_IF_INCOMPLETE_DOC = YES +WARN_NO_PARAMDOC = NO +WARN_AS_ERROR = NO +WARN_FORMAT = "$file:$line: $text" +WARN_LINE_FORMAT = "at line $line of file $file" +WARN_LOGFILE = +#--------------------------------------------------------------------------- +# Configuration options related to the input files +#--------------------------------------------------------------------------- +INPUT = src/ +INPUT_ENCODING = UTF-8 +FILE_PATTERNS = *.c \ + *.cc \ + *.cxx \ + *.cpp \ + *.c++ \ + *.java \ + *.ii \ + *.ixx \ + *.ipp \ + *.i++ \ + *.inl \ + *.idl \ + *.ddl \ + *.odl \ + *.h \ + *.hh \ + *.hxx \ + *.hpp \ + *.h++ \ + *.l \ + *.cs \ + *.d \ + *.php \ + *.php4 \ + *.php5 \ + *.phtml \ + *.inc \ + *.m \ + *.markdown \ + *.md \ + *.mm \ + *.dox \ + *.py \ + *.pyw \ + *.f90 \ + *.f95 \ + *.f03 \ + *.f08 \ + *.f18 \ + *.f \ + *.for \ + *.vhd \ + *.vhdl \ + *.ucf \ + *.qsf \ + *.ice +RECURSIVE = NO +EXCLUDE = +EXCLUDE_SYMLINKS = NO +EXCLUDE_PATTERNS = +EXCLUDE_SYMBOLS = +EXAMPLE_PATH = +EXAMPLE_PATTERNS = * +EXAMPLE_RECURSIVE = NO +IMAGE_PATH = +INPUT_FILTER = +FILTER_PATTERNS = +FILTER_SOURCE_FILES = NO +FILTER_SOURCE_PATTERNS = +USE_MDFILE_AS_MAINPAGE = +#--------------------------------------------------------------------------- +# Configuration options related to source browsing +#--------------------------------------------------------------------------- +SOURCE_BROWSER = NO +INLINE_SOURCES = NO +STRIP_CODE_COMMENTS = YES +REFERENCED_BY_RELATION = NO +REFERENCES_RELATION = NO +REFERENCES_LINK_SOURCE = YES +SOURCE_TOOLTIPS = YES +USE_HTAGS = NO +VERBATIM_HEADERS = YES +CLANG_ASSISTED_PARSING = NO +CLANG_ADD_INC_PATHS = YES +CLANG_OPTIONS = +CLANG_DATABASE_PATH = +#--------------------------------------------------------------------------- +# Configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- +ALPHABETICAL_INDEX = YES +IGNORE_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the HTML output +#--------------------------------------------------------------------------- +GENERATE_HTML = YES +HTML_OUTPUT = docs +HTML_FILE_EXTENSION = .html +HTML_HEADER = +HTML_FOOTER = +HTML_STYLESHEET = +HTML_EXTRA_STYLESHEET = +HTML_EXTRA_FILES = +HTML_COLORSTYLE_HUE = 220 +HTML_COLORSTYLE_SAT = 100 +HTML_COLORSTYLE_GAMMA = 80 +HTML_TIMESTAMP = NO +HTML_DYNAMIC_MENUS = YES +HTML_DYNAMIC_SECTIONS = NO +HTML_INDEX_NUM_ENTRIES = 100 +GENERATE_DOCSET = NO +DOCSET_FEEDNAME = "Doxygen generated docs" +DOCSET_FEEDURL = +DOCSET_BUNDLE_ID = org.doxygen.Project +DOCSET_PUBLISHER_ID = org.doxygen.Publisher +DOCSET_PUBLISHER_NAME = Publisher +GENERATE_HTMLHELP = NO +CHM_FILE = +HHC_LOCATION = +GENERATE_CHI = NO +CHM_INDEX_ENCODING = +BINARY_TOC = NO +TOC_EXPAND = NO +GENERATE_QHP = NO +QCH_FILE = +QHP_NAMESPACE = org.doxygen.Project +QHP_VIRTUAL_FOLDER = doc +QHP_CUST_FILTER_NAME = +QHP_CUST_FILTER_ATTRS = +QHP_SECT_FILTER_ATTRS = +QHG_LOCATION = +GENERATE_ECLIPSEHELP = NO +ECLIPSE_DOC_ID = org.doxygen.Project +DISABLE_INDEX = NO +GENERATE_TREEVIEW = NO +FULL_SIDEBAR = NO +ENUM_VALUES_PER_LINE = 4 +TREEVIEW_WIDTH = 250 +EXT_LINKS_IN_WINDOW = NO +OBFUSCATE_EMAILS = YES +HTML_FORMULA_FORMAT = png +FORMULA_FONTSIZE = 10 +FORMULA_TRANSPARENT = YES +FORMULA_MACROFILE = +USE_MATHJAX = NO +MATHJAX_VERSION = MathJax_2 +MATHJAX_FORMAT = HTML-CSS +MATHJAX_RELPATH = +MATHJAX_EXTENSIONS = +MATHJAX_CODEFILE = +SEARCHENGINE = YES +SERVER_BASED_SEARCH = NO +EXTERNAL_SEARCH = NO +SEARCHENGINE_URL = +SEARCHDATA_FILE = searchdata.xml +EXTERNAL_SEARCH_ID = +EXTRA_SEARCH_MAPPINGS = +#--------------------------------------------------------------------------- +# Configuration options related to the LaTeX output +#--------------------------------------------------------------------------- +GENERATE_LATEX = NO +LATEX_OUTPUT = latex +LATEX_CMD_NAME = +MAKEINDEX_CMD_NAME = makeindex +LATEX_MAKEINDEX_CMD = makeindex +COMPACT_LATEX = NO +PAPER_TYPE = a4 +EXTRA_PACKAGES = +LATEX_HEADER = +LATEX_FOOTER = +LATEX_EXTRA_STYLESHEET = +LATEX_EXTRA_FILES = +PDF_HYPERLINKS = YES +USE_PDFLATEX = YES +LATEX_BATCHMODE = NO +LATEX_HIDE_INDICES = NO +LATEX_BIB_STYLE = plain +LATEX_TIMESTAMP = NO +LATEX_EMOJI_DIRECTORY = +#--------------------------------------------------------------------------- +# Configuration options related to the RTF output +#--------------------------------------------------------------------------- +GENERATE_RTF = NO +RTF_OUTPUT = rtf +COMPACT_RTF = NO +RTF_HYPERLINKS = NO +RTF_STYLESHEET_FILE = +RTF_EXTENSIONS_FILE = +#--------------------------------------------------------------------------- +# Configuration options related to the man page output +#--------------------------------------------------------------------------- +GENERATE_MAN = NO +MAN_OUTPUT = man +MAN_EXTENSION = .3 +MAN_SUBDIR = +MAN_LINKS = NO +#--------------------------------------------------------------------------- +# Configuration options related to the XML output +#--------------------------------------------------------------------------- +GENERATE_XML = NO +XML_OUTPUT = xml +XML_PROGRAMLISTING = YES +XML_NS_MEMB_FILE_SCOPE = NO +#--------------------------------------------------------------------------- +# Configuration options related to the DOCBOOK output +#--------------------------------------------------------------------------- +GENERATE_DOCBOOK = NO +DOCBOOK_OUTPUT = docbook +#--------------------------------------------------------------------------- +# Configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- +GENERATE_AUTOGEN_DEF = NO +#--------------------------------------------------------------------------- +# Configuration options related to the Perl module output +#--------------------------------------------------------------------------- +GENERATE_PERLMOD = NO +PERLMOD_LATEX = NO +PERLMOD_PRETTY = YES +PERLMOD_MAKEVAR_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- +ENABLE_PREPROCESSING = YES +MACRO_EXPANSION = NO +EXPAND_ONLY_PREDEF = NO +SEARCH_INCLUDES = YES +INCLUDE_PATH = +INCLUDE_FILE_PATTERNS = +PREDEFINED = +EXPAND_AS_DEFINED = +SKIP_FUNCTION_MACROS = YES +#--------------------------------------------------------------------------- +# Configuration options related to external references +#--------------------------------------------------------------------------- +TAGFILES = +GENERATE_TAGFILE = +ALLEXTERNALS = NO +EXTERNAL_GROUPS = YES +EXTERNAL_PAGES = YES +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- +DIA_PATH = +HIDE_UNDOC_RELATIONS = YES +HAVE_DOT = YES +DOT_NUM_THREADS = 0 +DOT_FONTNAME = Helvetica +DOT_FONTSIZE = 10 +DOT_FONTPATH = +CLASS_GRAPH = YES +COLLABORATION_GRAPH = YES +GROUP_GRAPHS = YES +UML_LOOK = NO +UML_LIMIT_NUM_FIELDS = 10 +DOT_UML_DETAILS = NO +DOT_WRAP_THRESHOLD = 17 +TEMPLATE_RELATIONS = NO +INCLUDE_GRAPH = YES +INCLUDED_BY_GRAPH = YES +CALL_GRAPH = NO +CALLER_GRAPH = NO +GRAPHICAL_HIERARCHY = YES +DIRECTORY_GRAPH = YES +DIR_GRAPH_MAX_DEPTH = 1 +DOT_IMAGE_FORMAT = png +INTERACTIVE_SVG = NO +DOT_PATH = +DOTFILE_DIRS = +MSCFILE_DIRS = +DIAFILE_DIRS = +PLANTUML_JAR_PATH = +PLANTUML_CFG_FILE = +PLANTUML_INCLUDE_PATH = +DOT_GRAPH_MAX_NODES = 50 +MAX_DOT_GRAPH_DEPTH = 0 +DOT_TRANSPARENT = NO +DOT_MULTI_TARGETS = NO +GENERATE_LEGEND = YES +DOT_CLEANUP = YES diff --git a/src/application.cpp b/src/application.cpp index 8ec150dd..c752a8ea 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -11,33 +11,11 @@ Application::Application( m_renderer{ m_window }, m_command_line_arguments{ std::move(command_line_arguments) } { } -Application::Application( - const std::string& title, - int x, - int y, - int width, - int height, - CommandLineArguments command_line_arguments -) - : m_window{ title, x, y, width, height }, - m_renderer{ m_window }, - m_command_line_arguments{ std::move(command_line_arguments) } { } - Application::Application(const std::string& title, WindowPosition position, CommandLineArguments command_line_arguments) : m_window{ title, position }, m_renderer{ m_window }, m_command_line_arguments{ std::move(command_line_arguments) } { } -Application::Application( - const std::string& title, - int x, - int y, - CommandLineArguments command_line_arguments -) - : m_window{ title, x, y, }, - m_renderer{ m_window }, - m_command_line_arguments{ std::move(command_line_arguments) } { } - void Application::run() { const auto start_time = elapsed_time(); s_num_steps_simulated = 0; diff --git a/src/application.hpp b/src/application.hpp index 24eb599d..1aaf80c5 100644 --- a/src/application.hpp +++ b/src/application.hpp @@ -8,6 +8,10 @@ #include "types.hpp" #include "window.hpp" +/** + * @brief Abstract base for an application. Inherit from this struct to create an application and customize the + * behavior. Do not create multiple instances of this struct. + */ struct Application : public EventListener { private: SdlContext context; @@ -20,7 +24,15 @@ struct Application : public EventListener { protected: EventDispatcher m_event_dispatcher; -public: +protected: + /** + * @brief Creates a new application. + * @param title The text in the window title bar. + * @param position The position where the window should spawn at. + * @param width The width of the window. + * @param height The height of the window. + * @param command_line_arguments The command line arguments. + */ Application( const std::string& title, WindowPosition position, @@ -29,46 +41,74 @@ struct Application : public EventListener { CommandLineArguments command_line_arguments ); - Application( - const std::string& title, - int x, - int y, - int width, - int height, - CommandLineArguments command_line_arguments - ); - Application(const std::string& title, WindowPosition position, CommandLineArguments command_line_arguments); - Application(const std::string& title, int x, int y, CommandLineArguments command_line_arguments); - +public: Application(const Application&) = delete; Application& operator=(const Application&) = delete; + /** + * @brief Starts the main game loop. + */ void run(); + /** + * @brief Obtains the renderer for this application's window. + * @return The renderer. + */ [[nodiscard]] const Renderer& renderer() const { return m_renderer; } + /** + * @brief Obtains the window for this application. + * @return The window. + */ [[nodiscard]] const Window& window() const { return m_window; } + /** + * @brief Returns the elapsed time (in seconds) since the SDL2 library has been initialized. + * @return The elapsed time in seconds. + */ static double elapsed_time() { return static_cast(SDL_GetTicks()) / 1000.0; } + /** + * @brief Returns the index of the current simulation step. The application tries to execute a certain number + * of simulation steps per second. This is specified in the command line arguments (passed to the constructor). + * @return The index of the current simulation step. + */ static u64 simulation_step_index() { return s_num_steps_simulated; } + /** + * @brief Implements the EventListener. If the escape key is pressed or the windows is closed, the application + * gets closed. + * @param event The SDL event to handle. + */ void handle_event(const SDL_Event& event) override; protected: + /** + * @brief This function gets called from within the main game loop (i.e. by the run() function). Override this + * function to specify what should be done in every simulation step. + */ virtual void update() = 0; + + /** + * @brief Clears the contents of the window with a black color. Override this function to render additional + * window contents. + */ virtual void render() const; + /** + * @brief Returns the CommandLineArguments instance passed to this struct's constructor. + * @return The command line arguments. + */ [[nodiscard]] const CommandLineArguments& command_line_arguments() const { return m_command_line_arguments; } diff --git a/src/meson.build b/src/meson.build index b4511124..715277b7 100644 --- a/src/meson.build +++ b/src/meson.build @@ -41,8 +41,6 @@ src_files += files( 'random.hpp', 'recording.hpp', 'input_event.hpp', - 'utils.hpp', - 'utils.cpp', 'command_line_arguments.hpp', 'controls.hpp', 'mino_stack.hpp', @@ -52,3 +50,5 @@ src_files += files( ) inc_dirs += include_directories('.') + +subdir('utils') diff --git a/src/random.hpp b/src/random.hpp index 11bf1d06..b3debb1c 100644 --- a/src/random.hpp +++ b/src/random.hpp @@ -1,6 +1,6 @@ #pragma once -#include "utils.hpp" +#include "utils/utils.hpp" #include struct Random { diff --git a/src/recording.hpp b/src/recording.hpp index 1df2515b..ba340e45 100644 --- a/src/recording.hpp +++ b/src/recording.hpp @@ -5,7 +5,7 @@ #include "tetrion.hpp" #include "tetrion_snapshot.hpp" #include "types.hpp" -#include "utils.hpp" +#include "utils/utils.hpp" #include #include #include diff --git a/src/tetrion.hpp b/src/tetrion.hpp index 81515509..be62a155 100644 --- a/src/tetrion.hpp +++ b/src/tetrion.hpp @@ -8,7 +8,7 @@ #include "tetromino.hpp" #include "text.hpp" #include "types.hpp" -#include "utils.hpp" +#include "utils/utils.hpp" #include #include #include diff --git a/src/tetrion_snapshot.hpp b/src/tetrion_snapshot.hpp index 2df9b018..127a49cf 100644 --- a/src/tetrion_snapshot.hpp +++ b/src/tetrion_snapshot.hpp @@ -2,7 +2,7 @@ #include "application.hpp" #include "tetrion.hpp" -#include "utils.hpp" +#include "utils/utils.hpp" #include #include #include diff --git a/src/tetris_application.hpp b/src/tetris_application.hpp index 8a786886..f8ae8a66 100644 --- a/src/tetris_application.hpp +++ b/src/tetris_application.hpp @@ -11,7 +11,11 @@ #include #include -struct TetrisApplication : public Application { +/** + * @brief This struct inherits from Application and implements the game's functionality. Instantiate this struct + * and call run() on it to start the main game loop. + */ +struct TetrisApplication final : public Application { private: using TetrionHeaders = std::vector; @@ -27,44 +31,134 @@ struct TetrisApplication : public Application { static constexpr int width = 800; static constexpr int height = 600; + /** + * @brief Constructs a TetrisApplication instance. + * @param command_line_arguments The command line arguments passed to the executable. + */ explicit TetrisApplication(CommandLineArguments command_line_arguments); protected: + /** + * @brief Calls the update() functions on all elements of m_inputs. + */ void update_inputs(); void late_update_inputs(); + + /** + * @brief Calls the update() functions on all elements of m_tetrions. + */ void update_tetrions(); + + /** + * @brief Updates all inputs and all tetrions. + */ void update() override; + + /** + * @brief Renders the contents of the window. + */ void render() const override; private: + /** + * @brief Creates an input from given controls. + * @param controls The controls (e.g. keyboard bindings) to apply for this control. + * @param associated_tetrion The tetrion that is targeted by this input. + * @param on_event_callback Callback that is invoked when an input event happens. + * @return The input. + */ [[nodiscard]] std::unique_ptr create_input(Controls controls, Tetrion* associated_tetrion, Input::OnEventCallback on_event_callback); + /** + * @brief Creates an input that replays a recorded game. + * @param tetrion_index The index of the tetrion that is targeted by this input. + * @param recording_reader A pointer to the RecordingReader that holds the recorded game. + * @param associated_tetrion The tetrion that is targeted by this input. + * @param on_event_callback Callback that is invoked when an input event happens. + * @return The input. + */ [[nodiscard]] static std::unique_ptr create_replay_input( u8 tetrion_index, - RecordingReader* constrecording_reader, - Tetrion *constassociated_tetrion, + RecordingReader* recording_reader, + Tetrion* associated_tetrion, Input::OnEventCallback on_event_callback ); + /** + * @brief Creates a callback that can be passed to create_input() or create_recording_input(). If a game is being + * recorded, the returned callback stores all events in the associated RecordingWriter (m_recording_writer). + * Otherwise, a callback that does nothing is returned. + * @param tetrion_index The index of the tetrion that shall be recorded. + * @return The callback. + */ [[nodiscard]] Input::OnEventCallback create_on_event_callback(int tetrion_index); + /** + * @brief Tries to load the settings from settings.json and stores them into m_settings. Applies default + * values upon failure. + */ void try_load_settings(); + /** + * @brief Returns true if the path to a recorded game was specified as command line argument, false otherwise. + * @return Whether a replay shall be replayed. + */ [[nodiscard]] bool is_replay_mode() const; + /** + * @brief Returns true if the current game shall be recorded, false otherwise. The game shall be recorded if it's + * not replaying a recorded game. + * @return Whether the game shall be recorded. + */ [[nodiscard]] bool game_is_recorded() const; + /** + * @brief Returns the random seed to be used for a given tetrion. The choice is based on whether a recorded + * game is being replayed (then the seed of the recording is used) or not (then the passed common_seed is used). + * @param tetrion_index The index of the tetrion this seed shall be used for. + * @param common_seed The common random seed to be used if the application is not replaying a recorded game. + * @return The seed for the tetrion. + */ [[nodiscard]] Random::Seed seed_for_tetrion(u8 tetrion_index, Random::Seed common_seed) const; + /** + * @brief Returns the starting level for a given tetrion. The choice is based on whether a recorded game is + * being replayed (then the starting level of the recording is used) or not (then the starting level is taken + * from the command line arguments (maybe its default value). + * @param tetrion_index The index of the tetrion this starting level shall be used for. + * @return The starting level. + */ [[nodiscard]] auto starting_level_for_tetrion(u8 tetrion_index) const -> decltype(CommandLineArguments::starting_level); + /** + * @brief Creates the TetrionHeader instances to be used when a game shall be recorded. This data is part of the + * header of the recording. + * @param seeds The seeds of all tetrions. + * @return The tetrion headers for all tetrions. + */ [[nodiscard]] TetrionHeaders create_tetrion_headers(std::span seeds) const; + /** + * @brief Creates the subdirectory for recorded games (if necessary) and instantiates a RecordingWriter to start + * recording the game. The output filename is based on the current date and time. + * @param tetrion_headers The tetrion headers of all tetrions. + * @return The RecordingWriter. + */ [[nodiscard]] static std::unique_ptr create_recording_writer(TetrionHeaders tetrion_headers); + /** + * @brief Creates seeds for a given number of tetrions. + * @param num_tetrions The number of tetrions. + * @return The seeds for all tetrions. + */ [[nodiscard]] std::vector create_seeds(u8 num_tetrions) const; + /** + * @brief Returns an tl::optional containing a pointer to the object managed by m_recording_writer if present. + * Otherwise returns an empty tl::optional. + * @return The pointer to the RecordingWriter as an tl::optional. + */ [[nodiscard]] tl::optional recording_writer_optional(); }; diff --git a/src/utils/meson.build b/src/utils/meson.build new file mode 100644 index 00000000..4eab4064 --- /dev/null +++ b/src/utils/meson.build @@ -0,0 +1,6 @@ +src_files += files( + 'utils.hpp', + 'utils.cpp', +) + +inc_dirs += include_directories('.') diff --git a/src/utils.cpp b/src/utils/utils.cpp similarity index 96% rename from src/utils.cpp rename to src/utils/utils.cpp index dff26bd2..d5824a4e 100644 --- a/src/utils.cpp +++ b/src/utils/utils.cpp @@ -1,7 +1,7 @@ #include "utils.hpp" +#include "spdlog/spdlog.h" #include #include -#include namespace utils { [[nodiscard]] std::string current_date_time_iso8601() { diff --git a/src/utils.hpp b/src/utils/utils.hpp similarity index 55% rename from src/utils.hpp rename to src/utils/utils.hpp index 57905602..7100bc88 100644 --- a/src/utils.hpp +++ b/src/utils/utils.hpp @@ -1,7 +1,6 @@ #pragma once -#include "types.hpp" -#include "utils.hpp" +#include "../types.hpp" #include #include #include @@ -15,8 +14,20 @@ namespace utils { template concept integral = std::is_integral_v; + /** + * @brief Returns the current date and time as a string in ISO 8601 format without using any separators except for + * 'T' between the date and the time. + * @return The current date and time string. + */ [[nodiscard]] std::string current_date_time_iso8601(); + /** + * @brief Converts between big endian and little endian. This function is needed since not all compilers support + * std::byteswap as of yet. + * @tparam Integral The type of the value to convert. + * @param value The value to convert. + * @return The converted value. + */ template [[nodiscard]] constexpr Integral byte_swap(Integral value) noexcept { // based on source: slartibartswift @@ -29,6 +40,12 @@ namespace utils { return result; } + /** + * @brief Converts from the machine's native byte order to little endian. On a little endian machine, this function + * does return the input value. + * @param value A value in the machine's native byte order. + * @return The value in little endian. + */ [[nodiscard]] constexpr auto to_little_endian(integral auto value) { if constexpr (std::endian::native == std::endian::little) { return value; @@ -37,6 +54,12 @@ namespace utils { } } + /** + * @brief Converts from little endian to the machine's native byte order. On a little endian machine, this function + * does return the input value. + * @param value A value in little endian. + * @return The value in the machine's native byte order. + */ [[nodiscard]] constexpr auto from_little_endian(integral auto value) { return to_little_endian(value); }