| 1 | /**************************************************************************/ |
| 2 | /* jolt_globals.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "jolt_globals.h" |
| 32 | |
| 33 | #include "objects/jolt_group_filter.h" |
| 34 | #include "shapes/jolt_custom_double_sided_shape.h" |
| 35 | #include "shapes/jolt_custom_ray_shape.h" |
| 36 | #include "shapes/jolt_custom_user_data_shape.h" |
| 37 | |
| 38 | #include "core/string/print_string.h" |
| 39 | #include "core/variant/variant.h" |
| 40 | |
| 41 | #include "Jolt/Jolt.h" |
| 42 | |
| 43 | #include "Jolt/RegisterTypes.h" |
| 44 | |
| 45 | #include <cstdarg> |
| 46 | |
| 47 | void *jolt_alloc(size_t p_size) { |
| 48 | return Memory::alloc_static(p_size); |
| 49 | } |
| 50 | |
| 51 | void *jolt_realloc(void *p_mem, size_t p_old_size, size_t p_new_size) { |
| 52 | return Memory::realloc_static(p_mem, p_new_size); |
| 53 | } |
| 54 | |
| 55 | void jolt_free(void *p_mem) { |
| 56 | if (unlikely(p_mem == nullptr)) { |
| 57 | return; |
| 58 | } |
| 59 | Memory::free_static(p_mem); |
| 60 | } |
| 61 | |
| 62 | void *jolt_aligned_alloc(size_t p_size, size_t p_alignment) { |
| 63 | return Memory::alloc_aligned_static(p_size, p_alignment); |
| 64 | } |
| 65 | |
| 66 | void jolt_aligned_free(void *p_mem) { |
| 67 | if (unlikely(p_mem == nullptr)) { |
| 68 | return; |
| 69 | } |
| 70 | Memory::free_aligned_static(p_mem); |
| 71 | } |
| 72 | |
| 73 | #ifdef JPH_ENABLE_ASSERTS |
| 74 | |
| 75 | void jolt_trace(const char *p_format, ...) { |
| 76 | va_list args; |
| 77 | va_start(args, p_format); |
| 78 | char buffer[1024] = { '\0' }; |
| 79 | vsnprintf(buffer, sizeof(buffer), p_format, args); |
| 80 | va_end(args); |
| 81 | print_verbose(buffer); |
| 82 | } |
| 83 | |
| 84 | bool jolt_assert(const char *p_expr, const char *p_msg, const char *p_file, uint32_t p_line) { |
| 85 | ERR_PRINT(vformat("Jolt Physics assertion '%s' failed with message '%s' at '%s:%d'", p_expr, p_msg != nullptr ? p_msg : "", p_file, p_line)); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | #endif |
| 90 | |
| 91 | void jolt_initialize() { |
| 92 | JPH::Allocate = &jolt_alloc; |
| 93 | JPH::Reallocate = &jolt_realloc; |
| 94 | JPH::Free = &jolt_free; |
| 95 | JPH::AlignedAllocate = &jolt_aligned_alloc; |
| 96 | JPH::AlignedFree = &jolt_aligned_free; |
| 97 | |
| 98 | #ifdef JPH_ENABLE_ASSERTS |
| 99 | JPH::Trace = &jolt_trace; |
| 100 | JPH::AssertFailed = &jolt_assert; |
| 101 | #endif |
| 102 | |
| 103 | JPH::Factory::sInstance = new JPH::Factory(); |
| 104 | |
| 105 | JPH::RegisterTypes(); |
| 106 | |
| 107 | JoltCustomRayShape::register_type(); |
| 108 | JoltCustomUserDataShape::register_type(); |
| 109 | JoltCustomDoubleSidedShape::register_type(); |
| 110 | |
| 111 | JoltGroupFilter::instance = new JoltGroupFilter(); |
| 112 | JoltGroupFilter::instance->SetEmbedded(); |
| 113 | } |
| 114 | |
| 115 | void jolt_deinitialize() { |
| 116 | if (JoltGroupFilter::instance != nullptr) { |
| 117 | delete JoltGroupFilter::instance; |
| 118 | JoltGroupFilter::instance = nullptr; |
| 119 | } |
| 120 | |
| 121 | JPH::UnregisterTypes(); |
| 122 | |
| 123 | if (JPH::Factory::sInstance != nullptr) { |
| 124 | delete JPH::Factory::sInstance; |
| 125 | JPH::Factory::sInstance = nullptr; |
| 126 | } |
| 127 | } |