[nostalgia] Rename common package to geo

This commit is contained in:
2022-02-13 04:03:10 -06:00
parent 0aa71f1dbb
commit 320df614a9
26 changed files with 131 additions and 130 deletions

View File

@@ -0,0 +1,25 @@
add_library(
NostalgiaCommon
bounds.cpp
vec.cpp
)
install(
TARGETS
NostalgiaCommon
DESTINATION
LIBRARY DESTINATION lib/nostalgia
ARCHIVE DESTINATION lib/nostalgia
)
install(
FILES
bounds.hpp
geo.hpp
point.hpp
size.hpp
vec.hpp
DESTINATION
include/nostalgia/common
)

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include "bounds.hpp"
namespace nostalgia::geo {
Bounds::Bounds(int x, int y, int w, int h) noexcept {
this->x = x;
this->y = y;
this->width = w;
this->height = h;
}
bool Bounds::intersects(const Bounds &o) const noexcept {
return o.x2() >= x && x2() >= o.x && o.y2() >= y && y2() >= o.y;
}
bool Bounds::contains(int x, int y) const noexcept {
return x >= this->x && y >= this->y && x <= x2() && y <= y2();
}
int Bounds::x2() const noexcept {
return x + width;
}
int Bounds::y2() const noexcept {
return y + height;
}
Point Bounds::pt1() const noexcept {
return {x, y};
}
Point Bounds::pt2() const noexcept {
return {x2(), y2()};
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include "point.hpp"
namespace nostalgia::geo {
class Bounds {
public:
static constexpr auto TypeName = "net.drinkingtea.nostalgia.geo.Bounds";
static constexpr auto TypeVersion = 1;
int x = 0;
int y = 0;
int width = 0;
int height = 0;
constexpr Bounds() noexcept = default;
Bounds(int x, int y, int w, int h) noexcept;
[[nodiscard]]
bool intersects(const Bounds &other) const noexcept;
[[nodiscard]]
bool contains(int x, int y) const noexcept;
[[nodiscard]]
int x2() const noexcept;
[[nodiscard]]
int y2() const noexcept;
[[nodiscard]]
Point pt1() const noexcept;
[[nodiscard]]
Point pt2() const noexcept;
};
template<typename T>
constexpr ox::Error model(T *io, Bounds *obj) noexcept {
io->template setTypeInfo<Point>();
oxReturnError(io->field("x", &obj->x));
oxReturnError(io->field("y", &obj->y));
oxReturnError(io->field("width", &obj->width));
oxReturnError(io->field("height", &obj->height));
return OxError(0);
}
}

View File

@@ -0,0 +1,9 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include "bounds.hpp"
#include "point.hpp"
#include "size.hpp"

192
src/nostalgia/geo/point.hpp Normal file
View File

@@ -0,0 +1,192 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <ox/std/error.hpp>
namespace nostalgia::geo {
class Point {
public:
static constexpr auto TypeName = "net.drinkingtea.nostalgia.geo.Point";
static constexpr auto TypeVersion = 1;
int x = 0;
int y = 0;
constexpr Point() noexcept = default;
constexpr Point(int x, int y) noexcept;
constexpr Point operator+(const geo::Point &p) const noexcept;
constexpr Point operator-(const geo::Point &p) const noexcept;
constexpr Point operator*(const geo::Point &p) const noexcept;
constexpr Point operator/(const geo::Point &p) const noexcept;
constexpr Point operator+=(const geo::Point &p) noexcept;
constexpr Point operator-=(const geo::Point &p) noexcept;
constexpr Point operator*=(const geo::Point &p) noexcept;
constexpr Point operator/=(const geo::Point &p) noexcept;
constexpr Point operator+(int i) const noexcept;
constexpr Point operator-(int i) const noexcept;
constexpr Point operator*(int i) const noexcept;
constexpr Point operator/(int i) const noexcept;
constexpr Point operator+=(int i) noexcept;
constexpr Point operator-=(int i) noexcept;
constexpr Point operator*=(int i) noexcept;
constexpr Point operator/=(int i) noexcept;
constexpr bool operator==(const Point&) const noexcept;
constexpr bool operator!=(const Point&) const noexcept;
};
constexpr Point::Point(int x, int y) noexcept {
this->x = x;
this->y = y;
}
constexpr Point Point::operator+(const geo::Point &p) const noexcept {
auto out = *this;
out.x += x;
out.y += y;
return p;
}
constexpr Point Point::operator-(const geo::Point &p) const noexcept {
auto out = *this;
out.x -= p.x;
out.y -= p.y;
return out;
}
constexpr Point Point::operator*(const geo::Point &p) const noexcept {
auto out = *this;
out.x *= x;
out.y *= y;
return p;
}
constexpr Point Point::operator/(const geo::Point &p) const noexcept {
auto out = *this;
out.x /= p.x;
out.y /= p.y;
return out;
}
constexpr Point Point::operator+=(const geo::Point &p) noexcept {
x += p.x;
y += p.y;
return *this;
}
constexpr Point Point::operator-=(const geo::Point &p) noexcept {
x -= p.x;
y -= p.y;
return *this;
}
constexpr Point Point::operator*=(const geo::Point &p) noexcept {
x *= p.x;
y *= p.y;
return *this;
}
constexpr Point Point::operator/=(const geo::Point &p) noexcept {
x /= p.x;
y /= p.y;
return *this;
}
constexpr Point Point::operator+(int i) const noexcept {
auto out = *this;
out.x += i;
out.y += i;
return out;
}
constexpr Point Point::operator-(int i) const noexcept {
auto out = *this;
out.x -= i;
out.y -= i;
return out;
}
constexpr Point Point::operator*(int i) const noexcept {
auto out = *this;
out.x *= i;
out.y *= i;
return out;
}
constexpr Point Point::operator/(int i) const noexcept {
auto out = *this;
out.x /= i;
out.y /= i;
return out;
}
constexpr Point Point::operator+=(int i) noexcept {
x += i;
y += i;
return *this;
}
constexpr Point Point::operator-=(int i) noexcept {
x -= i;
y -= i;
return *this;
}
constexpr Point Point::operator*=(int i) noexcept {
x *= i;
y *= i;
return *this;
}
constexpr Point Point::operator/=(int i) noexcept {
x /= i;
y /= i;
return *this;
}
constexpr bool Point::operator==(const Point &p) const noexcept {
return x == p.x && y == p.y;
}
constexpr bool Point::operator!=(const Point &p) const noexcept {
return x != p.x || y != p.y;
}
template<typename T>
constexpr ox::Error model(T *io, Point *obj) noexcept {
io->template setTypeInfo<Point>();
oxReturnError(io->field("x", &obj->x));
oxReturnError(io->field("y", &obj->y));
return OxError(0);
}
}

190
src/nostalgia/geo/size.hpp Normal file
View File

@@ -0,0 +1,190 @@
/*
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#pragma once
#include <ox/std/error.hpp>
namespace nostalgia::geo {
class Size {
public:
static constexpr auto TypeName = "net.drinkingtea.nostalgia.geo.Size";
static constexpr auto TypeVersion = 1;
int width = 0;
int height = 0;
constexpr Size() noexcept = default;
constexpr Size(int width, int height) noexcept;
constexpr Size operator+(geo::Size p) const noexcept;
constexpr Size operator-(geo::Size p) const noexcept;
constexpr Size operator*(geo::Size p) const noexcept;
constexpr Size operator/(geo::Size p) const noexcept;
constexpr Size operator+=(geo::Size p) noexcept;
constexpr Size operator-=(geo::Size p) noexcept;
constexpr Size operator*=(geo::Size p) noexcept;
constexpr Size operator/=(geo::Size p) noexcept;
constexpr Size operator+(int i) const noexcept;
constexpr Size operator-(int i) const noexcept;
constexpr Size operator*(int i) const noexcept;
constexpr Size operator/(int i) const noexcept;
constexpr Size operator+=(int i) noexcept;
constexpr Size operator-=(int i) noexcept;
constexpr Size operator*=(int i) noexcept;
constexpr Size operator/=(int i) noexcept;
constexpr bool operator==(const Size&) const noexcept;
constexpr bool operator!=(const Size&) const noexcept;
};
constexpr Size::Size(int width, int height) noexcept {
this->width = width;
this->height = height;
}
constexpr Size Size::operator+(geo::Size p) const noexcept {
p.width += width;
p.height += height;
return p;
}
constexpr Size Size::operator-(geo::Size p) const noexcept {
auto out = *this;
out.width -= p.width;
out.height -= p.height;
return out;
}
constexpr Size Size::operator*(geo::Size p) const noexcept {
p.width *= width;
p.height *= height;
return p;
}
constexpr Size Size::operator/(geo::Size p) const noexcept {
auto out = *this;
out.width /= p.width;
out.height /= p.height;
return out;
}
constexpr Size Size::operator+=(geo::Size p) noexcept {
width += p.width;
height += p.height;
return *this;
}
constexpr Size Size::operator-=(geo::Size p) noexcept {
width -= p.width;
height -= p.height;
return *this;
}
constexpr Size Size::operator*=(geo::Size p) noexcept {
width *= p.width;
height *= p.height;
return *this;
}
constexpr Size Size::operator/=(geo::Size p) noexcept {
width /= p.width;
height /= p.height;
return *this;
}
constexpr Size Size::operator+(int i) const noexcept {
auto out = *this;
out.width += i;
out.height += i;
return out;
}
constexpr Size Size::operator-(int i) const noexcept {
auto out = *this;
out.width -= i;
out.height -= i;
return out;
}
constexpr Size Size::operator*(int i) const noexcept {
auto out = *this;
out.width *= i;
out.height *= i;
return out;
}
constexpr Size Size::operator/(int i) const noexcept {
auto out = *this;
out.width /= i;
out.height /= i;
return out;
}
constexpr Size Size::operator+=(int i) noexcept {
width += i;
height += i;
return *this;
}
constexpr Size Size::operator-=(int i) noexcept {
width -= i;
height -= i;
return *this;
}
constexpr Size Size::operator*=(int i) noexcept {
width *= i;
height *= i;
return *this;
}
constexpr Size Size::operator/=(int i) noexcept {
width /= i;
height /= i;
return *this;
}
constexpr bool Size::operator==(const Size &p) const noexcept {
return width == p.width && height == p.height;
}
constexpr bool Size::operator!=(const Size &p) const noexcept {
return width != p.width || height != p.height;
}
template<typename T>
constexpr ox::Error model(T *io, Size *obj) noexcept {
io->template setTypeInfo<Size>();
oxReturnError(io->field("width", &obj->width));
oxReturnError(io->field("height", &obj->height));
return OxError(0);
}
}

18
src/nostalgia/geo/vec.cpp Normal file
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::geo {
#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
}

44
src/nostalgia/geo/vec.hpp Normal file
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::geo {
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
};
}