aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/types
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation/types')
-rw-r--r--src/openvic-simulation/types/Colour.hpp14
-rw-r--r--src/openvic-simulation/types/IdentifierRegistry.cpp8
-rw-r--r--src/openvic-simulation/types/IdentifierRegistry.hpp4
-rw-r--r--src/openvic-simulation/types/Vector.cpp86
-rw-r--r--src/openvic-simulation/types/Vector.hpp35
5 files changed, 137 insertions, 10 deletions
diff --git a/src/openvic-simulation/types/Colour.hpp b/src/openvic-simulation/types/Colour.hpp
index 01f3852..15c574f 100644
--- a/src/openvic-simulation/types/Colour.hpp
+++ b/src/openvic-simulation/types/Colour.hpp
@@ -7,30 +7,36 @@
#include <string>
namespace OpenVic {
- // Represents a 24-bit RGB integer OR a 32-bit ARGB integer
+ /* Colour represented by an unsigned integer, either 24-bit RGB or 32-bit ARGB. */
using colour_t = uint32_t;
+
/* When colour_t is used as an identifier, NULL_COLOUR is disallowed
* and should be reserved as an error value.
* When colour_t is used in a purely graphical context, NULL_COLOUR
* should be allowed.
*/
- static constexpr colour_t NULL_COLOUR = 0, FULL_COLOUR = 0xFF, MAX_COLOUR_RGB = 0xFFFFFF;
+ static constexpr colour_t NULL_COLOUR = 0, FULL_COLOUR = 0xFF,
+ MAX_COLOUR_RGB = 0xFFFFFF, MAX_COLOUR_ARGB = 0xFFFFFFFF;
+
constexpr colour_t float_to_colour_byte(float f, float min = 0.0f, float max = 1.0f) {
return static_cast<colour_t>(std::clamp(min + f * (max - min), min, max) * 255.0f);
}
+
constexpr colour_t fraction_to_colour_byte(int n, int d, float min = 0.0f, float max = 1.0f) {
return float_to_colour_byte(static_cast<float>(n) / static_cast<float>(d), min, max);
}
+
constexpr colour_t float_to_alpha_value(float a) {
return float_to_colour_byte(a) << 24;
}
+
constexpr float colour_byte_to_float(colour_t colour) {
return std::clamp(static_cast<float>(colour) / 255.0f, 0.0f, 1.0f);
}
- inline std::string colour_to_hex_string(colour_t colour) {
+ inline std::string colour_to_hex_string(colour_t colour, bool alpha = false) {
std::ostringstream stream;
- stream << std::hex << std::setfill('0') << std::setw(6) << colour;
+ stream << std::uppercase << std::hex << std::setfill('0') << std::setw(!alpha ? 6 : 8) << colour;
return stream.str();
}
}
diff --git a/src/openvic-simulation/types/IdentifierRegistry.cpp b/src/openvic-simulation/types/IdentifierRegistry.cpp
index e33bc29..f284164 100644
--- a/src/openvic-simulation/types/IdentifierRegistry.cpp
+++ b/src/openvic-simulation/types/IdentifierRegistry.cpp
@@ -21,8 +21,8 @@ std::ostream& OpenVic::operator<<(std::ostream& stream, HasIdentifier const* obj
return obj != nullptr ? stream << *obj : stream << "<NULL>";
}
-HasColour::HasColour(colour_t const new_colour, bool can_be_null) : colour(new_colour) {
- assert((can_be_null || colour != NULL_COLOUR) && colour <= MAX_COLOUR_RGB);
+HasColour::HasColour(colour_t const new_colour, bool can_be_null, bool can_have_alpha) : colour(new_colour) {
+ assert((can_be_null || colour != NULL_COLOUR) && colour <= (!can_have_alpha ? MAX_COLOUR_RGB : MAX_COLOUR_ARGB));
}
colour_t HasColour::get_colour() const { return colour; }
@@ -32,9 +32,9 @@ std::string HasColour::colour_to_hex_string() const {
}
HasIdentifierAndColour::HasIdentifierAndColour(const std::string_view new_identifier,
- const colour_t new_colour, bool can_be_null)
+ const colour_t new_colour, bool can_be_null, bool can_have_alpha)
: HasIdentifier { new_identifier },
- HasColour { new_colour, can_be_null } {}
+ HasColour { new_colour, can_be_null, can_have_alpha } {}
distribution_t::value_type OpenVic::get_largest_item(distribution_t const& dist) {
const distribution_t::const_iterator result = std::max_element(dist.begin(), dist.end(),
diff --git a/src/openvic-simulation/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp
index 20eebb9..3f73a2c 100644
--- a/src/openvic-simulation/types/IdentifierRegistry.hpp
+++ b/src/openvic-simulation/types/IdentifierRegistry.hpp
@@ -37,7 +37,7 @@ namespace OpenVic {
const colour_t colour;
protected:
- HasColour(const colour_t new_colour, bool can_be_null);
+ HasColour(const colour_t new_colour, bool can_be_null, bool can_have_alpha);
public:
HasColour(HasColour const&) = delete;
@@ -55,7 +55,7 @@ namespace OpenVic {
*/
class HasIdentifierAndColour : public HasIdentifier, public HasColour {
protected:
- HasIdentifierAndColour(const std::string_view new_identifier, const colour_t new_colour, bool can_be_null);
+ HasIdentifierAndColour(const std::string_view new_identifier, const colour_t new_colour, bool can_be_null, bool can_have_alpha);
public:
HasIdentifierAndColour(HasIdentifierAndColour const&) = delete;
diff --git a/src/openvic-simulation/types/Vector.cpp b/src/openvic-simulation/types/Vector.cpp
new file mode 100644
index 0000000..a5b8566
--- /dev/null
+++ b/src/openvic-simulation/types/Vector.cpp
@@ -0,0 +1,86 @@
+#include "Vector.hpp"
+
+#include <ostream>
+#include <tuple>
+
+using namespace OpenVic;
+
+template<typename T>
+constexpr vec2_t<T>::vec2_t() = default;
+
+template<typename T>
+constexpr vec2_t<T>::vec2_t(T new_val) : x { new_val }, y { new_val } {}
+
+template<typename T>
+constexpr vec2_t<T>::vec2_t(T new_x, T new_y) : x { new_x }, y { new_y } {}
+
+
+template<typename T>
+constexpr vec2_t<T> vec2_t<T>::abs() const {
+ return { };
+}
+
+template<typename T>
+constexpr T vec2_t<T>::length_squared() const {
+ return x * x + y * y;
+}
+
+template<typename T>
+constexpr T* vec2_t<T>::data() {
+ return reinterpret_cast<T*>(this);
+}
+
+template<typename T>
+constexpr T const* vec2_t<T>::data() const {
+ return reinterpret_cast<T const*>(this);
+}
+
+template<typename T>
+constexpr T& vec2_t<T>::operator[](size_t index) {
+ return data()[index & 1];
+}
+
+template<typename T>
+constexpr T const& vec2_t<T>::operator[](size_t index) const {
+ return data()[index & 1];
+}
+
+template<typename T>
+constexpr vec2_t<T> operator+(vec2_t<T> const& left, vec2_t<T> const& right) {
+ return { left.x + right.x, left.y + right.y };
+}
+
+template<typename T>
+constexpr vec2_t<T>& vec2_t<T>::operator+=(vec2_t const& right) {
+ x += right.x;
+ y += right.y;
+ return *this;
+}
+
+template<typename T>
+constexpr vec2_t<T> operator-(vec2_t<T> const& arg) {
+ return { -arg.x, -arg.y };
+}
+
+template<typename T>
+constexpr vec2_t<T> operator-(vec2_t<T> const& left, vec2_t<T> const& right) {
+ return { left.x - right.x, left.y - right.y };
+}
+
+template<typename T>
+constexpr vec2_t<T>& vec2_t<T>::operator-=(vec2_t const& right) {
+ x -= right.x;
+ y -= right.y;
+ return *this;
+}
+
+template<typename T>
+constexpr std::ostream& operator<<(std::ostream& stream, vec2_t<T> const& value) {
+ return stream << "(" << value.x << ", " << value.y << ")";
+}
+
+template struct vec2_t<int64_t>;
+template struct vec2_t<fixed_point_t>;
+
+static_assert(sizeof(ivec2_t) == 2 * sizeof(int64_t), "ivec2_t size does not equal the sum of its parts' sizes");
+static_assert(sizeof(fvec2_t) == 2 * sizeof(fixed_point_t), "fvec2_t size does not equal the sum of its parts' sizes");
diff --git a/src/openvic-simulation/types/Vector.hpp b/src/openvic-simulation/types/Vector.hpp
new file mode 100644
index 0000000..fdf7d70
--- /dev/null
+++ b/src/openvic-simulation/types/Vector.hpp
@@ -0,0 +1,35 @@
+#pragma once
+
+#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
+
+namespace OpenVic {
+
+ template<typename T>
+ struct vec2_t {
+ T x, y;
+
+ constexpr vec2_t();
+ constexpr vec2_t(T new_val);
+ constexpr vec2_t(T new_x, T new_y);
+
+ constexpr vec2_t abs() const;
+ constexpr T length_squared() const;
+
+ constexpr T* data();
+ constexpr T const* data() const;
+
+ constexpr T& operator[](size_t index);
+ constexpr T const& operator[](size_t index) const;
+
+ constexpr friend vec2_t operator+(vec2_t const& left, vec2_t const& right);
+ constexpr vec2_t& operator+=(vec2_t const& right);
+
+ constexpr friend vec2_t operator-(vec2_t const& arg);
+ constexpr friend vec2_t operator-(vec2_t const& left, vec2_t const& right);
+ constexpr vec2_t& operator-=(vec2_t const& right);
+ constexpr friend std::ostream& operator<<(std::ostream& stream, vec2_t const& value);
+ };
+
+ using ivec2_t = vec2_t<int64_t>;
+ using fvec2_t = vec2_t<fixed_point_t>;
+}