media: add "options" cvar abstraction

This commit is contained in:
James R 2023-02-12 02:05:25 -08:00
parent 3b5245f974
commit e9f5a75d4a
3 changed files with 171 additions and 0 deletions

View file

@ -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
)

118
src/media/options.cpp Normal file
View file

@ -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 <cstddef>
#include <cstdint>
#include <vector>
#include <fmt/format.h>
#include "../cxxutil.hpp"
#include "../m_fixed.h"
#include "options.hpp"
using namespace srb2::media;
static std::vector<consvar_t*> 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<int>(const char* option) const
{
return cvar(option).value;
}
template <>
float Options::get<float>(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<float>(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<float>(const char* default_value, float min)
{
return range_cvar(default_value, FloatToFixed(min), INT32_MAX);
}
template <>
consvar_t Options::range<int>(const char* default_value, int min, int max)
{
return range_cvar(default_value, min, max);
}
template <>
consvar_t Options::range_min<int>(const char* default_value, int min)
{
return range_cvar(default_value, min, INT32_MAX);
}
template <>
consvar_t Options::value_map<int>(const char* default_value, std::map<const char*, int> 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 = {};
}

51
src/media/options.hpp Normal file
View file

@ -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 <map>
#include <unordered_map>
#include "../command.h"
namespace srb2::media
{
class Options
{
public:
using map_t = std::unordered_map<const char*, consvar_t>;
Options(const char* prefix, map_t map);
template <typename T>
T get(const char* option) const;
template <typename T>
static consvar_t range(const char* default_value, T min, T max);
template <typename T>
static consvar_t range_min(const char* default_value, T min);
template <typename T>
static consvar_t value_map(const char* default_value, std::map<const char*, T> 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__