From 0e7a090d28eae3fbe17fce04c8002431508687ed Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 13 Feb 2022 02:18:53 -0600 Subject: [PATCH] [nostalgia/common] Add Vec2 type --- src/nostalgia/common/CMakeLists.txt | 2 ++ src/nostalgia/common/vec.cpp | 18 ++++++++++++ src/nostalgia/common/vec.hpp | 44 +++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 src/nostalgia/common/vec.cpp create mode 100644 src/nostalgia/common/vec.hpp diff --git a/src/nostalgia/common/CMakeLists.txt b/src/nostalgia/common/CMakeLists.txt index 4b4818c2..842b0776 100644 --- a/src/nostalgia/common/CMakeLists.txt +++ b/src/nostalgia/common/CMakeLists.txt @@ -2,6 +2,7 @@ add_library( NostalgiaCommon bounds.cpp + vec.cpp ) install( @@ -18,6 +19,7 @@ install( common.hpp point.hpp size.hpp + vec.hpp DESTINATION include/nostalgia/common ) diff --git a/src/nostalgia/common/vec.cpp b/src/nostalgia/common/vec.cpp new file mode 100644 index 00000000..731b2460 --- /dev/null +++ b/src/nostalgia/common/vec.cpp @@ -0,0 +1,18 @@ +/* + * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#include + +#include "vec.hpp" + +namespace nostalgia::common { + +#ifndef OX_OS_BareMetal // doesn't compile in devKitPro for some reason +static_assert([] { + Vec2 v(1, 2); + return v.x == 1 && v.y == 2 && v[0] == 1 && v[1] == 2; +}); +#endif + +} \ No newline at end of file diff --git a/src/nostalgia/common/vec.hpp b/src/nostalgia/common/vec.hpp new file mode 100644 index 00000000..943eda0a --- /dev/null +++ b/src/nostalgia/common/vec.hpp @@ -0,0 +1,44 @@ +/* + * Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved. + */ + +#pragma once + +#if __has_include() +#include +#endif + +#include + +namespace nostalgia::common { + +struct Vec2 { + float x = 0; + float y = 0; + + constexpr Vec2() noexcept = default; + + constexpr Vec2(float pX, float pY) noexcept: x(pX), y(pY) { + } + +#if __has_include() + explicit constexpr Vec2(const ImVec2 &v) noexcept: x(v.x), y(v.y) { + } +#endif + + constexpr auto &operator[](std::size_t i) noexcept { + return (&x)[i]; + } + + constexpr const auto &operator[](std::size_t i) const noexcept { + return (&x)[i]; + } + +#if __has_include() + explicit operator ImVec2() const noexcept { + return {x, y}; + } +#endif +}; + +} \ No newline at end of file