KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
internals.hpp
1/*
2 KTech, Kaup's C++ 2D terminal game engine library.
3 Copyright (C) 2023-2025 Ethan Kaufman (AKA Kaup)
4
5 This file is part of KTech.
6
7 KTech is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 any later version.
11
12 KTech is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with KTech. If not, see <https://www.gnu.org/licenses/>.
19*/
20
21#pragma once
22
23#define KTECH_DEFINITION
24#include "../ktech.hpp"
25#undef KTECH_DEFINITION
26#include "../basic/point.hpp"
27#include "../basic/upoint.hpp"
28#include "../basic/rgb.hpp"
29#include "../basic/rgba.hpp"
30
31#include <limits>
32
33constexpr uint8_t uint8Max = std::numeric_limits<uint8_t>::max();
34
35// Delimits the rectangle formed by `start` and `end` within `res`, assuming `start` is equal to `end` or that it is closer to (0, 0) than `end` (i.e. `start` is smaller).
36// Returns false if the rectangle formed by `start` and `end` doesn't overlap at all with `res` (i.e. the rectangle formed by (0, 0) and `end.`)
37// In the case that it returns false, `start` and `end` might be partially modified and deemed obsolete.
38static inline auto Delimit(KTech::Point& p_start, KTech::Point& p_end, KTech::UPoint& p_res) -> bool
39{
40 if (p_start.x < 0)
41 {
42 p_start.x = 0;
43 }
44 else if (p_start.x > static_cast<int>(p_res.x))
45 {
46 return false;
47 }
48 if (p_start.y < 0)
49 {
50 p_start.y = 0;
51 }
52 else if (p_start.y > static_cast<int>(p_res.y))
53 {
54 return false;
55 }
56 if (p_end.x > static_cast<int>(p_res.x))
57 {
58 p_end.x = p_res.x;
59 }
60 else if (p_end.x < 0)
61 {
62 return false;
63 }
64 if (p_end.y > static_cast<int>(p_res.y))
65 {
66 p_end.y = p_res.y;
67 }
68 else if (p_end.y < 0)
69 {
70 return false;
71 }
72 return true;
73}
74
75// Returns false if space (' '). Returns true if printable or fixed.
76static inline auto DetermineCharacter(char& p_char) -> bool
77{
78 if (p_char == ' ')
79 {
80 return false;
81 }
82 if (p_char < ' ' || '~' < p_char)
83 {
84 p_char = ' ';
85 }
86 return true;
87}
88
89// Populates `dst` with `src`, after calculating RGB values based on `src` alpha channel.
90// Returns false if alpha channel is 0 (`dst` isn't changed). Returns true otherwise.
91// Overriding `BakeRGBA` would impair readability.
92static inline auto BakeRGBA(KTech::RGBA& p_dst, const KTech::RGBA& p_src) -> bool
93{
94 if (p_src.a == 0) // Won't add anything; return false so no needless drawing will be done
95 {
96 return false;
97 }
98 p_dst.a = p_src.a;
99 p_dst.r = p_src.r * p_dst.a / uint8Max; // Bake alpha into RGB
100 p_dst.g = p_src.g * p_dst.a / uint8Max;
101 p_dst.b = p_src.b * p_dst.a / uint8Max;
102 return true; // Will add something; return true so drawing will be done
103}
104
105// Populates `dst` with `src`, after calculating RGB values based on `src` alpha channel and additional `m_alpha`.
106// Returns false if alpha channel is 0 (still sets `dst.a`). Returns true otherwise.
107static inline auto BakeRGBAWith(KTech::RGBA& p_dst, const KTech::RGBA& p_src, uint8_t p_alpha) -> bool
108{
109 p_dst.a = p_src.a * p_alpha / uint8Max; // Take `Layer::m_alpha` into consideration
110 if (p_dst.a == 0) // Won't add anything; return false so no needless drawing will be done
111 {
112 return false;
113 }
114 p_dst.r = p_src.r * p_dst.a / uint8Max; // Bake alpha into RGB
115 p_dst.g = p_src.g * p_dst.a / uint8Max;
116 p_dst.b = p_src.b * p_dst.a / uint8Max;
117 return true; // Will add something; return true so drawing will be done
118}
119
120// Draws `src` on `dst`, with respect to alpha channels.
121// Assumes `src.a` was baked into the RGB values of `src` already ("baked").
122static inline void DrawBakedToRGB(KTech::RGB& p_dst, const KTech::RGBA& p_src)
123{
124 p_dst.r = p_src.r + p_dst.r * (uint8Max - p_src.a) / uint8Max;
125 p_dst.g = p_src.g + p_dst.g * (uint8Max - p_src.a) / uint8Max;
126 p_dst.b = p_src.b + p_dst.b * (uint8Max - p_src.a) / uint8Max;
127}
128
129// Draws `src` on `dst`, with respect to alpha channels.
130// Assumes `src.a` was baked into the RGB values of `src` already ("baked").
131static inline void DrawBakedToRGBA(KTech::RGBA& p_dst, const KTech::RGBA& p_src)
132{
133 p_dst.r = p_src.r + p_dst.r * (uint8Max - p_src.a) / uint8Max;
134 p_dst.g = p_src.g + p_dst.g * (uint8Max - p_src.a) / uint8Max;
135 p_dst.b = p_src.b + p_dst.b * (uint8Max - p_src.a) / uint8Max;
136 p_dst.a += (uint8Max - p_dst.a) * p_src.a / uint8Max;
137}
138
139static inline auto GetThirdDigit(uint8_t uint8) -> char
140{
141 return (uint8 / 100) + '0';
142}
143
144static inline auto GetSecondDigit(uint8_t uint8) -> char
145{
146 return ((uint8 % 100) / 10) + '0';
147}
148
149static inline auto GetFirstDigit(uint8_t uint8) -> char
150{
151 return (uint8 % 10) + '0';
152}
2D vector, mostly used to store positions and directions.
Definition point.hpp:30
Like RGB, but also has an alpha channel representing transparency.
Definition rgba.hpp:30
uint8_t a
Alpha channel.
Definition rgba.hpp:31
24-bit color, able of representing 16,777,216 (2^24) different colors.
Definition rgb.hpp:29
uint8_t g
Green primary color.
Definition rgb.hpp:31
uint8_t r
Red primary color.
Definition rgb.hpp:30
uint8_t b
Blue primary color.
Definition rgb.hpp:32
Unsigned 2D vector, mostly used to store sizes and 2D indexes.
Definition upoint.hpp:29