jolt_globals.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "jolt_globals.h"
  31. #include "objects/jolt_group_filter.h"
  32. #include "shapes/jolt_custom_double_sided_shape.h"
  33. #include "shapes/jolt_custom_ray_shape.h"
  34. #include "shapes/jolt_custom_user_data_shape.h"
  35. #include "core/string/print_string.h"
  36. #include "core/variant/variant.h"
  37. #include "Jolt/Jolt.h"
  38. #include "Jolt/RegisterTypes.h"
  39. #include <cstdarg>
  40. void *jolt_alloc(size_t p_size) {
  41. return Memory::alloc_static(p_size);
  42. }
  43. void *jolt_realloc(void *p_mem, size_t p_old_size, size_t p_new_size) {
  44. return Memory::realloc_static(p_mem, p_new_size);
  45. }
  46. void jolt_free(void *p_mem) {
  47. if (unlikely(p_mem == nullptr)) {
  48. return;
  49. }
  50. Memory::free_static(p_mem);
  51. }
  52. void *jolt_aligned_alloc(size_t p_size, size_t p_alignment) {
  53. return Memory::alloc_aligned_static(p_size, p_alignment);
  54. }
  55. void jolt_aligned_free(void *p_mem) {
  56. if (unlikely(p_mem == nullptr)) {
  57. return;
  58. }
  59. Memory::free_aligned_static(p_mem);
  60. }
  61. #ifdef JPH_ENABLE_ASSERTS
  62. void jolt_trace(const char *p_format, ...) {
  63. va_list args;
  64. va_start(args, p_format);
  65. char buffer[1024] = { '\0' };
  66. vsnprintf(buffer, sizeof(buffer), p_format, args);
  67. va_end(args);
  68. print_verbose(buffer);
  69. }
  70. bool jolt_assert(const char *p_expr, const char *p_msg, const char *p_file, uint32_t p_line) {
  71. 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));
  72. return false;
  73. }
  74. #endif
  75. void jolt_initialize() {
  76. JPH::Allocate = &jolt_alloc;
  77. JPH::Reallocate = &jolt_realloc;
  78. JPH::Free = &jolt_free;
  79. JPH::AlignedAllocate = &jolt_aligned_alloc;
  80. JPH::AlignedFree = &jolt_aligned_free;
  81. #ifdef JPH_ENABLE_ASSERTS
  82. JPH::Trace = &jolt_trace;
  83. JPH::AssertFailed = &jolt_assert;
  84. #endif
  85. JPH::Factory::sInstance = new JPH::Factory();
  86. JPH::RegisterTypes();
  87. JoltCustomRayShape::register_type();
  88. JoltCustomUserDataShape::register_type();
  89. JoltCustomDoubleSidedShape::register_type();
  90. JoltGroupFilter::instance = new JoltGroupFilter();
  91. JoltGroupFilter::instance->SetEmbedded();
  92. }
  93. void jolt_deinitialize() {
  94. if (JoltGroupFilter::instance != nullptr) {
  95. delete JoltGroupFilter::instance;
  96. JoltGroupFilter::instance = nullptr;
  97. }
  98. JPH::UnregisterTypes();
  99. if (JPH::Factory::sInstance != nullptr) {
  100. delete JPH::Factory::sInstance;
  101. JPH::Factory::sInstance = nullptr;
  102. }
  103. }