mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
add a safety check for box2d
This commit is contained in:
parent
5e54967903
commit
c2f13e4f03
@ -67,11 +67,11 @@ struct PyBody{
|
|||||||
PK_ALWAYS_PASS_BY_POINTER(PyBody)
|
PK_ALWAYS_PASS_BY_POINTER(PyBody)
|
||||||
|
|
||||||
b2Body* body;
|
b2Body* body;
|
||||||
b2Fixture* fixture;
|
b2Fixture* _fixture;
|
||||||
PyObject* node_like;
|
PyObject* node_like;
|
||||||
|
|
||||||
bool _is_destroyed;
|
bool _is_destroyed;
|
||||||
PyBody(): body(nullptr), fixture(nullptr), node_like(nullptr), _is_destroyed(false){}
|
PyBody(): body(nullptr), _fixture(nullptr), node_like(nullptr), _is_destroyed(false){}
|
||||||
|
|
||||||
void _gc_mark() {
|
void _gc_mark() {
|
||||||
if(node_like != nullptr){
|
if(node_like != nullptr){
|
||||||
@ -81,7 +81,18 @@ struct PyBody{
|
|||||||
|
|
||||||
PyBody* _() { return this; }
|
PyBody* _() { return this; }
|
||||||
b2Body* _b2Body() { return body; }
|
b2Body* _b2Body() { return body; }
|
||||||
b2Fixture* _b2Fixture() { return fixture; }
|
|
||||||
|
b2Fixture* _b2Fixture() {
|
||||||
|
if(_fixture == nullptr) throw std::runtime_error("`_fixture == nullptr` in PyBody::_b2Fixture()");
|
||||||
|
return _fixture;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _set_b2Fixture(b2Fixture* fixture){
|
||||||
|
if(_fixture != nullptr){
|
||||||
|
body->DestroyFixture(_fixture);
|
||||||
|
}
|
||||||
|
_fixture = fixture;
|
||||||
|
}
|
||||||
|
|
||||||
static void _register(VM* vm, PyObject* mod, PyObject* type);
|
static void _register(VM* vm, PyObject* mod, PyObject* type);
|
||||||
|
|
||||||
|
@ -14,9 +14,7 @@ void PyBody::_register(VM* vm, PyObject* mod, PyObject* type){
|
|||||||
// a weak reference to this object
|
// a weak reference to this object
|
||||||
def.userData.pointer = reinterpret_cast<uintptr_t>(obj);
|
def.userData.pointer = reinterpret_cast<uintptr_t>(obj);
|
||||||
body.body = world.world.CreateBody(&def);
|
body.body = world.world.CreateBody(&def);
|
||||||
body.fixture = nullptr;
|
|
||||||
body.node_like = node;
|
body.node_like = node;
|
||||||
body._is_destroyed = false;
|
|
||||||
return obj;
|
return obj;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -49,7 +47,7 @@ void PyBody::_register(VM* vm, PyObject* mod, PyObject* type){
|
|||||||
float hy = CAST(float, args[2]);
|
float hy = CAST(float, args[2]);
|
||||||
b2PolygonShape shape;
|
b2PolygonShape shape;
|
||||||
shape.SetAsBox(hx, hy);
|
shape.SetAsBox(hx, hy);
|
||||||
body.fixture = body.body->CreateFixture(&shape, 1.0f);
|
body._set_b2Fixture(body.body->CreateFixture(&shape, 1.0f));
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -59,7 +57,7 @@ void PyBody::_register(VM* vm, PyObject* mod, PyObject* type){
|
|||||||
float radius = CAST(float, args[1]);
|
float radius = CAST(float, args[1]);
|
||||||
b2CircleShape shape;
|
b2CircleShape shape;
|
||||||
shape.m_radius = radius;
|
shape.m_radius = radius;
|
||||||
body.fixture = body.body->CreateFixture(&shape, 1.0f);
|
body._set_b2Fixture(body.body->CreateFixture(&shape, 1.0f));
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -77,7 +75,7 @@ void PyBody::_register(VM* vm, PyObject* mod, PyObject* type){
|
|||||||
vertices.push_back(b2Vec2(vec.x, vec.y));
|
vertices.push_back(b2Vec2(vec.x, vec.y));
|
||||||
}
|
}
|
||||||
shape.Set(vertices.data(), vertices.size());
|
shape.Set(vertices.data(), vertices.size());
|
||||||
body.fixture = body.body->CreateFixture(&shape, 1.0f);
|
body._set_b2Fixture(body.body->CreateFixture(&shape, 1.0f));
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -95,7 +93,7 @@ void PyBody::_register(VM* vm, PyObject* mod, PyObject* type){
|
|||||||
vertices.push_back(b2Vec2(vec.x, vec.y));
|
vertices.push_back(b2Vec2(vec.x, vec.y));
|
||||||
}
|
}
|
||||||
shape.CreateLoop(vertices.data(), vertices.size());
|
shape.CreateLoop(vertices.data(), vertices.size());
|
||||||
body.fixture = body.body->CreateFixture(&shape, 1.0f);
|
body._set_b2Fixture(body.body->CreateFixture(&shape, 1.0f));
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -135,7 +135,7 @@ void PyWorld::_register(VM* vm, PyObject* mod, PyObject* type){
|
|||||||
if(body._is_destroyed){
|
if(body._is_destroyed){
|
||||||
body.body->GetWorld()->DestroyBody(body.body);
|
body.body->GetWorld()->DestroyBody(body.body);
|
||||||
body.body = nullptr;
|
body.body = nullptr;
|
||||||
body.fixture = nullptr;
|
body._fixture = nullptr;
|
||||||
body.node_like = nullptr;
|
body.node_like = nullptr;
|
||||||
}
|
}
|
||||||
p = next;
|
p = next;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user