diff --git a/.appveyor.yml b/.appveyor.yml index dbd56ab7e0f4..e31e29e0d8e0 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -1,7 +1,9 @@ os: Visual Studio 2015 environment: + HOME: "%HOMEDRIVE%%HOMEPATH%" PYTHON: C:\Python27 + SCONS_CACHE: "%HOME%\\scons_cache" matrix: - VS: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat GD_PLATFORM: windows @@ -9,6 +11,9 @@ environment: TARGET: release_debug ARCH: amd64 +cache: + - "%SCONS_CACHE%" + install: - SET "PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%" - pip install --egg scons # it will fail on AppVeyor without --egg flag diff --git a/.gitignore b/.gitignore index b347b348a59a..0de1e682a09f 100644 --- a/.gitignore +++ b/.gitignore @@ -88,6 +88,9 @@ bld/ [Tt]est[Rr]esult*/ [Bb]uild[Ll]og.* +# Hints for improving IntelliSense, created together with VS project +cpp.hint + #NUNIT *.VisualState.xml TestResult.xml diff --git a/.travis.yml b/.travis.yml index 409c870e7963..01821cef2dd4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,13 @@ dist: trusty sudo: false -cache: ccache +env: + global: + - SCONS_CACHE_ROOT=$HOME/.scons_cache + +cache: + directories: + - $SCONS_CACHE_ROOT matrix: include: @@ -20,12 +26,12 @@ matrix: #- env: GODOT_TARGET=windows TOOLS=yes # os: linux # compiler: gcc + - env: GODOT_TARGET=android TOOLS=no + os: linux + compiler: gcc - env: GODOT_TARGET=osx TOOLS=yes os: osx compiler: clang - #- env: GODOT_TARGET=android TOOLS=no - # os: osx - # compiler: clang #- env: GODOT_TARGET=iphone TOOLS=no # os: osx # compiler: clang @@ -61,20 +67,27 @@ addons: # For style checks. - clang-format-3.9 -before_script: +install: + - if [ "$TRAVIS_OS_NAME" = "linux" ] && [ "$GODOT_TARGET" = "android" ]; then + misc/travis/android-tools-linux.sh; + fi - if [ "$TRAVIS_OS_NAME" = "osx" ]; then - brew update; brew install scons ccache; - export PATH="/usr/local/opt/ccache/libexec:$PATH"; + misc/travis/scons-local-osx.sh; fi - if [ "$TRAVIS_OS_NAME" = "osx" ] && [ "$GODOT_TARGET" = "android" ]; then - brew update; brew install -v android-sdk; - brew install -v android-ndk | grep -v "inflating:" | grep -v "creating:"; - export ANDROID_HOME=/usr/local/opt/android-sdk; export ANDROID_NDK_ROOT=/usr/local/opt/android-ndk; + misc/travis/android-tools-osx.sh; + fi + +before_script: + - if [ "$GODOT_TARGET" = "android" ]; then + export ANDROID_HOME=$TRAVIS_BUILD_DIR/godot-dev/build-tools/android-sdk; + export ANDROID_NDK_ROOT=$TRAVIS_BUILD_DIR/godot-dev/build-tools/android-ndk; fi script: - if [ "$STATIC_CHECKS" = "yes" ]; then sh ./misc/travis/clang-format.sh; else + export SCONS_CACHE=$SCONS_CACHE_ROOT/${GODOT_TARGET}-${CXX}-tools_${TOOLS}/; scons -j2 CC=$CC CXX=$CXX platform=$GODOT_TARGET TOOLS=$TOOLS verbose=yes progress=no; fi diff --git a/AUTHORS.md b/AUTHORS.md index 3fd8227fa72b..73712bbfc4d3 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -30,16 +30,20 @@ name is available. Alexander Holland (AlexHolly) Alexey Velikiy (jonyrock) Alket Rexhepi (alketii) + Andrea Catania (AndreaCatania) Andreas Haas (Hinsbart) Anton Yabchinskiy (a12n) Aren Villanueva (kurikaesu) Ariel Manzur (punto-) Bastiaan Olij (BastiaanOlij) + Ben Brookshire (sheepandshepherd) + Bernard Liebl (poke1024) Bojidar Marinov (bojidar-bg) Błażej Szczygieł (zaps166) Carl Olsson (not-surt) Dana Olson (adolson) Daniel J. Ramirez (djrm) + Дмитрий Сальников (DmitriySalnikov) Emmanuel Leblond (touilleMan) Fabio Alessandrelli (Faless) Ferenc Arn (tagcup) @@ -54,6 +58,7 @@ name is available. Hiroshi Ogawa (hi-ogawa) Hubert Jarosz (Marqin) Hugo Locurcio (Calinou) + Ian (ianb96) Ignacio Etcheverry (neikeq) Indah Sylvia (ISylvox) J08nY @@ -63,12 +68,14 @@ name is available. Juan Linietsky (reduz) Julian Murgia (StraToN) Kostadin Damyanov (Max-Might) - L. Krause (eska014) + Leon Krause (eska014) Marc Gilleron (Zylann) Marcelo Fernandez (marcelofg55) Mariano Javier Suligoy (MarianoGnu) Mario Schlack (hurikhan) Masoud BH (masoudbh3) + Matthias Hölzl (hoelzl) + Max Hilbrunner (mhilbrunner) Nathan Warden (NathanWarden) Nuno Donato (nunodonato) Ovnuniarchos @@ -77,6 +84,7 @@ name is available. Pawel Kowal (pkowal1982) Pedro J. Estébanez (RandomShaper) Poommetee Ketson (Noshyaar) + Przemysław Gołąb (n-pigeon) Ralf Hölzemer (rollenrolm) Ramesh Ravone (RameshRavone) Ray Koopa (RayKoopa) diff --git a/DONORS.md b/DONORS.md index 784cf5825548..3891a708ced7 100644 --- a/DONORS.md +++ b/DONORS.md @@ -17,32 +17,35 @@ None so far, but your company could be the first! :) ## Gold sponsors Gamblify + GameDev.TV ## Mini sponsors Andreas Andreas Hirschauer Christian Uldall Pedersen + Christoph Woinke E Hewert Hein-Pieter van Braam Matthieu Huvé Nathan Warden Neal Gompa (Conan Kudo) Olimpiu Metiu + Pascal Julien Ruslan Mustakov Slobodan Milnovic ## Gold donors + 3Dexplorer Alexander Otto + Andy Meier Asdf - Blair Allen cheese65536 Jake Bo Javier Manuele Finocchiaro Officine Pixel S.n.c. - Ranoller Rémi Verschelde Stephan Lanfermann @@ -50,8 +53,10 @@ None so far, but your company could be the first! :) Austen McRae Bernhard Liebl Gerald E Butler + Jahn Johansen Jordan M Lucas Kris Michael + Ranoller BanjoNode2D Chris Serino @@ -63,7 +68,6 @@ None so far, but your company could be the first! :) Henrique Alves Laurence Bannister Leo - mhilbrunner Przemysław Gołąb (n-pigeon) Robert Willes Robin Arys @@ -72,9 +76,12 @@ None so far, but your company could be the first! :) Testus Maximus Thomas Bjarnelöf Xavier Tan + Zaq Poi Amanda Haldy Andreas Haas + Andres Cuevas + Arnaud Verstuyf Bryanna M Chris Brown Cody Parker @@ -82,16 +89,16 @@ None so far, but your company could be the first! :) Ezra Theunissen flesk François Cantin + Giovanni Solimeno Hendrik Mans Jeppe Zapp Justin Arnold Justo Delgado Baudí Leandro Voltolino Lucien Boudy - Myles Noah + Ryan Estes Trent McPheron - x1212 ## Silver donors @@ -102,23 +109,36 @@ None so far, but your company could be the first! :) Avencherus Bastian Böhm Ben Vercammen + Blair Allen Bryan Stevenson Christian Baune Christian Winter Collin Shooltz + Daniel Kaplan + David Cravens + David May + Diego Moreira Guimarães Dominik Wetzel + Eric Martini Fabian Becker fengjiongmax - Fredy Romero Sam + Frank C. Simmons Geequlim Gerrit Großkopf Guldoman + Gustav Dahlström + HardRound hatniX HeartBeast Heribert Hirth Hunter Jones + Jaime Ruiz-Borau Vizárraga + Jeff Hungerford + Jesse Liles + joe513 Jonathon Josh 'Cheeseness' Bush + Juan Negrier JuDelCo Julian Murgia Juraj Móza @@ -127,6 +147,7 @@ None so far, but your company could be the first! :) Kevin Kamper Meejach Petersen Klavdij Voncina Kobi Malul + Linus Lind Lundgren Lisandro Lorea magodev Martin Novák @@ -134,7 +155,9 @@ None so far, but your company could be the first! :) Matthew Valancy Matthias Hölzl Max R.R. Collada + mhilbrunner Michael Gringauz + Michael Tintiuc Mikael Olsson MoM Moritz Laass @@ -142,9 +165,10 @@ None so far, but your company could be the first! :) Neil Blakey-Milner Nik Lee Niko Leopold - nvgrod + Oleg Tyshchenko Pablo Seibelt Pan Ip + Pat LaBine Patrick Nafarrete Paul Mason Paweł Kowal @@ -153,17 +177,15 @@ None so far, but your company could be the first! :) rayos Richman Stewart Roger Smith - Ryan Estes + Roman Tinkov Sam Van Campenhout - Sam Vila Sasori Olkof Scott D. Yelich Sootstone - Tavo Tell TheHappieCat Theo Cranmore + Thomas Norman Tom Larrow - Troy Bonneau UltyX Wout Standaert Xananax & karroffel diff --git a/SConstruct b/SConstruct index 9d536e0d16ce..b5885f896fb6 100644 --- a/SConstruct +++ b/SConstruct @@ -1,4 +1,3 @@ - #!/usr/bin/env python EnsureSConsVersion(0, 98, 1) @@ -23,6 +22,7 @@ platform_flags = {} # flags for each platform active_platforms = [] active_platform_ids = [] platform_exporters = [] +platform_apis = [] global_defaults = [] for x in glob.glob("platform/*"): @@ -35,6 +35,8 @@ for x in glob.glob("platform/*"): if (os.path.exists(x + "/export/export.cpp")): platform_exporters.append(x[9:]) + if (os.path.exists(x + "/api/api.cpp")): + platform_apis.append(x[9:]) if (os.path.exists(x + "/globals/global_defaults.cpp")): global_defaults.append(x[9:]) if (detect.is_active()): @@ -216,6 +218,7 @@ env_base.Append(CPPPATH=['#core', '#core/math', '#editor', '#drivers', '#']) # configure ENV for platform env_base.platform_exporters = platform_exporters +env_base.platform_apis = platform_apis """ sys.path.append("./platform/"+env_base["platform"]) @@ -271,9 +274,12 @@ if selected_platform in platform_list: if len(pieces) > 0: basename = pieces[0] basename = basename.replace('\\\\', '/') - env.vs_srcs = env.vs_srcs + [basename + ".cpp"] - env.vs_incs = env.vs_incs + [basename + ".h"] - # print basename + if os.path.isfile(basename + ".h"): + env.vs_incs = env.vs_incs + [basename + ".h"] + if os.path.isfile(basename + ".c"): + env.vs_srcs = env.vs_srcs + [basename + ".c"] + elif os.path.isfile(basename + ".cpp"): + env.vs_srcs = env.vs_srcs + [basename + ".cpp"] env.AddToVSProject = AddToVSProject env.extra_suffix = "" @@ -320,6 +326,8 @@ if selected_platform in platform_list: env.Append(CCFLAGS=['/W2'] + disable_nonessential_warnings) else: # 'no' env.Append(CCFLAGS=['/w']) + # Set exception handling model to avoid warnings caused by Windows system headers. + env.Append(CCFLAGS=['/EHsc']) else: # Rest of the world if (env["warnings"] == 'extra'): env.Append(CCFLAGS=['-Wall', '-Wextra']) @@ -367,7 +375,7 @@ if selected_platform in platform_list: sys.modules.pop('detect') env.module_list = [] - env.doc_class_path={} + env.doc_class_path = {} for x in module_list: if not env['module_' + x + '_enabled']: @@ -383,11 +391,10 @@ if selected_platform in platform_list: doc_classes = config.get_doc_classes() doc_path = config.get_doc_path() for c in doc_classes: - env.doc_class_path[c]="modules/"+x+"/"+doc_path + env.doc_class_path[c] = "modules/" + x + "/" + doc_path except: pass - sys.path.remove(tmppath) sys.modules.pop('config') @@ -427,6 +434,11 @@ if selected_platform in platform_list: if (True): # FIXME: detect GLES3 env.Append( BUILDERS = { 'GLES3_GLSL' : env.Builder(action = methods.build_gles3_headers, suffix = 'glsl.gen.h',src_suffix = '.glsl') } ) + scons_cache_path = os.environ.get("SCONS_CACHE") + if scons_cache_path != None: + CacheDir(scons_cache_path) + print("Scons cache enabled... (path: '" + scons_cache_path + "')") + Export('env') # build subdirs, the build order is dependent on link order. @@ -437,6 +449,7 @@ if selected_platform in platform_list: SConscript("editor/SCsub") SConscript("drivers/SCsub") + SConscript("platform/SCsub") SConscript("modules/SCsub") SConscript("main/SCsub") @@ -446,6 +459,7 @@ if selected_platform in platform_list: if env['vsproj']: env['CPPPATH'] = [Dir(path) for path in env['CPPPATH']] methods.generate_vs_project(env, GetOption("num_jobs")) + methods.generate_cpp_hint_file("cpp.hint") # Check for the existence of headers conf = Configure(env) diff --git a/core/array.cpp b/core/array.cpp index 171c11776c97..1ccbbae14791 100644 --- a/core/array.cpp +++ b/core/array.cpp @@ -261,10 +261,53 @@ Array &Array::sort_custom(Object *p_obj, const StringName &p_function) { SortArray avs; avs.compare.obj = p_obj; avs.compare.func = p_function; - avs.sort(_p->array.ptr(), _p->array.size()); + avs.sort(_p->array.ptrw(), _p->array.size()); return *this; } +template +_FORCE_INLINE_ int bisect(const Vector &p_array, const Variant &p_value, bool p_before, const Less &p_less) { + + int lo = 0; + int hi = p_array.size(); + if (p_before) { + while (lo < hi) { + const int mid = (lo + hi) / 2; + if (p_less(p_array.get(mid), p_value)) { + lo = mid + 1; + } else { + hi = mid; + } + } + } else { + while (lo < hi) { + const int mid = (lo + hi) / 2; + if (p_less(p_value, p_array.get(mid))) { + hi = mid; + } else { + lo = mid + 1; + } + } + } + return lo; +} + +int Array::bsearch(const Variant &p_value, bool p_before) { + + return bisect(_p->array, p_value, p_before, _ArrayVariantSort()); +} + +int Array::bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before) { + + ERR_FAIL_NULL_V(p_obj, 0); + + _ArrayVariantSortCustom less; + less.obj = p_obj; + less.func = p_function; + + return bisect(_p->array, p_value, p_before, less); +} + Array &Array::invert() { _p->array.invert(); diff --git a/core/array.h b/core/array.h index 2c291031085f..3d70a31d2fba 100644 --- a/core/array.h +++ b/core/array.h @@ -70,6 +70,8 @@ class Array { Array &sort(); Array &sort_custom(Object *p_obj, const StringName &p_function); + int bsearch(const Variant &p_value, bool p_before = true); + int bsearch_custom(const Variant &p_value, Object *p_obj, const StringName &p_function, bool p_before = true); Array &invert(); int find(const Variant &p_value, int p_from = 0) const; diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index c369f4bffedf..999befaf67ab 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -888,9 +888,9 @@ void _OS::dump_resources_to_file(const String &p_file) { OS::get_singleton()->dump_resources_to_file(p_file.utf8().get_data()); } -String _OS::get_data_dir() const { +String _OS::get_user_data_dir() const { - return OS::get_singleton()->get_data_dir(); + return OS::get_singleton()->get_user_data_dir(); }; Error _OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) { @@ -1088,7 +1088,7 @@ void _OS::_bind_methods() { ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &_OS::get_static_memory_peak_usage); ClassDB::bind_method(D_METHOD("get_dynamic_memory_usage"), &_OS::get_dynamic_memory_usage); - ClassDB::bind_method(D_METHOD("get_data_dir"), &_OS::get_data_dir); + ClassDB::bind_method(D_METHOD("get_user_data_dir"), &_OS::get_user_data_dir); ClassDB::bind_method(D_METHOD("get_system_dir", "dir"), &_OS::get_system_dir); ClassDB::bind_method(D_METHOD("get_unique_id"), &_OS::get_unique_id); @@ -2592,6 +2592,16 @@ bool _Engine::is_in_physics_frame() const { return Engine::get_singleton()->is_in_physics_frame(); } +bool _Engine::has_singleton(const String &p_name) const { + + return Engine::get_singleton()->has_singleton(p_name); +} + +Object *_Engine::get_singleton_object(const String &p_name) const { + + return Engine::get_singleton()->get_singleton_object(p_name); +} + void _Engine::set_editor_hint(bool p_enabled) { Engine::get_singleton()->set_editor_hint(p_enabled); @@ -2621,6 +2631,9 @@ void _Engine::_bind_methods() { ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &_Engine::is_in_physics_frame); + ClassDB::bind_method(D_METHOD("has_singleton", "name"), &_Engine::has_singleton); + ClassDB::bind_method(D_METHOD("get_singleton", "name"), &_Engine::get_singleton_object); + ClassDB::bind_method(D_METHOD("set_editor_hint", "enabled"), &_Engine::set_editor_hint); ClassDB::bind_method(D_METHOD("is_editor_hint"), &_Engine::is_editor_hint); } diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index bbbb40d92629..8163b08d7692 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -297,7 +297,7 @@ class _OS : public Object { String get_system_dir(SystemDir p_dir) const; - String get_data_dir() const; + String get_user_data_dir() const; void alert(const String &p_alert, const String &p_title = "ALERT!"); @@ -670,6 +670,9 @@ class _Engine : public Object { bool is_in_physics_frame() const; + bool has_singleton(const String &p_name) const; + Object *get_singleton_object(const String &p_name) const; + void set_editor_hint(bool p_enabled); bool is_editor_hint() const; diff --git a/core/color.h b/core/color.h index 972b6a1b33ad..da2bfdcd9823 100644 --- a/core/color.h +++ b/core/color.h @@ -101,6 +101,24 @@ struct Color { return res; } + _FORCE_INLINE_ Color darkened(float p_amount) const { + + Color res = *this; + res.r = CLAMP(res.r * (1.0f - p_amount), 0.0, 1.0); + res.g = CLAMP(res.g * (1.0f - p_amount), 0.0, 1.0); + res.b = CLAMP(res.b * (1.0f - p_amount), 0.0, 1.0); + return res; + } + + _FORCE_INLINE_ Color lightened(float p_amount) const { + + Color res = *this; + res.r = CLAMP(res.r + (1.0f - res.r) * p_amount, 0.0, 1.0); + res.g = CLAMP(res.g + (1.0f - res.g) * p_amount, 0.0, 1.0); + res.b = CLAMP(res.b + (1.0f - res.b) * p_amount, 0.0, 1.0); + return res; + } + _FORCE_INLINE_ uint32_t to_rgbe9995() const { const float pow2to9 = 512.0f; diff --git a/core/command_queue_mt.cpp b/core/command_queue_mt.cpp index 8e2aa24c22a4..2028a18a06d0 100644 --- a/core/command_queue_mt.cpp +++ b/core/command_queue_mt.cpp @@ -76,6 +76,30 @@ CommandQueueMT::SyncSemaphore *CommandQueueMT::_alloc_sync_sem() { return &sync_sems[idx]; } +bool CommandQueueMT::dealloc_one() { +tryagain: + if (dealloc_ptr == write_ptr) { + // The queue is empty + return false; + } + + uint32_t size = *(uint32_t *)&command_mem[dealloc_ptr]; + + if (size == 0) { + // End of command buffer wrap down + dealloc_ptr = 0; + goto tryagain; + } + + if (size & 1) { + // Still used, nothing can be deallocated + return false; + } + + dealloc_ptr += (size >> 1) + sizeof(uint32_t); + return true; +} + CommandQueueMT::CommandQueueMT(bool p_sync) { read_ptr = 0; diff --git a/core/command_queue_mt.h b/core/command_queue_mt.h index e37d593f9fba..af91ee0cd9a8 100644 --- a/core/command_queue_mt.h +++ b/core/command_queue_mt.h @@ -39,6 +39,230 @@ @author Juan Linietsky */ +#define COMMA(N) _COMMA_##N +#define _COMMA_0 +#define _COMMA_1 , +#define _COMMA_2 , +#define _COMMA_3 , +#define _COMMA_4 , +#define _COMMA_5 , +#define _COMMA_6 , +#define _COMMA_7 , +#define _COMMA_8 , +#define _COMMA_9 , +#define _COMMA_10 , +#define _COMMA_11 , +#define _COMMA_12 , + +// 1-based comma separed list of ITEMs +#define COMMA_SEP_LIST(ITEM, LENGTH) _COMMA_SEP_LIST_##LENGTH(ITEM) +#define _COMMA_SEP_LIST_12(ITEM) \ + _COMMA_SEP_LIST_11(ITEM) \ + , ITEM(12) +#define _COMMA_SEP_LIST_11(ITEM) \ + _COMMA_SEP_LIST_10(ITEM) \ + , ITEM(11) +#define _COMMA_SEP_LIST_10(ITEM) \ + _COMMA_SEP_LIST_9(ITEM) \ + , ITEM(10) +#define _COMMA_SEP_LIST_9(ITEM) \ + _COMMA_SEP_LIST_8(ITEM) \ + , ITEM(9) +#define _COMMA_SEP_LIST_8(ITEM) \ + _COMMA_SEP_LIST_7(ITEM) \ + , ITEM(8) +#define _COMMA_SEP_LIST_7(ITEM) \ + _COMMA_SEP_LIST_6(ITEM) \ + , ITEM(7) +#define _COMMA_SEP_LIST_6(ITEM) \ + _COMMA_SEP_LIST_5(ITEM) \ + , ITEM(6) +#define _COMMA_SEP_LIST_5(ITEM) \ + _COMMA_SEP_LIST_4(ITEM) \ + , ITEM(5) +#define _COMMA_SEP_LIST_4(ITEM) \ + _COMMA_SEP_LIST_3(ITEM) \ + , ITEM(4) +#define _COMMA_SEP_LIST_3(ITEM) \ + _COMMA_SEP_LIST_2(ITEM) \ + , ITEM(3) +#define _COMMA_SEP_LIST_2(ITEM) \ + _COMMA_SEP_LIST_1(ITEM) \ + , ITEM(2) +#define _COMMA_SEP_LIST_1(ITEM) \ + _COMMA_SEP_LIST_0(ITEM) \ + ITEM(1) +#define _COMMA_SEP_LIST_0(ITEM) + +// 1-based semicolon separed list of ITEMs +#define SEMIC_SEP_LIST(ITEM, LENGTH) _SEMIC_SEP_LIST_##LENGTH(ITEM) +#define _SEMIC_SEP_LIST_12(ITEM) \ + _SEMIC_SEP_LIST_11(ITEM); \ + ITEM(12) +#define _SEMIC_SEP_LIST_11(ITEM) \ + _SEMIC_SEP_LIST_10(ITEM); \ + ITEM(11) +#define _SEMIC_SEP_LIST_10(ITEM) \ + _SEMIC_SEP_LIST_9(ITEM); \ + ITEM(10) +#define _SEMIC_SEP_LIST_9(ITEM) \ + _SEMIC_SEP_LIST_8(ITEM); \ + ITEM(9) +#define _SEMIC_SEP_LIST_8(ITEM) \ + _SEMIC_SEP_LIST_7(ITEM); \ + ITEM(8) +#define _SEMIC_SEP_LIST_7(ITEM) \ + _SEMIC_SEP_LIST_6(ITEM); \ + ITEM(7) +#define _SEMIC_SEP_LIST_6(ITEM) \ + _SEMIC_SEP_LIST_5(ITEM); \ + ITEM(6) +#define _SEMIC_SEP_LIST_5(ITEM) \ + _SEMIC_SEP_LIST_4(ITEM); \ + ITEM(5) +#define _SEMIC_SEP_LIST_4(ITEM) \ + _SEMIC_SEP_LIST_3(ITEM); \ + ITEM(4) +#define _SEMIC_SEP_LIST_3(ITEM) \ + _SEMIC_SEP_LIST_2(ITEM); \ + ITEM(3) +#define _SEMIC_SEP_LIST_2(ITEM) \ + _SEMIC_SEP_LIST_1(ITEM); \ + ITEM(2) +#define _SEMIC_SEP_LIST_1(ITEM) \ + _SEMIC_SEP_LIST_0(ITEM) \ + ITEM(1) +#define _SEMIC_SEP_LIST_0(ITEM) + +// 1-based space separed list of ITEMs +#define SPACE_SEP_LIST(ITEM, LENGTH) _SPACE_SEP_LIST_##LENGTH(ITEM) +#define _SPACE_SEP_LIST_12(ITEM) \ + _SPACE_SEP_LIST_11(ITEM) \ + ITEM(12) +#define _SPACE_SEP_LIST_11(ITEM) \ + _SPACE_SEP_LIST_10(ITEM) \ + ITEM(11) +#define _SPACE_SEP_LIST_10(ITEM) \ + _SPACE_SEP_LIST_9(ITEM) \ + ITEM(10) +#define _SPACE_SEP_LIST_9(ITEM) \ + _SPACE_SEP_LIST_8(ITEM) \ + ITEM(9) +#define _SPACE_SEP_LIST_8(ITEM) \ + _SPACE_SEP_LIST_7(ITEM) \ + ITEM(8) +#define _SPACE_SEP_LIST_7(ITEM) \ + _SPACE_SEP_LIST_6(ITEM) \ + ITEM(7) +#define _SPACE_SEP_LIST_6(ITEM) \ + _SPACE_SEP_LIST_5(ITEM) \ + ITEM(6) +#define _SPACE_SEP_LIST_5(ITEM) \ + _SPACE_SEP_LIST_4(ITEM) \ + ITEM(5) +#define _SPACE_SEP_LIST_4(ITEM) \ + _SPACE_SEP_LIST_3(ITEM) \ + ITEM(4) +#define _SPACE_SEP_LIST_3(ITEM) \ + _SPACE_SEP_LIST_2(ITEM) \ + ITEM(3) +#define _SPACE_SEP_LIST_2(ITEM) \ + _SPACE_SEP_LIST_1(ITEM) \ + ITEM(2) +#define _SPACE_SEP_LIST_1(ITEM) \ + _SPACE_SEP_LIST_0(ITEM) \ + ITEM(1) +#define _SPACE_SEP_LIST_0(ITEM) + +#define ARG(N) p##N +#define PARAM(N) P##N p##N +#define TYPE_PARAM(N) class P##N +#define PARAM_DECL(N) typename GetSimpleTypeT::type_t p##N + +#define DECL_CMD(N) \ + template \ + struct Command##N : public CommandBase { \ + T *instance; \ + M method; \ + SEMIC_SEP_LIST(PARAM_DECL, N); \ + virtual void call() { \ + (instance->*method)(COMMA_SEP_LIST(ARG, N)); \ + } \ + }; + +#define DECL_CMD_RET(N) \ + template \ + struct CommandRet##N : public SyncCommand { \ + R *ret; \ + T *instance; \ + M method; \ + SEMIC_SEP_LIST(PARAM_DECL, N); \ + virtual void call() { \ + *ret = (instance->*method)(COMMA_SEP_LIST(ARG, N)); \ + } \ + }; + +#define DECL_CMD_SYNC(N) \ + template \ + struct CommandSync##N : public SyncCommand { \ + T *instance; \ + M method; \ + SEMIC_SEP_LIST(PARAM_DECL, N); \ + virtual void call() { \ + (instance->*method)(COMMA_SEP_LIST(ARG, N)); \ + } \ + }; + +#define TYPE_ARG(N) P##N +#define CMD_TYPE(N) Command##N +#define CMD_ASSIGN_PARAM(N) cmd->p##N = p##N + +#define DECL_PUSH(N) \ + template \ + void push(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \ + CMD_TYPE(N) *cmd = allocate_and_lock(); \ + cmd->instance = p_instance; \ + cmd->method = p_method; \ + SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \ + unlock(); \ + if (sync) sync->post(); \ + } + +#define CMD_RET_TYPE(N) CommandRet##N + +#define DECL_PUSH_AND_RET(N) \ + template \ + void push_and_ret(T *p_instance, M p_method, COMMA_SEP_LIST(PARAM, N) COMMA(N) R *r_ret) { \ + SyncSemaphore *ss = _alloc_sync_sem(); \ + CMD_RET_TYPE(N) *cmd = allocate_and_lock(); \ + cmd->instance = p_instance; \ + cmd->method = p_method; \ + SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \ + cmd->ret = r_ret; \ + cmd->sync_sem = ss; \ + unlock(); \ + if (sync) sync->post(); \ + ss->sem->wait(); \ + } + +#define CMD_SYNC_TYPE(N) CommandSync##N + +#define DECL_PUSH_AND_SYNC(N) \ + template \ + void push_and_sync(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \ + SyncSemaphore *ss = _alloc_sync_sem(); \ + CMD_SYNC_TYPE(N) *cmd = allocate_and_lock(); \ + cmd->instance = p_instance; \ + cmd->method = p_method; \ + SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \ + cmd->sync_sem = ss; \ + unlock(); \ + if (sync) sync->post(); \ + ss->sem->wait(); \ + } + +#define MAX_CMD_PARAMS 12 + class CommandQueueMT { struct SyncSemaphore { @@ -50,551 +274,30 @@ class CommandQueueMT { struct CommandBase { virtual void call() = 0; + virtual void post(){}; virtual ~CommandBase(){}; }; - template - struct Command0 : public CommandBase { - - T *instance; - M method; - - virtual void call() { (instance->*method)(); } - }; - - template - struct Command1 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - - virtual void call() { (instance->*method)(p1); } - }; - - template - struct Command2 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - - virtual void call() { (instance->*method)(p1, p2); } - }; - - template - struct Command3 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - - virtual void call() { (instance->*method)(p1, p2, p3); } - }; - - template - struct Command4 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - - virtual void call() { (instance->*method)(p1, p2, p3, p4); } - }; - - template - struct Command5 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - - virtual void call() { (instance->*method)(p1, p2, p3, p4, p5); } - }; - - template - struct Command6 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - - virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6); } - }; - - template - struct Command7 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - typename GetSimpleTypeT::type_t p7; - - virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7); } - }; - - template - struct Command8 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - typename GetSimpleTypeT::type_t p7; - typename GetSimpleTypeT::type_t p8; - - virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8); } - }; - - template - struct Command9 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - typename GetSimpleTypeT::type_t p7; - typename GetSimpleTypeT::type_t p8; - typename GetSimpleTypeT::type_t p9; - - virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8, p9); } - }; - - template - struct Command10 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - typename GetSimpleTypeT::type_t p7; - typename GetSimpleTypeT::type_t p8; - typename GetSimpleTypeT::type_t p9; - typename GetSimpleTypeT::type_t p10; - - virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); } - }; - - template - struct Command11 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - typename GetSimpleTypeT::type_t p7; - typename GetSimpleTypeT::type_t p8; - typename GetSimpleTypeT::type_t p9; - typename GetSimpleTypeT::type_t p10; - typename GetSimpleTypeT::type_t p11; - - virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11); } - }; - - template - struct Command12 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - typename GetSimpleTypeT::type_t p7; - typename GetSimpleTypeT::type_t p8; - typename GetSimpleTypeT::type_t p9; - typename GetSimpleTypeT::type_t p10; - typename GetSimpleTypeT::type_t p11; - typename GetSimpleTypeT::type_t p12; - - virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12); } - }; - - /* comands that return */ - - template - struct CommandRet0 : public CommandBase { - - T *instance; - M method; - R *ret; - SyncSemaphore *sync; - - virtual void call() { - *ret = (instance->*method)(); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandRet1 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - R *ret; - SyncSemaphore *sync; - - virtual void call() { - *ret = (instance->*method)(p1); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandRet2 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - R *ret; - SyncSemaphore *sync; - - virtual void call() { - *ret = (instance->*method)(p1, p2); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandRet3 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - R *ret; - SyncSemaphore *sync; - - virtual void call() { - *ret = (instance->*method)(p1, p2, p3); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandRet4 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - R *ret; - SyncSemaphore *sync; - - virtual void call() { - *ret = (instance->*method)(p1, p2, p3, p4); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandRet5 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - R *ret; - SyncSemaphore *sync; - - virtual void call() { - *ret = (instance->*method)(p1, p2, p3, p4, p5); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandRet6 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - R *ret; - SyncSemaphore *sync; - - virtual void call() { - *ret = (instance->*method)(p1, p2, p3, p4, p5, p6); - sync->sem->post(); - sync->in_use = false; - } - }; + struct SyncCommand : public CommandBase { - template - struct CommandRet7 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - typename GetSimpleTypeT::type_t p7; - R *ret; - SyncSemaphore *sync; - - virtual void call() { - *ret = (instance->*method)(p1, p2, p3, p4, p5, p6, p7); - sync->sem->post(); - sync->in_use = false; - } - }; + SyncSemaphore *sync_sem; - template - struct CommandRet8 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - typename GetSimpleTypeT::type_t p7; - typename GetSimpleTypeT::type_t p8; - R *ret; - SyncSemaphore *sync; - - virtual void call() { - *ret = (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8); - sync->sem->post(); - sync->in_use = false; + virtual void post() { + sync_sem->sem->post(); + sync_sem->in_use = false; } }; - /** commands that don't return but sync */ + DECL_CMD(0) + SPACE_SEP_LIST(DECL_CMD, 12) /* comands that return */ + DECL_CMD_RET(0) + SPACE_SEP_LIST(DECL_CMD_RET, 12) - template - struct CommandSync0 : public CommandBase { - - T *instance; - M method; - - SyncSemaphore *sync; - - virtual void call() { - (instance->*method)(); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandSync1 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - - SyncSemaphore *sync; - - virtual void call() { - (instance->*method)(p1); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandSync2 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - - SyncSemaphore *sync; - - virtual void call() { - (instance->*method)(p1, p2); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandSync3 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - - SyncSemaphore *sync; - - virtual void call() { - (instance->*method)(p1, p2, p3); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandSync4 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - - SyncSemaphore *sync; - - virtual void call() { - (instance->*method)(p1, p2, p3, p4); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandSync5 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - - SyncSemaphore *sync; - - virtual void call() { - (instance->*method)(p1, p2, p3, p4, p5); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandSync6 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - - SyncSemaphore *sync; - - virtual void call() { - (instance->*method)(p1, p2, p3, p4, p5, p6); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandSync7 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - typename GetSimpleTypeT::type_t p7; - - SyncSemaphore *sync; - - virtual void call() { - (instance->*method)(p1, p2, p3, p4, p5, p6, p7); - sync->sem->post(); - sync->in_use = false; - } - }; - - template - struct CommandSync8 : public CommandBase { - - T *instance; - M method; - typename GetSimpleTypeT::type_t p1; - typename GetSimpleTypeT::type_t p2; - typename GetSimpleTypeT::type_t p3; - typename GetSimpleTypeT::type_t p4; - typename GetSimpleTypeT::type_t p5; - typename GetSimpleTypeT::type_t p6; - typename GetSimpleTypeT::type_t p7; - typename GetSimpleTypeT::type_t p8; - - SyncSemaphore *sync; - - virtual void call() { - (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8); - sync->sem->post(); - sync->in_use = false; - } - }; + /* commands that don't return but sync */ + DECL_CMD_SYNC(0) + SPACE_SEP_LIST(DECL_CMD_SYNC, 12) /***** BASE *******/ @@ -607,6 +310,7 @@ class CommandQueueMT { uint8_t command_mem[COMMAND_MEM_SIZE]; uint32_t read_ptr; uint32_t write_ptr; + uint32_t dealloc_ptr; SyncSemaphore sync_sems[SYNC_SEMAPHORES]; Mutex *mutex; Semaphore *sync; @@ -619,18 +323,30 @@ class CommandQueueMT { tryagain: - if (write_ptr < read_ptr) { - // behind read_ptr, check that there is room - if ((read_ptr - write_ptr) <= alloc_size) + if (write_ptr < dealloc_ptr) { + // behind dealloc_ptr, check that there is room + if ((dealloc_ptr - write_ptr) <= alloc_size) { + + // There is no more room, try to deallocate something + if (dealloc_one()) { + goto tryagain; + } return NULL; - } else if (write_ptr >= read_ptr) { - // ahead of read_ptr, check that there is room + } + } else if (write_ptr >= dealloc_ptr) { + // ahead of dealloc_ptr, check that there is room - if ((COMMAND_MEM_SIZE - write_ptr) < alloc_size + 4) { + if ((COMMAND_MEM_SIZE - write_ptr) < alloc_size + sizeof(uint32_t)) { // no room at the end, wrap down; - if (read_ptr == 0) // don't want write_ptr to become read_ptr + if (dealloc_ptr == 0) { // don't want write_ptr to become dealloc_ptr + + // There is no more room, try to deallocate something + if (dealloc_one()) { + goto tryagain; + } return NULL; + } // if this happens, it's a bug ERR_FAIL_COND_V((COMMAND_MEM_SIZE - write_ptr) < sizeof(uint32_t), NULL); @@ -642,9 +358,11 @@ class CommandQueueMT { goto tryagain; } } - // allocate the size + // Allocate the size and the 'in use' bit. + // First bit used to mark if command is still in use (1) + // or if it has been destroyed and can be deallocated (0). uint32_t *p = (uint32_t *)&command_mem[write_ptr]; - *p = sizeof(T); + *p = (sizeof(T) << 1) | 1; write_ptr += sizeof(uint32_t); // allocate the command T *cmd = memnew_placement(&command_mem[write_ptr], T); @@ -669,15 +387,16 @@ class CommandQueueMT { return ret; } - bool flush_one() { - + bool flush_one(bool p_lock = true) { + if (p_lock) lock(); tryagain: // tried to read an empty queue if (read_ptr == write_ptr) return false; - uint32_t size = *(uint32_t *)(&command_mem[read_ptr]); + uint32_t size_ptr = read_ptr; + uint32_t size = *(uint32_t *)&command_mem[read_ptr] >> 1; if (size == 0) { //end of ringbuffer, wrap @@ -689,11 +408,17 @@ class CommandQueueMT { CommandBase *cmd = reinterpret_cast(&command_mem[read_ptr]); + read_ptr += size; + + if (p_lock) unlock(); cmd->call(); - cmd->~CommandBase(); + if (p_lock) lock(); - read_ptr += size; + cmd->post(); + cmd->~CommandBase(); + *(uint32_t *)&command_mem[size_ptr] &= ~1; + if (p_lock) unlock(); return true; } @@ -701,681 +426,33 @@ class CommandQueueMT { void unlock(); void wait_for_flush(); SyncSemaphore *_alloc_sync_sem(); + bool dealloc_one(); public: /* NORMAL PUSH COMMANDS */ + DECL_PUSH(0) + SPACE_SEP_LIST(DECL_PUSH, 12) - template - void push(T *p_instance, M p_method) { - - Command0 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1) { - - Command1 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1, P2 p2) { - - Command2 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3) { - - Command3 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4) { - - Command4 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { - - Command5 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) { - - Command6 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) { - - Command7 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->p7 = p7; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) { - - Command8 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->p7 = p7; - cmd->p8 = p8; - - unlock(); - - if (sync) sync->post(); - } + /* PUSH AND RET COMMANDS */ + DECL_PUSH_AND_RET(0) + SPACE_SEP_LIST(DECL_PUSH_AND_RET, 12) - template - void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9) { - - Command9 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->p7 = p7; - cmd->p8 = p8; - cmd->p9 = p9; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10) { - - Command10 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->p7 = p7; - cmd->p8 = p8; - cmd->p9 = p9; - cmd->p10 = p10; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10, P11 p11) { - - Command11 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->p7 = p7; - cmd->p8 = p8; - cmd->p9 = p9; - cmd->p10 = p10; - cmd->p11 = p11; - - unlock(); - - if (sync) sync->post(); - } - - template - void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10, P11 p11, P12 p12) { - - Command12 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->p7 = p7; - cmd->p8 = p8; - cmd->p9 = p9; - cmd->p10 = p10; - cmd->p11 = p11; - cmd->p12 = p12; - - unlock(); - - if (sync) sync->post(); - } - - /*** PUSH AND RET COMMANDS ***/ - - template - void push_and_ret(T *p_instance, M p_method, R *r_ret) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandRet0 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->ret = r_ret; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_ret(T *p_instance, M p_method, P1 p1, R *r_ret) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandRet1 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->ret = r_ret; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, R *r_ret) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandRet2 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->ret = r_ret; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, R *r_ret) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandRet3 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->ret = r_ret; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, R *r_ret) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandRet4 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->ret = r_ret; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, R *r_ret) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandRet5 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->ret = r_ret; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, R *r_ret) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandRet6 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->ret = r_ret; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, R *r_ret) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandRet7 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->p7 = p7; - cmd->ret = r_ret; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, R *r_ret) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandRet8 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->p7 = p7; - cmd->p8 = p8; - cmd->ret = r_ret; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_sync(T *p_instance, M p_method) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandSync0 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_sync(T *p_instance, M p_method, P1 p1) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandSync1 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandSync2 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandSync3 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandSync4 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandSync5 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandSync6 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandSync7 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->p7 = p7; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } - - template - void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) { - - SyncSemaphore *ss = _alloc_sync_sem(); - - CommandSync8 *cmd = allocate_and_lock >(); - - cmd->instance = p_instance; - cmd->method = p_method; - cmd->p1 = p1; - cmd->p2 = p2; - cmd->p3 = p3; - cmd->p4 = p4; - cmd->p5 = p5; - cmd->p6 = p6; - cmd->p7 = p7; - cmd->p8 = p8; - - cmd->sync = ss; - - unlock(); - - if (sync) sync->post(); - ss->sem->wait(); - } + /* PUSH AND RET SYNC COMMANDS*/ + DECL_PUSH_AND_SYNC(0) + SPACE_SEP_LIST(DECL_PUSH_AND_SYNC, 12) void wait_and_flush_one() { ERR_FAIL_COND(!sync); sync->wait(); - lock(); flush_one(); - unlock(); } void flush_all() { //ERR_FAIL_COND(sync); lock(); - while (true) { - bool exit = !flush_one(); - if (exit) - break; - } + while (flush_one(false)) + ; unlock(); } @@ -1383,4 +460,20 @@ class CommandQueueMT { ~CommandQueueMT(); }; +#undef ARG +#undef PARAM +#undef TYPE_PARAM +#undef PARAM_DECL +#undef DECL_CMD +#undef DECL_CMD_RET +#undef DECL_CMD_SYNC +#undef TYPE_ARG +#undef CMD_TYPE +#undef CMD_ASSIGN_PARAM +#undef DECL_PUSH +#undef CMD_RET_TYPE +#undef DECL_PUSH_AND_RET +#undef CMD_SYNC_TYPE +#undef DECL_CMD_SYNC + #endif diff --git a/core/compressed_translation.cpp b/core/compressed_translation.cpp index 74565d2e3241..5c7b79359029 100644 --- a/core/compressed_translation.cpp +++ b/core/compressed_translation.cpp @@ -288,7 +288,7 @@ StringName PHashTranslation::get_message(const StringName &p_src_text) const { CharString uncomp; uncomp.resize(bucket.elem[idx].uncomp_size + 1); - smaz_decompress(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].comp_size, uncomp.ptr(), bucket.elem[idx].uncomp_size); + smaz_decompress(&sptr[bucket.elem[idx].str_offset], bucket.elem[idx].comp_size, uncomp.ptrw(), bucket.elem[idx].uncomp_size); String rstr; rstr.parse_utf8(uncomp.get_data()); //print_line("Compressed, size: "+itos(bucket.elem[idx].comp_size)); diff --git a/core/engine.cpp b/core/engine.cpp index c609ae9520c0..53c7a73b43be 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -84,22 +84,47 @@ Dictionary Engine::get_version_info() const { #else dict["patch"] = 0; #endif - dict["status"] = _MKSTR(VERSION_STATUS); - dict["revision"] = _MKSTR(VERSION_REVISION); + dict["status"] = VERSION_STATUS; + dict["build"] = VERSION_BUILD; dict["year"] = VERSION_YEAR; - String hash = String(VERSION_HASH); + String hash = VERSION_HASH; dict["hash"] = hash.length() == 0 ? String("unknown") : hash; String stringver = String(dict["major"]) + "." + String(dict["minor"]); if ((int)dict["patch"] != 0) stringver += "." + String(dict["patch"]); - stringver += "-" + String(dict["status"]) + " (" + String(dict["revision"]) + ")"; + stringver += "-" + String(dict["status"]) + " (" + String(dict["build"]) + ")"; dict["string"] = stringver; return dict; } +void Engine::add_singleton(const Singleton &p_singleton) { + + singletons.push_back(p_singleton); + singleton_ptrs[p_singleton.name] = p_singleton.ptr; +} + +Object *Engine::get_singleton_object(const String &p_name) const { + + const Map::Element *E = singleton_ptrs.find(p_name); + ERR_EXPLAIN("Failed to retrieve non-existent singleton '" + p_name + "'"); + ERR_FAIL_COND_V(!E, NULL); + return E->get(); +}; + +bool Engine::has_singleton(const String &p_name) const { + + return singleton_ptrs.has(p_name); +}; + +void Engine::get_singletons(List *p_singletons) { + + for (List::Element *E = singletons.front(); E; E = E->next()) + p_singletons->push_back(E->get()); +} + Engine *Engine::singleton = NULL; Engine *Engine::get_singleton() { diff --git a/core/engine.h b/core/engine.h index 3b4979582fb7..4a573c153984 100644 --- a/core/engine.h +++ b/core/engine.h @@ -37,6 +37,17 @@ class Engine { +public: + struct Singleton { + StringName name; + Object *ptr; + Singleton(const StringName &p_name = StringName(), Object *p_ptr = NULL) + : name(p_name), + ptr(p_ptr) { + } + }; + +private: friend class Main; uint64_t frames_drawn; @@ -54,6 +65,9 @@ class Engine { uint64_t _idle_frames; bool _in_physics; + List singletons; + Map singleton_ptrs; + bool editor_hint; static Engine *singleton; @@ -83,6 +97,11 @@ class Engine { void set_frame_delay(uint32_t p_msec); uint32_t get_frame_delay() const; + void add_singleton(const Singleton &p_singleton); + void get_singletons(List *p_singletons); + bool has_singleton(const String &p_name) const; + Object *get_singleton_object(const String &p_name) const; + _FORCE_INLINE_ bool get_use_pixel_snap() const { return _pixel_snap; } #ifdef TOOLS_ENABLED diff --git a/core/error_macros.cpp b/core/error_macros.cpp index 7d85aa900155..a942b1dd2d53 100644 --- a/core/error_macros.cpp +++ b/core/error_macros.cpp @@ -101,6 +101,6 @@ void _err_print_error(const char *p_function, const char *p_file, int p_line, co void _err_print_index_error(const char *p_function, const char *p_file, int p_line, int64_t p_index, int64_t p_size, const char *p_index_str, const char *p_size_str, bool fatal) { String fstr(fatal ? "FATAL: " : ""); - String err(fstr + "Index" + p_index_str + "=" + itos(p_index) + " out of size (" + p_size_str + "=" + itos(p_size) + ")"); + String err(fstr + "Index " + p_index_str + "=" + itos(p_index) + " out of size (" + p_size_str + "=" + itos(p_size) + ")"); _err_print_error(p_function, p_file, p_line, err.utf8().get_data()); } diff --git a/core/global_constants.cpp b/core/global_constants.cpp index 7854f342b0a6..48101c8cf11c 100644 --- a/core/global_constants.cpp +++ b/core/global_constants.cpp @@ -557,7 +557,7 @@ void register_global_constants() { BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("TYPE_TRANSFORM2D", Variant::TRANSFORM2D); BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("TYPE_PLANE", Variant::PLANE); BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("TYPE_QUAT", Variant::QUAT); // 10 - BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("TYPE_RECT3", Variant::RECT3); + BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("TYPE_AABB", Variant::AABB); BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("TYPE_BASIS", Variant::BASIS); BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("TYPE_TRANSFORM", Variant::TRANSFORM); BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("TYPE_COLOR", Variant::COLOR); diff --git a/core/helper/math_fieldwise.cpp b/core/helper/math_fieldwise.cpp index 228611f8b3b6..2cd8a4f39250 100644 --- a/core/helper/math_fieldwise.cpp +++ b/core/helper/math_fieldwise.cpp @@ -46,8 +46,8 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const switch (p_source.get_type()) { - /* clang-format makes a mess of this macro usage */ - /* clang-format off */ + /* clang-format makes a mess of this macro usage */ + /* clang-format off */ case Variant::VECTOR2: { @@ -106,9 +106,9 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const return target; } - case Variant::RECT3: { + case Variant::AABB: { - SETUP_TYPE(Rect3) + SETUP_TYPE(AABB) /**/ TRY_TRANSFER_FIELD("px", position.x) else TRY_TRANSFER_FIELD("py", position.y) diff --git a/core/image.cpp b/core/image.cpp index 42684e7ea733..422c0e407b07 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -757,22 +757,24 @@ void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { _copy_internals_from(dst); } -void Image::crop(int p_width, int p_height) { +void Image::crop_from_point(int p_x, int p_y, int p_width, int p_height) { if (!_can_modify(format)) { ERR_EXPLAIN("Cannot crop in indexed, compressed or custom image formats."); ERR_FAIL(); } + ERR_FAIL_COND(p_x < 0); + ERR_FAIL_COND(p_y < 0); ERR_FAIL_COND(p_width <= 0); ERR_FAIL_COND(p_height <= 0); - ERR_FAIL_COND(p_width > MAX_WIDTH); - ERR_FAIL_COND(p_height > MAX_HEIGHT); + ERR_FAIL_COND(p_x + p_width > MAX_WIDTH); + ERR_FAIL_COND(p_y + p_height > MAX_HEIGHT); /* to save memory, cropping should be done in-place, however, since this function will most likely either not be used much, or in critical areas, for now it wont, because it's a waste of time. */ - if (p_width == width && p_height == height) + if (p_width == width && p_height == height && p_x == 0 && p_y == 0) return; uint8_t pdata[16]; //largest is 16 @@ -784,9 +786,11 @@ void Image::crop(int p_width, int p_height) { PoolVector::Read r = data.read(); PoolVector::Write w = dst.data.write(); - for (int y = 0; y < p_height; y++) { + int m_h = p_y + p_height; + int m_w = p_x + p_width; + for (int y = p_y; y < m_h; y++) { - for (int x = 0; x < p_width; x++) { + for (int x = p_x; x < m_w; x++) { if ((x >= width || y >= height)) { for (uint32_t i = 0; i < pixel_size; i++) @@ -795,7 +799,7 @@ void Image::crop(int p_width, int p_height) { _get_pixelb(x, y, pixel_size, r.ptr(), pdata); } - dst._put_pixelb(x, y, pixel_size, w.ptr(), pdata); + dst._put_pixelb(x - p_x, y - p_y, pixel_size, w.ptr(), pdata); } } } @@ -805,6 +809,11 @@ void Image::crop(int p_width, int p_height) { _copy_internals_from(dst); } +void Image::crop(int p_width, int p_height) { + + crop_from_point(0, 0, p_width, p_height); +} + void Image::flip_y() { if (!_can_modify(format)) { diff --git a/core/image.h b/core/image.h index 27df65a89895..24693aa70648 100644 --- a/core/image.h +++ b/core/image.h @@ -207,6 +207,7 @@ class Image : public Resource { /** * Crop the image to a specific size, if larger, then the image is filled by black */ + void crop_from_point(int p_x, int p_y, int p_width, int p_height); void crop(int p_width, int p_height); void flip_x(); diff --git a/core/io/file_access_compressed.cpp b/core/io/file_access_compressed.cpp index 514e3c65f014..c6f31dc8f079 100644 --- a/core/io/file_access_compressed.cpp +++ b/core/io/file_access_compressed.cpp @@ -51,7 +51,7 @@ void FileAccessCompressed::configure(const String &p_magic, Compression::Mode p_ if (write_max > write_buffer_size) { \ write_buffer_size = next_power_of_2(write_max); \ buffer.resize(write_buffer_size); \ - write_ptr = buffer.ptr(); \ + write_ptr = buffer.ptrw(); \ } \ } @@ -76,14 +76,14 @@ Error FileAccessCompressed::open_after_magic(FileAccess *p_base) { comp_buffer.resize(max_bs); buffer.resize(block_size); - read_ptr = buffer.ptr(); - f->get_buffer(comp_buffer.ptr(), read_blocks[0].csize); + read_ptr = buffer.ptrw(); + f->get_buffer(comp_buffer.ptrw(), read_blocks[0].csize); at_end = false; read_eof = false; read_block_count = bc; read_block_size = read_blocks.size() == 1 ? read_total : block_size; - Compression::decompress(buffer.ptr(), read_block_size, comp_buffer.ptr(), read_blocks[0].csize, cmode); + Compression::decompress(buffer.ptrw(), read_block_size, comp_buffer.ptr(), read_blocks[0].csize, cmode); read_block = 0; read_pos = 0; @@ -114,7 +114,7 @@ Error FileAccessCompressed::_open(const String &p_path, int p_mode_flags) { write_buffer_size = 256; buffer.resize(256); write_max = 0; - write_ptr = buffer.ptr(); + write_ptr = buffer.ptrw(); //don't store anything else unless it's done saving! } else { @@ -160,7 +160,7 @@ void FileAccessCompressed::close() { Vector cblock; cblock.resize(Compression::get_max_compressed_buffer_size(bl, cmode)); - int s = Compression::compress(cblock.ptr(), bp, bl, cmode); + int s = Compression::compress(cblock.ptrw(), bp, bl, cmode); f->store_buffer(cblock.ptr(), s); block_sizes.push_back(s); @@ -211,8 +211,8 @@ void FileAccessCompressed::seek(size_t p_position) { read_block = block_idx; f->seek(read_blocks[read_block].offset); - f->get_buffer(comp_buffer.ptr(), read_blocks[read_block].csize); - Compression::decompress(buffer.ptr(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode); + f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize); + Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode); read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size; } @@ -282,8 +282,8 @@ uint8_t FileAccessCompressed::get_8() const { if (read_block < read_block_count) { //read another block of compressed data - f->get_buffer(comp_buffer.ptr(), read_blocks[read_block].csize); - Compression::decompress(buffer.ptr(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode); + f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize); + Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode); read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size; read_pos = 0; @@ -315,8 +315,8 @@ int FileAccessCompressed::get_buffer(uint8_t *p_dst, int p_length) const { if (read_block < read_block_count) { //read another block of compressed data - f->get_buffer(comp_buffer.ptr(), read_blocks[read_block].csize); - Compression::decompress(buffer.ptr(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode); + f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize); + Compression::decompress(buffer.ptrw(), read_blocks.size() == 1 ? read_total : block_size, comp_buffer.ptr(), read_blocks[read_block].csize, cmode); read_block_size = read_block == read_block_count - 1 ? read_total % block_size : block_size; read_pos = 0; diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index e5da307153d0..71ebf5750842 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -80,11 +80,11 @@ Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vectorget_buffer(data.ptr(), ds); + uint32_t blen = p_base->get_buffer(data.ptrw(), ds); ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT); aes256_context ctx; - aes256_init(&ctx, key.ptr()); + aes256_init(&ctx, key.ptrw()); for (size_t i = 0; i < ds; i += 16) { @@ -97,7 +97,7 @@ Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector block; block.resize(len); - client->get_data(block.ptr(), len); + client->get_data(block.ptrw(), len); if (fa) //may have been queued fa->_set_block(offset, block); @@ -434,12 +434,12 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const { _queue_page(page + j); } - buff = pages[page].buffer.ptr(); + buff = pages[page].buffer.ptrw(); //queue pages buffer_mutex->unlock(); } - buff = pages[page].buffer.ptr(); + buff = pages[page].buffer.ptrw(); last_page_buff = buff; last_page = page; } diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 46d52384e515..5097898314e0 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -30,6 +30,7 @@ #include "http_client.h" #include "io/stream_peer_ssl.h" +#ifndef JAVASCRIPT_ENABLED Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl, bool p_verify_host) { close(); @@ -405,38 +406,6 @@ Error HTTPClient::poll() { return OK; } -Dictionary HTTPClient::_get_response_headers_as_dictionary() { - - List rh; - get_response_headers(&rh); - Dictionary ret; - for (const List::Element *E = rh.front(); E; E = E->next()) { - String s = E->get(); - int sp = s.find(":"); - if (sp == -1) - continue; - String key = s.substr(0, sp).strip_edges(); - String value = s.substr(sp + 1, s.length()).strip_edges(); - ret[key] = value; - } - - return ret; -} - -PoolStringArray HTTPClient::_get_response_headers() { - - List rh; - get_response_headers(&rh); - PoolStringArray ret; - ret.resize(rh.size()); - int idx = 0; - for (const List::Element *E = rh.front(); E; E = E->next()) { - ret.set(idx++, E->get()); - } - - return ret; -} - int HTTPClient::get_response_body_length() const { return body_size; @@ -612,6 +581,74 @@ Error HTTPClient::_get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received } } +void HTTPClient::set_read_chunk_size(int p_size) { + ERR_FAIL_COND(p_size < 256 || p_size > (1 << 24)); + read_chunk_size = p_size; +} + +HTTPClient::HTTPClient() { + + tcp_connection = StreamPeerTCP::create_ref(); + resolving = IP::RESOLVER_INVALID_ID; + status = STATUS_DISCONNECTED; + conn_port = 80; + body_size = 0; + chunked = false; + body_left = 0; + chunk_left = 0; + response_num = 0; + ssl = false; + blocking = false; + read_chunk_size = 4096; +} + +HTTPClient::~HTTPClient() { +} + +#endif // #ifndef JAVASCRIPT_ENABLED + +String HTTPClient::query_string_from_dict(const Dictionary &p_dict) { + String query = ""; + Array keys = p_dict.keys(); + for (int i = 0; i < keys.size(); ++i) { + query += "&" + String(keys[i]).http_escape() + "=" + String(p_dict[keys[i]]).http_escape(); + } + query.erase(0, 1); + return query; +} + +Dictionary HTTPClient::_get_response_headers_as_dictionary() { + + List rh; + get_response_headers(&rh); + Dictionary ret; + for (const List::Element *E = rh.front(); E; E = E->next()) { + String s = E->get(); + int sp = s.find(":"); + if (sp == -1) + continue; + String key = s.substr(0, sp).strip_edges(); + String value = s.substr(sp + 1, s.length()).strip_edges(); + ret[key] = value; + } + + return ret; +} + +PoolStringArray HTTPClient::_get_response_headers() { + + List rh; + get_response_headers(&rh); + PoolStringArray ret; + ret.resize(rh.size()); + int idx = 0; + for (const List::Element *E = rh.front(); E; E = E->next()) { + ret.set(idx++, E->get()); + } + + return ret; +} + void HTTPClient::_bind_methods() { ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port", "use_ssl", "verify_host"), &HTTPClient::connect_to_host, DEFVAL(false), DEFVAL(true)); @@ -717,37 +754,3 @@ void HTTPClient::_bind_methods() { BIND_ENUM_CONSTANT(RESPONSE_INSUFFICIENT_STORAGE); BIND_ENUM_CONSTANT(RESPONSE_NOT_EXTENDED); } - -void HTTPClient::set_read_chunk_size(int p_size) { - ERR_FAIL_COND(p_size < 256 || p_size > (1 << 24)); - read_chunk_size = p_size; -} - -String HTTPClient::query_string_from_dict(const Dictionary &p_dict) { - String query = ""; - Array keys = p_dict.keys(); - for (int i = 0; i < keys.size(); ++i) { - query += "&" + String(keys[i]).http_escape() + "=" + String(p_dict[keys[i]]).http_escape(); - } - query.erase(0, 1); - return query; -} - -HTTPClient::HTTPClient() { - - tcp_connection = StreamPeerTCP::create_ref(); - resolving = IP::RESOLVER_INVALID_ID; - status = STATUS_DISCONNECTED; - conn_port = 80; - body_size = 0; - chunked = false; - body_left = 0; - chunk_left = 0; - response_num = 0; - ssl = false; - blocking = false; - read_chunk_size = 4096; -} - -HTTPClient::~HTTPClient() { -} diff --git a/core/io/http_client.h b/core/io/http_client.h index f8a3349e6e7b..db5dd115bd86 100644 --- a/core/io/http_client.h +++ b/core/io/http_client.h @@ -131,6 +131,7 @@ class HTTPClient : public Reference { }; private: +#ifndef JAVASCRIPT_ENABLED Status status; IP::ResolverID resolving; int conn_port; @@ -152,13 +153,18 @@ class HTTPClient : public Reference { int response_num; Vector response_headers; + int read_chunk_size; + + Error _get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received); + +#else +#include "platform/javascript/http_client.h.inc" +#endif - static void _bind_methods(); PoolStringArray _get_response_headers(); Dictionary _get_response_headers_as_dictionary(); - int read_chunk_size; - Error _get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received); + static void _bind_methods(); public: //Error connect_and_get(const String& p_url,bool p_verify_host=true); //connects to a full url and perform request diff --git a/core/io/logger.cpp b/core/io/logger.cpp index ce2ce44b1d23..7177359c8afb 100644 --- a/core/io/logger.cpp +++ b/core/io/logger.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "logger.h" + #include "os/dir_access.h" #include "os/os.h" #include "print_string.h" @@ -259,6 +260,10 @@ void CompositeLogger::log_error(const char *p_function, const char *p_file, int } } +void CompositeLogger::add_logger(Logger *p_logger) { + loggers.push_back(p_logger); +} + CompositeLogger::~CompositeLogger() { for (int i = 0; i < loggers.size(); ++i) { memdelete(loggers[i]); diff --git a/core/io/logger.h b/core/io/logger.h index cf0cc7699f57..f8a394193fea 100644 --- a/core/io/logger.h +++ b/core/io/logger.h @@ -101,6 +101,8 @@ class CompositeLogger : public Logger { virtual void logv(const char *p_format, va_list p_list, bool p_err); virtual void log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, ErrorType p_type = ERR_ERROR); + void add_logger(Logger *p_logger); + virtual ~CompositeLogger(); }; diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index d388a622dead..37320d7a7795 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -159,7 +159,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int r_variant = str; } break; - // math types + // math types case Variant::VECTOR2: { @@ -245,10 +245,10 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int (*r_len) += 4 * 4; } break; - case Variant::RECT3: { + case Variant::AABB: { ERR_FAIL_COND_V(len < (int)4 * 6, ERR_INVALID_DATA); - Rect3 val; + AABB val; val.position.x = decode_float(&buf[0]); val.position.y = decode_float(&buf[4]); val.position.z = decode_float(&buf[8]); @@ -324,7 +324,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int ERR_FAIL_COND_V(len < 12, ERR_INVALID_DATA); Vector names; Vector subnames; - StringName prop; uint32_t namecount = strlen &= 0x7FFFFFFF; uint32_t subnamecount = decode_uint32(buf + 4); @@ -333,9 +332,10 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int len -= 12; buf += 12; + if (flags & 2) // Obsolete format with property seperate from subpath + subnamecount++; + uint32_t total = namecount + subnamecount; - if (flags & 2) - total++; if (r_len) (*r_len) += 12; @@ -359,10 +359,8 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int if (i < namecount) names.push_back(str); - else if (i < namecount + subnamecount) - subnames.push_back(str); else - prop = str; + subnames.push_back(str); buf += strlen + pad; len -= strlen + pad; @@ -371,7 +369,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int (*r_len) += 4 + strlen + pad; } - r_variant = NodePath(names, subnames, flags & 1, prop); + r_variant = NodePath(names, subnames, flags & 1); } else { //old format, just a string @@ -919,8 +917,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo uint32_t flags = 0; if (np.is_absolute()) flags |= 1; - if (np.get_property() != StringName()) - flags |= 2; encode_uint32(flags, buf + 8); @@ -930,8 +926,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 12; int total = np.get_name_count() + np.get_subname_count(); - if (np.get_property() != StringName()) - total++; for (int i = 0; i < total; i++) { @@ -939,10 +933,8 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo if (i < np.get_name_count()) str = np.get_name(i); - else if (i < np.get_name_count() + np.get_subname_count()) - str = np.get_subname(i - np.get_subname_count()); else - str = np.get_property(); + str = np.get_subname(i - np.get_name_count()); CharString utf8 = str.utf8(); @@ -967,7 +959,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo _encode_string(p_variant, buf, r_len); } break; - // math types + // math types case Variant::VECTOR2: { @@ -1045,10 +1037,10 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4 * 4; } break; - case Variant::RECT3: { + case Variant::AABB: { if (buf) { - Rect3 aabb = p_variant; + AABB aabb = p_variant; encode_float(aabb.position.x, &buf[0]); encode_float(aabb.position.y, &buf[4]); encode_float(aabb.position.z, &buf[8]); diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 03c3c5f6152f..df0d41ea9d43 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -52,7 +52,7 @@ enum { VARIANT_VECTOR3 = 12, VARIANT_PLANE = 13, VARIANT_QUAT = 14, - VARIANT_RECT3 = 15, + VARIANT_AABB = 15, VARIANT_MATRIX3 = 16, VARIANT_TRANSFORM = 17, VARIANT_MATRIX32 = 18, @@ -84,8 +84,10 @@ enum { OBJECT_INTERNAL_RESOURCE = 2, OBJECT_EXTERNAL_RESOURCE_INDEX = 3, //version 2: added 64 bits support for float and int - FORMAT_VERSION = 2, - FORMAT_VERSION_CAN_RENAME_DEPS = 1 + //version 3: changed nodepath encoding + FORMAT_VERSION = 3, + FORMAT_VERSION_CAN_RENAME_DEPS = 1, + FORMAT_VERSION_NO_NODEPATH_PROPERTY = 3, }; @@ -196,9 +198,9 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { r_v = v; } break; - case VARIANT_RECT3: { + case VARIANT_AABB: { - Rect3 v; + AABB v; v.position.x = f->get_real(); v.position.y = f->get_real(); v.position.z = f->get_real(); @@ -267,21 +269,22 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { Vector names; Vector subnames; - StringName property; bool absolute; int name_count = f->get_16(); uint32_t subname_count = f->get_16(); absolute = subname_count & 0x8000; subname_count &= 0x7FFF; + if (ver_format < FORMAT_VERSION_NO_NODEPATH_PROPERTY) { + subname_count += 1; // has a property field, so we should count it as well + } for (int i = 0; i < name_count; i++) names.push_back(_get_string()); for (uint32_t i = 0; i < subname_count; i++) subnames.push_back(_get_string()); - property = _get_string(); - NodePath np = NodePath(names, subnames, absolute, property); + NodePath np = NodePath(names, subnames, absolute); r_v = np; @@ -856,7 +859,7 @@ void ResourceInteractiveLoaderBinary::open(FileAccess *p_f) { uint32_t ver_major = f->get_32(); uint32_t ver_minor = f->get_32(); - uint32_t ver_format = f->get_32(); + ver_format = f->get_32(); print_bl("big endian: " + itos(big_endian)); #ifdef BIG_ENDIAN_ENABLED @@ -1374,10 +1377,10 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property, f->store_real(val.w); } break; - case Variant::RECT3: { + case Variant::AABB: { - f->store_32(VARIANT_RECT3); - Rect3 val = p_property; + f->store_32(VARIANT_AABB); + AABB val = p_property; f->store_real(val.position.x); f->store_real(val.position.y); f->store_real(val.position.z); @@ -1454,7 +1457,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property, f->store_32(get_string_index(np.get_name(i))); for (int i = 0; i < np.get_subname_count(); i++) f->store_32(get_string_index(np.get_subname(i))); - f->store_32(get_string_index(np.get_property())); } break; case Variant::_RID: { @@ -1685,7 +1687,6 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant get_string_index(np.get_name(i)); for (int i = 0; i < np.get_subname_count(); i++) get_string_index(np.get_subname(i)); - get_string_index(np.get_property()); } break; default: {} diff --git a/core/io/resource_format_binary.h b/core/io/resource_format_binary.h index 2316f05b3c12..687da0a9b42c 100644 --- a/core/io/resource_format_binary.h +++ b/core/io/resource_format_binary.h @@ -41,6 +41,7 @@ class ResourceInteractiveLoaderBinary : public ResourceInteractiveLoader { String res_path; String type; Ref resource; + uint32_t ver_format; FileAccess *f; diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index 2583eb369d15..42a258a10d64 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -220,7 +220,7 @@ void StreamPeer::put_var(const Variant &p_variant) { encode_variant(p_variant, NULL, len); buf.resize(len); put_32(len); - encode_variant(p_variant, buf.ptr(), len); + encode_variant(p_variant, buf.ptrw(), len); put_data(buf.ptr(), buf.size()); } @@ -340,7 +340,7 @@ String StreamPeer::get_utf8_string(int p_bytes) { Vector buf; Error err = buf.resize(p_bytes); ERR_FAIL_COND_V(err != OK, String()); - err = get_data(buf.ptr(), p_bytes); + err = get_data(buf.ptrw(), p_bytes); ERR_FAIL_COND_V(err != OK, String()); String ret; @@ -353,7 +353,7 @@ Variant StreamPeer::get_var() { Vector var; Error err = var.resize(len); ERR_FAIL_COND_V(err != OK, Variant()); - err = get_data(var.ptr(), len); + err = get_data(var.ptrw(), len); ERR_FAIL_COND_V(err != OK, Variant()); Variant ret; diff --git a/core/list.h b/core/list.h index da201e986817..448be4a5ab6b 100644 --- a/core/list.h +++ b/core/list.h @@ -306,6 +306,8 @@ class List { if (!p_element->next_ptr) { _data->last = n; + } else { + p_element->next_ptr->prev_ptr = n; } p_element->next_ptr = n; @@ -330,6 +332,8 @@ class List { if (!p_element->prev_ptr) { _data->first = n; + } else { + p_element->prev_ptr->next_ptr = n; } p_element->prev_ptr = n; diff --git a/core/math/rect3.cpp b/core/math/aabb.cpp similarity index 88% rename from core/math/rect3.cpp rename to core/math/aabb.cpp index 6f01000f614c..737a42b337d3 100644 --- a/core/math/rect3.cpp +++ b/core/math/aabb.cpp @@ -1,5 +1,5 @@ /*************************************************************************/ -/* rect3.cpp */ +/* aabb.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -27,25 +27,25 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "rect3.h" +#include "aabb.h" #include "print_string.h" -real_t Rect3::get_area() const { +real_t AABB::get_area() const { return size.x * size.y * size.z; } -bool Rect3::operator==(const Rect3 &p_rval) const { +bool AABB::operator==(const AABB &p_rval) const { return ((position == p_rval.position) && (size == p_rval.size)); } -bool Rect3::operator!=(const Rect3 &p_rval) const { +bool AABB::operator!=(const AABB &p_rval) const { return ((position != p_rval.position) || (size != p_rval.size)); } -void Rect3::merge_with(const Rect3 &p_aabb) { +void AABB::merge_with(const AABB &p_aabb) { Vector3 beg_1, beg_2; Vector3 end_1, end_2; @@ -68,7 +68,7 @@ void Rect3::merge_with(const Rect3 &p_aabb) { size = max - min; } -Rect3 Rect3::intersection(const Rect3 &p_aabb) const { +AABB AABB::intersection(const AABB &p_aabb) const { Vector3 src_min = position; Vector3 src_max = position + size; @@ -78,7 +78,7 @@ Rect3 Rect3::intersection(const Rect3 &p_aabb) const { Vector3 min, max; if (src_min.x > dst_max.x || src_max.x < dst_min.x) - return Rect3(); + return AABB(); else { min.x = (src_min.x > dst_min.x) ? src_min.x : dst_min.x; @@ -86,7 +86,7 @@ Rect3 Rect3::intersection(const Rect3 &p_aabb) const { } if (src_min.y > dst_max.y || src_max.y < dst_min.y) - return Rect3(); + return AABB(); else { min.y = (src_min.y > dst_min.y) ? src_min.y : dst_min.y; @@ -94,17 +94,17 @@ Rect3 Rect3::intersection(const Rect3 &p_aabb) const { } if (src_min.z > dst_max.z || src_max.z < dst_min.z) - return Rect3(); + return AABB(); else { min.z = (src_min.z > dst_min.z) ? src_min.z : dst_min.z; max.z = (src_max.z < dst_max.z) ? src_max.z : dst_max.z; } - return Rect3(min, max - min); + return AABB(min, max - min); } -bool Rect3::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const { +bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const { Vector3 c1, c2; Vector3 end = position + size; @@ -147,7 +147,7 @@ bool Rect3::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 return true; } -bool Rect3::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const { +bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const { real_t min = 0, max = 1; int axis = 0; @@ -205,7 +205,7 @@ bool Rect3::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vecto return true; } -bool Rect3::intersects_plane(const Plane &p_plane) const { +bool AABB::intersects_plane(const Plane &p_plane) const { Vector3 points[8] = { Vector3(position.x, position.y, position.z), @@ -232,7 +232,7 @@ bool Rect3::intersects_plane(const Plane &p_plane) const { return under && over; } -Vector3 Rect3::get_longest_axis() const { +Vector3 AABB::get_longest_axis() const { Vector3 axis(1, 0, 0); real_t max_size = size.x; @@ -249,7 +249,7 @@ Vector3 Rect3::get_longest_axis() const { return axis; } -int Rect3::get_longest_axis_index() const { +int AABB::get_longest_axis_index() const { int axis = 0; real_t max_size = size.x; @@ -267,7 +267,7 @@ int Rect3::get_longest_axis_index() const { return axis; } -Vector3 Rect3::get_shortest_axis() const { +Vector3 AABB::get_shortest_axis() const { Vector3 axis(1, 0, 0); real_t max_size = size.x; @@ -284,7 +284,7 @@ Vector3 Rect3::get_shortest_axis() const { return axis; } -int Rect3::get_shortest_axis_index() const { +int AABB::get_shortest_axis_index() const { int axis = 0; real_t max_size = size.x; @@ -302,25 +302,25 @@ int Rect3::get_shortest_axis_index() const { return axis; } -Rect3 Rect3::merge(const Rect3 &p_with) const { +AABB AABB::merge(const AABB &p_with) const { - Rect3 aabb = *this; + AABB aabb = *this; aabb.merge_with(p_with); return aabb; } -Rect3 Rect3::expand(const Vector3 &p_vector) const { - Rect3 aabb = *this; +AABB AABB::expand(const Vector3 &p_vector) const { + AABB aabb = *this; aabb.expand_to(p_vector); return aabb; } -Rect3 Rect3::grow(real_t p_by) const { +AABB AABB::grow(real_t p_by) const { - Rect3 aabb = *this; + AABB aabb = *this; aabb.grow_by(p_by); return aabb; } -void Rect3::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { +void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { ERR_FAIL_INDEX(p_edge, 12); switch (p_edge) { @@ -394,7 +394,7 @@ void Rect3::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const { } } -Rect3::operator String() const { +AABB::operator String() const { return String() + position + " - " + size; } diff --git a/core/math/rect3.h b/core/math/aabb.h similarity index 84% rename from core/math/rect3.h rename to core/math/aabb.h index c3a2f5fbfb00..c60213496a59 100644 --- a/core/math/rect3.h +++ b/core/math/aabb.h @@ -1,5 +1,5 @@ /*************************************************************************/ -/* rect3.h */ +/* aabb.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -39,7 +39,7 @@ * This is implemented by a point (position) and the box size */ -class Rect3 { +class AABB { public: Vector3 position; Vector3 size; @@ -60,16 +60,16 @@ class Rect3 { const Vector3 &get_size() const { return size; } void set_size(const Vector3 &p_size) { size = p_size; } - bool operator==(const Rect3 &p_rval) const; - bool operator!=(const Rect3 &p_rval) const; + bool operator==(const AABB &p_rval) const; + bool operator!=(const AABB &p_rval) const; - _FORCE_INLINE_ bool intersects(const Rect3 &p_aabb) const; /// Both AABBs overlap - _FORCE_INLINE_ bool intersects_inclusive(const Rect3 &p_aabb) const; /// Both AABBs (or their faces) overlap - _FORCE_INLINE_ bool encloses(const Rect3 &p_aabb) const; /// p_aabb is completely inside this + _FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap + _FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap + _FORCE_INLINE_ bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this - Rect3 merge(const Rect3 &p_with) const; - void merge_with(const Rect3 &p_aabb); ///merge with another AABB - Rect3 intersection(const Rect3 &p_aabb) const; ///get box where two intersect, empty if no intersection occurs + AABB merge(const AABB &p_with) const; + void merge_with(const AABB &p_aabb); ///merge with another AABB + AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip = NULL, Vector3 *r_normal = NULL) const; bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip = NULL, Vector3 *r_normal = NULL) const; _FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const; @@ -88,26 +88,26 @@ class Rect3 { int get_shortest_axis_index() const; _FORCE_INLINE_ real_t get_shortest_axis_size() const; - Rect3 grow(real_t p_by) const; + AABB grow(real_t p_by) const; _FORCE_INLINE_ void grow_by(real_t p_amount); void get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const; _FORCE_INLINE_ Vector3 get_endpoint(int p_point) const; - Rect3 expand(const Vector3 &p_vector) const; + AABB expand(const Vector3 &p_vector) const; _FORCE_INLINE_ void project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const; _FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necessary */ operator String() const; - _FORCE_INLINE_ Rect3() {} - inline Rect3(const Vector3 &p_pos, const Vector3 &p_size) + _FORCE_INLINE_ AABB() {} + inline AABB(const Vector3 &p_pos, const Vector3 &p_size) : position(p_pos), size(p_size) { } }; -inline bool Rect3::intersects(const Rect3 &p_aabb) const { +inline bool AABB::intersects(const AABB &p_aabb) const { if (position.x >= (p_aabb.position.x + p_aabb.size.x)) return false; @@ -125,7 +125,7 @@ inline bool Rect3::intersects(const Rect3 &p_aabb) const { return true; } -inline bool Rect3::intersects_inclusive(const Rect3 &p_aabb) const { +inline bool AABB::intersects_inclusive(const AABB &p_aabb) const { if (position.x > (p_aabb.position.x + p_aabb.size.x)) return false; @@ -143,7 +143,7 @@ inline bool Rect3::intersects_inclusive(const Rect3 &p_aabb) const { return true; } -inline bool Rect3::encloses(const Rect3 &p_aabb) const { +inline bool AABB::encloses(const AABB &p_aabb) const { Vector3 src_min = position; Vector3 src_max = position + size; @@ -159,7 +159,7 @@ inline bool Rect3::encloses(const Rect3 &p_aabb) const { (src_max.z > dst_max.z)); } -Vector3 Rect3::get_support(const Vector3 &p_normal) const { +Vector3 AABB::get_support(const Vector3 &p_normal) const { Vector3 half_extents = size * 0.5; Vector3 ofs = position + half_extents; @@ -171,7 +171,7 @@ Vector3 Rect3::get_support(const Vector3 &p_normal) const { ofs; } -Vector3 Rect3::get_endpoint(int p_point) const { +Vector3 AABB::get_endpoint(int p_point) const { switch (p_point) { case 0: return Vector3(position.x, position.y, position.z); @@ -187,7 +187,7 @@ Vector3 Rect3::get_endpoint(int p_point) const { ERR_FAIL_V(Vector3()); } -bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const { +bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const { Vector3 half_extents = size * 0.5; Vector3 ofs = position + half_extents; @@ -206,7 +206,7 @@ bool Rect3::intersects_convex_shape(const Plane *p_planes, int p_plane_count) co return true; } -bool Rect3::has_point(const Vector3 &p_point) const { +bool AABB::has_point(const Vector3 &p_point) const { if (p_point.x < position.x) return false; @@ -224,7 +224,7 @@ bool Rect3::has_point(const Vector3 &p_point) const { return true; } -inline void Rect3::expand_to(const Vector3 &p_vector) { +inline void AABB::expand_to(const Vector3 &p_vector) { Vector3 begin = position; Vector3 end = position + size; @@ -247,7 +247,7 @@ inline void Rect3::expand_to(const Vector3 &p_vector) { size = end - begin; } -void Rect3::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const { +void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const { Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5); Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z); @@ -258,7 +258,7 @@ void Rect3::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t & r_max = distance + length; } -inline real_t Rect3::get_longest_axis_size() const { +inline real_t AABB::get_longest_axis_size() const { real_t max_size = size.x; @@ -273,7 +273,7 @@ inline real_t Rect3::get_longest_axis_size() const { return max_size; } -inline real_t Rect3::get_shortest_axis_size() const { +inline real_t AABB::get_shortest_axis_size() const { real_t max_size = size.x; @@ -288,7 +288,7 @@ inline real_t Rect3::get_shortest_axis_size() const { return max_size; } -bool Rect3::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const { +bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const { real_t divx = 1.0 / p_dir.x; real_t divy = 1.0 / p_dir.y; @@ -332,7 +332,7 @@ bool Rect3::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, rea return ((tmin < t1) && (tmax > t0)); } -void Rect3::grow_by(real_t p_amount) { +void AABB::grow_by(real_t p_amount) { position.x -= p_amount; position.y -= p_amount; diff --git a/core/math/bsp_tree.cpp b/core/math/bsp_tree.cpp index be950568cf5f..bdc040160f99 100644 --- a/core/math/bsp_tree.cpp +++ b/core/math/bsp_tree.cpp @@ -31,7 +31,7 @@ #include "error_macros.h" #include "print_string.h" -void BSP_Tree::from_aabb(const Rect3 &p_aabb) { +void BSP_Tree::from_aabb(const AABB &p_aabb) { planes.clear(); @@ -67,7 +67,7 @@ Vector BSP_Tree::get_planes() const { return planes; } -Rect3 BSP_Tree::get_aabb() const { +AABB BSP_Tree::get_aabb() const { return aabb; } @@ -577,7 +577,7 @@ BSP_Tree::BSP_Tree(const PoolVector &p_faces, real_t p_error_radius) { error_radius = p_error_radius; } -BSP_Tree::BSP_Tree(const Vector &p_nodes, const Vector &p_planes, const Rect3 &p_aabb, real_t p_error_radius) +BSP_Tree::BSP_Tree(const Vector &p_nodes, const Vector &p_planes, const AABB &p_aabb, real_t p_error_radius) : nodes(p_nodes), planes(p_planes), aabb(p_aabb), diff --git a/core/math/bsp_tree.h b/core/math/bsp_tree.h index 2e762ba4dea6..f64a13ce3929 100644 --- a/core/math/bsp_tree.h +++ b/core/math/bsp_tree.h @@ -30,11 +30,11 @@ #ifndef BSP_TREE_H #define BSP_TREE_H +#include "aabb.h" #include "dvector.h" #include "face3.h" #include "method_ptrcall.h" #include "plane.h" -#include "rect3.h" #include "variant.h" #include "vector.h" /** @@ -64,7 +64,7 @@ class BSP_Tree { Vector nodes; Vector planes; - Rect3 aabb; + AABB aabb; real_t error_radius; int _get_points_inside(int p_node, const Vector3 *p_points, int *p_indices, const Vector3 &p_center, const Vector3 &p_half_extents, int p_indices_count) const; @@ -76,7 +76,7 @@ class BSP_Tree { bool is_empty() const { return nodes.size() == 0; } Vector get_nodes() const; Vector get_planes() const; - Rect3 get_aabb() const; + AABB get_aabb() const; bool point_is_inside(const Vector3 &p_point) const; int get_points_inside(const Vector3 *p_points, int p_point_count) const; @@ -85,12 +85,12 @@ class BSP_Tree { operator Variant() const; - void from_aabb(const Rect3 &p_aabb); + void from_aabb(const AABB &p_aabb); BSP_Tree(); BSP_Tree(const Variant &p_variant); BSP_Tree(const PoolVector &p_faces, real_t p_error_radius = 0); - BSP_Tree(const Vector &p_nodes, const Vector &p_planes, const Rect3 &p_aabb, real_t p_error_radius = 0); + BSP_Tree(const Vector &p_nodes, const Vector &p_planes, const AABB &p_aabb, real_t p_error_radius = 0); ~BSP_Tree(); }; diff --git a/core/math/camera_matrix.cpp b/core/math/camera_matrix.cpp index 2c587762e83c..c5f1d5744181 100644 --- a/core/math/camera_matrix.cpp +++ b/core/math/camera_matrix.cpp @@ -596,7 +596,7 @@ void CameraMatrix::make_scale(const Vector3 &p_scale) { matrix[2][2] = p_scale.z; } -void CameraMatrix::scale_translate_to_fit(const Rect3 &p_aabb) { +void CameraMatrix::scale_translate_to_fit(const AABB &p_aabb) { Vector3 min = p_aabb.position; Vector3 max = p_aabb.position + p_aabb.size; diff --git a/core/math/camera_matrix.h b/core/math/camera_matrix.h index 3145d73356c0..15d6b8128e4d 100644 --- a/core/math/camera_matrix.h +++ b/core/math/camera_matrix.h @@ -86,7 +86,7 @@ struct CameraMatrix { operator String() const; - void scale_translate_to_fit(const Rect3 &p_aabb); + void scale_translate_to_fit(const AABB &p_aabb); void make_scale(const Vector3 &p_scale); int get_pixels_per_meter(int p_for_pixel_width) const; operator Transform() const; diff --git a/core/math/face3.cpp b/core/math/face3.cpp index e1b172e4919b..070ce77db494 100644 --- a/core/math/face3.cpp +++ b/core/math/face3.cpp @@ -189,13 +189,13 @@ ClockDirection Face3::get_clock_dir() const { return (normal.dot(vertex[0]) >= 0) ? CLOCKWISE : COUNTERCLOCKWISE; } -bool Face3::intersects_aabb(const Rect3 &p_aabb) const { +bool Face3::intersects_aabb(const AABB &p_aabb) const { /** TEST PLANE **/ if (!p_aabb.intersects_plane(get_plane())) return false; -/** TEST FACE AXIS */ + /** TEST FACE AXIS */ #define TEST_AXIS(m_ax) \ { \ diff --git a/core/math/face3.h b/core/math/face3.h index 8e4a25fb7abd..561fa31238d7 100644 --- a/core/math/face3.h +++ b/core/math/face3.h @@ -30,8 +30,8 @@ #ifndef FACE3_H #define FACE3_H +#include "aabb.h" #include "plane.h" -#include "rect3.h" #include "transform.h" #include "vector3.h" @@ -76,16 +76,16 @@ class Face3 { void get_support(const Vector3 &p_normal, const Transform &p_transform, Vector3 *p_vertices, int *p_count, int p_max) const; void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const; - Rect3 get_aabb() const { + AABB get_aabb() const { - Rect3 aabb(vertex[0], Vector3()); + AABB aabb(vertex[0], Vector3()); aabb.expand_to(vertex[1]); aabb.expand_to(vertex[2]); return aabb; } - bool intersects_aabb(const Rect3 &p_aabb) const; - _FORCE_INLINE_ bool intersects_aabb2(const Rect3 &p_aabb) const; + bool intersects_aabb(const AABB &p_aabb) const; + _FORCE_INLINE_ bool intersects_aabb2(const AABB &p_aabb) const; operator String() const; inline Face3() {} @@ -96,7 +96,7 @@ class Face3 { } }; -bool Face3::intersects_aabb2(const Rect3 &p_aabb) const { +bool Face3::intersects_aabb2(const AABB &p_aabb) const { Vector3 perp = (vertex[0] - vertex[2]).cross(vertex[0] - vertex[1]); @@ -256,6 +256,6 @@ bool Face3::intersects_aabb2(const Rect3 &p_aabb) const { return true; } -//this sucks... + //this sucks... #endif // FACE3_H diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp index 7c8fb6f17dc3..39bd34f03c45 100644 --- a/core/math/geometry.cpp +++ b/core/math/geometry.cpp @@ -300,7 +300,7 @@ enum _CellFlags { static inline void _plot_face(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, const Vector3 &voxelsize, const Face3 &p_face) { - Rect3 aabb(Vector3(x, y, z), Vector3(len_x, len_y, len_z)); + AABB aabb(Vector3(x, y, z), Vector3(len_x, len_y, len_z)); aabb.position = aabb.position * voxelsize; aabb.size = aabb.size * voxelsize; @@ -575,7 +575,7 @@ PoolVector Geometry::wrap_geometry(PoolVector p_array, real_t *p_e PoolVector::Read facesr = p_array.read(); const Face3 *faces = facesr.ptr(); - Rect3 global_aabb; + AABB global_aabb; for (int i = 0; i < face_count; i++) { diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index d5135fe41478..bc0b3717ed22 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -39,6 +39,7 @@ #include #define Math_PI 3.14159265358979323846 +#define Math_TAU 6.28318530717958647692 #define Math_SQRT12 0.7071067811865475244008443621048490 #define Math_LN2 0.693147180559945309417 #define Math_INF INFINITY diff --git a/core/math/octree.h b/core/math/octree.h index 95a67943fd65..6acd4c5f75b1 100644 --- a/core/math/octree.h +++ b/core/math/octree.h @@ -30,10 +30,10 @@ #ifndef OCTREE_H #define OCTREE_H +#include "aabb.h" #include "list.h" #include "map.h" #include "print_string.h" -#include "rect3.h" #include "variant.h" #include "vector3.h" @@ -106,7 +106,7 @@ class Octree { struct Octant { // cached for FAST plane check - Rect3 aabb; + AABB aabb; uint64_t last_pass; Octant *parent; @@ -152,8 +152,8 @@ class Octree { OctreeElementID _id; Octant *common_parent; - Rect3 aabb; - Rect3 container_aabb; + AABB aabb; + AABB container_aabb; List pair_list; @@ -334,7 +334,7 @@ class Octree { } void _insert_element(Element *p_element, Octant *p_octant); - void _ensure_valid_root(const Rect3 &p_aabb); + void _ensure_valid_root(const AABB &p_aabb); bool _remove_element_from_octant(Element *p_element, Octant *p_octant, Octant *p_limit = NULL); void _remove_element(Element *p_element); void _pair_element(Element *p_element, Octant *p_octant); @@ -351,7 +351,7 @@ class Octree { }; void _cull_convex(Octant *p_octant, _CullConvexData *p_cull); - void _cull_aabb(Octant *p_octant, const Rect3 &p_aabb, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask); + void _cull_aabb(Octant *p_octant, const AABB &p_aabb, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask); void _cull_segment(Octant *p_octant, const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask); void _cull_point(Octant *p_octant, const Vector3 &p_point, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask); @@ -370,8 +370,8 @@ class Octree { } public: - OctreeElementID create(T *p_userdata, const Rect3 &p_aabb = Rect3(), int p_subindex = 0, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1); - void move(OctreeElementID p_id, const Rect3 &p_aabb); + OctreeElementID create(T *p_userdata, const AABB &p_aabb = AABB(), int p_subindex = 0, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1); + void move(OctreeElementID p_id, const AABB &p_aabb); void set_pairable(OctreeElementID p_id, bool p_pairable = false, uint32_t p_pairable_type = 0, uint32_t pairable_mask = 1); void erase(OctreeElementID p_id); @@ -380,7 +380,7 @@ class Octree { int get_subindex(OctreeElementID p_id) const; int cull_convex(const Vector &p_convex, T **p_result_array, int p_result_max, uint32_t p_mask = 0xFFFFFFFF); - int cull_aabb(const Rect3 &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array = NULL, uint32_t p_mask = 0xFFFFFFFF); + int cull_aabb(const AABB &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array = NULL, uint32_t p_mask = 0xFFFFFFFF); int cull_segment(const Vector3 &p_from, const Vector3 &p_to, T **p_result_array, int p_result_max, int *p_subindex_array = NULL, uint32_t p_mask = 0xFFFFFFFF); int cull_point(const Vector3 &p_point, T **p_result_array, int p_result_max, int *p_subindex_array = NULL, uint32_t p_mask = 0xFFFFFFFF); @@ -479,7 +479,7 @@ void Octree::_insert_element(Element *p_element, Octant *p_oct } else { /* check againt AABB where child should be */ - Rect3 aabb = p_octant->aabb; + AABB aabb = p_octant->aabb; aabb.size *= 0.5; if (i & 1) @@ -535,12 +535,12 @@ void Octree::_insert_element(Element *p_element, Octant *p_oct } template -void Octree::_ensure_valid_root(const Rect3 &p_aabb) { +void Octree::_ensure_valid_root(const AABB &p_aabb) { if (!root) { // octre is empty - Rect3 base(Vector3(), Vector3(1.0, 1.0, 1.0) * unit_size); + AABB base(Vector3(), Vector3(1.0, 1.0, 1.0) * unit_size); while (!base.encloses(p_aabb)) { @@ -563,7 +563,7 @@ void Octree::_ensure_valid_root(const Rect3 &p_aabb) { } else { - Rect3 base = root->aabb; + AABB base = root->aabb; while (!base.encloses(p_aabb)) { @@ -793,7 +793,7 @@ void Octree::_remove_element(Element *p_element) { } template -OctreeElementID Octree::create(T *p_userdata, const Rect3 &p_aabb, int p_subindex, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) { +OctreeElementID Octree::create(T *p_userdata, const AABB &p_aabb, int p_subindex, bool p_pairable, uint32_t p_pairable_type, uint32_t p_pairable_mask) { // check for AABB validity #ifdef DEBUG_ENABLED @@ -833,7 +833,7 @@ OctreeElementID Octree::create(T *p_userdata, const Rect3 &p_a } template -void Octree::move(OctreeElementID p_id, const Rect3 &p_aabb) { +void Octree::move(OctreeElementID p_id, const AABB &p_aabb) { #ifdef DEBUG_ENABLED // check for AABB validity @@ -859,7 +859,7 @@ void Octree::move(OctreeElementID p_id, const Rect3 &p_aabb) { if (old_has_surf) { _remove_element(&e); // removing e.common_parent = NULL; - e.aabb = Rect3(); + e.aabb = AABB(); _optimize(); } else { _ensure_valid_root(p_aabb); // inserting @@ -886,7 +886,7 @@ void Octree::move(OctreeElementID p_id, const Rect3 &p_aabb) { return; } - Rect3 combined = e.aabb; + AABB combined = e.aabb; combined.merge_with(p_aabb); _ensure_valid_root(combined); @@ -1072,7 +1072,7 @@ void Octree::_cull_convex(Octant *p_octant, _CullConvexData *p } template -void Octree::_cull_aabb(Octant *p_octant, const Rect3 &p_aabb, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) { +void Octree::_cull_aabb(Octant *p_octant, const AABB &p_aabb, T **p_result_array, int *p_result_idx, int p_result_max, int *p_subindex_array, uint32_t p_mask) { if (*p_result_idx == p_result_max) return; //pointless @@ -1313,7 +1313,7 @@ int Octree::cull_convex(const Vector &p_convex, T **p_r } template -int Octree::cull_aabb(const Rect3 &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) { +int Octree::cull_aabb(const AABB &p_aabb, T **p_result_array, int p_result_max, int *p_subindex_array, uint32_t p_mask) { if (!root) return 0; diff --git a/core/math/quick_hull.cpp b/core/math/quick_hull.cpp index e0137b692106..946d9f6b79ef 100644 --- a/core/math/quick_hull.cpp +++ b/core/math/quick_hull.cpp @@ -38,7 +38,7 @@ Error QuickHull::build(const Vector &p_points, Geometry::MeshData &r_me /* CREATE AABB VOLUME */ - Rect3 aabb; + AABB aabb; for (int i = 0; i < p_points.size(); i++) { if (i == 0) { diff --git a/core/math/quick_hull.h b/core/math/quick_hull.h index 47ed22615b26..f014d0decc0b 100644 --- a/core/math/quick_hull.h +++ b/core/math/quick_hull.h @@ -30,9 +30,9 @@ #ifndef QUICK_HULL_H #define QUICK_HULL_H +#include "aabb.h" #include "geometry.h" #include "list.h" -#include "rect3.h" #include "set.h" class QuickHull { diff --git a/core/math/transform.h b/core/math/transform.h index 566bf482a920..4d91869121f8 100644 --- a/core/math/transform.h +++ b/core/math/transform.h @@ -30,9 +30,9 @@ #ifndef TRANSFORM_H #define TRANSFORM_H +#include "aabb.h" #include "matrix3.h" #include "plane.h" -#include "rect3.h" /** @author Juan Linietsky */ @@ -80,8 +80,8 @@ class Transform { _FORCE_INLINE_ Plane xform(const Plane &p_plane) const; _FORCE_INLINE_ Plane xform_inv(const Plane &p_plane) const; - _FORCE_INLINE_ Rect3 xform(const Rect3 &p_aabb) const; - _FORCE_INLINE_ Rect3 xform_inv(const Rect3 &p_aabb) const; + _FORCE_INLINE_ AABB xform(const AABB &p_aabb) const; + _FORCE_INLINE_ AABB xform_inv(const AABB &p_aabb) const; void operator*=(const Transform &p_transform); Transform operator*(const Transform &p_transform) const; @@ -153,14 +153,14 @@ _FORCE_INLINE_ Plane Transform::xform_inv(const Plane &p_plane) const { return Plane(normal, d); } -_FORCE_INLINE_ Rect3 Transform::xform(const Rect3 &p_aabb) const { +_FORCE_INLINE_ AABB Transform::xform(const AABB &p_aabb) const { /* define vertices */ Vector3 x = basis.get_axis(0) * p_aabb.size.x; Vector3 y = basis.get_axis(1) * p_aabb.size.y; Vector3 z = basis.get_axis(2) * p_aabb.size.z; Vector3 pos = xform(p_aabb.position); //could be even further optimized - Rect3 new_aabb; + AABB new_aabb; new_aabb.position = pos; new_aabb.expand_to(pos + x); new_aabb.expand_to(pos + y); @@ -172,7 +172,7 @@ _FORCE_INLINE_ Rect3 Transform::xform(const Rect3 &p_aabb) const { return new_aabb; } -_FORCE_INLINE_ Rect3 Transform::xform_inv(const Rect3 &p_aabb) const { +_FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const { /* define vertices */ Vector3 vertices[8] = { @@ -186,7 +186,7 @@ _FORCE_INLINE_ Rect3 Transform::xform_inv(const Rect3 &p_aabb) const { Vector3(p_aabb.position.x, p_aabb.position.y, p_aabb.position.z) }; - Rect3 ret; + AABB ret; ret.position = xform_inv(vertices[0]); diff --git a/core/math/triangle_mesh.cpp b/core/math/triangle_mesh.cpp index 3b246cb18344..5f57c7c26a56 100644 --- a/core/math/triangle_mesh.cpp +++ b/core/math/triangle_mesh.cpp @@ -44,7 +44,7 @@ int TriangleMesh::_create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, in return -1; } - Rect3 aabb; + AABB aabb; aabb = p_bb[p_from]->aabb; for (int i = 1; i < p_size; i++) { @@ -166,7 +166,7 @@ void TriangleMesh::create(const PoolVector &p_faces) { valid = true; } -Vector3 TriangleMesh::get_area_normal(const Rect3 &p_aabb) const { +Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const { uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth); diff --git a/core/math/triangle_mesh.h b/core/math/triangle_mesh.h index 2bf67fffcb77..4bd9fecf3745 100644 --- a/core/math/triangle_mesh.h +++ b/core/math/triangle_mesh.h @@ -47,7 +47,7 @@ class TriangleMesh : public Reference { struct BVH { - Rect3 aabb; + AABB aabb; Vector3 center; //used for sorting int left; int right; @@ -88,7 +88,7 @@ class TriangleMesh : public Reference { bool is_valid() const; bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const; bool intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal) const; - Vector3 get_area_normal(const Rect3 &p_aabb) const; + Vector3 get_area_normal(const AABB &p_aabb) const; PoolVector get_faces() const; void create(const PoolVector &p_faces); diff --git a/core/method_ptrcall.h b/core/method_ptrcall.h index 2875eb912f50..d819dfbc2759 100644 --- a/core/method_ptrcall.h +++ b/core/method_ptrcall.h @@ -119,7 +119,7 @@ MAKE_PTRARG_BY_REFERENCE(Vector3); MAKE_PTRARG(Transform2D); MAKE_PTRARG_BY_REFERENCE(Plane); MAKE_PTRARG(Quat); -MAKE_PTRARG_BY_REFERENCE(Rect3); +MAKE_PTRARG_BY_REFERENCE(AABB); MAKE_PTRARG_BY_REFERENCE(Basis); MAKE_PTRARG_BY_REFERENCE(Transform); MAKE_PTRARG_BY_REFERENCE(Color); diff --git a/core/node_path.cpp b/core/node_path.cpp index 15f950f605db..a12152aca658 100644 --- a/core/node_path.cpp +++ b/core/node_path.cpp @@ -48,8 +48,6 @@ uint32_t NodePath::hash() const { h = h ^ ssn[i].hash(); } - h = h ^ data->property.hash(); - return h; } @@ -81,13 +79,6 @@ StringName NodePath::get_name(int p_idx) const { return data->path[p_idx]; } -StringName NodePath::get_property() const { - - if (!data) - return StringName(); - return data->property; -} - int NodePath::get_subname_count() const { if (!data) @@ -128,9 +119,6 @@ bool NodePath::operator==(const NodePath &p_path) const { if (data->subpath.size() != p_path.data->subpath.size()) return false; - if (data->property != p_path.data->property) - return false; - for (int i = 0; i < data->path.size(); i++) { if (data->path[i] != p_path.data->path[i]) @@ -184,8 +172,6 @@ NodePath::operator String() const { ret += ":" + data->subpath[i].operator String(); } - if (data->property.operator String() != "") - ret += ":" + String(data->property); return ret; } @@ -205,6 +191,7 @@ Vector NodePath::get_names() const { return data->path; return Vector(); } + Vector NodePath::get_subnames() const { if (data) @@ -212,6 +199,21 @@ Vector NodePath::get_subnames() const { return Vector(); } +StringName NodePath::get_concatenated_subnames() const { + ERR_FAIL_COND_V(!data, StringName()); + + if (!data->concatenated_subpath) { + int spc = data->subpath.size(); + String concatenated; + const StringName *ssn = data->subpath.ptr(); + for (int i = 0; i < spc; i++) { + concatenated += i == 0 ? ssn[i].operator String() : ":" + ssn[i]; + } + data->concatenated_subpath = concatenated; + } + return data->concatenated_subpath; +} + NodePath NodePath::rel_path_to(const NodePath &p_np) const { ERR_FAIL_COND_V(!is_absolute(), NodePath()); @@ -250,10 +252,27 @@ NodePath NodePath::rel_path_to(const NodePath &p_np) const { if (relpath.size() == 0) relpath.push_back("."); - return NodePath(relpath, p_np.get_subnames(), false, p_np.get_property()); + return NodePath(relpath, p_np.get_subnames(), false); } -NodePath::NodePath(const Vector &p_path, bool p_absolute, const String &p_property) { +NodePath NodePath::get_as_property_path() const { + + if (!data->path.size()) { + return *this; + } else { + Vector new_path = data->subpath; + + String initial_subname = data->path[0]; + for (size_t i = 1; i < data->path.size(); i++) { + initial_subname += i == 0 ? data->path[i].operator String() : "/" + data->path[i]; + } + new_path.insert(0, initial_subname); + + return NodePath(Vector(), new_path, false); + } +} + +NodePath::NodePath(const Vector &p_path, bool p_absolute) { data = NULL; @@ -264,14 +283,14 @@ NodePath::NodePath(const Vector &p_path, bool p_absolute, const Stri data->refcount.init(); data->absolute = p_absolute; data->path = p_path; - data->property = p_property; + data->has_slashes = true; } -NodePath::NodePath(const Vector &p_path, const Vector &p_subpath, bool p_absolute, const String &p_property) { +NodePath::NodePath(const Vector &p_path, const Vector &p_subpath, bool p_absolute) { data = NULL; - if (p_path.size() == 0) + if (p_path.size() == 0 && p_subpath.size() == 0) return; data = memnew(Data); @@ -279,7 +298,7 @@ NodePath::NodePath(const Vector &p_path, const Vector &p data->absolute = p_absolute; data->path = p_path; data->subpath = p_subpath; - data->property = p_property; + data->has_slashes = true; } void NodePath::simplify() { @@ -320,11 +339,11 @@ NodePath::NodePath(const String &p_path) { return; String path = p_path; - StringName property; Vector subpath; int absolute = (path[0] == '/') ? 1 : 0; bool last_is_slash = true; + bool has_slashes = false; int slices = 0; int subpath_pos = path.find(":"); @@ -337,16 +356,13 @@ NodePath::NodePath(const String &p_path) { if (path[i] == ':' || path[i] == 0) { String str = path.substr(from, i - from); - if (path[i] == ':') { - if (str == "") { - ERR_EXPLAIN("Invalid NodePath: " + p_path); - ERR_FAIL(); - } - subpath.push_back(str); - } else { - //property can be empty - property = str; + if (str == "") { + if (path[i] == 0) continue; // Allow end-of-path : + + ERR_EXPLAIN("Invalid NodePath: " + p_path); + ERR_FAIL(); } + subpath.push_back(str); from = i + 1; } @@ -360,6 +376,7 @@ NodePath::NodePath(const String &p_path) { if (path[i] == '/') { last_is_slash = true; + has_slashes = true; } else { if (last_is_slash) @@ -369,13 +386,13 @@ NodePath::NodePath(const String &p_path) { } } - if (slices == 0 && !absolute && !property) + if (slices == 0 && !absolute && !subpath.size()) return; data = memnew(Data); data->refcount.init(); data->absolute = absolute ? true : false; - data->property = property; + data->has_slashes = has_slashes; data->subpath = subpath; if (slices == 0) diff --git a/core/node_path.h b/core/node_path.h index eb5b9eb6cfc6..063c4f62db0b 100644 --- a/core/node_path.h +++ b/core/node_path.h @@ -41,10 +41,11 @@ class NodePath { struct Data { SafeRefCount refcount; - StringName property; Vector path; Vector subpath; + StringName concatenated_subpath; bool absolute; + bool has_slashes; }; Data *data; @@ -53,7 +54,7 @@ class NodePath { public: _FORCE_INLINE_ StringName get_sname() const { - if (data && data->path.size() == 1 && data->subpath.empty() && !data->property) { + if (data && data->path.size() == 1 && data->subpath.empty()) { return data->path[0]; } else { return operator String(); @@ -67,13 +68,13 @@ class NodePath { StringName get_subname(int p_idx) const; Vector get_names() const; Vector get_subnames() const; + StringName get_concatenated_subnames() const; NodePath rel_path_to(const NodePath &p_np) const; + NodePath get_as_property_path() const; void prepend_period(); - StringName get_property() const; - NodePath get_parent() const; uint32_t hash() const; @@ -88,8 +89,8 @@ class NodePath { void simplify(); NodePath simplified() const; - NodePath(const Vector &p_path, bool p_absolute, const String &p_property = ""); - NodePath(const Vector &p_path, const Vector &p_subpath, bool p_absolute, const String &p_property = ""); + NodePath(const Vector &p_path, bool p_absolute); + NodePath(const Vector &p_path, const Vector &p_subpath, bool p_absolute); NodePath(const NodePath &p_path); NodePath(const String &p_path); NodePath(); diff --git a/core/object.cpp b/core/object.cpp index 823cbe14d459..1be7337d96df 100644 --- a/core/object.cpp +++ b/core/object.cpp @@ -517,6 +517,80 @@ Variant Object::get(const StringName &p_name, bool *r_valid) const { } } +void Object::set_indexed(const Vector &p_names, const Variant &p_value, bool *r_valid) { + if (p_names.empty()) { + if (r_valid) + *r_valid = false; + return; + } + if (p_names.size() == 1) { + set(p_names[0], p_value, r_valid); + return; + } + + bool valid = false; + if (!r_valid) r_valid = &valid; + + List value_stack; + + value_stack.push_back(get(p_names[0], r_valid)); + + if (!*r_valid) { + value_stack.clear(); + return; + } + + for (int i = 1; i < p_names.size() - 1; i++) { + value_stack.push_back(value_stack.back()->get().get_named(p_names[i], r_valid)); + + if (!*r_valid) { + value_stack.clear(); + return; + } + } + + value_stack.push_back(p_value); // p_names[p_names.size() - 1] + + for (int i = p_names.size() - 1; i > 0; i--) { + + value_stack.back()->prev()->get().set_named(p_names[i], value_stack.back()->get(), r_valid); + value_stack.pop_back(); + + if (!*r_valid) { + value_stack.clear(); + return; + } + } + + set(p_names[0], value_stack.back()->get(), r_valid); + value_stack.pop_back(); + + ERR_FAIL_COND(!value_stack.empty()); +} + +Variant Object::get_indexed(const Vector &p_names, bool *r_valid) const { + if (p_names.empty()) { + if (r_valid) + *r_valid = false; + return Variant(); + } + bool valid = false; + + Variant current_value = get(p_names[0]); + for (int i = 1; i < p_names.size(); i++) { + current_value = current_value.get_named(p_names[i], &valid); + + if (!valid) { + if (r_valid) + *r_valid = false; + return Variant(); + } + } + if (r_valid) + *r_valid = true; + return current_value; +} + void Object::get_property_list(List *p_list, bool p_reversed) const { if (script_instance && p_reversed) { @@ -744,7 +818,7 @@ Variant Object::callv(const StringName &p_method, const Array &p_args) { } Variant::CallError ce; - return call(p_method, argptrs.ptr(), p_args.size(), ce); + return call(p_method, (const Variant **)argptrs.ptr(), p_args.size(), ce); } Variant Object::call(const StringName &p_name, VARIANT_ARG_DECLARE) { @@ -1109,7 +1183,7 @@ Error Object::emit_signal(const StringName &p_name, const Variant **p_args, int bind_mem[p_argcount + j] = &c.binds[j]; } - args = bind_mem.ptr(); + args = (const Variant **)bind_mem.ptr(); argc = bind_mem.size(); } @@ -1416,6 +1490,16 @@ Variant Object::_get_bind(const String &p_name) const { return get(p_name); } +void Object::_set_indexed_bind(const NodePath &p_name, const Variant &p_value) { + + set_indexed(p_name.get_as_property_path().get_subnames(), p_value); +} + +Variant Object::_get_indexed_bind(const NodePath &p_name) const { + + return get_indexed(p_name.get_as_property_path().get_subnames()); +} + void Object::initialize_class() { static bool initialized = false; @@ -1513,6 +1597,8 @@ void Object::_bind_methods() { ClassDB::bind_method(D_METHOD("is_class", "type"), &Object::is_class); ClassDB::bind_method(D_METHOD("set", "property", "value"), &Object::_set_bind); ClassDB::bind_method(D_METHOD("get", "property"), &Object::_get_bind); + ClassDB::bind_method(D_METHOD("set_indexed", "property", "value"), &Object::_set_indexed_bind); + ClassDB::bind_method(D_METHOD("get_indexed", "property"), &Object::_get_indexed_bind); ClassDB::bind_method(D_METHOD("get_property_list"), &Object::_get_property_list_bind); ClassDB::bind_method(D_METHOD("get_method_list"), &Object::_get_method_list_bind); ClassDB::bind_method(D_METHOD("notification", "what", "reversed"), &Object::notification, DEFVAL(false)); @@ -1661,6 +1747,50 @@ Variant::Type Object::get_static_property_type(const StringName &p_property, boo return Variant::NIL; } +Variant::Type Object::get_static_property_type_indexed(const Vector &p_path, bool *r_valid) const { + + if (p_path.size() == 0) { + if (r_valid) + *r_valid = false; + + return Variant::NIL; + } + + bool valid = false; + Variant::Type t = get_static_property_type(p_path[0], &valid); + if (!valid) { + if (r_valid) + *r_valid = false; + + return Variant::NIL; + } + + Variant::CallError ce; + Variant check = Variant::construct(t, NULL, 0, ce); + + for (int i = 1; i < p_path.size(); i++) { + if (check.get_type() == Variant::OBJECT || check.get_type() == Variant::DICTIONARY || check.get_type() == Variant::ARRAY) { + // We cannot be sure about the type of properties this types can have + if (r_valid) + *r_valid = false; + return Variant::NIL; + } + + check = check.get_named(p_path[i], &valid); + + if (!valid) { + if (r_valid) + *r_valid = false; + return Variant::NIL; + } + } + + if (r_valid) + *r_valid = true; + + return check.get_type(); +} + bool Object::is_queued_for_deletion() const { return _is_queued_for_deletion; } diff --git a/core/object.h b/core/object.h index 7af2c78fc3be..3ac699f978f9 100644 --- a/core/object.h +++ b/core/object.h @@ -477,6 +477,8 @@ class Object { Array _get_incoming_connections() const; void _set_bind(const String &p_set, const Variant &p_value); Variant _get_bind(const String &p_name) const; + void _set_indexed_bind(const NodePath &p_name, const Variant &p_value); + Variant _get_indexed_bind(const NodePath &p_name) const; void *_script_instance_bindings[MAX_SCRIPT_INSTANCE_BINDINGS]; @@ -627,6 +629,8 @@ class Object { void set(const StringName &p_name, const Variant &p_value, bool *r_valid = NULL); Variant get(const StringName &p_name, bool *r_valid = NULL) const; + void set_indexed(const Vector &p_names, const Variant &p_value, bool *r_valid = NULL); + Variant get_indexed(const Vector &p_names, bool *r_valid = NULL) const; void get_property_list(List *p_list, bool p_reversed = false) const; @@ -687,6 +691,7 @@ class Object { bool is_blocking_signals() const; Variant::Type get_static_property_type(const StringName &p_property, bool *r_valid = NULL) const; + Variant::Type get_static_property_type_indexed(const Vector &p_path, bool *r_valid = NULL) const; virtual void get_translatable_strings(List *p_strings) const; diff --git a/core/os/dir_access.cpp b/core/os/dir_access.cpp index 0875f78478b6..6d4b46f4dad3 100644 --- a/core/os/dir_access.cpp +++ b/core/os/dir_access.cpp @@ -38,7 +38,7 @@ String DirAccess::_get_root_path() const { switch (_access_type) { case ACCESS_RESOURCES: return ProjectSettings::get_singleton()->get_resource_path(); - case ACCESS_USERDATA: return OS::get_singleton()->get_data_dir(); + case ACCESS_USERDATA: return OS::get_singleton()->get_user_data_dir(); default: return ""; } @@ -98,6 +98,7 @@ static Error _erase_recursive(DirAccess *da) { err = _erase_recursive(da); if (err) { print_line("err recurso " + E->get()); + da->change_dir(".."); return err; } err = da->change_dir(".."); @@ -217,7 +218,7 @@ String DirAccess::fix_path(String p_path) const { if (p_path.begins_with("user://")) { - String data_dir = OS::get_singleton()->get_data_dir(); + String data_dir = OS::get_singleton()->get_user_data_dir(); if (data_dir != "") { return p_path.replace_first("user:/", data_dir); @@ -340,6 +341,102 @@ Error DirAccess::copy(String p_from, String p_to, int chmod_flags) { return err; } +// Changes dir for the current scope, returning back to the original dir +// when scope exits +class DirChanger { + DirAccess *da; + String original_dir; + +public: + DirChanger(DirAccess *p_da, String p_dir) { + da = p_da; + original_dir = p_da->get_current_dir(); + p_da->change_dir(p_dir); + } + + ~DirChanger() { + da->change_dir(original_dir); + } +}; + +Error DirAccess::_copy_dir(DirAccess *p_target_da, String p_to, int p_chmod_flags) { + List dirs; + + String curdir = get_current_dir(); + list_dir_begin(); + String n = get_next(); + while (n != String()) { + + if (n != "." && n != "..") { + + if (current_is_dir()) + dirs.push_back(n); + else { + String rel_path = n; + if (!n.is_rel_path()) { + list_dir_end(); + return ERR_BUG; + } + Error err = copy(get_current_dir() + "/" + n, p_to + rel_path, p_chmod_flags); + if (err) { + list_dir_end(); + return err; + } + } + } + + n = get_next(); + } + + list_dir_end(); + + for (List::Element *E = dirs.front(); E; E = E->next()) { + String rel_path = E->get(); + String target_dir = p_to + rel_path; + if (!p_target_da->dir_exists(target_dir)) { + Error err = p_target_da->make_dir(target_dir); + ERR_FAIL_COND_V(err, err); + } + + Error err = change_dir(E->get()); + ERR_FAIL_COND_V(err, err); + err = _copy_dir(p_target_da, p_to + rel_path + "/", p_chmod_flags); + if (err) { + change_dir(".."); + ERR_PRINT("Failed to copy recursively"); + return err; + } + err = change_dir(".."); + if (err) { + ERR_PRINT("Failed to go back"); + return err; + } + } + + return OK; +} + +Error DirAccess::copy_dir(String p_from, String p_to, int p_chmod_flags) { + ERR_FAIL_COND_V(!dir_exists(p_from), ERR_FILE_NOT_FOUND); + + DirAccess *target_da = DirAccess::create_for_path(p_to); + ERR_FAIL_COND_V(!target_da, ERR_CANT_CREATE); + + if (!target_da->dir_exists(p_to)) { + Error err = target_da->make_dir_recursive(p_to); + if (err) { + memdelete(target_da); + } + ERR_FAIL_COND_V(err, err); + } + + DirChanger dir_changer(this, p_from); + Error err = _copy_dir(target_da, p_to + "/", p_chmod_flags); + memdelete(target_da); + + return err; +} + bool DirAccess::exists(String p_dir) { DirAccess *da = DirAccess::create_for_path(p_dir); diff --git a/core/os/dir_access.h b/core/os/dir_access.h index 7fa3ce5cf1b7..f3d132004107 100644 --- a/core/os/dir_access.h +++ b/core/os/dir_access.h @@ -52,6 +52,9 @@ class DirAccess { private: AccessType _access_type; static CreateFunc create_func[ACCESS_MAX]; ///< set this to instance a filesystem object + + Error _copy_dir(DirAccess *p_target_da, String p_to, int p_chmod_flags); + protected: String _get_root_path() const; String _get_root_string() const; @@ -89,6 +92,7 @@ class DirAccess { static bool exists(String p_dir); virtual size_t get_space_left() = 0; + Error copy_dir(String p_from, String p_to, int chmod_flags = -1); virtual Error copy(String p_from, String p_to, int chmod_flags = -1); virtual Error rename(String p_from, String p_to) = 0; virtual Error remove(String p_name) = 0; diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp index fcb3b58fedae..7b2062936b4c 100644 --- a/core/os/file_access.cpp +++ b/core/os/file_access.cpp @@ -152,7 +152,7 @@ String FileAccess::fix_path(const String &p_path) const { if (r_path.begins_with("user://")) { - String data_dir = OS::get_singleton()->get_data_dir(); + String data_dir = OS::get_singleton()->get_user_data_dir(); if (data_dir != "") { return r_path.replace("user:/", data_dir); @@ -481,7 +481,7 @@ Vector FileAccess::get_file_as_array(const String &p_path) { ERR_FAIL_COND_V(!f, Vector()); Vector data; data.resize(f->get_len()); - f->get_buffer(data.ptr(), data.size()); + f->get_buffer(data.ptrw(), data.size()); memdelete(f); return data; } diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp index 6b43f2c63b27..9b2bd3086897 100644 --- a/core/os/input_event.cpp +++ b/core/os/input_event.cpp @@ -177,6 +177,14 @@ bool InputEventWithModifiers::get_command() const { return command; } +void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModifiers *event) { + + set_alt(event->get_alt()); + set_shift(event->get_shift()); + set_control(event->get_control()); + set_metakey(event->get_metakey()); +} + void InputEventWithModifiers::_bind_methods() { ClassDB::bind_method(D_METHOD("set_alt", "enable"), &InputEventWithModifiers::set_alt); @@ -270,16 +278,16 @@ String InputEventKey::as_text() const { return kc; if (get_metakey()) { - kc = "Meta+" + kc; + kc = find_keycode_name(KEY_META) + ("+" + kc); } if (get_alt()) { - kc = "Alt+" + kc; + kc = find_keycode_name(KEY_ALT) + ("+" + kc); } if (get_shift()) { - kc = "Shift+" + kc; + kc = find_keycode_name(KEY_SHIFT) + ("+" + kc); } if (get_control()) { - kc = "Ctrl+" + kc; + kc = find_keycode_name(KEY_CONTROL) + ("+" + kc); } return kc; } @@ -436,10 +444,7 @@ Ref InputEventMouseButton::xformed_by(const Transform2D &p_xform, co mb->set_id(get_id()); mb->set_device(get_device()); - mb->set_alt(get_alt()); - mb->set_shift(get_shift()); - mb->set_control(get_control()); - mb->set_metakey(get_metakey()); + mb->set_modifiers_from_event(this); mb->set_position(l); mb->set_global_position(g); @@ -555,10 +560,7 @@ Ref InputEventMouseMotion::xformed_by(const Transform2D &p_xform, co mm->set_id(get_id()); mm->set_device(get_device()); - mm->set_alt(get_alt()); - mm->set_shift(get_shift()); - mm->set_control(get_control()); - mm->set_metakey(get_metakey()); + mm->set_modifiers_from_event(this); mm->set_position(l); mm->set_global_position(g); @@ -930,3 +932,99 @@ void InputEventAction::_bind_methods() { InputEventAction::InputEventAction() { pressed = false; } +///////////////////////////// + +void InputEventGesture::set_position(const Vector2 &p_pos) { + + pos = p_pos; +} + +void InputEventGesture::_bind_methods() { + + ClassDB::bind_method(D_METHOD("set_position", "position"), &InputEventGesture::set_position); + ClassDB::bind_method(D_METHOD("get_position"), &InputEventGesture::get_position); + + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position"); +} + +Vector2 InputEventGesture::get_position() const { + + return pos; +} +///////////////////////////// + +void InputEventMagnifyGesture::set_factor(real_t p_factor) { + + factor = p_factor; +} + +real_t InputEventMagnifyGesture::get_factor() const { + + return factor; +} + +Ref InputEventMagnifyGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const { + + Ref ev; + ev.instance(); + + ev->set_id(get_id()); + ev->set_device(get_device()); + ev->set_modifiers_from_event(this); + + ev->set_position(p_xform.xform(get_position() + p_local_ofs)); + ev->set_factor(get_factor()); + + return ev; +} + +void InputEventMagnifyGesture::_bind_methods() { + + ClassDB::bind_method(D_METHOD("set_factor", "factor"), &InputEventMagnifyGesture::set_factor); + ClassDB::bind_method(D_METHOD("get_factor"), &InputEventMagnifyGesture::get_factor); + + ADD_PROPERTY(PropertyInfo(Variant::REAL, "factor"), "set_factor", "get_factor"); +} + +InputEventMagnifyGesture::InputEventMagnifyGesture() { + + factor = 1.0; +} +///////////////////////////// + +void InputEventPanGesture::set_delta(const Vector2 &p_delta) { + + delta = p_delta; +} + +Vector2 InputEventPanGesture::get_delta() const { + return delta; +} + +Ref InputEventPanGesture::xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs) const { + + Ref ev; + ev.instance(); + + ev->set_id(get_id()); + ev->set_device(get_device()); + ev->set_modifiers_from_event(this); + + ev->set_position(p_xform.xform(get_position() + p_local_ofs)); + ev->set_delta(get_delta()); + + return ev; +} + +void InputEventPanGesture::_bind_methods() { + + ClassDB::bind_method(D_METHOD("set_delta", "delta"), &InputEventPanGesture::set_delta); + ClassDB::bind_method(D_METHOD("get_delta"), &InputEventPanGesture::get_delta); + + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "delta"), "set_delta", "get_delta"); +} + +InputEventPanGesture::InputEventPanGesture() { + + delta = Vector2(0, 0); +} diff --git a/core/os/input_event.h b/core/os/input_event.h index de3c0232ff67..614a3289ba4f 100644 --- a/core/os/input_event.h +++ b/core/os/input_event.h @@ -213,6 +213,8 @@ class InputEventWithModifiers : public InputEvent { void set_command(bool p_enabled); bool get_command() const; + void set_modifiers_from_event(const InputEventWithModifiers *event); + InputEventWithModifiers(); }; @@ -468,4 +470,51 @@ class InputEventAction : public InputEvent { InputEventAction(); }; +class InputEventGesture : public InputEventWithModifiers { + + GDCLASS(InputEventGesture, InputEventWithModifiers) + + Vector2 pos; + +protected: + static void _bind_methods(); + +public: + void set_position(const Vector2 &p_pos); + Vector2 get_position() const; +}; + +class InputEventMagnifyGesture : public InputEventGesture { + + GDCLASS(InputEventMagnifyGesture, InputEventGesture) + real_t factor; + +protected: + static void _bind_methods(); + +public: + void set_factor(real_t p_factor); + real_t get_factor() const; + + virtual Ref xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const; + + InputEventMagnifyGesture(); +}; + +class InputEventPanGesture : public InputEventGesture { + + GDCLASS(InputEventPanGesture, InputEventGesture) + Vector2 delta; + +protected: + static void _bind_methods(); + +public: + void set_delta(const Vector2 &p_delta); + Vector2 get_delta() const; + + virtual Ref xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const; + + InputEventPanGesture(); +}; #endif diff --git a/core/os/keyboard.cpp b/core/os/keyboard.cpp index edf4f3e2f9f5..dead3b6ac085 100644 --- a/core/os/keyboard.cpp +++ b/core/os/keyboard.cpp @@ -60,7 +60,11 @@ static const _KeyCodeText _keycodes[] = { {KEY_PAGEDOWN ,"PageDown"}, {KEY_SHIFT ,"Shift"}, {KEY_CONTROL ,"Control"}, +#ifdef OSX_ENABLED + {KEY_META ,"Command"}, +#else {KEY_META ,"Meta"}, +#endif {KEY_ALT ,"Alt"}, {KEY_CAPSLOCK ,"CapsLock"}, {KEY_NUMLOCK ,"NumLock"}, @@ -390,14 +394,22 @@ bool keycode_has_unicode(uint32_t p_keycode) { String keycode_get_string(uint32_t p_code) { String codestr; - if (p_code & KEY_MASK_SHIFT) - codestr += "Shift+"; - if (p_code & KEY_MASK_ALT) - codestr += "Alt+"; - if (p_code & KEY_MASK_CTRL) - codestr += "Ctrl+"; - if (p_code & KEY_MASK_META) - codestr += "Meta+"; + if (p_code & KEY_MASK_SHIFT) { + codestr += find_keycode_name(KEY_SHIFT); + codestr += "+"; + } + if (p_code & KEY_MASK_ALT) { + codestr += find_keycode_name(KEY_ALT); + codestr += "+"; + } + if (p_code & KEY_MASK_CTRL) { + codestr += find_keycode_name(KEY_CONTROL); + codestr += "+"; + } + if (p_code & KEY_MASK_META) { + codestr += find_keycode_name(KEY_META); + codestr += "+"; + } p_code &= KEY_CODE_MASK; @@ -433,6 +445,21 @@ int find_keycode(const String &p_code) { return 0; } +const char *find_keycode_name(int p_keycode) { + + const _KeyCodeText *kct = &_keycodes[0]; + + while (kct->text) { + + if (kct->code == p_keycode) { + return kct->text; + } + kct++; + } + + return ""; +} + struct _KeyCodeReplace { int from; int to; diff --git a/core/os/keyboard.h b/core/os/keyboard.h index 509ff23a935b..f49cbc6b189a 100644 --- a/core/os/keyboard.h +++ b/core/os/keyboard.h @@ -326,6 +326,7 @@ enum KeyModifierMask { String keycode_get_string(uint32_t p_code); bool keycode_has_unicode(uint32_t p_keycode); int find_keycode(const String &p_code); +const char *find_keycode_name(int p_keycode); int keycode_get_count(); int keycode_get_value_by_index(int p_index); const char *keycode_get_name_by_index(int p_index); diff --git a/core/os/memory.cpp b/core/os/memory.cpp index 74d5cbbea1b5..439951f71136 100644 --- a/core/os/memory.cpp +++ b/core/os/memory.cpp @@ -44,6 +44,26 @@ void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)) { return p_allocfunc(p_size); } +#ifdef _MSC_VER +void operator delete(void *p_mem, const char *p_description) { + + ERR_EXPLAINC("Call to placement delete should not happen."); + CRASH_NOW(); +} + +void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size)) { + + ERR_EXPLAINC("Call to placement delete should not happen."); + CRASH_NOW(); +} + +void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description) { + + ERR_EXPLAINC("Call to placement delete should not happen."); + CRASH_NOW(); +} +#endif + #ifdef DEBUG_ENABLED uint64_t Memory::mem_usage = 0; uint64_t Memory::max_usage = 0; diff --git a/core/os/memory.h b/core/os/memory.h index f8b3da579b0a..7801d56837c0 100644 --- a/core/os/memory.h +++ b/core/os/memory.h @@ -72,6 +72,14 @@ void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)); ///< ope void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory +#ifdef _MSC_VER +// When compiling with VC++ 2017, the above declarations of placement new generate many irrelevant warnings (C4291). +// The purpose of the following definitions is to muffle these warnings, not to provide a usable implementation of placement delete. +void operator delete(void *p_mem, const char *p_description); +void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size)); +void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description); +#endif + #define memalloc(m_size) Memory::alloc_static(m_size) #define memrealloc(m_mem, m_size) Memory::realloc_static(m_mem, m_size) #define memfree(m_size) Memory::free_static(m_size) diff --git a/core/os/os.cpp b/core/os/os.cpp index eb5d5be33df8..8088a6fa7464 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -33,6 +33,7 @@ #include "input.h" #include "os/file_access.h" #include "project_settings.h" +#include "version_generated.gen.h" #include @@ -62,15 +63,21 @@ void OS::debug_break(){ // something }; -void OS::_set_logger(Logger *p_logger) { +void OS::_set_logger(CompositeLogger *p_logger) { if (_logger) { memdelete(_logger); } _logger = p_logger; } -void OS::initialize_logger() { - _set_logger(memnew(StdLogger)); +void OS::add_logger(Logger *p_logger) { + if (!_logger) { + Vector loggers; + loggers.push_back(p_logger); + _logger = memnew(CompositeLogger(loggers)); + } else { + _logger->add_logger(p_logger); + } } void OS::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, Logger::ErrorType p_type) { @@ -115,6 +122,16 @@ bool OS::is_in_low_processor_usage_mode() const { return low_processor_usage_mode; } +void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) { + + low_processor_usage_mode_sleep_usec = p_usec; +} + +int OS::get_low_processor_usage_mode_sleep_usec() const { + + return low_processor_usage_mode_sleep_usec; +} + void OS::set_clipboard(const String &p_text) { _local_clipboard = p_text; @@ -262,30 +279,69 @@ String OS::get_locale() const { return "en"; } -String OS::get_resource_dir() const { +// Helper function to ensure that a dir name/path will be valid on the OS +String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator) const { - return ProjectSettings::get_singleton()->get_resource_path(); + Vector invalid_chars = String(": * ? \" < > |").split(" "); + if (p_allow_dir_separator) { + // Dir separators are allowed, but disallow ".." to avoid going up the filesystem + invalid_chars.push_back(".."); + } else { + invalid_chars.push_back("/"); + } + + String safe_dir_name = p_dir_name.replace("\\", "/").strip_edges(); + for (int i = 0; i < invalid_chars.size(); i++) { + safe_dir_name = safe_dir_name.replace(invalid_chars[i], "-"); + } + return safe_dir_name; } -String OS::get_system_dir(SystemDir p_dir) const { +// Path to data, config, cache, etc. OS-specific folders + +// Get properly capitalized engine name for system paths +String OS::get_godot_dir_name() const { + + // Default to lowercase, so only override when different case is needed + return String(VERSION_SHORT_NAME).to_lower(); +} + +// OS equivalent of XDG_DATA_HOME +String OS::get_data_path() const { return "."; } -String OS::get_safe_application_name() const { - String an = ProjectSettings::get_singleton()->get("application/config/name"); - Vector invalid_char = String("\\ / : * ? \" < > |").split(" "); - for (int i = 0; i < invalid_char.size(); i++) { - an = an.replace(invalid_char[i], "-"); - } - return an; +// OS equivalent of XDG_CONFIG_HOME +String OS::get_config_path() const { + + return "."; +} + +// OS equivalent of XDG_CACHE_HOME +String OS::get_cache_path() const { + + return "."; } -String OS::get_data_dir() const { +// OS specific path for user:// +String OS::get_user_data_dir() const { return "."; }; +// Absolute path to res:// +String OS::get_resource_dir() const { + + return ProjectSettings::get_singleton()->get_resource_path(); +} + +// Access system-specific dirs like Documents, Downloads, etc. +String OS::get_system_dir(SystemDir p_dir) const { + + return "."; +} + Error OS::shell_open(String p_uri) { return ERR_UNAVAILABLE; }; @@ -374,9 +430,9 @@ OS::ScreenOrientation OS::get_screen_orientation() const { return (OS::ScreenOrientation)_orientation; } -void OS::_ensure_data_dir() { +void OS::_ensure_user_data_dir() { - String dd = get_data_dir(); + String dd = get_user_data_dir(); DirAccess *da = DirAccess::open(dd); if (da) { memdelete(da); @@ -516,6 +572,33 @@ bool OS::has_feature(const String &p_feature) { if (sizeof(void *) == 4 && p_feature == "32") { return true; } +#if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) + if (p_feature == "x86_64") { + return true; + } +#elif (defined(__i386) || defined(__i386__)) + if (p_feature == "x86") { + return true; + } +#elif defined(__aarch64__) + if (p_feature == "arm64") { + return true; + } +#elif defined(__arm__) +#if defined(__ARM_ARCH_7A__) + if (p_feature == "armv7a" || p_feature == "armv7") { + return true; + } +#endif +#if defined(__ARM_ARCH_7S__) + if (p_feature == "armv7s" || p_feature == "armv7") { + return true; + } +#endif + if (p_feature == "arm") { + return true; + } +#endif if (_check_internal_feature_support(p_feature)) return true; @@ -534,6 +617,7 @@ OS::OS() { singleton = this; _keep_screen_on = true; // set default value to true, because this had been true before godot 2.0. low_processor_usage_mode = false; + low_processor_usage_mode_sleep_usec = 10000; _verbose_stdout = false; _no_window = false; _exit_code = 0; @@ -545,7 +629,10 @@ OS::OS() { _stack_bottom = (void *)(&stack_bottom); _logger = NULL; - _set_logger(memnew(StdLogger)); + + Vector loggers; + loggers.push_back(memnew(StdLogger)); + _set_logger(memnew(CompositeLogger(loggers))); } OS::~OS() { diff --git a/core/os/os.h b/core/os/os.h index faecdb0e076a..c72696fe373f 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -50,6 +50,7 @@ class OS { List _cmdline; bool _keep_screen_on; bool low_processor_usage_mode; + int low_processor_usage_mode_sleep_usec; bool _verbose_stdout; String _local_clipboard; uint64_t _msec_splash; @@ -62,10 +63,10 @@ class OS { void *_stack_bottom; - Logger *_logger; + CompositeLogger *_logger; protected: - void _set_logger(Logger *p_logger); + void _set_logger(CompositeLogger *p_logger); public: typedef void (*ImeCallback)(void *p_inp, String p_text, Point2 p_selection); @@ -90,13 +91,15 @@ class OS { bool fullscreen; bool resizable; bool borderless_window; + bool maximized; float get_aspect() const { return (float)width / (float)height; } - VideoMode(int p_width = 1024, int p_height = 600, bool p_fullscreen = false, bool p_resizable = true, bool p_borderless_window = false) { + VideoMode(int p_width = 1024, int p_height = 600, bool p_fullscreen = false, bool p_resizable = true, bool p_borderless_window = false, bool p_maximized = false) { width = p_width; height = p_height; fullscreen = p_fullscreen; resizable = p_resizable; borderless_window = p_borderless_window; + maximized = p_maximized; } }; @@ -112,7 +115,8 @@ class OS { virtual int get_audio_driver_count() const = 0; virtual const char *get_audio_driver_name(int p_driver) const = 0; - virtual void initialize_logger(); + void add_logger(Logger *p_logger); + virtual void initialize_core() = 0; virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) = 0; @@ -124,7 +128,7 @@ class OS { virtual void set_cmdline(const char *p_execpath, const List &p_args); - void _ensure_data_dir(); + void _ensure_user_data_dir(); virtual bool _check_internal_feature_support(const String &p_feature) = 0; public: @@ -199,8 +203,9 @@ class OS { virtual bool is_keep_screen_on() const; virtual void set_low_processor_usage_mode(bool p_enabled); virtual bool is_in_low_processor_usage_mode() const; + virtual void set_low_processor_usage_mode_sleep_usec(int p_usec); + virtual int get_low_processor_usage_mode_sleep_usec() const; - virtual String get_installed_templates_path() const { return ""; } virtual String get_executable_path() const; virtual Error execute(const String &p_path, const List &p_arguments, bool p_blocking, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false) = 0; virtual Error kill(const ProcessID &p_pid) = 0; @@ -333,11 +338,15 @@ class OS { virtual String get_locale() const; - String get_safe_application_name() const; - virtual String get_data_dir() const; - virtual String get_resource_dir() const; + String get_safe_dir_name(const String &p_dir_name, bool p_allow_dir_separator = false) const; + virtual String get_godot_dir_name() const; - virtual Error move_to_trash(const String &p_path) { return FAILED; } + virtual String get_data_path() const; + virtual String get_config_path() const; + virtual String get_cache_path() const; + + virtual String get_user_data_dir() const; + virtual String get_resource_dir() const; enum SystemDir { SYSTEM_DIR_DESKTOP, @@ -352,6 +361,8 @@ class OS { virtual String get_system_dir(SystemDir p_dir) const; + virtual Error move_to_trash(const String &p_path) { return FAILED; } + virtual void set_no_window_mode(bool p_enable); virtual bool is_no_window_mode_enabled() const; diff --git a/core/packed_data_container.cpp b/core/packed_data_container.cpp index ad8438e41623..3748df12f759 100644 --- a/core/packed_data_container.cpp +++ b/core/packed_data_container.cpp @@ -234,7 +234,7 @@ uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector &tmpd case Variant::TRANSFORM2D: case Variant::PLANE: case Variant::QUAT: - case Variant::RECT3: + case Variant::AABB: case Variant::BASIS: case Variant::TRANSFORM: case Variant::POOL_BYTE_ARRAY: diff --git a/core/print_string.cpp b/core/print_string.cpp index 92a04cbf0b12..520fb3daec4b 100644 --- a/core/print_string.cpp +++ b/core/print_string.cpp @@ -82,7 +82,25 @@ void print_line(String p_string) { PrintHandlerList *l = print_handler_list; while (l) { - l->printfunc(l->userdata, p_string); + l->printfunc(l->userdata, p_string, false); + l = l->next; + } + + _global_unlock(); +} + +void print_error(String p_string) { + + if (!_print_error_enabled) + return; + + OS::get_singleton()->printerr("%s\n", p_string.utf8().get_data()); + + _global_lock(); + PrintHandlerList *l = print_handler_list; + while (l) { + + l->printfunc(l->userdata, p_string, true); l = l->next; } diff --git a/core/print_string.h b/core/print_string.h index 9f8420c31a3a..6b68380b9dde 100644 --- a/core/print_string.h +++ b/core/print_string.h @@ -34,7 +34,7 @@ extern void (*_print_func)(String); -typedef void (*PrintHandlerFunc)(void *, const String &p_string); +typedef void (*PrintHandlerFunc)(void *, const String &p_string, bool p_error); struct PrintHandlerList { @@ -56,5 +56,6 @@ void remove_print_handler(PrintHandlerList *p_handler); extern bool _print_line_enabled; extern bool _print_error_enabled; extern void print_line(String p_string); +extern void print_error(String p_string); #endif diff --git a/core/project_settings.cpp b/core/project_settings.cpp index 2e4fc267845f..67b081de34ca 100644 --- a/core/project_settings.cpp +++ b/core/project_settings.cpp @@ -116,7 +116,7 @@ String ProjectSettings::globalize_path(const String &p_path) const { return p_path.replace("res://", ""); } else if (p_path.begins_with("user://")) { - String data_dir = OS::get_singleton()->get_data_dir(); + String data_dir = OS::get_singleton()->get_user_data_dir(); if (data_dir != "") { return p_path.replace("user:/", data_dir); @@ -429,7 +429,7 @@ Error ProjectSettings::_load_settings_binary(const String p_path) { uint32_t vlen = f->get_32(); Vector d; d.resize(vlen); - f->get_buffer(d.ptr(), vlen); + f->get_buffer(d.ptrw(), vlen); Variant value; Error err = decode_variant(value, d.ptr(), d.size()); ERR_EXPLAIN("Error decoding property: " + key); @@ -776,32 +776,6 @@ Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default) { return ret; } -void ProjectSettings::add_singleton(const Singleton &p_singleton) { - - singletons.push_back(p_singleton); - singleton_ptrs[p_singleton.name] = p_singleton.ptr; -} - -Object *ProjectSettings::get_singleton_object(const String &p_name) const { - - const Map::Element *E = singleton_ptrs.find(p_name); - if (!E) - return NULL; - else - return E->get(); -}; - -bool ProjectSettings::has_singleton(const String &p_name) const { - - return get_singleton_object(p_name) != NULL; -}; - -void ProjectSettings::get_singletons(List *p_singletons) { - - for (List::Element *E = singletons.front(); E; E = E->next()) - p_singletons->push_back(E->get()); -} - Vector ProjectSettings::get_optimizer_presets() const { List pi; @@ -893,8 +867,6 @@ void ProjectSettings::_bind_methods() { ClassDB::bind_method(D_METHOD("localize_path", "path"), &ProjectSettings::localize_path); ClassDB::bind_method(D_METHOD("globalize_path", "path"), &ProjectSettings::globalize_path); ClassDB::bind_method(D_METHOD("save"), &ProjectSettings::save); - ClassDB::bind_method(D_METHOD("has_singleton", "name"), &ProjectSettings::has_singleton); - ClassDB::bind_method(D_METHOD("get_singleton", "name"), &ProjectSettings::get_singleton_object); ClassDB::bind_method(D_METHOD("load_resource_pack", "pack"), &ProjectSettings::_load_resource_pack); ClassDB::bind_method(D_METHOD("property_can_revert", "name"), &ProjectSettings::property_can_revert); ClassDB::bind_method(D_METHOD("property_get_revert", "name"), &ProjectSettings::property_get_revert); @@ -919,7 +891,8 @@ ProjectSettings::ProjectSettings() { custom_prop_info["application/run/main_scene"] = PropertyInfo(Variant::STRING, "application/run/main_scene", PROPERTY_HINT_FILE, "tscn,scn,res"); GLOBAL_DEF("application/run/disable_stdout", false); GLOBAL_DEF("application/run/disable_stderr", false); - GLOBAL_DEF("application/config/use_shared_user_dir", true); + GLOBAL_DEF("application/config/use_custom_user_dir", false); + GLOBAL_DEF("application/config/custom_user_dir_name", ""); key.instance(); key->set_scancode(KEY_ENTER); diff --git a/core/project_settings.h b/core/project_settings.h index f75cad815f61..1c4078cebb4c 100644 --- a/core/project_settings.h +++ b/core/project_settings.h @@ -45,14 +45,6 @@ class ProjectSettings : public Object { public: typedef Map CustomMap; - struct Singleton { - StringName name; - Object *ptr; - Singleton(const StringName &p_name = StringName(), Object *p_ptr = NULL) - : name(p_name), - ptr(p_ptr) { - } - }; enum { //properties that are not for built in values begin from this value, so builtin ones are displayed first NO_BUILTIN_ORDER_BASE = 1 << 16 @@ -106,9 +98,6 @@ class ProjectSettings : public Object { Error _save_settings_text(const String &p_file, const Map > &props, const CustomMap &p_custom = CustomMap(), const String &p_custom_features = String()); Error _save_settings_binary(const String &p_file, const Map > &props, const CustomMap &p_custom = CustomMap(), const String &p_custom_features = String()); - List singletons; - Map singleton_ptrs; - Error _save_custom_bnd(const String &p_file); bool _load_resource_pack(const String &p_pack); @@ -145,17 +134,11 @@ class ProjectSettings : public Object { Error save(); void set_custom_property_info(const String &p_prop, const PropertyInfo &p_info); - void add_singleton(const Singleton &p_singleton); - void get_singletons(List *p_singletons); - - bool has_singleton(const String &p_name) const; - Vector get_optimizer_presets() const; List get_input_presets() const { return input_presets; } void set_disable_feature_overrides(bool p_disable); - Object *get_singleton_object(const String &p_name) const; void register_global_defaults(); diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index c6d7cd44e8d9..17f951e4f496 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -34,6 +34,7 @@ #include "compressed_translation.h" #include "core/io/xml_parser.h" #include "core_string_names.h" +#include "engine.h" #include "func_ref.h" #include "geometry.h" #include "input_map.h" @@ -128,6 +129,9 @@ void register_core_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_virtual_class(); + ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_virtual_class(); @@ -203,19 +207,19 @@ void register_core_singletons() { ClassDB::register_class(); ClassDB::register_class<_JSON>(); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("ProjectSettings", ProjectSettings::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("IP", IP::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("Geometry", _Geometry::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("ResourceLoader", _ResourceLoader::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("ResourceSaver", _ResourceSaver::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("OS", _OS::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("Engine", _Engine::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("ClassDB", _classdb)); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("Marshalls", _Marshalls::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("TranslationServer", TranslationServer::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("Input", Input::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("InputMap", InputMap::get_singleton())); - ProjectSettings::get_singleton()->add_singleton(ProjectSettings::Singleton("JSON", _JSON::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("ProjectSettings", ProjectSettings::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("IP", IP::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("Geometry", _Geometry::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("ResourceLoader", _ResourceLoader::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("ResourceSaver", _ResourceSaver::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("OS", _OS::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("Engine", _Engine::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("ClassDB", _classdb)); + Engine::get_singleton()->add_singleton(Engine::Singleton("Marshalls", _Marshalls::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("TranslationServer", TranslationServer::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("Input", Input::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("InputMap", InputMap::get_singleton())); + Engine::get_singleton()->add_singleton(Engine::Singleton("JSON", _JSON::get_singleton())); } void unregister_core_types() { diff --git a/core/script_debugger_local.cpp b/core/script_debugger_local.cpp index 8d2600e52dd9..94c48f1c8f0b 100644 --- a/core/script_debugger_local.cpp +++ b/core/script_debugger_local.cpp @@ -212,7 +212,7 @@ void ScriptDebuggerLocal::idle_poll() { } SortArray sort; - sort.sort(pinfo.ptr(), ofs); + sort.sort(pinfo.ptrw(), ofs); //falta el frame time @@ -264,7 +264,7 @@ void ScriptDebuggerLocal::profiling_end() { } SortArray sort; - sort.sort(pinfo.ptr(), ofs); + sort.sort(pinfo.ptrw(), ofs); uint64_t total_us = 0; for (int i = 0; i < ofs; i++) { diff --git a/core/script_debugger_remote.cpp b/core/script_debugger_remote.cpp index 2feb068ecb03..495c99c122d8 100644 --- a/core/script_debugger_remote.cpp +++ b/core/script_debugger_remote.cpp @@ -29,11 +29,14 @@ /*************************************************************************/ #include "script_debugger_remote.h" +#include "engine.h" #include "io/ip.h" #include "io/marshalls.h" #include "os/input.h" #include "os/os.h" #include "project_settings.h" +#include "scene/main/node.h" + void ScriptDebuggerRemote::_send_video_memory() { List usage; @@ -200,20 +203,39 @@ void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue) List members; List member_vals; - + if (ScriptInstance *inst = p_script->debug_get_stack_level_instance(lv)) { + members.push_back("self"); + member_vals.push_back(inst->get_owner()); + } p_script->debug_get_stack_level_members(lv, &members, &member_vals); - ERR_CONTINUE(members.size() != member_vals.size()); List locals; List local_vals; - p_script->debug_get_stack_level_locals(lv, &locals, &local_vals); - ERR_CONTINUE(locals.size() != local_vals.size()); + List globals; + List globals_vals; + p_script->debug_get_globals(&globals, &globals_vals); + ERR_CONTINUE(globals.size() != globals_vals.size()); + packet_peer_stream->put_var("stack_frame_vars"); - packet_peer_stream->put_var(2 + locals.size() * 2 + members.size() * 2); + packet_peer_stream->put_var(3 + (locals.size() + members.size() + globals.size()) * 2); + + { //locals + packet_peer_stream->put_var(locals.size()); + + List::Element *E = locals.front(); + List::Element *F = local_vals.front(); + + while (E) { + _put_variable(E->get(), F->get()); + + E = E->next(); + F = F->next(); + } + } { //members packet_peer_stream->put_var(members.size()); @@ -230,11 +252,11 @@ void ScriptDebuggerRemote::debug(ScriptLanguage *p_script, bool p_can_continue) } } - { //locals - packet_peer_stream->put_var(locals.size()); + { //globals + packet_peer_stream->put_var(globals.size()); - List::Element *E = locals.front(); - List::Element *F = local_vals.front(); + List::Element *E = globals.front(); + List::Element *F = globals_vals.front(); while (E) { _put_variable(E->get(), F->get()); @@ -531,56 +553,88 @@ void ScriptDebuggerRemote::_send_object_id(ObjectID p_id) { if (!obj) return; - List pinfo; - obj->get_property_list(&pinfo, true); + typedef Pair PropertyDesc; + List properties; - int props_to_send = 0; - for (List::Element *E = pinfo.front(); E; E = E->next()) { + if (ScriptInstance *si = obj->get_script_instance()) { + if (!si->get_script().is_null()) { - if (E->get().usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) { - props_to_send++; - } - } + Set members; + si->get_script()->get_members(&members); + for (Set::Element *E = members.front(); E; E = E->next()) { - packet_peer_stream->put_var("message:inspect_object"); - packet_peer_stream->put_var(props_to_send * 5 + 4); - packet_peer_stream->put_var(p_id); - packet_peer_stream->put_var(obj->get_class()); - if (obj->is_class("Resource") || obj->is_class("Node")) - packet_peer_stream->put_var(obj->call("get_path")); - else - packet_peer_stream->put_var(""); + Variant m; + if (si->get(E->get(), m)) { + PropertyInfo pi(m.get_type(), String("Members/") + E->get()); + properties.push_back(PropertyDesc(pi, m)); + } + } - packet_peer_stream->put_var(props_to_send); + Map constants; + si->get_script()->get_constants(&constants); + for (Map::Element *E = constants.front(); E; E = E->next()) { + PropertyInfo pi(E->value().get_type(), (String("Constants/") + E->key())); + properties.push_back(PropertyDesc(pi, E->value())); + } + } + } + if (Node *node = Object::cast_to(obj)) { + PropertyInfo pi(Variant::NODE_PATH, String("Node/path")); + properties.push_front(PropertyDesc(pi, node->get_path())); + } else if (Resource *res = Object::cast_to(obj)) { + if (Script *s = Object::cast_to