diff --git a/core/math/geometry.h b/core/math/geometry.h index 9f4807159..b3ccb8d80 100644 --- a/core/math/geometry.h +++ b/core/math/geometry.h @@ -35,6 +35,7 @@ #include "core/math/rect2.h" #include "core/math/triangulate.h" #include "core/math/vector3.h" +#include "core/math/vector2i.h" #include "core/object.h" #include "core/pool_vector.h" #include "core/print_string.h" diff --git a/core/math/rect2.h b/core/math/rect2.h index 0d3eba6cb..5ea342d47 100644 --- a/core/math/rect2.h +++ b/core/math/rect2.h @@ -31,6 +31,7 @@ /*************************************************************************/ #include "core/math/vector2.h" // also includes math_funcs and ustring +#include "core/math/vector2i.h" struct Transform2D; diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index 6956ca121..204c287ad 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -199,55 +199,3 @@ bool Vector2::is_equal_approx(const Vector2 &p_v) const { return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y); } -/* Vector2i */ - -Vector2i Vector2i::operator+(const Vector2i &p_v) const { - return Vector2i(x + p_v.x, y + p_v.y); -} -void Vector2i::operator+=(const Vector2i &p_v) { - x += p_v.x; - y += p_v.y; -} -Vector2i Vector2i::operator-(const Vector2i &p_v) const { - return Vector2i(x - p_v.x, y - p_v.y); -} -void Vector2i::operator-=(const Vector2i &p_v) { - x -= p_v.x; - y -= p_v.y; -} - -Vector2i Vector2i::operator*(const Vector2i &p_v1) const { - return Vector2i(x * p_v1.x, y * p_v1.y); -}; - -Vector2i Vector2i::operator*(const int &rvalue) const { - return Vector2i(x * rvalue, y * rvalue); -}; -void Vector2i::operator*=(const int &rvalue) { - x *= rvalue; - y *= rvalue; -}; - -Vector2i Vector2i::operator/(const Vector2i &p_v1) const { - return Vector2i(x / p_v1.x, y / p_v1.y); -}; - -Vector2i Vector2i::operator/(const int &rvalue) const { - return Vector2i(x / rvalue, y / rvalue); -}; - -void Vector2i::operator/=(const int &rvalue) { - x /= rvalue; - y /= rvalue; -}; - -Vector2i Vector2i::operator-() const { - return Vector2i(-x, -y); -} - -bool Vector2i::operator==(const Vector2i &p_vec2) const { - return x == p_vec2.x && y == p_vec2.y; -} -bool Vector2i::operator!=(const Vector2i &p_vec2) const { - return x != p_vec2.x || y != p_vec2.y; -} diff --git a/core/math/vector2.h b/core/math/vector2.h index 899eb9e23..60fd1522e 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -1,5 +1,6 @@ #ifndef VECTOR2_H #define VECTOR2_H + /*************************************************************************/ /* vector2.h */ /*************************************************************************/ @@ -33,8 +34,6 @@ #include "core/math/math_funcs.h" #include "core/ustring.h" -struct Vector2i; - struct _NO_DISCARD_CLASS_ Vector2 { static const int AXIS_COUNT = 2; @@ -268,80 +267,4 @@ Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real typedef Vector2 Size2; typedef Vector2 Point2; -/* INTEGER STUFF */ - -struct _NO_DISCARD_CLASS_ Vector2i { - enum Axis { - AXIS_X, - AXIS_Y, - }; - - union { - struct { - union { - int x; - int width; - }; - union { - int y; - int height; - }; - }; - - int coord[2]; - }; - - _FORCE_INLINE_ int &operator[](int p_idx) { - DEV_ASSERT((unsigned int)p_idx < 2); - return coord[p_idx]; - } - _FORCE_INLINE_ const int &operator[](int p_idx) const { - DEV_ASSERT((unsigned int)p_idx < 2); - return coord[p_idx]; - } - - Vector2i operator+(const Vector2i &p_v) const; - void operator+=(const Vector2i &p_v); - Vector2i operator-(const Vector2i &p_v) const; - void operator-=(const Vector2i &p_v); - Vector2i operator*(const Vector2i &p_v1) const; - - Vector2i operator*(const int &rvalue) const; - void operator*=(const int &rvalue); - - Vector2i operator/(const Vector2i &p_v1) const; - - Vector2i operator/(const int &rvalue) const; - - void operator/=(const int &rvalue); - - Vector2i operator-() const; - bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); } - bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); } - - bool operator==(const Vector2i &p_vec2) const; - bool operator!=(const Vector2i &p_vec2) const; - - real_t get_aspect() const { return width / (real_t)height; } - - operator String() const { return String::num(x) + ", " + String::num(y); } - - operator Vector2() const { return Vector2(x, y); } - inline Vector2i(const Vector2 &p_vec2) { - x = (int)p_vec2.x; - y = (int)p_vec2.y; - } - inline Vector2i(int p_x, int p_y) { - x = p_x; - y = p_y; - } - inline Vector2i() { - x = 0; - y = 0; - } -}; - -typedef Vector2i Size2i; -typedef Vector2i Point2i; - #endif // VECTOR2_H diff --git a/core/math/vector2i.cpp b/core/math/vector2i.cpp new file mode 100644 index 000000000..104db6e29 --- /dev/null +++ b/core/math/vector2i.cpp @@ -0,0 +1,82 @@ +/*************************************************************************/ +/* vector2.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "vector2i.h" + +Vector2i Vector2i::operator+(const Vector2i &p_v) const { + return Vector2i(x + p_v.x, y + p_v.y); +} +void Vector2i::operator+=(const Vector2i &p_v) { + x += p_v.x; + y += p_v.y; +} +Vector2i Vector2i::operator-(const Vector2i &p_v) const { + return Vector2i(x - p_v.x, y - p_v.y); +} +void Vector2i::operator-=(const Vector2i &p_v) { + x -= p_v.x; + y -= p_v.y; +} + +Vector2i Vector2i::operator*(const Vector2i &p_v1) const { + return Vector2i(x * p_v1.x, y * p_v1.y); +}; + +Vector2i Vector2i::operator*(const int &rvalue) const { + return Vector2i(x * rvalue, y * rvalue); +}; +void Vector2i::operator*=(const int &rvalue) { + x *= rvalue; + y *= rvalue; +}; + +Vector2i Vector2i::operator/(const Vector2i &p_v1) const { + return Vector2i(x / p_v1.x, y / p_v1.y); +}; + +Vector2i Vector2i::operator/(const int &rvalue) const { + return Vector2i(x / rvalue, y / rvalue); +}; + +void Vector2i::operator/=(const int &rvalue) { + x /= rvalue; + y /= rvalue; +}; + +Vector2i Vector2i::operator-() const { + return Vector2i(-x, -y); +} + +bool Vector2i::operator==(const Vector2i &p_vec2) const { + return x == p_vec2.x && y == p_vec2.y; +} +bool Vector2i::operator!=(const Vector2i &p_vec2) const { + return x != p_vec2.x || y != p_vec2.y; +} diff --git a/core/math/vector2i.h b/core/math/vector2i.h new file mode 100644 index 000000000..afd826b81 --- /dev/null +++ b/core/math/vector2i.h @@ -0,0 +1,115 @@ +#ifndef VECTOR2I_H +#define VECTOR2I_H + +/*************************************************************************/ +/* vector2.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "core/math/math_funcs.h" +#include "core/ustring.h" + +#include "vector2.h" + +struct _NO_DISCARD_CLASS_ Vector2i { + enum Axis { + AXIS_X, + AXIS_Y, + }; + + union { + struct { + union { + int x; + int width; + }; + union { + int y; + int height; + }; + }; + + int coord[2]; + }; + + _FORCE_INLINE_ int &operator[](int p_idx) { + DEV_ASSERT((unsigned int)p_idx < 2); + return coord[p_idx]; + } + _FORCE_INLINE_ const int &operator[](int p_idx) const { + DEV_ASSERT((unsigned int)p_idx < 2); + return coord[p_idx]; + } + + Vector2i operator+(const Vector2i &p_v) const; + void operator+=(const Vector2i &p_v); + Vector2i operator-(const Vector2i &p_v) const; + void operator-=(const Vector2i &p_v); + Vector2i operator*(const Vector2i &p_v1) const; + + Vector2i operator*(const int &rvalue) const; + void operator*=(const int &rvalue); + + Vector2i operator/(const Vector2i &p_v1) const; + + Vector2i operator/(const int &rvalue) const; + + void operator/=(const int &rvalue); + + Vector2i operator-() const; + bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); } + bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); } + + bool operator==(const Vector2i &p_vec2) const; + bool operator!=(const Vector2i &p_vec2) const; + + real_t get_aspect() const { return width / (real_t)height; } + + Vector2 to_vector2() const { return Vector2(x, y); } + + operator String() const { return String::num(x) + ", " + String::num(y); } + operator Vector2() const { return Vector2(x, y); } + + inline Vector2i(const Vector2 &p_vec2) { + x = (int)p_vec2.x; + y = (int)p_vec2.y; + } + inline Vector2i(int p_x, int p_y) { + x = p_x; + y = p_y; + } + inline Vector2i() { + x = 0; + y = 0; + } +}; + +typedef Vector2i Size2i; +typedef Vector2i Point2i; + +#endif // VECTOR2_H diff --git a/core/os/input.h b/core/os/input.h index f7d5f4964..9eeccd1be 100644 --- a/core/os/input.h +++ b/core/os/input.h @@ -33,6 +33,7 @@ #include "core/object.h" #include "core/os/main_loop.h" #include "core/os/thread_safe.h" +#include "core/math/vector2i.h" class Input : public Object { GDCLASS(Input, Object); diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 8423e7f01..8ecb305bf 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -39,6 +39,7 @@ #include "scene/resources/texture.h" #include "servers/visual/visual_server_raster.h" #include "servers/visual/visual_server_wrap_mt.h" +#include "core/math/vector2i.h" #ifdef HAVE_MNTENT #include