This commit is contained in:
bjorn 2017-05-15 23:09:32 -06:00
parent c06a13f124
commit f4d4664318
5 changed files with 44 additions and 0 deletions

View File

@ -16,6 +16,7 @@ int l_lovrTimerInit(lua_State* L);
extern const luaL_Reg lovrAudio[];
extern const luaL_Reg lovrController[];
extern const luaL_Reg lovrBlob[];
extern const luaL_Reg lovrBody[];
extern const luaL_Reg lovrEvent[];
extern const luaL_Reg lovrFilesystem[];
extern const luaL_Reg lovrFont[];

View File

@ -5,6 +5,7 @@ int l_lovrPhysicsInit(lua_State* L) {
lua_newtable(L);
luaL_register(L, NULL, lovrPhysics);
luax_registertype(L, "World", lovrWorld);
luax_registertype(L, "Body", lovrBody);
lovrPhysicsInit();
return 1;
}
@ -14,7 +15,14 @@ int l_lovrPhysicsNewWorld(lua_State* L) {
return 1;
}
int l_lovrPhysicsNewBody(lua_State* L) {
World* world = luax_checktype(L, 1, World);
luax_pushtype(L, Body, lovrBodyCreate(world));
return 1;
}
const luaL_Reg lovrPhysics[] = {
{ "newWorld", l_lovrPhysicsNewWorld },
{ "newBody", l_lovrPhysicsNewBody },
{ NULL, NULL }
};

6
src/api/types/body.c Normal file
View File

@ -0,0 +1,6 @@
#include "api/lovr.h"
#include "physics/physics.h"
const luaL_Reg lovrBody[] = {
{ NULL, NULL }
};

View File

@ -73,3 +73,23 @@ void lovrWorldSetSleepingAllowed(World* world, int allowed) {
void lovrWorldUpdate(World* world, float dt) {
dWorldQuickStep(world->id, dt);
}
Body* lovrBodyCreate(World* world) {
if (!world) {
error("No world specified");
}
Body* body = lovrAlloc(sizeof(Body), lovrBodyDestroy);
if (!body) return NULL;
body->id = dBodyCreate(world->id);
body->world = world;
return body;
}
void lovrBodyDestroy(const Ref* ref) {
Body* body = containerof(ref, Body);
dBodyDestroy(body->id);
free(body);
}

View File

@ -6,6 +6,12 @@ typedef struct {
dWorldID id;
} World;
typedef struct {
Ref ref;
dBodyID id;
World* world;
} Body;
void lovrPhysicsInit();
void lovrPhysicsDestroy();
@ -20,3 +26,6 @@ void lovrWorldSetAngularDamping(World* world, float damping, float threshold);
int lovrWorldIsSleepingAllowed(World* world);
void lovrWorldSetSleepingAllowed(World* world, int allowed);
void lovrWorldUpdate(World* world, float dt);
Body* lovrBodyCreate();
void lovrBodyDestroy(const Ref* ref);