Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/jolt_physics/jolt_globals.cpp
10277 views
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/os/memory.h"
39
#include "core/string/print_string.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
Memory::free_static(p_mem);
57
}
58
59
void *jolt_aligned_alloc(size_t p_size, size_t p_alignment) {
60
return Memory::alloc_aligned_static(p_size, p_alignment);
61
}
62
63
void jolt_aligned_free(void *p_mem) {
64
Memory::free_aligned_static(p_mem);
65
}
66
67
#ifdef JPH_ENABLE_ASSERTS
68
69
void jolt_trace(const char *p_format, ...) {
70
va_list args;
71
va_start(args, p_format);
72
char buffer[1024] = { '\0' };
73
vsnprintf(buffer, sizeof(buffer), p_format, args);
74
va_end(args);
75
print_verbose(buffer);
76
}
77
78
bool jolt_assert(const char *p_expr, const char *p_msg, const char *p_file, uint32_t p_line) {
79
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));
80
return false;
81
}
82
83
#endif
84
85
void jolt_initialize() {
86
JPH::Allocate = &jolt_alloc;
87
JPH::Reallocate = &jolt_realloc;
88
JPH::Free = &jolt_free;
89
JPH::AlignedAllocate = &jolt_aligned_alloc;
90
JPH::AlignedFree = &jolt_aligned_free;
91
92
#ifdef JPH_ENABLE_ASSERTS
93
JPH::Trace = &jolt_trace;
94
JPH::AssertFailed = &jolt_assert;
95
#endif
96
97
JPH::Factory::sInstance = new JPH::Factory();
98
99
JPH::RegisterTypes();
100
101
JoltCustomRayShape::register_type();
102
JoltCustomUserDataShape::register_type();
103
JoltCustomDoubleSidedShape::register_type();
104
105
JoltGroupFilter::instance = new JoltGroupFilter();
106
JoltGroupFilter::instance->SetEmbedded();
107
}
108
109
void jolt_deinitialize() {
110
if (JoltGroupFilter::instance != nullptr) {
111
delete JoltGroupFilter::instance;
112
JoltGroupFilter::instance = nullptr;
113
}
114
115
JPH::UnregisterTypes();
116
117
if (JPH::Factory::sInstance != nullptr) {
118
delete JPH::Factory::sInstance;
119
JPH::Factory::sInstance = nullptr;
120
}
121
}
122
123