diff --git a/src/media/CMakeLists.txt b/src/media/CMakeLists.txt index f0a7575bd..fc4c6202a 100644 --- a/src/media/CMakeLists.txt +++ b/src/media/CMakeLists.txt @@ -2,6 +2,8 @@ target_sources(SRB2SDL2 PRIVATE audio_encoder.hpp container.hpp encoder.hpp + options.cpp + options.hpp video_encoder.hpp video_frame.hpp ) diff --git a/src/media/options.cpp b/src/media/options.cpp new file mode 100644 index 000000000..b933d9ad0 --- /dev/null +++ b/src/media/options.cpp @@ -0,0 +1,118 @@ +// RING RACERS +//----------------------------------------------------------------------------- +// Copyright (C) 2023 by James Robert Roman +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- + +#include +#include +#include + +#include + +#include "../cxxutil.hpp" +#include "../m_fixed.h" +#include "options.hpp" + +using namespace srb2::media; + +static std::vector g_cvars; + +Options::Options(const char* prefix, map_t map) : prefix_(prefix), map_(map) +{ + for (auto& [suffix, cvar] : map_) + { + cvar.name = strdup(fmt::format("{}_{}", prefix_, suffix).c_str()); + g_cvars.emplace_back(&cvar); + } +} + +const consvar_t& Options::cvar(const char* option) const +{ + const consvar_t& cvar = map_.at(option); + + SRB2_ASSERT(cvar.string != nullptr); + + return cvar; +} + +template <> +int Options::get(const char* option) const +{ + return cvar(option).value; +} + +template <> +float Options::get(const char* option) const +{ + return FixedToFloat(cvar(option).value); +} + +static consvar_t range_cvar(const char* default_value, int32_t min, int32_t max, int32_t flags = 0) +{ + return CVAR_INIT( + nullptr, + default_value, + CV_SAVE | flags, + new CV_PossibleValue_t[] {{min, "MIN"}, {max, "MAX"}, {}}, + nullptr + ); +} + +template <> +consvar_t Options::range(const char* default_value, float min, float max) +{ + return range_cvar(default_value, FloatToFixed(min), FloatToFixed(max), CV_FLOAT); +} + +template <> +consvar_t Options::range_min(const char* default_value, float min) +{ + return range_cvar(default_value, FloatToFixed(min), INT32_MAX); +} + +template <> +consvar_t Options::range(const char* default_value, int min, int max) +{ + return range_cvar(default_value, min, max); +} + +template <> +consvar_t Options::range_min(const char* default_value, int min) +{ + return range_cvar(default_value, min, INT32_MAX); +} + +template <> +consvar_t Options::value_map(const char* default_value, std::map values) +{ + auto* arr = new CV_PossibleValue_t[values.size() + 1]; + + std::size_t i = 0; + + for (const auto& [k, v] : values) + { + arr[i].value = v; + arr[i].strvalue = k; + + i++; + } + + arr[i].value = 0; + arr[i].strvalue = nullptr; + + return CVAR_INIT(nullptr, default_value, CV_SAVE, arr, nullptr); +} + +void srb2::media::register_options() +{ + for (auto cvar : g_cvars) + { + CV_RegisterVar(cvar); + } + + g_cvars = {}; +} diff --git a/src/media/options.hpp b/src/media/options.hpp new file mode 100644 index 000000000..d7ce7fd2a --- /dev/null +++ b/src/media/options.hpp @@ -0,0 +1,51 @@ +// RING RACERS +//----------------------------------------------------------------------------- +// Copyright (C) 2023 by James Robert Roman +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- + +#ifndef __SRB2_MEDIA_OPTIONS_HPP__ +#define __SRB2_MEDIA_OPTIONS_HPP__ + +#include +#include + +#include "../command.h" + +namespace srb2::media +{ + +class Options +{ +public: + using map_t = std::unordered_map; + + Options(const char* prefix, map_t map); + + template + T get(const char* option) const; + + template + static consvar_t range(const char* default_value, T min, T max); + + template + static consvar_t range_min(const char* default_value, T min); + + template + static consvar_t value_map(const char* default_value, std::map values); + +private: + const char* prefix_; + map_t map_; + + const consvar_t& cvar(const char* option) const; +}; + +void register_options(); + +}; // namespace srb2::media + +#endif // __SRB2_MEDIA_OPTIONS_HPP__