Porytiles
Loading...
Searching...
No Matches
layer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <format>
5#include <optional>
6#include <ostream>
7#include <string>
8
11
12namespace porytiles {
13
21enum class LayerMode { dual, triple };
22
32[[nodiscard]] inline LayerMode layer_mode_from_val(std::size_t s)
33{
34 if (s == 8) {
35 return LayerMode::dual;
36 }
37 if (s == 12) {
38 return LayerMode::triple;
39 }
40 panic("invalid LayerMode integer: " + std::to_string(s));
41}
42
49[[nodiscard]] inline std::optional<LayerMode> layer_mode_from_str(const std::string &s)
50{
51 if (s == "dual") {
52 return LayerMode::dual;
53 }
54 if (s == "triple") {
55 return LayerMode::triple;
56 }
57 return std::nullopt;
58}
59
66[[nodiscard]] inline std::string to_string(LayerMode mode)
67{
68 switch (mode) {
69 case LayerMode::dual:
70 return "dual";
72 return "triple";
73 }
74 panic("unhandled LayerMode value");
75}
76
84inline std::ostream &operator<<(std::ostream &os, const LayerMode mode)
85{
86 return os << to_string(mode);
87}
88
97enum class LayerType : unsigned int { normal = 0, covered = 1, split = 2 };
98
105[[nodiscard]] inline std::string to_string(LayerType layer_type)
106{
107 switch (layer_type) {
109 return "Normal - Middle/Top";
111 return "Covered - Bottom/Middle";
112 case LayerType::split:
113 return "Split - Bottom/Top";
114 default:
115 panic("to_string(LayerType) unknown LayerType");
116 }
117}
118
125[[nodiscard]] inline ChainableResult<LayerType> layer_type_from_int(unsigned int i)
126{
127 if (i > static_cast<unsigned int>(LayerType::split)) {
128 return FormattableError{
129 "invalid layer type integer value '{}': must be 0, 1, or 2", FormatParam{i, Style::bold}};
130 }
131 return static_cast<LayerType>(i);
132}
133
134} // namespace porytiles
135
136template <>
137struct std::formatter<porytiles::LayerMode> {
138 constexpr auto parse(std::format_parse_context &ctx)
139 {
140 return ctx.begin();
141 }
142
143 auto format(const porytiles::LayerMode &value, auto &ctx) const
144 {
145 return std::format_to(ctx.out(), "{}", porytiles::to_string(value));
146 }
147};
148
149template <>
150struct std::formatter<porytiles::LayerType> {
151 constexpr auto parse(std::format_parse_context &ctx)
152 {
153 return ctx.begin();
154 }
155
156 auto format(const porytiles::LayerType &value, auto &ctx) const
157 {
158 return std::format_to(ctx.out(), "{}", porytiles::to_string(value));
159 }
160};
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:63
static const Style bold
Bold text formatting.
std::optional< LayerMode > layer_mode_from_str(const std::string &s)
Converts a string to LayerMode.
Definition layer.hpp:49
LayerMode
Specifies whether a metatile uses dual-layer or triple-layer mode.
Definition layer.hpp:21
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43
LayerMode layer_mode_from_val(std::size_t s)
Converts a numeric value to LayerMode.
Definition layer.hpp:32
ChainableResult< LayerType > layer_type_from_int(unsigned int i)
Converts an integer to LayerType.
Definition layer.hpp:125
@ split
Split the animation into separate animations for each palette variant (not yet implemented).
std::ostream & operator<<(std::ostream &os, const PrimaryPairingMode m)
Stream insertion operator for PrimaryPairingMode.
LayerType
Specifies which layers of a metatile are used for rendering.
Definition layer.hpp:97
std::string to_string(const PrimaryPairingMode m)
Converts a PrimaryPairingMode to its canonical string representation.
auto format(const porytiles::LayerMode &value, auto &ctx) const
Definition layer.hpp:143
constexpr auto parse(std::format_parse_context &ctx)
Definition layer.hpp:138
constexpr auto parse(std::format_parse_context &ctx)
Definition layer.hpp:151
auto format(const porytiles::LayerType &value, auto &ctx) const
Definition layer.hpp:156