static int cpPolyShape_new (lua_State *L) { cpBody *body = check_cpBody(L, 1); luaL_checktype(L, 2, LUA_TTABLE); int n = lua_objlen(L, 2); luaL_argcheck(L, (n % 2 == 0) && (n > 4), 2, "at least 3 pairs of coordinates are requires"); cpVect offset = check_cpVect(L, 3); cpVect *verts = (cpVect *)malloc(n/2 * sizeof(cpVect)); cpVect *tv = verts; int i; for (i=1; i<n; i=i+2) { lua_pushinteger(L, i); lua_gettable(L, 2); lua_pushinteger(L, i+1); lua_gettable(L, 2); tv->x = (cpFloat)luaL_checknumber(L, -2); tv->y = (cpFloat)luaL_checknumber(L, -1); tv++; lua_pop(L, 2); } cpPolyShape *poly = push_cpPolyShape(L); cpPolyShapeInit(poly, body, n/2, verts, offset); free(verts); /* cpReferences.shape_userdata = body_userdata */ lua_pushliteral(L, "cpReferences"); lua_rawget(L, LUA_REGISTRYINDEX); lua_pushvalue(L, -2); lua_pushvalue(L, 1); lua_rawset(L, -3); lua_pop(L, 1); return 1; }
/** * Initializes a new player. * The player is placed in the queue of available players. */ struct player *player_init(struct player *p, struct game *g, double x, double y, double w, double h, uint32_t score, uint8_t data) { cpVect all[4] = {cpv(0,0), cpv(0,h), cpv(w,h), cpv(w,0)}; cpBody *body = cpBodyInit(&p->body, 10, cpMomentForBox(10, w, h)); if (!body) { ERR_ERRNO(); return 0; } cpBodySetPos(body, cpv(x,y)); //cpShape *shape = cpPolyShapeNew(body,4,all,cpv((p->l+p->r)/2.0,(p->b+p->t)/2.0)); cpShape *shape = cpPolyShapeInit(&p->shape, body, 4, all, cpv(0, 0)); if (!shape) { ERR_ERRNO(); cpBodyDestroy(body); return 0; } shape->data = p; shape->collision_type = PLAYER; if (linkedlist_add_last(g->p_q, p)) { ERR_TRACE(); cpBodyDestroy(body); cpShapeDestroy(shape); return 0; } p->x = x; p->y = y; p->node = g->p_q->last; return p; }
struct wall *wall_init(struct wall *wa, struct game *g, double l, double r, double b, double t) { cpVect all[4] = {cpv(l,t), cpv(r,t), cpv(r,b), cpv(l,b)}; //cpShape *shape = cpPolyShapeNew(body,4,all,cpv((wa->l+wa->r)/2.0,(wa->b+wa->t)/2.0)); cpShape *shape = cpPolyShapeInit(&wa->shape, &g->cp->staticBody, 4, all, cpvzero); shape->data = wa; shape->e = 1.0; shape->collision_type = WALL; return wa; }
cpPolyShape * cpBoxShapeInit2(cpPolyShape *poly, cpBody *body, cpBB box) { cpVect verts[] = { cpv(box.l, box.b), cpv(box.l, box.t), cpv(box.r, box.t), cpv(box.r, box.b), }; return cpPolyShapeInit(poly, body, 4, verts, cpvzero); }
cpPolyShape * cpBoxShapeInit(cpPolyShape *poly, cpBody *body, cpFloat width, cpFloat height) { cpFloat hw = width/2.0f; cpFloat hh = height/2.0f; cpVect verts[] = { cpv(-hw,-hh), cpv(-hw, hh), cpv( hw, hh), cpv( hw,-hh), }; return cpPolyShapeInit(poly, body, 4, verts, cpvzero); }
static VALUE rb_cpPolyInitialize(VALUE self, VALUE body, VALUE arr, VALUE offset) { cpPolyShape *poly = (cpPolyShape *)SHAPE(self); Check_Type(arr, T_ARRAY); int numVerts = RARRAY_LEN(arr); VALUE *ary_ptr = RARRAY_PTR(arr); cpVect verts[numVerts]; for(int i=0; i<numVerts; i++) verts[i] = *VGET(ary_ptr[i]); cpPolyShapeInit(poly, BODY(body), numVerts, verts, *VGET(offset)); poly->shape.data = (void *)self; poly->shape.collision_type = Qnil; rb_ivar_set(self, id_body, body); return self; }
cpShape * cpPolyShapeNew(cpBody *body, int numVerts, cpVect *verts, cpVect offset) { return (cpShape *)cpPolyShapeInit(cpPolyShapeAlloc(), body, numVerts, verts, offset); }
cpShape * cpPolyShapeNew(cpBody *body, int count, const cpVect *verts, cpTransform transform, cpFloat radius) { return (cpShape *)cpPolyShapeInit(cpPolyShapeAlloc(), body, count, verts, transform, radius); }
// From cpPolyShape.h void wrPolyShapeInit(cpPolyShape *poly, cpBody *body, int numVerts, cpVect *verts, cpVect *offset) { cpPolyShapeInit(poly, body, numVerts, verts, *offset); }