Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Move PhysicsServer largely out of main.cpp

smix8f2d60fd3f270
diff --git a/main/main.cpp b/main/main.cpp
index ba7119ef979..62e4dfc5894 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -93,7 +93,7 @@
#ifndef PHYSICS_2D_DISABLED
#include "servers/physics_2d/physics_server_2d.h"
-#include "servers/physics_2d/physics_server_2d_dummy.h"
+#include "servers/physics_2d/physics_server_2d_manager.h"
#endif // PHYSICS_2D_DISABLED
// 3D
@@ -103,7 +103,7 @@
#ifndef PHYSICS_3D_DISABLED
#include "servers/physics_3d/physics_server_3d.h"
-#include "servers/physics_3d/physics_server_3d_dummy.h"
+#include "servers/physics_3d/physics_server_3d_manager.h"
#endif // PHYSICS_3D_DISABLED
#ifndef XR_DISABLED
@@ -185,14 +185,6 @@ static DisplayServer *display_server = nullptr;
static RenderingServer *rendering_server = nullptr;
static TextServerManager *tsman = nullptr;
static ThemeDB *theme_db = nullptr;
-#ifndef PHYSICS_2D_DISABLED
-static PhysicsServer2DManager *physics_server_2d_manager = nullptr;
-static PhysicsServer2D *physics_server_2d = nullptr;
-#endif // PHYSICS_2D_DISABLED
-#ifndef PHYSICS_3D_DISABLED
-static PhysicsServer3DManager *physics_server_3d_manager = nullptr;
-static PhysicsServer3D *physics_server_3d = nullptr;
-#endif // PHYSICS_3D_DISABLED
#ifndef XR_DISABLED
static XRServer *xr_server = nullptr;
#endif // XR_DISABLED
@@ -355,61 +347,6 @@ static Vector<String> get_files_with_extension(const String &p_root, const Strin
}
#endif
-// FIXME: Could maybe be moved to have less code in main.cpp.
-void initialize_physics() {
-#ifndef PHYSICS_3D_DISABLED
- /// 3D Physics Server
- physics_server_3d = PhysicsServer3DManager::get_singleton()->new_server(
- GLOBAL_GET(PhysicsServer3DManager::setting_property_name));
- if (!physics_server_3d) {
- // Physics server not found, Use the default physics
- physics_server_3d = PhysicsServer3DManager::get_singleton()->new_default_server();
- }
-
- // Fall back to dummy if no default server has been registered.
- if (!physics_server_3d) {
- WARN_PRINT(vformat("Falling back to dummy PhysicsServer3D; 3D physics functionality will be disabled. If this is intended, set the %s project setting to Dummy.", PhysicsServer3DManager::setting_property_name));
- physics_server_3d = memnew(PhysicsServer3DDummy);
- }
-
- // Should be impossible, but make sure it's not null.
- ERR_FAIL_NULL_MSG(physics_server_3d, "Failed to initialize PhysicsServer3D.");
- physics_server_3d->init();
-#endif // PHYSICS_3D_DISABLED
-
-#ifndef PHYSICS_2D_DISABLED
- // 2D Physics server
- physics_server_2d = PhysicsServer2DManager::get_singleton()->new_server(
- GLOBAL_GET(PhysicsServer2DManager::get_singleton()->setting_property_name));
- if (!physics_server_2d) {
- // Physics server not found, Use the default physics
- physics_server_2d = PhysicsServer2DManager::get_singleton()->new_default_server();
- }
-
- // Fall back to dummy if no default server has been registered.
- if (!physics_server_2d) {
- WARN_PRINT(vformat("Falling back to dummy PhysicsServer2D; 2D physics functionality will be disabled. If this is intended, set the %s project setting to Dummy.", PhysicsServer2DManager::setting_property_name));
- physics_server_2d = memnew(PhysicsServer2DDummy);
- }
-
- // Should be impossible, but make sure it's not null.
- ERR_FAIL_NULL_MSG(physics_server_2d, "Failed to initialize PhysicsServer2D.");
- physics_server_2d->init();
-#endif // PHYSICS_2D_DISABLED
-}
-
-void finalize_physics() {
-#ifndef PHYSICS_3D_DISABLED
- physics_server_3d->finish();
- memdelete(physics_server_3d);
-#endif // PHYSICS_3D_DISABLED
-
-#ifndef PHYSICS_2D_DISABLED
- physics_server_2d->finish();
- memdelete(physics_server_2d);
-#endif // PHYSICS_2D_DISABLED
-}
-
void finalize_display() {
rendering_server->finish();
memdelete(rendering_server);
@@ -773,10 +710,10 @@ Error Main::test_setup() {
}
#ifndef PHYSICS_3D_DISABLED
- physics_server_3d_manager = memnew(PhysicsServer3DManager);
+ PhysicsServer3DManager::initialize_server_manager();
#endif // PHYSICS_3D_DISABLED
#ifndef PHYSICS_2D_DISABLED
- physics_server_2d_manager = memnew(PhysicsServer2DManager);
+ PhysicsServer2DManager::initialize_server_manager();
#endif // PHYSICS_2D_DISABLED
#ifndef NAVIGATION_2D_DISABLED
@@ -944,10 +881,10 @@ void Main::test_cleanup() {
memdelete(translation_server);
memdelete(tsman);
#ifndef PHYSICS_3D_DISABLED
- memdelete(physics_server_3d_manager);
+ PhysicsServer3DManager::finalize_server_manager();
#endif // PHYSICS_3D_DISABLED
#ifndef PHYSICS_2D_DISABLED
- memdelete(physics_server_2d_manager);
+ PhysicsServer2DManager::finalize_server_manager();
#endif // PHYSICS_2D_DISABLED
memdelete(globals);
@@ -3179,10 +3116,10 @@ Error Main::setup2(bool p_show_boot_logo) {
}
#ifndef PHYSICS_3D_DISABLED
- physics_server_3d_manager = memnew(PhysicsServer3DManager);
+ PhysicsServer3DManager::initialize_server_manager();
#endif // PHYSICS_3D_DISABLED
#ifndef PHYSICS_2D_DISABLED
- physics_server_2d_manager = memnew(PhysicsServer2DManager);
+ PhysicsServer2DManager::initialize_server_manager();
#endif // PHYSICS_2D_DISABLED
#ifndef NAVIGATION_2D_DISABLED
@@ -3371,10 +3308,10 @@ Error Main::setup2(bool p_show_boot_logo) {
memdelete(input);
memdelete(tsman);
#ifndef PHYSICS_3D_DISABLED
- memdelete(physics_server_3d_manager);
+ PhysicsServer3DManager::finalize_server_manager();
#endif // PHYSICS_3D_DISABLED
#ifndef PHYSICS_2D_DISABLED
- memdelete(physics_server_2d_manager);
+ PhysicsServer2DManager::finalize_server_manager();
#endif // PHYSICS_2D_DISABLED
return err;
@@ -3822,7 +3759,12 @@ Error Main::setup2(bool p_show_boot_logo) {
MAIN_PRINT("Main: Load Physics");
- initialize_physics();
+#ifndef PHYSICS_2D_DISABLED
+ PhysicsServer2DManager::initialize_server();
+#endif // PHYSICS_2D_DISABLED
+#ifndef PHYSICS_3D_DISABLED
+ PhysicsServer3DManager::initialize_server();
+#endif // PHYSICS_3D_DISABLED
register_server_singletons();
@@ -5269,7 +5211,13 @@ void Main::cleanup(bool p_force) {
NavigationServer3DManager::finalize_server();
NavigationServer3DManager::finalize_server_manager();
#endif // NAVIGATION_3D_DISABLED
- finalize_physics();
+
+#ifndef PHYSICS_2D_DISABLED
+ PhysicsServer2DManager::finalize_server();
+#endif // PHYSICS_2D_DISABLED
+#ifndef PHYSICS_3D_DISABLED
+ PhysicsServer3DManager::finalize_server();
+#endif // PHYSICS_3D_DISABLED
GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
@@ -5299,12 +5247,12 @@ void Main::cleanup(bool p_force) {
memdelete(input_map);
memdelete(translation_server);
memdelete(tsman);
-#ifndef PHYSICS_3D_DISABLED
- memdelete(physics_server_3d_manager);
-#endif // PHYSICS_3D_DISABLED
#ifndef PHYSICS_2D_DISABLED
- memdelete(physics_server_2d_manager);
+ PhysicsServer2DManager::finalize_server_manager();
#endif // PHYSICS_2D_DISABLED
+#ifndef PHYSICS_3D_DISABLED
+ PhysicsServer3DManager::finalize_server_manager();
+#endif // PHYSICS_3D_DISABLED
memdelete(globals);
if (OS::get_singleton()->is_restart_on_exit_set()) {
diff --git a/modules/godot_physics_2d/register_types.cpp b/modules/godot_physics_2d/register_types.cpp
index 830f0d5c26d..cf5d6230ffa 100644
--- a/modules/godot_physics_2d/register_types.cpp
+++ b/modules/godot_physics_2d/register_types.cpp
@@ -35,6 +35,7 @@
#include "core/config/project_settings.h"
#include "core/object/callable_mp.h"
#include "servers/physics_2d/physics_server_2d.h"
+#include "servers/physics_2d/physics_server_2d_manager.h"
#include "servers/physics_2d/physics_server_2d_wrap_mt.h"
static PhysicsServer2D *_createGodotPhysics2DCallback() {
diff --git a/modules/godot_physics_3d/register_types.cpp b/modules/godot_physics_3d/register_types.cpp
index 19292f9f908..4d54cbf7377 100644
--- a/modules/godot_physics_3d/register_types.cpp
+++ b/modules/godot_physics_3d/register_types.cpp
@@ -35,6 +35,7 @@
#include "core/config/project_settings.h"
#include "core/object/callable_mp.h"
#include "servers/physics_3d/physics_server_3d.h"
+#include "servers/physics_3d/physics_server_3d_manager.h"
#include "servers/physics_3d/physics_server_3d_wrap_mt.h"
static PhysicsServer3D *_createGodotPhysics3DCallback() {
diff --git a/modules/jolt_physics/register_types.cpp b/modules/jolt_physics/register_types.cpp
index 51aadf0607f..dec74a28763 100644
--- a/modules/jolt_physics/register_types.cpp
+++ b/modules/jolt_physics/register_types.cpp
@@ -36,6 +36,7 @@
#include "core/config/project_settings.h"
#include "core/object/callable_mp.h"
+#include "servers/physics_3d/physics_server_3d_manager.h"
#include "servers/physics_3d/physics_server_3d_wrap_mt.h"
PhysicsServer3D *create_jolt_physics_server() {
diff --git a/servers/physics_2d/physics_server_2d.cpp b/servers/physics_2d/physics_server_2d.cpp
index 7084efb141d..d414c76865c 100644
--- a/servers/physics_2d/physics_server_2d.cpp
+++ b/servers/physics_2d/physics_server_2d.cpp
@@ -349,91 +349,3 @@ PhysicsServer2D::PhysicsServer2D() {
PhysicsServer2D::~PhysicsServer2D() {
singleton = nullptr;
}
-
-PhysicsServer2DManager *PhysicsServer2DManager::singleton = nullptr;
-const String PhysicsServer2DManager::setting_property_name(PNAME("physics/2d/physics_engine"));
-
-void PhysicsServer2DManager::on_servers_changed() {
- String physics_servers("DEFAULT");
- for (int i = get_servers_count() - 1; 0 <= i; --i) {
- physics_servers += "," + get_server_name(i);
- }
- ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers));
- ProjectSettings::get_singleton()->set_restart_if_changed(setting_property_name, true);
- ProjectSettings::get_singleton()->set_as_basic(setting_property_name, true);
-}
-
-void PhysicsServer2DManager::_bind_methods() {
- ClassDB::bind_method(D_METHOD("register_server", "name", "create_callback"), &PhysicsServer2DManager::register_server);
- ClassDB::bind_method(D_METHOD("set_default_server", "name", "priority"), &PhysicsServer2DManager::set_default_server);
-}
-
-PhysicsServer2DManager *PhysicsServer2DManager::get_singleton() {
- return singleton;
-}
-
-void PhysicsServer2DManager::register_server(const String &p_name, const Callable &p_create_callback) {
- //ERR_FAIL_COND(!p_create_callback.is_valid());
- ERR_FAIL_COND(find_server_id(p_name) != -1);
- physics_2d_servers.push_back(ClassInfo(p_name, p_create_callback));
- on_servers_changed();
-}
-
-void PhysicsServer2DManager::set_default_server(const String &p_name, int p_priority) {
- const int id = find_server_id(p_name);
- ERR_FAIL_COND(id == -1); // Not found
- if (default_server_priority < p_priority) {
- default_server_id = id;
- default_server_priority = p_priority;
- }
-}
-
-int PhysicsServer2DManager::find_server_id(const String &p_name) {
- for (int i = physics_2d_servers.size() - 1; 0 <= i; --i) {
- if (p_name == physics_2d_servers[i].name) {
- return i;
- }
- }
- return -1;
-}
-
-int PhysicsServer2DManager::get_servers_count() {
- return physics_2d_servers.size();
-}
-
-String PhysicsServer2DManager::get_server_name(int p_id) {
- ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
- return physics_2d_servers[p_id].name;
-}
-
-PhysicsServer2D *PhysicsServer2DManager::new_default_server() {
- if (default_server_id == -1) {
- return nullptr;
- }
- Variant ret;
- Callable::CallError ce;
- physics_2d_servers[default_server_id].create_callback.callp(nullptr, 0, ret, ce);
- ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
- return Object::cast_to<PhysicsServer2D>(ret.get_validated_object());
-}
-
-PhysicsServer2D *PhysicsServer2DManager::new_server(const String &p_name) {
- int id = find_server_id(p_name);
- if (id == -1) {
- return nullptr;
- } else {
- Variant ret;
- Callable::CallError ce;
- physics_2d_servers[id].create_callback.callp(nullptr, 0, ret, ce);
- ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
- return Object::cast_to<PhysicsServer2D>(ret.get_validated_object());
- }
-}
-
-PhysicsServer2DManager::PhysicsServer2DManager() {
- singleton = this;
-}
-
-PhysicsServer2DManager::~PhysicsServer2DManager() {
- singleton = nullptr;
-}
diff --git a/servers/physics_2d/physics_server_2d.h b/servers/physics_2d/physics_server_2d.h
index 7870e09959c..d5d9bff49e3 100644
--- a/servers/physics_2d/physics_server_2d.h
+++ b/servers/physics_2d/physics_server_2d.h
@@ -293,57 +293,6 @@ public:
~PhysicsServer2D();
};
-class PhysicsServer2DManager : public Object {
- GDCLASS(PhysicsServer2DManager, Object);
-
- static PhysicsServer2DManager *singleton;
-
- struct ClassInfo {
- String name;
- Callable create_callback;
-
- ClassInfo() {}
-
- ClassInfo(String p_name, Callable p_create_callback) :
- name(p_name),
- create_callback(p_create_callback) {}
-
- ClassInfo(const ClassInfo &p_ci) :
- name(p_ci.name),
- create_callback(p_ci.create_callback) {}
-
- void operator=(const ClassInfo &p_ci) {
- name = p_ci.name;
- create_callback = p_ci.create_callback;
- }
- };
-
- Vector<ClassInfo> physics_2d_servers;
- int default_server_id = -1;
- int default_server_priority = -1;
-
- void on_servers_changed();
-
-protected:
- static void _bind_methods();
-
-public:
- static const String setting_property_name;
-
- static PhysicsServer2DManager *get_singleton();
-
- void register_server(const String &p_name, const Callable &p_create_callback);
- void set_default_server(const String &p_name, int p_priority = 0);
- int find_server_id(const String &p_name);
- int get_servers_count();
- String get_server_name(int p_id);
- PhysicsServer2D *new_default_server();
- PhysicsServer2D *new_server(const String &p_name);
-
- PhysicsServer2DManager();
- ~PhysicsServer2DManager();
-};
-
VARIANT_ENUM_CAST_EXT(PS2DE::ShapeType, PhysicsServer2D::ShapeType);
VARIANT_ENUM_CAST_EXT(PS2DE::SpaceParameter, PhysicsServer2D::SpaceParameter);
VARIANT_ENUM_CAST_EXT(PS2DE::AreaParameter, PhysicsServer2D::AreaParameter);
diff --git a/servers/physics_2d/physics_server_2d_manager.cpp b/servers/physics_2d/physics_server_2d_manager.cpp
new file mode 100644
index 00000000000..c393880a3e4
--- /dev/null
+++ b/servers/physics_2d/physics_server_2d_manager.cpp
@@ -0,0 +1,158 @@
+/**************************************************************************/
+/* physics_server_2d_manager.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* 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 "physics_server_2d_manager.h"
+
+#include "core/config/project_settings.h"
+#include "core/object/class_db.h"
+#include "servers/physics_2d/physics_server_2d.h"
+#include "servers/physics_2d/physics_server_2d_dummy.h"
+
+static PhysicsServer2D *physics_server_2d = nullptr;
+
+PhysicsServer2DManager *PhysicsServer2DManager::singleton = nullptr;
+const String PhysicsServer2DManager::setting_property_name(PNAME("physics/2d/physics_engine"));
+
+void PhysicsServer2DManager::initialize_server_manager() {
+ ERR_FAIL_COND(singleton != nullptr);
+ singleton = memnew(PhysicsServer2DManager);
+}
+
+void PhysicsServer2DManager::finalize_server_manager() {
+ ERR_FAIL_NULL(singleton);
+ memdelete(singleton);
+ singleton = nullptr;
+}
+
+void PhysicsServer2DManager::initialize_server() {
+#ifndef PHYSICS_2D_DISABLED
+ // 2D Physics server
+ physics_server_2d = PhysicsServer2DManager::get_singleton()->new_server(
+ GLOBAL_GET(PhysicsServer2DManager::get_singleton()->setting_property_name));
+ if (!physics_server_2d) {
+ // Physics server not found, Use the default physics
+ physics_server_2d = PhysicsServer2DManager::get_singleton()->new_default_server();
+ }
+
+ // Fall back to dummy if no default server has been registered.
+ if (!physics_server_2d) {
+ WARN_PRINT(vformat("Falling back to dummy PhysicsServer2D; 2D physics functionality will be disabled. If this is intended, set the %s project setting to Dummy.", PhysicsServer2DManager::setting_property_name));
+ physics_server_2d = memnew(PhysicsServer2DDummy);
+ }
+
+ // Should be impossible, but make sure it's not null.
+ ERR_FAIL_NULL_MSG(physics_server_2d, "Failed to initialize PhysicsServer2D.");
+ physics_server_2d->init();
+#endif // PHYSICS_2D_DISABLED
+}
+
+void PhysicsServer2DManager::finalize_server() {
+ ERR_FAIL_NULL(physics_server_2d);
+ physics_server_2d->finish();
+ memdelete(physics_server_2d);
+ physics_server_2d = nullptr;
+}
+
+void PhysicsServer2DManager::on_servers_changed() {
+ String physics_servers("DEFAULT");
+ for (int i = get_servers_count() - 1; 0 <= i; --i) {
+ physics_servers += "," + get_server_name(i);
+ }
+ ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers));
+ ProjectSettings::get_singleton()->set_restart_if_changed(setting_property_name, true);
+ ProjectSettings::get_singleton()->set_as_basic(setting_property_name, true);
+}
+
+void PhysicsServer2DManager::_bind_methods() {
+ ClassDB::bind_method(D_METHOD("register_server", "name", "create_callback"), &PhysicsServer2DManager::register_server);
+ ClassDB::bind_method(D_METHOD("set_default_server", "name", "priority"), &PhysicsServer2DManager::set_default_server);
+}
+
+PhysicsServer2DManager *PhysicsServer2DManager::get_singleton() {
+ return singleton;
+}
+
+void PhysicsServer2DManager::register_server(const String &p_name, const Callable &p_create_callback) {
+ //ERR_FAIL_COND(!p_create_callback.is_valid());
+ ERR_FAIL_COND(find_server_id(p_name) != -1);
+ physics_2d_servers.push_back(ClassInfo(p_name, p_create_callback));
+ on_servers_changed();
+}
+
+void PhysicsServer2DManager::set_default_server(const String &p_name, int p_priority) {
+ const int id = find_server_id(p_name);
+ ERR_FAIL_COND(id == -1); // Not found
+ if (default_server_priority < p_priority) {
+ default_server_id = id;
+ default_server_priority = p_priority;
+ }
+}
+
+int PhysicsServer2DManager::find_server_id(const String &p_name) {
+ for (int i = physics_2d_servers.size() - 1; 0 <= i; --i) {
+ if (p_name == physics_2d_servers[i].name) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+int PhysicsServer2DManager::get_servers_count() {
+ return physics_2d_servers.size();
+}
+
+String PhysicsServer2DManager::get_server_name(int p_id) {
+ ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
+ return physics_2d_servers[p_id].name;
+}
+
+PhysicsServer2D *PhysicsServer2DManager::new_default_server() {
+ if (default_server_id == -1) {
+ return nullptr;
+ }
+ Variant ret;
+ Callable::CallError ce;
+ physics_2d_servers[default_server_id].create_callback.callp(nullptr, 0, ret, ce);
+ ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
+ return Object::cast_to<PhysicsServer2D>(ret.get_validated_object());
+}
+
+PhysicsServer2D *PhysicsServer2DManager::new_server(const String &p_name) {
+ int id = find_server_id(p_name);
+ if (id == -1) {
+ return nullptr;
+ } else {
+ Variant ret;
+ Callable::CallError ce;
+ physics_2d_servers[id].create_callback.callp(nullptr, 0, ret, ce);
+ ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
+ return Object::cast_to<PhysicsServer2D>(ret.get_validated_object());
+ }
+}
diff --git a/servers/physics_2d/physics_server_2d_manager.h b/servers/physics_2d/physics_server_2d_manager.h
new file mode 100644
index 00000000000..89e2a98b4eb
--- /dev/null
+++ b/servers/physics_2d/physics_server_2d_manager.h
@@ -0,0 +1,89 @@
+/**************************************************************************/
+/* physics_server_2d_manager.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* 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. */
+/**************************************************************************/
+
+#pragma once
+
+#include "core/object/object.h"
+
+class PhysicsServer2D;
+
+class PhysicsServer2DManager : public Object {
+ GDCLASS(PhysicsServer2DManager, Object);
+
+ static PhysicsServer2DManager *singleton;
+
+ struct ClassInfo {
+ String name;
+ Callable create_callback;
+
+ ClassInfo() {}
+
+ ClassInfo(String p_name, Callable p_create_callback) :
+ name(p_name),
+ create_callback(p_create_callback) {}
+
+ ClassInfo(const ClassInfo &p_ci) :
+ name(p_ci.name),
+ create_callback(p_ci.create_callback) {}
+
+ void operator=(const ClassInfo &p_ci) {
+ name = p_ci.name;
+ create_callback = p_ci.create_callback;
+ }
+ };
+
+ Vector<ClassInfo> physics_2d_servers;
+ int default_server_id = -1;
+ int default_server_priority = -1;
+
+ void on_servers_changed();
+
+protected:
+ static void _bind_methods();
+
+public:
+ static const String setting_property_name;
+
+ static PhysicsServer2DManager *get_singleton();
+
+ void register_server(const String &p_name, const Callable &p_create_callback);
+ void set_default_server(const String &p_name, int p_priority = 0);
+ int find_server_id(const String &p_name);
+ int get_servers_count();
+ String get_server_name(int p_id);
+ PhysicsServer2D *new_default_server();
+ PhysicsServer2D *new_server(const String &p_name);
+
+ static void initialize_server();
+ static void finalize_server();
+
+ static void initialize_server_manager();
+ static void finalize_server_manager();
+};
diff --git a/servers/physics_3d/physics_server_3d.cpp b/servers/physics_3d/physics_server_3d.cpp
index 4521b51fc5e..bba8ec29afc 100644
--- a/servers/physics_3d/physics_server_3d.cpp
+++ b/servers/physics_3d/physics_server_3d.cpp
@@ -549,91 +549,3 @@ PhysicsServer3D::PhysicsServer3D() {
PhysicsServer3D::~PhysicsServer3D() {
singleton = nullptr;
}
-
-PhysicsServer3DManager *PhysicsServer3DManager::singleton = nullptr;
-const String PhysicsServer3DManager::setting_property_name(PNAME("physics/3d/physics_engine"));
-
-void PhysicsServer3DManager::on_servers_changed() {
- String physics_servers2("DEFAULT");
- for (int i = get_servers_count() - 1; 0 <= i; --i) {
- physics_servers2 += "," + get_server_name(i);
- }
- ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers2));
- ProjectSettings::get_singleton()->set_restart_if_changed(setting_property_name, true);
- ProjectSettings::get_singleton()->set_as_basic(setting_property_name, true);
-}
-
-void PhysicsServer3DManager::_bind_methods() {
- ClassDB::bind_method(D_METHOD("register_server", "name", "create_callback"), &PhysicsServer3DManager::register_server);
- ClassDB::bind_method(D_METHOD("set_default_server", "name", "priority"), &PhysicsServer3DManager::set_default_server);
-}
-
-PhysicsServer3DManager *PhysicsServer3DManager::get_singleton() {
- return singleton;
-}
-
-void PhysicsServer3DManager::register_server(const String &p_name, const Callable &p_create_callback) {
- //ERR_FAIL_COND(!p_create_callback.is_valid());
- ERR_FAIL_COND(find_server_id(p_name) != -1);
- physics_servers.push_back(ClassInfo(p_name, p_create_callback));
- on_servers_changed();
-}
-
-void PhysicsServer3DManager::set_default_server(const String &p_name, int p_priority) {
- const int id = find_server_id(p_name);
- ERR_FAIL_COND(id == -1); // Not found
- if (default_server_priority < p_priority) {
- default_server_id = id;
- default_server_priority = p_priority;
- }
-}
-
-int PhysicsServer3DManager::find_server_id(const String &p_name) {
- for (int i = physics_servers.size() - 1; 0 <= i; --i) {
- if (p_name == physics_servers[i].name) {
- return i;
- }
- }
- return -1;
-}
-
-int PhysicsServer3DManager::get_servers_count() {
- return physics_servers.size();
-}
-
-String PhysicsServer3DManager::get_server_name(int p_id) {
- ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
- return physics_servers[p_id].name;
-}
-
-PhysicsServer3D *PhysicsServer3DManager::new_default_server() {
- if (default_server_id == -1) {
- return nullptr;
- }
- Variant ret;
- Callable::CallError ce;
- physics_servers[default_server_id].create_callback.callp(nullptr, 0, ret, ce);
- ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
- return Object::cast_to<PhysicsServer3D>(ret.get_validated_object());
-}
-
-PhysicsServer3D *PhysicsServer3DManager::new_server(const String &p_name) {
- int id = find_server_id(p_name);
- if (id == -1) {
- return nullptr;
- } else {
- Variant ret;
- Callable::CallError ce;
- physics_servers[id].create_callback.callp(nullptr, 0, ret, ce);
- ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
- return Object::cast_to<PhysicsServer3D>(ret.get_validated_object());
- }
-}
-
-PhysicsServer3DManager::PhysicsServer3DManager() {
- singleton = this;
-}
-
-PhysicsServer3DManager::~PhysicsServer3DManager() {
- singleton = nullptr;
-}
diff --git a/servers/physics_3d/physics_server_3d.h b/servers/physics_3d/physics_server_3d.h
index 430764bd2e0..8acbe395c70 100644
--- a/servers/physics_3d/physics_server_3d.h
+++ b/servers/physics_3d/physics_server_3d.h
@@ -395,57 +395,6 @@ public:
~PhysicsServer3D();
};
-class PhysicsServer3DManager : public Object {
- GDCLASS(PhysicsServer3DManager, Object);
-
- static PhysicsServer3DManager *singleton;
-
- struct ClassInfo {
- String name;
- Callable create_callback;
-
- ClassInfo() {}
-
- ClassInfo(String p_name, Callable p_create_callback) :
- name(p_name),
- create_callback(p_create_callback) {}
-
- ClassInfo(const ClassInfo &p_ci) :
- name(p_ci.name),
- create_callback(p_ci.create_callback) {}
-
- void operator=(const ClassInfo &p_ci) {
- name = p_ci.name;
- create_callback = p_ci.create_callback;
- }
- };
-
- Vector<ClassInfo> physics_servers;
- int default_server_id = -1;
- int default_server_priority = -1;
-
- void on_servers_changed();
-
-protected:
- static void _bind_methods();
-
-public:
- static const String setting_property_name;
-
- static PhysicsServer3DManager *get_singleton();
-
- void register_server(const String &p_name, const Callable &p_create_callback);
- void set_default_server(const String &p_name, int p_priority = 0);
- int find_server_id(const String &p_name);
- int get_servers_count();
- String get_server_name(int p_id);
- PhysicsServer3D *new_default_server();
- PhysicsServer3D *new_server(const String &p_name);
-
- PhysicsServer3DManager();
- ~PhysicsServer3DManager();
-};
-
VARIANT_ENUM_CAST_EXT(PS3DE::ShapeType, PhysicsServer3D::ShapeType);
VARIANT_ENUM_CAST_EXT(PS3DE::SpaceParameter, PhysicsServer3D::SpaceParameter);
VARIANT_ENUM_CAST_EXT(PS3DE::AreaParameter, PhysicsServer3D::AreaParameter);
diff --git a/servers/physics_3d/physics_server_3d_manager.cpp b/servers/physics_3d/physics_server_3d_manager.cpp
new file mode 100644
index 00000000000..cc70dfd3de8
--- /dev/null
+++ b/servers/physics_3d/physics_server_3d_manager.cpp
@@ -0,0 +1,158 @@
+/**************************************************************************/
+/* physics_server_3d_manager.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* 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 "physics_server_3d_manager.h"
+
+#include "core/config/project_settings.h"
+#include "core/object/class_db.h"
+#include "servers/physics_3d/physics_server_3d.h"
+#include "servers/physics_3d/physics_server_3d_dummy.h"
+
+static PhysicsServer3D *physics_server_3d = nullptr;
+
+PhysicsServer3DManager *PhysicsServer3DManager::singleton = nullptr;
+const String PhysicsServer3DManager::setting_property_name(PNAME("physics/3d/physics_engine"));
+
+void PhysicsServer3DManager::initialize_server_manager() {
+ ERR_FAIL_COND(singleton != nullptr);
+ singleton = memnew(PhysicsServer3DManager);
+}
+
+void PhysicsServer3DManager::finalize_server_manager() {
+ ERR_FAIL_NULL(singleton);
+ memdelete(singleton);
+ singleton = nullptr;
+}
+
+void PhysicsServer3DManager::initialize_server() {
+#ifndef PHYSICS_3D_DISABLED
+ /// 3D Physics Server
+ physics_server_3d = PhysicsServer3DManager::get_singleton()->new_server(
+ GLOBAL_GET(PhysicsServer3DManager::setting_property_name));
+ if (!physics_server_3d) {
+ // Physics server not found, Use the default physics
+ physics_server_3d = PhysicsServer3DManager::get_singleton()->new_default_server();
+ }
+
+ // Fall back to dummy if no default server has been registered.
+ if (!physics_server_3d) {
+ WARN_PRINT(vformat("Falling back to dummy PhysicsServer3D; 3D physics functionality will be disabled. If this is intended, set the %s project setting to Dummy.", PhysicsServer3DManager::setting_property_name));
+ physics_server_3d = memnew(PhysicsServer3DDummy);
+ }
+
+ // Should be impossible, but make sure it's not null.
+ ERR_FAIL_NULL_MSG(physics_server_3d, "Failed to initialize PhysicsServer3D.");
+ physics_server_3d->init();
+#endif // PHYSICS_3D_DISABLED
+}
+
+void PhysicsServer3DManager::finalize_server() {
+ ERR_FAIL_NULL(physics_server_3d);
+ physics_server_3d->finish();
+ memdelete(physics_server_3d);
+ physics_server_3d = nullptr;
+}
+
+void PhysicsServer3DManager::on_servers_changed() {
+ String physics_servers2("DEFAULT");
+ for (int i = get_servers_count() - 1; 0 <= i; --i) {
+ physics_servers2 += "," + get_server_name(i);
+ }
+ ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers2));
+ ProjectSettings::get_singleton()->set_restart_if_changed(setting_property_name, true);
+ ProjectSettings::get_singleton()->set_as_basic(setting_property_name, true);
+}
+
+void PhysicsServer3DManager::_bind_methods() {
+ ClassDB::bind_method(D_METHOD("register_server", "name", "create_callback"), &PhysicsServer3DManager::register_server);
+ ClassDB::bind_method(D_METHOD("set_default_server", "name", "priority"), &PhysicsServer3DManager::set_default_server);
+}
+
+PhysicsServer3DManager *PhysicsServer3DManager::get_singleton() {
+ return singleton;
+}
+
+void PhysicsServer3DManager::register_server(const String &p_name, const Callable &p_create_callback) {
+ //ERR_FAIL_COND(!p_create_callback.is_valid());
+ ERR_FAIL_COND(find_server_id(p_name) != -1);
+ physics_servers.push_back(ClassInfo(p_name, p_create_callback));
+ on_servers_changed();
+}
+
+void PhysicsServer3DManager::set_default_server(const String &p_name, int p_priority) {
+ const int id = find_server_id(p_name);
+ ERR_FAIL_COND(id == -1); // Not found
+ if (default_server_priority < p_priority) {
+ default_server_id = id;
+ default_server_priority = p_priority;
+ }
+}
+
+int PhysicsServer3DManager::find_server_id(const String &p_name) {
+ for (int i = physics_servers.size() - 1; 0 <= i; --i) {
+ if (p_name == physics_servers[i].name) {
+ return i;
+ }
+ }
+ return -1;
+}
+
+int PhysicsServer3DManager::get_servers_count() {
+ return physics_servers.size();
+}
+
+String PhysicsServer3DManager::get_server_name(int p_id) {
+ ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
+ return physics_servers[p_id].name;
+}
+
+PhysicsServer3D *PhysicsServer3DManager::new_default_server() {
+ if (default_server_id == -1) {
+ return nullptr;
+ }
+ Variant ret;
+ Callable::CallError ce;
+ physics_servers[default_server_id].create_callback.callp(nullptr, 0, ret, ce);
+ ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
+ return Object::cast_to<PhysicsServer3D>(ret.get_validated_object());
+}
+
+PhysicsServer3D *PhysicsServer3DManager::new_server(const String &p_name) {
+ int id = find_server_id(p_name);
+ if (id == -1) {
+ return nullptr;
+ } else {
+ Variant ret;
+ Callable::CallError ce;
+ physics_servers[id].create_callback.callp(nullptr, 0, ret, ce);
+ ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
+ return Object::cast_to<PhysicsServer3D>(ret.get_validated_object());
+ }
+}
diff --git a/servers/physics_3d/physics_server_3d_manager.h b/servers/physics_3d/physics_server_3d_manager.h
new file mode 100644
index 00000000000..7324ab28b98
--- /dev/null
+++ b/servers/physics_3d/physics_server_3d_manager.h
@@ -0,0 +1,89 @@
+/**************************************************************************/
+/* physics_server_3d_manager.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* 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. */
+/**************************************************************************/
+
+#pragma once
+
+#include "core/object/object.h"
+
+class PhysicsServer3D;
+
+class PhysicsServer3DManager : public Object {
+ GDCLASS(PhysicsServer3DManager, Object);
+
+ static PhysicsServer3DManager *singleton;
+
+ struct ClassInfo {
+ String name;
+ Callable create_callback;
+
+ ClassInfo() {}
+
+ ClassInfo(String p_name, Callable p_create_callback) :
+ name(p_name),
+ create_callback(p_create_callback) {}
+
+ ClassInfo(const ClassInfo &p_ci) :
+ name(p_ci.name),
+ create_callback(p_ci.create_callback) {}
+
+ void operator=(const ClassInfo &p_ci) {
+ name = p_ci.name;
+ create_callback = p_ci.create_callback;
+ }
+ };
+
+ Vector<ClassInfo> physics_servers;
+ int default_server_id = -1;
+ int default_server_priority = -1;
+
+ void on_servers_changed();
+
+protected:
+ static void _bind_methods();
+
+public:
+ static const String setting_property_name;
+
+ static PhysicsServer3DManager *get_singleton();
+
+ void register_server(const String &p_name, const Callable &p_create_callback);
+ void set_default_server(const String &p_name, int p_priority = 0);
+ int find_server_id(const String &p_name);
+ int get_servers_count();
+ String get_server_name(int p_id);
+ PhysicsServer3D *new_default_server();
+ PhysicsServer3D *new_server(const String &p_name);
+
+ static void initialize_server();
+ static void finalize_server();
+
+ static void initialize_server_manager();
+ static void finalize_server_manager();
+};
diff --git a/servers/register_server_types.cpp b/servers/register_server_types.cpp
index de403337857..d3b0c68e5f6 100644
--- a/servers/register_server_types.cpp
+++ b/servers/register_server_types.cpp
@@ -92,6 +92,7 @@
#include "servers/physics_2d/physics_server_2d.h"
#include "servers/physics_2d/physics_server_2d_dummy.h"
#include "servers/physics_2d/physics_server_2d_extension.h"
+#include "servers/physics_2d/physics_server_2d_manager.h"
#endif // PHYSICS_2D_DISABLED
// 3D physics and navigation.
@@ -102,6 +103,7 @@
#include "servers/physics_3d/physics_server_3d.h"
#include "servers/physics_3d/physics_server_3d_dummy.h"
#include "servers/physics_3d/physics_server_3d_extension.h"
+#include "servers/physics_3d/physics_server_3d_manager.h"
#endif // PHYSICS_3D_DISABLED
// XR
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index 355e9f4e328..274598891d5 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -64,10 +64,12 @@
#ifndef PHYSICS_2D_DISABLED
#include "servers/physics_2d/physics_server_2d.h"
#include "servers/physics_2d/physics_server_2d_dummy.h"
+#include "servers/physics_2d/physics_server_2d_manager.h"
#endif // PHYSICS_2D_DISABLED
#ifndef PHYSICS_3D_DISABLED
#include "servers/physics_3d/physics_server_3d.h"
#include "servers/physics_3d/physics_server_3d_dummy.h"
+#include "servers/physics_3d/physics_server_3d_manager.h"
#endif // PHYSICS_3D_DISABLED
#include "modules/modules_tests.gen.h" // IWYU pragma: keep // TODO: Migrate module tests to compilation files.