From 7a9ca08f16c500aa0caccc21a8e42564f962971a Mon Sep 17 00:00:00 2001 From: AndreaCatania Date: Sat, 21 Oct 2017 13:02:06 +0200 Subject: [PATCH] Implemented physics plug Moved init_physics Implemented physics 2D plug Fix clang Fix clang Fix static check Fix clang Fix static check Moved physics server initialization Moved physics server settings initialization --- main/main.cpp | 40 ++++++++++++++- platform/android/os_android.cpp | 5 -- platform/android/os_android.h | 5 -- platform/haiku/os_haiku.cpp | 13 ----- platform/haiku/os_haiku.h | 4 -- platform/iphone/os_iphone.cpp | 13 ----- platform/iphone/os_iphone.h | 5 -- platform/javascript/os_javascript.cpp | 5 -- platform/javascript/os_javascript.h | 4 -- platform/osx/crash_handler_osx.mm | 1 + platform/osx/os_osx.h | 6 --- platform/osx/os_osx.mm | 14 ------ platform/server/os_server.cpp | 12 ----- platform/server/os_server.h | 5 -- platform/uwp/os_uwp.cpp | 13 ----- platform/uwp/os_uwp.h | 4 -- platform/windows/crash_handler_win.cpp | 1 + platform/windows/os_windows.cpp | 13 ----- platform/windows/os_windows.h | 7 +-- platform/x11/crash_handler_x11.cpp | 1 + platform/x11/os_x11.cpp | 13 ----- platform/x11/os_x11.h | 5 -- servers/physics_2d_server.cpp | 67 ++++++++++++++++++++++++++ servers/physics_2d_server.h | 37 ++++++++++++++ servers/physics_server.cpp | 67 ++++++++++++++++++++++++++ servers/physics_server.h | 37 ++++++++++++++ servers/register_server_types.cpp | 39 ++++++++++++--- servers/register_server_types.h | 2 + 28 files changed, 286 insertions(+), 152 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 68e518ae3da..a9a0295abda 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -47,6 +47,8 @@ #include "scene/main/scene_tree.h" #include "servers/arvr_server.h" #include "servers/audio_server.h" +#include "servers/physics_2d_server.h" +#include "servers/physics_server.h" #include "io/resource_loader.h" #include "script_language.h" @@ -84,6 +86,8 @@ static bool _start_success = false; static ScriptDebugger *script_debugger = NULL; AudioServer *audio_server = NULL; ARVRServer *arvr_server = NULL; +PhysicsServer *physics_server = NULL; +Physics2DServer *physics_2d_server = NULL; static MessageQueue *message_queue = NULL; static Performance *performance = NULL; @@ -120,6 +124,35 @@ static int fixed_fps = -1; static OS::ProcessID allow_focus_steal_pid = 0; +void initialize_physics() { + + /// 3D Physics Server + physics_server = PhysicsServerManager::new_server(ProjectSettings::get_singleton()->get(PhysicsServerManager::setting_property_name)); + if (!physics_server) { + // Physics server not found, Use the default physics + physics_server = PhysicsServerManager::new_default_server(); + } + ERR_FAIL_COND(!physics_server); + physics_server->init(); + + /// 2D Physics server + physics_2d_server = Physics2DServerManager::new_server(ProjectSettings::get_singleton()->get(Physics2DServerManager::setting_property_name)); + if (!physics_2d_server) { + // Physics server not found, Use the default physics + physics_2d_server = Physics2DServerManager::new_default_server(); + } + ERR_FAIL_COND(!physics_2d_server); + physics_2d_server->init(); +} + +void finalize_physics() { + physics_server->finish(); + memdelete(physics_server); + + physics_2d_server->finish(); + memdelete(physics_2d_server); +} + static String unescape_cmdline(const String &p_str) { return p_str.replace("%20", " "); @@ -1072,9 +1105,13 @@ Error Main::setup2(Thread::ID p_main_tid_override) { OS::get_singleton()->enable_for_stealing_focus(allow_focus_steal_pid); } - MAIN_PRINT("Main: Load Scripts, Modules, Drivers"); + MAIN_PRINT("Main: Load Modules, Physics, Drivers, Scripts"); register_module_types(); + + initialize_physics(); + register_server_singletons(); + register_driver_types(); ScriptServer::init_languages(); @@ -1791,6 +1828,7 @@ void Main::cleanup() { unregister_server_types(); OS::get_singleton()->finalize(); + finalize_physics(); if (packed_data) memdelete(packed_data); diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index 473a0930770..853392cba55 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -155,11 +155,6 @@ void OS_Android::initialize(const VideoMode &p_desired, int p_video_driver, int AudioDriverManager::initialize(p_audio_driver); - physics_server = memnew(PhysicsServerSW); - physics_server->init(); - physics_2d_server = Physics2DServerWrapMT::init_server(); - physics_2d_server->init(); - input = memnew(InputDefault); input->set_fallback_mapping("Default Android Gamepad"); diff --git a/platform/android/os_android.h b/platform/android/os_android.h index 0c78c198a80..d9a66b4e3a3 100644 --- a/platform/android/os_android.h +++ b/platform/android/os_android.h @@ -38,9 +38,6 @@ #include "os/main_loop.h" //#include "power_android.h" #include "servers/audio_server.h" -#include "servers/physics/physics_server_sw.h" -#include "servers/physics_2d/physics_2d_server_sw.h" -#include "servers/physics_2d/physics_2d_server_wrap_mt.h" #include "servers/visual/rasterizer.h" #ifdef ANDROID_NATIVE_ACTIVITY @@ -106,8 +103,6 @@ private: bool use_16bits_fbo; VisualServer *visual_server; - PhysicsServer *physics_server; - Physics2DServer *physics_2d_server; mutable String data_dir_cache; diff --git a/platform/haiku/os_haiku.cpp b/platform/haiku/os_haiku.cpp index 1d52752f21c..7a681d46f71 100644 --- a/platform/haiku/os_haiku.cpp +++ b/platform/haiku/os_haiku.cpp @@ -130,13 +130,6 @@ void OS_Haiku::initialize(const VideoMode &p_desired, int p_video_driver, int p_ window->Show(); visual_server->init(); - physics_server = memnew(PhysicsServerSW); - physics_server->init(); - physics_2d_server = memnew(Physics2DServerSW); - // TODO: enable multithreaded PS - //physics_2d_server = Physics2DServerWrapMT::init_server(); - physics_2d_server->init(); - AudioDriverManager::initialize(p_audio_driver); power_manager = memnew(PowerHaiku); @@ -153,12 +146,6 @@ void OS_Haiku::finalize() { memdelete(visual_server); memdelete(rasterizer); - physics_server->finish(); - memdelete(physics_server); - - physics_2d_server->finish(); - memdelete(physics_2d_server); - memdelete(input); #if defined(OPENGL_ENABLED) diff --git a/platform/haiku/os_haiku.h b/platform/haiku/os_haiku.h index d929f7e43bf..501650c94f2 100644 --- a/platform/haiku/os_haiku.h +++ b/platform/haiku/os_haiku.h @@ -38,8 +38,6 @@ #include "main/input_default.h" #include "power_haiku.h" #include "servers/audio_server.h" -#include "servers/physics_2d/physics_2d_server_sw.h" -#include "servers/physics_server.h" #include "servers/visual/rasterizer.h" #include "servers/visual_server.h" @@ -52,8 +50,6 @@ private: Rasterizer *rasterizer; VisualServer *visual_server; VideoMode current_video_mode; - PhysicsServer *physics_server; - Physics2DServer *physics_2d_server; PowerHaiku *power_manager; #ifdef MEDIA_KIT_ENABLED diff --git a/platform/iphone/os_iphone.cpp b/platform/iphone/os_iphone.cpp index 08792b86314..09654f343aa 100644 --- a/platform/iphone/os_iphone.cpp +++ b/platform/iphone/os_iphone.cpp @@ -136,13 +136,6 @@ void OSIPhone::initialize(const VideoMode &p_desired, int p_video_driver, int p_ AudioDriverManager::add_driver(&audio_driver); AudioDriverManager::initialize(p_audio_driver); - // init physics servers - physics_server = memnew(PhysicsServerSW); - physics_server->init(); - //physics_2d_server = memnew( Physics2DServerSW ); - physics_2d_server = Physics2DServerWrapMT::init_server(); - physics_2d_server->init(); - input = memnew(InputDefault); /* @@ -382,12 +375,6 @@ void OSIPhone::finalize() { memdelete(visual_server); // memdelete(rasterizer); - physics_server->finish(); - memdelete(physics_server); - - physics_2d_server->finish(); - memdelete(physics_2d_server); - memdelete(input); }; diff --git a/platform/iphone/os_iphone.h b/platform/iphone/os_iphone.h index e70ac9ba98e..eccd294d63f 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -41,9 +41,6 @@ #include "in_app_store.h" #include "main/input_default.h" #include "servers/audio_server.h" -#include "servers/physics/physics_server_sw.h" -#include "servers/physics_2d/physics_2d_server_sw.h" -#include "servers/physics_2d/physics_2d_server_wrap_mt.h" #include "servers/visual/rasterizer.h" #include "servers/visual_server.h" @@ -66,8 +63,6 @@ private: uint8_t supported_orientations; VisualServer *visual_server; - PhysicsServer *physics_server; - Physics2DServer *physics_2d_server; AudioDriverCoreAudio audio_driver; diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index f6446e77dab..3a57de26462 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -480,11 +480,6 @@ void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, i print_line("Init Physicsserver"); - physics_server = memnew(PhysicsServerSW); - physics_server->init(); - physics_2d_server = memnew(Physics2DServerSW); - physics_2d_server->init(); - input = memnew(InputDefault); _input = input; diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h index 1c939d3fd56..908a252905d 100644 --- a/platform/javascript/os_javascript.h +++ b/platform/javascript/os_javascript.h @@ -39,8 +39,6 @@ #include "os/main_loop.h" #include "power_javascript.h" #include "servers/audio_server.h" -#include "servers/physics/physics_server_sw.h" -#include "servers/physics_2d/physics_2d_server_sw.h" #include "servers/visual/rasterizer.h" #include @@ -54,8 +52,6 @@ class OS_JavaScript : public OS_Unix { int64_t last_sync_time; VisualServer *visual_server; - PhysicsServer *physics_server; - Physics2DServer *physics_2d_server; AudioDriverJavaScript audio_driver_javascript; const char *gl_extensions; diff --git a/platform/osx/crash_handler_osx.mm b/platform/osx/crash_handler_osx.mm index 2ed88db3090..5635fe01876 100644 --- a/platform/osx/crash_handler_osx.mm +++ b/platform/osx/crash_handler_osx.mm @@ -29,6 +29,7 @@ /*************************************************************************/ #include "main/main.h" #include "os_osx.h" +#include "project_settings.h" #include #include diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index 05adfeb0f59..5f761b672ed 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -38,9 +38,6 @@ #include "os/input.h" #include "power_osx.h" #include "servers/audio_server.h" -#include "servers/physics_2d/physics_2d_server_sw.h" -#include "servers/physics_2d/physics_2d_server_wrap_mt.h" -#include "servers/physics_server.h" #include "servers/visual/rasterizer.h" #include "servers/visual/visual_server_wrap_mt.h" #include "servers/visual_server.h" @@ -62,9 +59,6 @@ public: List args; MainLoop *main_loop; - PhysicsServer *physics_server; - Physics2DServer *physics_2d_server; - IP_Unix *ip_unix; AudioDriverCoreAudio audio_driver; diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index 2c81a02014c..0d03d546b9e 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -35,7 +35,6 @@ #include "os/keyboard.h" #include "print_string.h" #include "sem_osx.h" -#include "servers/physics/physics_server_sw.h" #include "servers/visual/visual_server_raster.h" #include @@ -1092,13 +1091,6 @@ void OS_OSX::initialize(const VideoMode &p_desired, int p_video_driver, int p_au AudioDriverManager::initialize(p_audio_driver); - // - physics_server = memnew(PhysicsServerSW); - physics_server->init(); - //physics_2d_server = memnew( Physics2DServerSW ); - physics_2d_server = Physics2DServerWrapMT::init_server(); - physics_2d_server->init(); - input = memnew(InputDefault); joypad_osx = memnew(JoypadOSX); @@ -1120,12 +1112,6 @@ void OS_OSX::finalize() { visual_server->finish(); memdelete(visual_server); //memdelete(rasterizer); - - physics_server->finish(); - memdelete(physics_server); - - physics_2d_server->finish(); - memdelete(physics_2d_server); } void OS_OSX::set_main_loop(MainLoop *p_main_loop) { diff --git a/platform/server/os_server.cpp b/platform/server/os_server.cpp index 300c5cffcc3..80c04aa5ad3 100644 --- a/platform/server/os_server.cpp +++ b/platform/server/os_server.cpp @@ -31,7 +31,6 @@ //#include "servers/visual/rasterizer_dummy.h" #include "os_server.h" #include "print_string.h" -#include "servers/physics/physics_server_sw.h" #include #include @@ -75,11 +74,6 @@ void OS_Server::initialize(const VideoMode &p_desired, int p_video_driver, int p ERR_FAIL_COND(!visual_server); visual_server->init(); - // - physics_server = memnew(PhysicsServerSW); - physics_server->init(); - physics_2d_server = memnew(Physics2DServerSW); - physics_2d_server->init(); input = memnew(InputDefault); @@ -111,12 +105,6 @@ void OS_Server::finalize() { memdelete(visual_server); //memdelete(rasterizer); - physics_server->finish(); - memdelete(physics_server); - - physics_2d_server->finish(); - memdelete(physics_2d_server); - memdelete(input); args.clear(); diff --git a/platform/server/os_server.h b/platform/server/os_server.h index ba12f649be9..630c82ccdd0 100644 --- a/platform/server/os_server.h +++ b/platform/server/os_server.h @@ -35,8 +35,6 @@ #include "drivers/unix/os_unix.h" #include "main/input_default.h" #include "servers/audio_server.h" -#include "servers/physics_2d/physics_2d_server_sw.h" -#include "servers/physics_server.h" #include "servers/visual/rasterizer.h" #include "servers/visual_server.h" @@ -56,9 +54,6 @@ class OS_Server : public OS_Unix { bool grab; - PhysicsServer *physics_server; - Physics2DServer *physics_2d_server; - virtual void delete_main_loop(); IP_Unix *ip_unix; diff --git a/platform/uwp/os_uwp.cpp b/platform/uwp/os_uwp.cpp index ff5a935229f..90bd64c0b42 100644 --- a/platform/uwp/os_uwp.cpp +++ b/platform/uwp/os_uwp.cpp @@ -262,13 +262,6 @@ void OSUWP::initialize(const VideoMode &p_desired, int p_video_driver, int p_aud } */ - // - physics_server = memnew(PhysicsServerSW); - physics_server->init(); - - physics_2d_server = memnew(Physics2DServerSW); - physics_2d_server->init(); - visual_server->init(); input = memnew(InputDefault); @@ -367,12 +360,6 @@ void OSUWP::finalize() { memdelete(input); - physics_server->finish(); - memdelete(physics_server); - - physics_2d_server->finish(); - memdelete(physics_2d_server); - joypad = nullptr; } diff --git a/platform/uwp/os_uwp.h b/platform/uwp/os_uwp.h index 22f89380493..e46e4cc0201 100644 --- a/platform/uwp/os_uwp.h +++ b/platform/uwp/os_uwp.h @@ -40,8 +40,6 @@ #include "os/os.h" #include "power_uwp.h" #include "servers/audio_server.h" -#include "servers/physics/physics_server_sw.h" -#include "servers/physics_2d/physics_2d_server_sw.h" #include "servers/visual/rasterizer.h" #include "servers/visual_server.h" @@ -94,8 +92,6 @@ private: int old_x, old_y; Point2i center; VisualServer *visual_server; - PhysicsServer *physics_server; - Physics2DServer *physics_2d_server; int pressrc; ContextEGL *gl_context; diff --git a/platform/windows/crash_handler_win.cpp b/platform/windows/crash_handler_win.cpp index 2f5ee7956e5..feea3911b20 100644 --- a/platform/windows/crash_handler_win.cpp +++ b/platform/windows/crash_handler_win.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "main/main.h" #include "os_windows.h" +#include "project_settings.h" #ifdef CRASH_HANDLER_EXCEPTION diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index c27e7c0d2b0..179ce2cde85 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -42,7 +42,6 @@ #include "lang_table.h" #include "main/main.h" #include "packet_peer_udp_winsock.h" -#include "project_settings.h" #include "servers/audio_server.h" #include "servers/visual/visual_server_raster.h" #include "servers/visual/visual_server_wrap_mt.h" @@ -1056,12 +1055,6 @@ void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int visual_server = memnew(VisualServerWrapMT(visual_server, get_render_thread_mode() == RENDER_SEPARATE_THREAD)); } - physics_server = memnew(PhysicsServerSW); - physics_server->init(); - - physics_2d_server = Physics2DServerWrapMT::init_server(); - physics_2d_server->init(); - if (!is_no_window_mode_enabled()) { ShowWindow(hWnd, SW_SHOW); // Show The Window SetForegroundWindow(hWnd); // Slightly Higher Priority @@ -1223,12 +1216,6 @@ void OS_Windows::finalize() { memdelete(debugger_connection_console); } */ - - physics_server->finish(); - memdelete(physics_server); - - physics_2d_server->finish(); - memdelete(physics_2d_server); } void OS_Windows::finalize_core() { diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index c0b8dfc6917..c9c00b5e36d 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -29,8 +29,8 @@ /*************************************************************************/ #ifndef OS_WINDOWS_H #define OS_WINDOWS_H - #include "context_gl_win.h" +#include "core/project_settings.h" #include "crash_handler_win.h" #include "drivers/rtaudio/audio_driver_rtaudio.h" #include "drivers/wasapi/audio_driver_wasapi.h" @@ -38,7 +38,6 @@ #include "os/os.h" #include "power_windows.h" #include "servers/audio_server.h" -#include "servers/physics/physics_server_sw.h" #include "servers/visual/rasterizer.h" #include "servers/visual_server.h" #ifdef XAUDIO2_ENABLED @@ -47,8 +46,6 @@ #include "drivers/unix/ip_unix.h" #include "key_mapping_win.h" #include "main/input_default.h" -#include "servers/physics_2d/physics_2d_server_sw.h" -#include "servers/physics_2d/physics_2d_server_wrap_mt.h" #include #include @@ -90,8 +87,6 @@ class OS_Windows : public OS { ContextGL_Win *gl_context; #endif VisualServer *visual_server; - PhysicsServer *physics_server; - Physics2DServer *physics_2d_server; int pressrc; HDC hDC; // Private GDI Device Context HINSTANCE hInstance; // Holds The Instance Of The Application diff --git a/platform/x11/crash_handler_x11.cpp b/platform/x11/crash_handler_x11.cpp index 3c54d5cbc23..005a7459f91 100644 --- a/platform/x11/crash_handler_x11.cpp +++ b/platform/x11/crash_handler_x11.cpp @@ -33,6 +33,7 @@ #include "main/main.h" #include "os_x11.h" +#include "project_settings.h" #ifdef CRASH_HANDLER_ENABLED #include diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index bc18d0c1f0d..f49a81ee1f7 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -32,7 +32,6 @@ #include "errno.h" #include "key_mapping_x11.h" #include "print_string.h" -#include "servers/physics/physics_server_sw.h" #include "servers/visual/visual_server_raster.h" #include "servers/visual/visual_server_wrap_mt.h" #include @@ -458,12 +457,6 @@ void OS_X11::initialize(const VideoMode &p_desired, int p_video_driver, int p_au requested = None; visual_server->init(); - // - physics_server = memnew(PhysicsServerSW); - physics_server->init(); - //physics_2d_server = memnew( Physics2DServerSW ); - physics_2d_server = Physics2DServerWrapMT::init_server(); - physics_2d_server->init(); input = memnew(InputDefault); @@ -519,12 +512,6 @@ void OS_X11::finalize() { memdelete(visual_server); //memdelete(rasterizer); - physics_server->finish(); - memdelete(physics_server); - - physics_2d_server->finish(); - memdelete(physics_2d_server); - memdelete(power_manager); if (xrandr_handle) diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h index 36355f11bcc..02b2110da19 100644 --- a/platform/x11/os_x11.h +++ b/platform/x11/os_x11.h @@ -42,9 +42,6 @@ #include "main/input_default.h" #include "power_x11.h" #include "servers/audio_server.h" -#include "servers/physics_2d/physics_2d_server_sw.h" -#include "servers/physics_2d/physics_2d_server_wrap_mt.h" -#include "servers/physics_server.h" #include "servers/visual/rasterizer.h" #include @@ -121,10 +118,8 @@ class OS_X11 : public OS_Unix { uint64_t last_click_ms; uint32_t last_button_state; - PhysicsServer *physics_server; unsigned int get_mouse_button_state(unsigned int p_x11_state); void get_key_modifier_state(unsigned int p_x11_state, Ref state); - Physics2DServer *physics_2d_server; MouseMode mouse_mode; Point2i center; diff --git a/servers/physics_2d_server.cpp b/servers/physics_2d_server.cpp index 671c31e6a38..a27f2bb4026 100644 --- a/servers/physics_2d_server.cpp +++ b/servers/physics_2d_server.cpp @@ -28,7 +28,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "physics_2d_server.h" +#include "core/project_settings.h" #include "print_string.h" + Physics2DServer *Physics2DServer::singleton = NULL; void Physics2DDirectBodyState::integrate_forces() { @@ -691,3 +693,68 @@ Physics2DServer::~Physics2DServer() { singleton = NULL; } + +Vector Physics2DServerManager::physics_2d_servers; +int Physics2DServerManager::default_server_id = -1; +int Physics2DServerManager::default_server_priority = -1; +const String Physics2DServerManager::setting_property_name("physics/2d/physics_engine"); + +void Physics2DServerManager::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(setting_property_name, PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers)); +} + +void Physics2DServerManager::register_server(const String &p_name, CreatePhysics2DServerCallback p_creat_callback) { + + ERR_FAIL_COND(!p_creat_callback); + ERR_FAIL_COND(find_server_id(p_name) != -1); + physics_2d_servers.push_back(ClassInfo(p_name, p_creat_callback)); + on_servers_changed(); +} + +void Physics2DServerManager::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 Physics2DServerManager::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 Physics2DServerManager::get_servers_count() { + return physics_2d_servers.size(); +} + +String Physics2DServerManager::get_server_name(int p_id) { + ERR_FAIL_INDEX_V(p_id, get_servers_count(), ""); + return physics_2d_servers[p_id].name; +} + +Physics2DServer *Physics2DServerManager::new_default_server() { + ERR_FAIL_COND_V(default_server_id == -1, NULL); + return physics_2d_servers[default_server_id].create_callback(); +} + +Physics2DServer *Physics2DServerManager::new_server(const String &p_name) { + int id = find_server_id(p_name); + if (id == -1) { + return NULL; + } else { + return physics_2d_servers[id].create_callback(); + } +} diff --git a/servers/physics_2d_server.h b/servers/physics_2d_server.h index 18f4f460b6e..ddd15557685 100644 --- a/servers/physics_2d_server.h +++ b/servers/physics_2d_server.h @@ -590,6 +590,43 @@ public: Physics2DTestMotionResult(); }; +typedef Physics2DServer *(*CreatePhysics2DServerCallback)(); + +class Physics2DServerManager { + struct ClassInfo { + String name; + CreatePhysics2DServerCallback create_callback; + + ClassInfo() + : name(""), create_callback(NULL) {} + + ClassInfo(String p_name, CreatePhysics2DServerCallback 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) {} + }; + + static Vector physics_2d_servers; + static int default_server_id; + static int default_server_priority; + +public: + static const String setting_property_name; + +private: + static void on_servers_changed(); + +public: + static void register_server(const String &p_name, CreatePhysics2DServerCallback p_creat_callback); + static void set_default_server(const String &p_name, int p_priority = 0); + static int find_server_id(const String &p_name); + static int get_servers_count(); + static String get_server_name(int p_id); + static Physics2DServer *new_default_server(); + static Physics2DServer *new_server(const String &p_name); +}; + VARIANT_ENUM_CAST(Physics2DServer::ShapeType); VARIANT_ENUM_CAST(Physics2DServer::SpaceParameter); VARIANT_ENUM_CAST(Physics2DServer::AreaParameter); diff --git a/servers/physics_server.cpp b/servers/physics_server.cpp index 6d192886a52..e1d505011c6 100644 --- a/servers/physics_server.cpp +++ b/servers/physics_server.cpp @@ -28,7 +28,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "physics_server.h" +#include "core/project_settings.h" #include "print_string.h" + PhysicsServer *PhysicsServer::singleton = NULL; void PhysicsDirectBodyState::integrate_forces() { @@ -732,3 +734,68 @@ PhysicsServer::~PhysicsServer() { singleton = NULL; } + +Vector PhysicsServerManager::physics_servers; +int PhysicsServerManager::default_server_id = -1; +int PhysicsServerManager::default_server_priority = -1; +const String PhysicsServerManager::setting_property_name("physics/3d/physics_engine"); + +void PhysicsServerManager::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(setting_property_name, PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers)); +} + +void PhysicsServerManager::register_server(const String &p_name, CreatePhysicsServerCallback p_creat_callback) { + + ERR_FAIL_COND(!p_creat_callback); + ERR_FAIL_COND(find_server_id(p_name) != -1); + physics_servers.push_back(ClassInfo(p_name, p_creat_callback)); + on_servers_changed(); +} + +void PhysicsServerManager::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 PhysicsServerManager::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 PhysicsServerManager::get_servers_count() { + return physics_servers.size(); +} + +String PhysicsServerManager::get_server_name(int p_id) { + ERR_FAIL_INDEX_V(p_id, get_servers_count(), ""); + return physics_servers[p_id].name; +} + +PhysicsServer *PhysicsServerManager::new_default_server() { + ERR_FAIL_COND_V(default_server_id == -1, NULL); + return physics_servers[default_server_id].create_callback(); +} + +PhysicsServer *PhysicsServerManager::new_server(const String &p_name) { + int id = find_server_id(p_name); + if (id == -1) { + return NULL; + } else { + return physics_servers[id].create_callback(); + } +} diff --git a/servers/physics_server.h b/servers/physics_server.h index 8cec1256465..09990cd02a1 100644 --- a/servers/physics_server.h +++ b/servers/physics_server.h @@ -658,6 +658,43 @@ public: ~PhysicsServer(); }; +typedef PhysicsServer *(*CreatePhysicsServerCallback)(); + +class PhysicsServerManager { + struct ClassInfo { + String name; + CreatePhysicsServerCallback create_callback; + + ClassInfo() + : name(""), create_callback(NULL) {} + + ClassInfo(String p_name, CreatePhysicsServerCallback 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) {} + }; + + static Vector physics_servers; + static int default_server_id; + static int default_server_priority; + +public: + static const String setting_property_name; + +private: + static void on_servers_changed(); + +public: + static void register_server(const String &p_name, CreatePhysicsServerCallback p_creat_callback); + static void set_default_server(const String &p_name, int p_priority = 0); + static int find_server_id(const String &p_name); + static int get_servers_count(); + static String get_server_name(int p_id); + static PhysicsServer *new_default_server(); + static PhysicsServer *new_server(const String &p_name); +}; + VARIANT_ENUM_CAST(PhysicsServer::ShapeType); VARIANT_ENUM_CAST(PhysicsServer::SpaceParameter); VARIANT_ENUM_CAST(PhysicsServer::AreaParameter); diff --git a/servers/register_server_types.cpp b/servers/register_server_types.cpp index 7a9328e30ff..1ba9e7b174a 100644 --- a/servers/register_server_types.cpp +++ b/servers/register_server_types.cpp @@ -49,6 +49,9 @@ #include "audio/effects/audio_effect_reverb.h" #include "audio/effects/audio_effect_stereo_enhance.h" #include "audio_server.h" +#include "physics/physics_server_sw.h" +#include "physics_2d/physics_2d_server_sw.h" +#include "physics_2d/physics_2d_server_wrap_mt.h" #include "physics_2d_server.h" #include "physics_server.h" #include "script_debugger_remote.h" @@ -74,6 +77,14 @@ static void _debugger_get_resource_usage(List(); +} + void register_server_types() { ClassDB::register_virtual_class(); @@ -82,12 +93,6 @@ void register_server_types() { ClassDB::register_virtual_class(); ClassDB::register_class(); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("VisualServer", VisualServer::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("AudioServer", AudioServer::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("PhysicsServer", PhysicsServer::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("Physics2DServer", Physics2DServer::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("ARVRServer", ARVRServer::get_singleton())); - shader_types = memnew(ShaderTypes); ClassDB::register_virtual_class(); @@ -144,9 +149,31 @@ void register_server_types() { ClassDB::register_virtual_class(); ScriptDebuggerRemote::resource_usage_func = _debugger_get_resource_usage; + + // Physics 2D + GLOBAL_DEF(Physics2DServerManager::setting_property_name, "DEFAULT"); + ProjectSettings::get_singleton()->set_custom_property_info(Physics2DServerManager::setting_property_name, PropertyInfo(Variant::STRING, Physics2DServerManager::setting_property_name, PROPERTY_HINT_ENUM, "DEFAULT")); + + Physics2DServerManager::register_server("GodotPhysics", &_createGodotPhysics2DCallback); + Physics2DServerManager::set_default_server("GodotPhysics"); + + // Physics 3D + GLOBAL_DEF(PhysicsServerManager::setting_property_name, "DEFAULT"); + ProjectSettings::get_singleton()->set_custom_property_info(PhysicsServerManager::setting_property_name, PropertyInfo(Variant::STRING, PhysicsServerManager::setting_property_name, PROPERTY_HINT_ENUM, "DEFAULT")); + + PhysicsServerManager::register_server("GodotPhysics", &_createGodotPhysicsCallback); + PhysicsServerManager::set_default_server("GodotPhysics"); } void unregister_server_types() { memdelete(shader_types); } + +void register_server_singletons() { + ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("VisualServer", VisualServer::get_singleton())); + ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("AudioServer", AudioServer::get_singleton())); + ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("PhysicsServer", PhysicsServer::get_singleton())); + ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("Physics2DServer", Physics2DServer::get_singleton())); + ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("ARVRServer", ARVRServer::get_singleton())); +} diff --git a/servers/register_server_types.h b/servers/register_server_types.h index 2b3ac816ac4..e53626ca30d 100644 --- a/servers/register_server_types.h +++ b/servers/register_server_types.h @@ -33,4 +33,6 @@ void register_server_types(); void unregister_server_types(); +void register_server_singletons(); + #endif // REGISTER_SERVER_TYPES_H