浏览代码

Define GDNative sizes using sizeof(godot_real_t) and sizeof(int32_t)

Aaron Franke 4 年之前
父节点
当前提交
6c197cf259

+ 6 - 6
core/math/vector2.cpp

@@ -211,11 +211,11 @@ 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 {
+Vector2i Vector2i::operator*(const int32_t &rvalue) const {
 	return Vector2i(x * rvalue, y * rvalue);
 }
 
-void Vector2i::operator*=(const int &rvalue) {
+void Vector2i::operator*=(const int32_t &rvalue) {
 	x *= rvalue;
 	y *= rvalue;
 }
@@ -224,11 +224,11 @@ 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 {
+Vector2i Vector2i::operator/(const int32_t &rvalue) const {
 	return Vector2i(x / rvalue, y / rvalue);
 }
 
-void Vector2i::operator/=(const int &rvalue) {
+void Vector2i::operator/=(const int32_t &rvalue) {
 	x /= rvalue;
 	y /= rvalue;
 }
@@ -237,11 +237,11 @@ 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 {
+Vector2i Vector2i::operator%(const int32_t &rvalue) const {
 	return Vector2i(x % rvalue, y % rvalue);
 }
 
-void Vector2i::operator%=(const int &rvalue) {
+void Vector2i::operator%=(const int32_t &rvalue) {
 	x %= rvalue;
 	y %= rvalue;
 }

+ 15 - 15
core/math/vector2.h

@@ -265,18 +265,18 @@ struct Vector2i {
 	};
 
 	union {
-		int x = 0;
-		int width;
+		int32_t x = 0;
+		int32_t width;
 	};
 	union {
-		int y = 0;
-		int height;
+		int32_t y = 0;
+		int32_t height;
 	};
 
-	_FORCE_INLINE_ int &operator[](int p_idx) {
+	_FORCE_INLINE_ int32_t &operator[](int p_idx) {
 		return p_idx ? y : x;
 	}
-	_FORCE_INLINE_ const int &operator[](int p_idx) const {
+	_FORCE_INLINE_ const int32_t &operator[](int p_idx) const {
 		return p_idx ? y : x;
 	}
 
@@ -286,16 +286,16 @@ struct Vector2i {
 	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 int32_t &rvalue) const;
+	void operator*=(const int32_t &rvalue);
 
 	Vector2i operator/(const Vector2i &p_v1) const;
-	Vector2i operator/(const int &rvalue) const;
-	void operator/=(const int &rvalue);
+	Vector2i operator/(const int32_t &rvalue) const;
+	void operator/=(const int32_t &rvalue);
 
 	Vector2i operator%(const Vector2i &p_v1) const;
-	Vector2i operator%(const int &rvalue) const;
-	void operator%=(const int &rvalue);
+	Vector2i operator%(const int32_t &rvalue) const;
+	void operator%=(const int32_t &rvalue);
 
 	Vector2i operator-() const;
 	bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
@@ -317,10 +317,10 @@ struct Vector2i {
 
 	inline Vector2i() {}
 	inline Vector2i(const Vector2 &p_vec2) {
-		x = (int)p_vec2.x;
-		y = (int)p_vec2.y;
+		x = (int32_t)p_vec2.x;
+		y = (int32_t)p_vec2.y;
 	}
-	inline Vector2i(int p_x, int p_y) {
+	inline Vector2i(int32_t p_x, int32_t p_y) {
 		x = p_x;
 		y = p_y;
 	}

+ 2 - 2
modules/gdnative/include/gdnative/aabb.h

@@ -35,9 +35,9 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
 
-#define GODOT_AABB_SIZE 24
+#define GODOT_AABB_SIZE (sizeof(godot_real_t) * 6)
 
 #ifndef GODOT_CORE_API_GODOT_AABB_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_AABB_TYPE_DEFINED

+ 2 - 2
modules/gdnative/include/gdnative/basis.h

@@ -35,9 +35,9 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
 
-#define GODOT_BASIS_SIZE 36
+#define GODOT_BASIS_SIZE (sizeof(godot_real_t) * 9)
 
 #ifndef GODOT_CORE_API_GODOT_BASIS_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_BASIS_TYPE_DEFINED

+ 3 - 2
modules/gdnative/include/gdnative/color.h

@@ -35,9 +35,10 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
 
-#define GODOT_COLOR_SIZE 16
+// Colors should always use 32-bit floats, so don't use real_t here.
+#define GODOT_COLOR_SIZE (sizeof(float) * 4)
 
 #ifndef GODOT_CORE_API_GODOT_COLOR_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_COLOR_TYPE_DEFINED

+ 65 - 0
modules/gdnative/include/gdnative/math_defs.h

@@ -0,0 +1,65 @@
+/*************************************************************************/
+/*  math_defs.h                                                          */
+/*************************************************************************/
+/*                       This file is part of:                           */
+/*                           GODOT ENGINE                                */
+/*                      https://godotengine.org                          */
+/*************************************************************************/
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
+/* Copyright (c) 2014-2021 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.                */
+/*************************************************************************/
+
+#ifndef GODOT_GDNATIVE_MATH_DEFS_H
+#define GODOT_GDNATIVE_MATH_DEFS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+
+////// bool
+
+typedef bool godot_bool;
+
+#define GODOT_TRUE 1
+#define GODOT_FALSE 0
+
+/////// int
+
+typedef int64_t godot_int;
+
+/////// float
+
+typedef double godot_float;
+
+#ifdef REAL_T_IS_DOUBLE
+typedef double godot_real_t;
+#else
+typedef float godot_real_t;
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // GODOT_C_H

+ 2 - 2
modules/gdnative/include/gdnative/plane.h

@@ -35,9 +35,9 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
 
-#define GODOT_PLANE_SIZE 16
+#define GODOT_PLANE_SIZE (sizeof(godot_real_t) * 4)
 
 #ifndef GODOT_CORE_API_GODOT_PLANE_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_PLANE_TYPE_DEFINED

+ 2 - 2
modules/gdnative/include/gdnative/quat.h

@@ -35,9 +35,9 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
 
-#define GODOT_QUAT_SIZE 16
+#define GODOT_QUAT_SIZE (sizeof(godot_real_t) * 4)
 
 #ifndef GODOT_CORE_API_GODOT_QUAT_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_QUAT_TYPE_DEFINED

+ 7 - 3
modules/gdnative/include/gdnative/rect2.h

@@ -35,19 +35,23 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
+
+#define GODOT_RECT2_SIZE (sizeof(godot_real_t) * 4)
 
 #ifndef GODOT_CORE_API_GODOT_RECT2_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_RECT2_TYPE_DEFINED
 typedef struct godot_rect2 {
-	uint8_t _dont_touch_that[16];
+	uint8_t _dont_touch_that[GODOT_RECT2_SIZE];
 } godot_rect2;
 #endif
 
+#define GODOT_RECT2I_SIZE (sizeof(int32_t) * 4)
+
 #ifndef GODOT_CORE_API_GODOT_RECT2I_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_RECT2I_TYPE_DEFINED
 typedef struct godot_rect2i {
-	uint8_t _dont_touch_that[16];
+	uint8_t _dont_touch_that[GODOT_RECT2I_SIZE];
 } godot_rect2i;
 #endif
 

+ 2 - 2
modules/gdnative/include/gdnative/transform.h

@@ -35,9 +35,9 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
 
-#define GODOT_TRANSFORM_SIZE 48
+#define GODOT_TRANSFORM_SIZE (sizeof(godot_real_t) * 12)
 
 #ifndef GODOT_CORE_API_GODOT_TRANSFORM_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_TRANSFORM_TYPE_DEFINED

+ 2 - 2
modules/gdnative/include/gdnative/transform2d.h

@@ -35,9 +35,9 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
 
-#define GODOT_TRANSFORM2D_SIZE 24
+#define GODOT_TRANSFORM2D_SIZE (sizeof(godot_real_t) * 6)
 
 #ifndef GODOT_CORE_API_GODOT_TRANSFORM2D_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_TRANSFORM2D_TYPE_DEFINED

+ 2 - 17
modules/gdnative/include/gdnative/variant.h

@@ -35,24 +35,9 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
 
-////// bool
-
-typedef bool godot_bool;
-
-#define GODOT_TRUE 1
-#define GODOT_FALSE 0
-
-/////// int
-
-typedef int64_t godot_int;
-
-/////// float
-
-typedef double godot_float;
-
-#define GODOT_VARIANT_SIZE (16 + sizeof(int64_t))
+#define GODOT_VARIANT_SIZE (sizeof(godot_real_t) * 4 + sizeof(int64_t))
 
 #ifndef GODOT_CORE_API_GODOT_VARIANT_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_VARIANT_TYPE_DEFINED

+ 3 - 3
modules/gdnative/include/gdnative/vector2.h

@@ -35,9 +35,9 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
 
-#define GODOT_VECTOR2_SIZE 8
+#define GODOT_VECTOR2_SIZE (sizeof(godot_real_t) * 2)
 
 #ifndef GODOT_CORE_API_GODOT_VECTOR2_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_VECTOR2_TYPE_DEFINED
@@ -46,7 +46,7 @@ typedef struct {
 } godot_vector2;
 #endif
 
-#define GODOT_VECTOR2I_SIZE 8
+#define GODOT_VECTOR2I_SIZE (sizeof(int32_t) * 2)
 
 #ifndef GODOT_CORE_API_GODOT_VECTOR2I_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_VECTOR2I_TYPE_DEFINED

+ 3 - 3
modules/gdnative/include/gdnative/vector3.h

@@ -35,9 +35,9 @@
 extern "C" {
 #endif
 
-#include <stdint.h>
+#include <gdnative/math_defs.h>
 
-#define GODOT_VECTOR3_SIZE 12
+#define GODOT_VECTOR3_SIZE (sizeof(godot_real_t) * 3)
 
 #ifndef GODOT_CORE_API_GODOT_VECTOR3_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_VECTOR3_TYPE_DEFINED
@@ -46,7 +46,7 @@ typedef struct {
 } godot_vector3;
 #endif
 
-#define GODOT_VECTOR3I_SIZE 12
+#define GODOT_VECTOR3I_SIZE (sizeof(int32_t) * 3)
 
 #ifndef GODOT_CORE_API_GODOT_VECTOR3I_TYPE_DEFINED
 #define GODOT_CORE_API_GODOT_VECTOR3I_TYPE_DEFINED