MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
anonymous_namespace{binary_matrix.cpp} Namespace Reference

Classes

struct  Stream
struct  SurfaceDeleter
struct  TtfDeleter

Typedefs

using Clock = std::chrono::steady_clock
using FontPtr = std::unique_ptr<TTF_Font, TtfDeleter>
using SurfacePtr = std::unique_ptr<SDL_Surface, SurfaceDeleter>

Functions

SDL_Color matrixTrailColor (int level)
std::optional< SDL_Color > parse_color_spec (const std::string &spec)
std::optional< Uint8 > parse_u8_component (const std::string &value, int base)
SurfacePtr renderGlyph (TTF_Font *font, const std::string &glyph, const SDL_Color &color)
std::string trim_copy (const std::string &value)

Typedef Documentation

◆ Clock

using anonymous_namespace{binary_matrix.cpp}::Clock = std::chrono::steady_clock

Definition at line 26 of file binary_matrix.cpp.

◆ FontPtr

using anonymous_namespace{binary_matrix.cpp}::FontPtr = std::unique_ptr<TTF_Font, TtfDeleter>

Definition at line 46 of file binary_matrix.cpp.

◆ SurfacePtr

using anonymous_namespace{binary_matrix.cpp}::SurfacePtr = std::unique_ptr<SDL_Surface, SurfaceDeleter>

Definition at line 36 of file binary_matrix.cpp.

Function Documentation

◆ matrixTrailColor()

SDL_Color anonymous_namespace{binary_matrix.cpp}::matrixTrailColor ( int level)

Definition at line 59 of file binary_matrix.cpp.

59 {
60 constexpr int maxLevel = 7;
61 const float t = std::clamp(static_cast<float>(level) / static_cast<float>(maxLevel), 0.0f, 1.0f);
62 const Uint8 r = static_cast<Uint8>(std::lerp(0.0f, 180.0f, std::pow(t, 2.8f)));
63 const Uint8 g = static_cast<Uint8>(std::lerp(36.0f, 255.0f, std::pow(t, 0.72f)));
64 const Uint8 b = static_cast<Uint8>(std::lerp(8.0f, 205.0f, std::pow(t, 3.2f)));
65 const Uint8 a = static_cast<Uint8>(std::lerp(120.0f, 255.0f, t));
66 return SDL_Color{r, g, b, a};
67 }

◆ parse_color_spec()

std::optional< SDL_Color > anonymous_namespace{binary_matrix.cpp}::parse_color_spec ( const std::string & spec)

Definition at line 90 of file binary_matrix.cpp.

90 {
91 const std::string value = trim_copy(spec);
92 if (value.empty()) {
93 return std::nullopt;
94 }
95
96 if (value.front() == '#') {
97 if (value.size() != 7) {
98 return std::nullopt;
99 }
100 const auto r = parse_u8_component(value.substr(1, 2), 16);
101 const auto g = parse_u8_component(value.substr(3, 2), 16);
102 const auto b = parse_u8_component(value.substr(5, 2), 16);
103 if (!r || !g || !b) {
104 return std::nullopt;
105 }
106 return SDL_Color{*r, *g, *b, 255};
107 }
108
109 const std::size_t first = value.find(',');
110 if (first == std::string::npos) {
111 return std::nullopt;
112 }
113 const std::size_t second = value.find(',', first + 1);
114 if (second == std::string::npos || value.find(',', second + 1) != std::string::npos) {
115 return std::nullopt;
116 }
117
118 const auto r = parse_u8_component(value.substr(0, first), 10);
119 const auto g = parse_u8_component(value.substr(first + 1, second - first - 1), 10);
120 const auto b = parse_u8_component(value.substr(second + 1), 10);
121 if (!r || !g || !b) {
122 return std::nullopt;
123 }
124 return SDL_Color{*r, *g, *b, 255};
125 }
std::optional< Uint8 > parse_u8_component(const std::string &value, int base)
std::string trim_copy(const std::string &value)

◆ parse_u8_component()

std::optional< Uint8 > anonymous_namespace{binary_matrix.cpp}::parse_u8_component ( const std::string & value,
int base )

Definition at line 78 of file binary_matrix.cpp.

78 {
79 int parsed = 0;
80 const std::string trimmed = trim_copy(value);
81 const char *begin = trimmed.data();
82 const char *end = trimmed.data() + trimmed.size();
83 const std::from_chars_result result = std::from_chars(begin, end, parsed, base);
84 if (result.ec != std::errc{} || result.ptr != end || parsed < 0 || parsed > 255) {
85 return std::nullopt;
86 }
87 return static_cast<Uint8>(parsed);
88 }

◆ renderGlyph()

SurfacePtr anonymous_namespace{binary_matrix.cpp}::renderGlyph ( TTF_Font * font,
const std::string & glyph,
const SDL_Color & color )

Definition at line 127 of file binary_matrix.cpp.

127 {
128 SDL_Surface *rendered = TTF_RenderText_Blended(font, glyph.c_str(), 0, color);
129 if (rendered == nullptr) {
130 return {};
131 }
132
133 SurfacePtr converted(SDL_ConvertSurface(rendered, SDL_PIXELFORMAT_RGBA32));
134 SDL_DestroySurface(rendered);
135 if (converted != nullptr) {
136 SDL_SetSurfaceBlendMode(converted.get(), SDL_BLENDMODE_BLEND);
137 }
138 return converted;
139 }
std::unique_ptr< SDL_Surface, SurfaceDeleter > SurfacePtr

◆ trim_copy()

std::string anonymous_namespace{binary_matrix.cpp}::trim_copy ( const std::string & value)

Definition at line 69 of file binary_matrix.cpp.

69 {
70 const auto begin = value.find_first_not_of(" \t\r\n");
71 if (begin == std::string::npos) {
72 return {};
73 }
74 const auto end = value.find_last_not_of(" \t\r\n");
75 return value.substr(begin, end - begin + 1);
76 }