[nostalgia/common] Add Vec2 type

This commit is contained in:
Gary Talent 2022-02-13 02:18:53 -06:00
parent 142c78db0e
commit 0e7a090d28
3 changed files with 64 additions and 0 deletions

View File

@ -2,6 +2,7 @@
add_library( add_library(
NostalgiaCommon NostalgiaCommon
bounds.cpp bounds.cpp
vec.cpp
) )
install( install(
@ -18,6 +19,7 @@ install(
common.hpp common.hpp
point.hpp point.hpp
size.hpp size.hpp
vec.hpp
DESTINATION DESTINATION
include/nostalgia/common include/nostalgia/common
) )

View File

@ -0,0 +1,18 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <ox/std/defines.hpp>
#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
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#if __has_include(<imgui.h>)
#include <imgui.h>
#endif
#include <ox/std/types.hpp>
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(<imgui.h>)
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(<imgui.h>)
explicit operator ImVec2() const noexcept {
return {x, y};
}
#endif
};
}