libnedit
Lightweight C++ library for Nintendo DS(i) formats
Loading...
Searching...
No Matches
gfx_Base.hpp
1
2#pragma once
3#include <ntr/ntr_Include.hpp>
4
5namespace ntr::gfx {
6
7 enum class PixelFormat : u32 {
8 None,
9 A3I5,
10 Palette4,
11 Palette16,
12 Palette256,
13 COMP4x4,
14 A5I3,
15 Direct
16 };
17
18 static inline constexpr u32 GetPaletteColorCountForPixelFormat(const PixelFormat fmt) {
19 switch(fmt) {
20 case PixelFormat::Palette4: {
21 return 4;
22 }
23 case PixelFormat::Palette16: {
24 return 16;
25 }
26 case PixelFormat::Palette256: {
27 return 256;
28 }
29 default: {
30 return 0;
31 }
32 }
33 }
34
35 enum class MappingType : u32 {
36 Mode2D,
37 Mode1D32K = 16,
38 Mode1D64K,
39 Mode1D128K,
40 Mode1D256K,
41 };
42
43 enum class CharacterFormat : u32 {
44 Char,
45 Bitmap,
46 Max
47 };
48
49 enum class ScreenColorMode : u16 {
50 Mode16x16,
51 Mode256x1,
52 Mode256x16
53 };
54
55 enum class ScreenFormat : u16 {
56 Text,
57 Affine,
58 AffineExt
59 };
60
62 union {
63 struct {
64 u16 tile_number : 10;
65 u16 transform : 2;
66 u16 plt_idx : 4;
67 };
68 u16 raw_val;
69 };
70 };
71 static_assert(sizeof(ScreenDataValue) == sizeof(u16));
72
73 namespace abgr8888 {
74
75 struct Color {
76 union {
77 struct {
78 u8 r;
79 u8 g;
80 u8 b;
81 u8 a;
82 } clr;
83 u32 raw_val;
84 };
85
86 static constexpr u32 RedShift = 0;
87 static constexpr u32 RedBits = 8;
88
89 static constexpr u32 GreenShift = 8;
90 static constexpr u32 GreenBits = 8;
91
92 static constexpr u32 BlueShift = 16;
93 static constexpr u32 BlueBits = 8;
94
95 static constexpr u32 AlphaShift = 24;
96 static constexpr u32 AlphaBits = 8;
97 };
98 static_assert(sizeof(Color) == sizeof(u32));
99
100 constexpr Color TransparentColor = { .clr = { .r = 0, .g = 0, .b = 0, .a = 0 } };
101 constexpr Color BlackColor = { .clr = { .r = 0, .g = 0, .b = 0, .a = 0xff } };
102
103 }
104
105 namespace xbgr1555 {
106
107 struct Color {
108 union {
109 struct {
110 u16 r : 5;
111 u16 g : 5;
112 u16 b : 5;
113 u16 x : 1;
114 } clr;
115 u16 raw_val;
116 };
117
118 static constexpr u32 RedShift = 0;
119 static constexpr u32 RedBits = 5;
120
121 static constexpr u32 GreenShift = 5;
122 static constexpr u32 GreenBits = 5;
123
124 static constexpr u32 BlueShift = 10;
125 static constexpr u32 BlueBits = 5;
126
127 static constexpr u32 AlphaShift = 0;
128 static constexpr u32 AlphaBits = 0;
129 };
130 static_assert(sizeof(Color) == sizeof(u16));
131
132 }
133
134 namespace abgr1555 {
135
136 struct Color {
137 union {
138 struct {
139 u16 r : 5;
140 u16 g : 5;
141 u16 b : 5;
142 u16 a : 1;
143 } clr;
144 u16 raw_val;
145 };
146
147 static constexpr u32 RedShift = 0;
148 static constexpr u32 RedBits = 5;
149
150 static constexpr u32 GreenShift = 5;
151 static constexpr u32 GreenBits = 5;
152
153 static constexpr u32 BlueShift = 10;
154 static constexpr u32 BlueBits = 5;
155
156 static constexpr u32 AlphaShift = 15;
157 static constexpr u32 AlphaBits = 1;
158 };
159 static_assert(sizeof(Color) == sizeof(u16));
160
161 }
162
163 template<typename C1, typename C2>
164 inline constexpr C2 ConvertColor(const C1 clr) {
165 if constexpr(std::is_same_v<C1, C2>) {
166 return clr;
167 }
168
169 constexpr auto c1_r_mask = ~(UINT32_MAX << C1::RedBits);
170 constexpr auto c2_r_mask = ~(UINT32_MAX << C2::RedBits);
171 const auto r = ((((clr.raw_val >> C1::RedShift) & c1_r_mask) * 0xff) + c1_r_mask / 2) / c1_r_mask;
172 const decltype(C2::raw_val) r_cmp = static_cast<decltype(C2::raw_val)>(((r * c2_r_mask + 0x7f) / 0xff) << C2::RedShift);
173
174 constexpr auto c1_g_mask = ~(UINT32_MAX << C1::GreenBits);
175 constexpr auto c2_g_mask = ~(UINT32_MAX << C2::GreenBits);
176 const auto g = ((((clr.raw_val >> C1::GreenShift) & c1_g_mask) * 0xff) + c1_g_mask / 2) / c1_g_mask;
177 const decltype(C2::raw_val) g_cmp = static_cast<decltype(C2::raw_val)>(((g * c2_g_mask + 0x7f) / 0xff) << C2::GreenShift);
178
179 constexpr auto c1_b_mask = ~(UINT32_MAX << C1::BlueBits);
180 constexpr auto c2_b_mask = ~(UINT32_MAX << C2::BlueBits);
181 const auto b = ((((clr.raw_val >> C1::BlueShift) & c1_b_mask) * 0xff) + c1_b_mask / 2) / c1_b_mask;
182 const decltype(C2::raw_val) b_cmp = static_cast<decltype(C2::raw_val)>(((b * c2_b_mask + 0x7f) / 0xff) << C2::BlueShift);
183
184 if constexpr((C1::AlphaBits > 0) && (C2::AlphaBits > 0)) {
185 constexpr auto c1_a_mask = ~(UINT32_MAX << C1::AlphaBits);
186 constexpr auto c2_a_mask = ~(UINT32_MAX << C2::AlphaBits);
187 const auto a = ((((clr.raw_val >> C1::AlphaShift) & c1_a_mask) * 0xff) + c1_a_mask / 2) / c1_a_mask;
188 const decltype(C2::raw_val) a_cmp = static_cast<decltype(C2::raw_val)>(((a * c2_a_mask + 0x7f) / 0xff) << C2::AlphaShift);
189
190 return {
191 .raw_val = static_cast<decltype(C2::raw_val)>(r_cmp | g_cmp | b_cmp | a_cmp)
192 };
193 }
194 else if constexpr(C1::AlphaBits <= 0) {
195 constexpr auto c2_a_mask = ~(UINT32_MAX << C2::AlphaBits);
196 const auto a = 0xff;
197 const decltype(C2::raw_val) a_cmp = static_cast<decltype(C2::raw_val)>(((a * c2_a_mask + 0x7f) / 0xff) << C2::AlphaShift);
198
199 return {
200 .raw_val = static_cast<decltype(C2::raw_val)>(r_cmp | g_cmp | b_cmp | a_cmp)
201 };
202 }
203 else if constexpr(C2::AlphaBits <= 0) {
204 return {
205 .raw_val = static_cast<decltype(C2::raw_val)>(r_cmp | g_cmp | b_cmp)
206 };
207 }
208 }
209
210 constexpr size_t TileSize = 8;
211
212}
Definition gfx_Base.hpp:61
Definition gfx_Base.hpp:136
Definition gfx_Base.hpp:75
Definition gfx_Base.hpp:107