2
0

jolt_globals.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/os/memory.h"
  36. #include "core/string/print_string.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. Memory::free_static(p_mem);
  48. }
  49. void *jolt_aligned_alloc(size_t p_size, size_t p_alignment) {
  50. return Memory::alloc_aligned_static(p_size, p_alignment);
  51. }
  52. void jolt_aligned_free(void *p_mem) {
  53. Memory::free_aligned_static(p_mem);
  54. }
  55. #ifdef JPH_ENABLE_ASSERTS
  56. void jolt_trace(const char *p_format, ...) {
  57. va_list args;
  58. va_start(args, p_format);
  59. char buffer[1024] = { '\0' };
  60. vsnprintf(buffer, sizeof(buffer), p_format, args);
  61. va_end(args);
  62. print_verbose(buffer);
  63. }
  64. bool jolt_assert(const char *p_expr, const char *p_msg, const char *p_file, uint32_t p_line) {
  65. 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));
  66. return false;
  67. }
  68. #endif
  69. void jolt_initialize() {
  70. JPH::Allocate = &jolt_alloc;
  71. JPH::Reallocate = &jolt_realloc;
  72. JPH::Free = &jolt_free;
  73. JPH::AlignedAllocate = &jolt_aligned_alloc;
  74. JPH::AlignedFree = &jolt_aligned_free;
  75. #ifdef JPH_ENABLE_ASSERTS
  76. JPH::Trace = &jolt_trace;
  77. JPH::AssertFailed = &jolt_assert;
  78. #endif
  79. JPH::Factory::sInstance = new JPH::Factory();
  80. JPH::RegisterTypes();
  81. JoltCustomRayShape::register_type();
  82. JoltCustomUserDataShape::register_type();
  83. JoltCustomDoubleSidedShape::register_type();
  84. JoltGroupFilter::instance = new JoltGroupFilter();
  85. JoltGroupFilter::instance->SetEmbedded();
  86. }
  87. void jolt_deinitialize() {
  88. if (JoltGroupFilter::instance != nullptr) {
  89. delete JoltGroupFilter::instance;
  90. JoltGroupFilter::instance = nullptr;
  91. }
  92. JPH::UnregisterTypes();
  93. if (JPH::Factory::sInstance != nullptr) {
  94. delete JPH::Factory::sInstance;
  95. JPH::Factory::sInstance = nullptr;
  96. }
  97. }