Removed registry

This commit is contained in:
John Alanbrook 2023-01-12 23:41:54 +00:00
parent f0ce69f957
commit e0b7d6459d
12 changed files with 236 additions and 345 deletions

View file

@ -15,8 +15,6 @@
#include "log.h"
#include "registry.h"
cpSpace *space = NULL;
float phys2d_gravity = -50.f;
@ -39,15 +37,14 @@ void phys2d_update(float deltaT)
void phys2d_shape_apply(struct phys2d_shape *shape)
{
cpShapeSetFriction(shape->shape, shape->go->f);
cpShapeSetElasticity(shape->shape, shape->go->e);
cpShapeSetFriction(shape->shape, id2go(shape->go)->f);
cpShapeSetElasticity(shape->shape, id2go(shape->go)->e);
}
void init_phys2dshape(struct phys2d_shape *shape, struct gameobject *go)
void init_phys2dshape(struct phys2d_shape *shape, int go)
{
YughInfo("Making shape with GO %p", go);
shape->go = go;
cpShapeSetCollisionType(shape->shape, go);
cpShapeSetCollisionType(shape->shape, id2go(go));
phys2d_shape_apply(shape);
}
@ -56,7 +53,7 @@ void phys2d_shape_del(struct phys2d_shape *shape)
cpSpaceRemoveShape(space, shape->shape);
}
struct phys2d_circle *Make2DCircle(struct gameobject *go)
struct phys2d_circle *Make2DCircle(int go)
{
struct phys2d_circle *new = malloc(sizeof(struct phys2d_circle));
@ -64,13 +61,10 @@ struct phys2d_circle *Make2DCircle(struct gameobject *go)
new->offset[0] = 0.f;
new->offset[1] = 0.f;
return new;
}
new->shape.shape = cpSpaceAddShape(space, cpCircleShapeNew(id2go(go)->body, new->radius, cpvzero));
init_phys2dshape(&new->shape, go);
void phys2d_circleinit(struct phys2d_circle *circle, struct gameobject *go)
{
circle->shape.shape = cpSpaceAddShape(space, cpCircleShapeNew(go->body, circle->radius, cpvzero));
init_phys2dshape(&circle->shape, go);
return new;
}
void phys2d_circledel(struct phys2d_circle *c)
@ -88,6 +82,7 @@ void circle_gui(struct phys2d_circle *circle)
void phys2d_dbgdrawcpcirc(cpCircleShape *c)
{
YughInfo("DRAW CIRCLE");
cpVect pos = cpBodyGetPosition(cpShapeGetBody(c));
cpVect offset = cpCircleShapeGetOffset(c);
float radius = cpCircleShapeGetRadius(c);
@ -98,17 +93,21 @@ void phys2d_dbgdrawcpcirc(cpCircleShape *c)
void phys2d_dbgdrawcircle(struct phys2d_circle *circle)
{
YughInfo("Drawing a circle");
phys2d_dbgdrawcpcirc((cpCircleShape *)circle->shape.shape);
/*
cpVect p = cpBodyGetPosition(circle->shape.go->body);
cpVect p = cpBodyGetPosition(cpShapeGetBody(circle->shape.shape));
cpVect o = cpCircleShapeGetOffset(circle->shape.shape);
float d = sqrt(pow(o.x, 2.f) + pow(o.y, 2.f));
float a = atan2(o.y, o.x) + cpBodyGetAngle(circle->shape.go->body);
float a = atan2(o.y, o.x) + cpBodyGetAngle(cpShapeGetBody(circle->shape.shape));
draw_circle(p.x + (d * cos(a)), p.y + (d * sin(a)), cpCircleShapeGetRadius(circle->shape.shape), 1);
*/
}
struct phys2d_segment *Make2DSegment(struct gameobject *go)
/*********** SEGMENT2D **************/
struct phys2d_segment *Make2DSegment(int go)
{
struct phys2d_segment *new = malloc(sizeof(struct phys2d_segment));
@ -118,16 +117,12 @@ struct phys2d_segment *Make2DSegment(struct gameobject *go)
new->b[0] = 0.f;
new->b[1] = 0.f;
new->shape.shape = cpSpaceAddShape(space, cpSegmentShapeNew(id2go(go)->body, cpvzero, cpvzero, new->thickness));
init_phys2dshape(&new->shape, go);
return new;
}
void phys2d_seginit(struct phys2d_segment *seg, struct gameobject *go)
{
seg->shape.shape = cpSpaceAddShape(space, cpSegmentShapeNew(go->body, cpvzero, cpvzero, seg->thickness));
init_phys2dshape(&seg->shape, go);
}
void phys2d_segdel(struct phys2d_segment *seg)
{
phys2d_shape_del(&seg->shape);
@ -141,7 +136,10 @@ void segment_gui(struct phys2d_segment *seg)
phys2d_applyseg(seg);
}
struct phys2d_box *Make2DBox(struct gameobject *go)
/************* BOX2D ************/
struct phys2d_box *Make2DBox(int go)
{
struct phys2d_box *new = malloc(sizeof(struct phys2d_box));
@ -151,14 +149,11 @@ struct phys2d_box *Make2DBox(struct gameobject *go)
new->offset[0] = 0.f;
new->offset[1] = 0.f;
return new;
}
new->shape.shape = cpSpaceAddShape(space, cpBoxShapeNew(id2go(go)->body, new->w, new->h, new->r));
init_phys2dshape(&new->shape, go);
phys2d_applybox(new);
void phys2d_boxinit(struct phys2d_box *box, struct gameobject *go)
{
box->shape.shape = cpSpaceAddShape(space, cpBoxShapeNew(go->body, box->w, box->h, box->r));
init_phys2dshape(&box->shape, go);
phys2d_applybox(box);
return new;
}
void phys2d_boxdel(struct phys2d_box *box)
@ -175,8 +170,9 @@ void box_gui(struct phys2d_box *box)
phys2d_applybox(box);
}
/************** POLYGON ************/
struct phys2d_poly *Make2DPoly(struct gameobject *go)
struct phys2d_poly *Make2DPoly(int go)
{
struct phys2d_poly *new = malloc(sizeof(struct phys2d_poly));
@ -184,16 +180,12 @@ struct phys2d_poly *Make2DPoly(struct gameobject *go)
new->points = NULL;
new->radius = 0.f;
return new;
}
void phys2d_polyinit(struct phys2d_poly *poly, struct gameobject *go)
{
cpTransform T = { 0 };
poly->shape.shape =
cpSpaceAddShape(space, cpPolyShapeNew(go->body, 0, NULL, T, poly->radius));
init_phys2dshape(&poly->shape, go);
phys2d_applypoly(poly);
new->shape.shape = cpSpaceAddShape(space, cpPolyShapeNew(id2go(go)->body, 0, NULL, T, new->radius));
init_phys2dshape(&new->shape, go);
phys2d_applypoly(new);
return new;
}
void phys2d_polydel(struct phys2d_poly *poly)
@ -224,7 +216,11 @@ void poly_gui(struct phys2d_poly *poly)
phys2d_applypoly(poly);
}
struct phys2d_edge *Make2DEdge(struct gameobject *go)
/****************** EDGE 2D**************/
struct phys2d_edge *Make2DEdge(int go)
{
struct phys2d_edge *new = malloc(sizeof(struct phys2d_edge));
@ -233,19 +229,15 @@ struct phys2d_edge *Make2DEdge(struct gameobject *go)
new->thickness = 0.f;
new->shapes = malloc(sizeof(cpShape *));
new->shapes[0] = cpSpaceAddShape(space, cpSegmentShapeNew(id2go(go)->body, cpvzero, cpvzero, new->thickness));
new->shape.go = go;
phys2d_edgeshapeapply(&new->shape, new->shapes[0]);
phys2d_applyedge(new);
return new;
}
void phys2d_edgeinit(struct phys2d_edge *edge, struct gameobject *go)
{
edge->shapes[0] =
cpSpaceAddShape(space, cpSegmentShapeNew(go->body, cpvzero, cpvzero, edge->thickness));
edge->shape.go = go;
phys2d_edgeshapeapply(&edge->shape, edge->shapes[0]);
phys2d_applyedge(edge);
}
void phys2d_edgedel(struct phys2d_edge *edge)
{
phys2d_shape_del(&edge->shape);
@ -253,8 +245,8 @@ void phys2d_edgedel(struct phys2d_edge *edge)
void phys2d_edgeshapeapply(struct phys2d_shape *mshape, cpShape * shape)
{
cpShapeSetFriction(shape, mshape->go->f);
cpShapeSetElasticity(shape, mshape->go->e);
cpShapeSetFriction(shape, id2go(mshape->go)->f);
cpShapeSetElasticity(shape, id2go(mshape->go)->e);
}
void phys2d_edgeaddvert(struct phys2d_edge *edge)
@ -273,10 +265,7 @@ void phys2d_edgeaddvert(struct phys2d_edge *edge)
cpVect b =
{ edge->points[(edge->n - 1) * 2],
edge->points[(edge->n - 1) * 2 + 1] };
edge->shapes[edge->n - 2] =
cpSpaceAddShape(space,
cpSegmentShapeNew(edge->shape.go->body, a, b,
edge->thickness));
edge->shapes[edge->n - 2] = cpSpaceAddShape(space, cpSegmentShapeNew(id2go(edge->shape.go)->body, a, b, edge->thickness));
phys2d_edgeshapeapply(&edge->shape, edge->shapes[edge->n - 2]);
free(oldp);
@ -299,18 +288,20 @@ void edge_gui(struct phys2d_edge *edge)
void phys2d_applycircle(struct phys2d_circle *circle)
{
float radius = circle->radius * circle->shape.go->scale;
float s = circle->shape.go->scale;
struct gameobject *go = id2go(circle->shape.go);
float radius = circle->radius * go->scale;
float s = go->scale;
cpVect offset = { circle->offset[0] * s, circle->offset[1] * s };
cpCircleShapeSetRadius(circle->shape.shape, radius);
cpCircleShapeSetOffset(circle->shape.shape, offset);
cpBodySetMoment(circle->shape.go->body, cpMomentForCircle(circle->shape.go->mass, 0, radius, offset));
cpBodySetMoment(go->body, cpMomentForCircle(go->mass, 0, radius, offset));
}
void phys2d_applyseg(struct phys2d_segment *seg)
{
float s = seg->shape.go->scale;
float s = id2go(seg->shape.go)->scale;
cpVect a = { seg->a[0] * s, seg->a[1] * s };
cpVect b = { seg->b[0] * s, seg->b[1] * s };
cpSegmentShapeSetEndpoints(seg->shape.shape, a, b);
@ -319,7 +310,7 @@ void phys2d_applyseg(struct phys2d_segment *seg)
void phys2d_applybox(struct phys2d_box *box)
{
float s = box->shape.go->scale;
float s = id2go(box->shape.go)->scale;
cpTransform T = { 0 };
T.a = s;
T.d = s;
@ -344,7 +335,7 @@ void phys2d_applypoly(struct phys2d_poly *poly)
CP_CONVEX_HULL(poly->n, verts, hullCount, hullVerts);
float s = poly->shape.go->scale;
float s = id2go(poly->shape.go)->scale;
cpTransform T = { 0 };
T.a = s;
T.d = s;
@ -355,7 +346,7 @@ void phys2d_applypoly(struct phys2d_poly *poly)
void phys2d_applyedge(struct phys2d_edge *edge)
{
float s = edge->shape.go->scale;
float s = id2go(edge->shape.go)->scale;
for (int i = 0; i < edge->n - 1; i++) {
cpVect a =
@ -371,24 +362,23 @@ void phys2d_applyedge(struct phys2d_edge *edge)
void phys2d_dbgdrawseg(struct phys2d_segment *seg)
{
cpVect p = cpBodyGetPosition(seg->shape.go->body);
cpVect p = cpBodyGetPosition(cpShapeGetBody(seg->shape.shape));
cpVect a = cpSegmentShapeGetA(seg->shape.shape);
cpVect b = cpSegmentShapeGetB(seg->shape.shape);
float angle = cpBodyGetAngle(seg->shape.go->body);
float angle = cpBodyGetAngle(cpShapeGetBody(seg->shape.shape));
float ad = sqrt(pow(a.x, 2.f) + pow(a.y, 2.f));
float bd = sqrt(pow(b.x, 2.f) + pow(b.y, 2.f));
float aa = atan2(a.y, a.x) + angle;
float ba = atan2(b.y, b.x) + angle;
draw_line(ad * cos(aa) + p.x, ad * sin(aa) + p.y, bd * cos(ba) + p.x,
bd * sin(ba) + p.y);
draw_line(ad * cos(aa) + p.x, ad * sin(aa) + p.y, bd * cos(ba) + p.x, bd * sin(ba) + p.y);
}
void phys2d_dbgdrawbox(struct phys2d_box *box)
{
int n = cpPolyShapeGetCount(box->shape.shape);
cpVect b = cpBodyGetPosition(box->shape.go->body);
float angle = cpBodyGetAngle(box->shape.go->body);
cpVect b = cpBodyGetPosition(cpShapeGetBody(box->shape.shape));
float angle = cpBodyGetAngle(cpShapeGetBody(box->shape.shape));
float points[n * 2];
for (int i = 0; i < n; i++) {
@ -404,16 +394,13 @@ void phys2d_dbgdrawbox(struct phys2d_box *box)
void phys2d_dbgdrawpoly(struct phys2d_poly *poly)
{
cpVect b = cpBodyGetPosition(poly->shape.go->body);
float angle = cpBodyGetAngle(poly->shape.go->body);
cpVect b = cpBodyGetPosition(cpShapeGetBody(poly->shape.shape));
float angle = cpBodyGetAngle(cpShapeGetBody(poly->shape.shape));
float s = poly->shape.go->scale;
float s = id2go(poly->shape.go)->scale;
for (int i = 0; i < poly->n; i++) {
float d =
sqrt(pow(poly->points[i * 2] * s, 2.f) +
pow(poly->points[i * 2 + 1] * s, 2.f));
float a =
atan2(poly->points[i * 2 + 1], poly->points[i * 2]) + angle;
float d = sqrt(pow(poly->points[i * 2] * s, 2.f) + pow(poly->points[i * 2 + 1] * s, 2.f));
float a = atan2(poly->points[i * 2 + 1], poly->points[i * 2]) + angle;
draw_point(b.x + d * cos(a), b.y + d * sin(a), 3);
}
@ -436,16 +423,13 @@ void phys2d_dbgdrawpoly(struct phys2d_poly *poly)
void phys2d_dbgdrawedge(struct phys2d_edge *edge)
{
cpVect p = cpBodyGetPosition(edge->shape.go->body);
float s = edge->shape.go->scale;
float angle = cpBodyGetAngle(edge->shape.go->body);
cpVect p = cpBodyGetPosition(cpShapeGetBody(edge->shape.shape));
float s = id2go(edge->shape.go)->scale;
float angle = cpBodyGetAngle(cpShapeGetBody(edge->shape.shape));
for (int i = 0; i < edge->n; i++) {
float d =
sqrt(pow(edge->points[i * 2] * s, 2.f) +
pow(edge->points[i * 2 + 1] * s, 2.f));
float a =
atan2(edge->points[i * 2 + 1], edge->points[i * 2]) + angle;
float d = sqrt(pow(edge->points[i * 2] * s, 2.f) + pow(edge->points[i * 2 + 1] * s, 2.f));
float a = atan2(edge->points[i * 2 + 1], edge->points[i * 2]) + angle;
draw_point(p.x + d * cos(a), p.y + d * sin(a), 3);
}
@ -485,7 +469,7 @@ static cpBool script_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) {
vect2duk(cpArbiterGetNormal(arb));
duk_put_prop_literal(duk, obj, "normal");
duk_push_int(duk, g2->editor.id);
duk_push_int(duk, g2);
duk_put_prop_literal(duk, obj, "hit");
duk_call_method(duk, 1);
@ -507,15 +491,16 @@ static void s7_phys_cb_separate(cpArbiter *Arb, cpSpace *space, void *data) {
//script_call_sym(go->cbs->separate);
}
void phys2d_add_handler_type(int cmd, struct gameobject *go, struct callee c) {
void phys2d_add_handler_type(int cmd, int go, struct callee c) {
/*
cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, go);
handler->userData = go;
handler->userData = id2go(go);
switch (cmd) {
case 0:
handler->beginFunc = script_phys_cb_begin;
go->cbs.begin = c;
id2go(go)->cbs.begin = c;
break;
case 1:
@ -529,4 +514,5 @@ void phys2d_add_handler_type(int cmd, struct gameobject *go, struct callee c) {
//go->cbs->separate = cb;
break;
}
*/
}

View file

@ -4,8 +4,6 @@
#include <chipmunk/chipmunk.h>
#include "script.h"
struct gameobject;
extern cpBody *ballBody;
extern float phys2d_gravity;
extern int physOn;
@ -13,7 +11,7 @@ extern cpSpace *space;
struct phys2d_shape {
cpShape *shape;
struct gameobject *go;
int go;
};
struct phys2d_circle {
@ -52,37 +50,32 @@ struct phys2d_poly {
struct phys2d_shape shape;
};
struct phys2d_circle *Make2DCircle(struct gameobject *go);
void phys2d_circleinit(struct phys2d_circle *circle, struct gameobject *go);
struct phys2d_circle *Make2DCircle(int go);
void phys2d_circledel(struct phys2d_circle *c);
void phys2d_applycircle(struct phys2d_circle *circle);
void phys2d_dbgdrawcircle(struct phys2d_circle *circle);
void circle_gui(struct phys2d_circle *circle);
struct phys2d_segment *Make2DSegment(struct gameobject *go);
void phys2d_seginit(struct phys2d_segment *seg, struct gameobject *go);
struct phys2d_segment *Make2DSegment(int go);
void phys2d_segdel(struct phys2d_segment *seg);
void phys2d_applyseg(struct phys2d_segment *seg);
void phys2d_dbgdrawseg(struct phys2d_segment *seg);
void segment_gui(struct phys2d_segment *seg);
struct phys2d_box *Make2DBox(struct gameobject *go);
void phys2d_boxinit(struct phys2d_box *box, struct gameobject *go);
struct phys2d_box *Make2DBox(int go);
void phys2d_boxdel(struct phys2d_box *box);
void phys2d_applybox(struct phys2d_box *box);
void phys2d_dbgdrawbox(struct phys2d_box *box);
void box_gui(struct phys2d_box *box);
struct phys2d_poly *Make2DPoly(struct gameobject *go);
void phys2d_polyinit(struct phys2d_poly *poly, struct gameobject *go);
struct phys2d_poly *Make2DPoly(int go);
void phys2d_polydel(struct phys2d_poly *poly);
void phys2d_applypoly(struct phys2d_poly *poly);
void phys2d_dbgdrawpoly(struct phys2d_poly *poly);
void phys2d_polyaddvert(struct phys2d_poly *poly);
void poly_gui(struct phys2d_poly *poly);
struct phys2d_edge *Make2DEdge(struct gameobject *go);
void phys2d_edgeinit(struct phys2d_edge *edge, struct gameobject *go);
struct phys2d_edge *Make2DEdge(int go);
void phys2d_edgedel(struct phys2d_edge *edge);
void phys2d_applyedge(struct phys2d_edge *edge);
void phys2d_edgeshapeapply(struct phys2d_shape *mshape, cpShape * shape);
@ -99,7 +92,7 @@ struct phys_cbs {
struct callee separate;
};
void phys2d_add_handler_type(int cmd, struct gameobject *go, struct callee c);
void phys2d_add_handler_type(int cmd, int go, struct callee c);
void register_collide(void *sym);
void phys2d_set_gravity(cpVect v);

View file

@ -1,3 +1,8 @@
void editor_init(){}
#ifdef GOOGLE
#include "editor.h"
#include "ed_project.h"
@ -13,7 +18,6 @@
#include "level.h"
#include "math.h"
#include "openglrender.h"
#include "registry.h"
#include "resources.h"
#include "script.h"
#include "shader.h"
@ -1023,3 +1027,4 @@ void sprite_gui(struct sprite *sprite) {
}
#endif

View file

@ -30,7 +30,6 @@
#include "sprite.h"
#include "2dphysics.h"
#include "gameobject.h"
#include "registry.h"
#include "log.h"
#include "resources.h"
#include "timer.h"
@ -60,7 +59,6 @@ void engine_init()
YughInfo("Starting scripts ...");
script_init();
registry_init();
YughInfo("Starting physics ...");
phys2d_init();

View file

@ -68,7 +68,7 @@ duk_ret_t duk_cmd(duk_context *duk) {
break;
case 2:
gameobject_delete(duk_get_int(duk, 1));
break;
case 3:
@ -96,7 +96,7 @@ duk_ret_t duk_cmd(duk_context *duk) {
break;
case 9:
sprite_delete(duk_to_int(duk, 1));
break;
case 10:
@ -225,18 +225,7 @@ duk_ret_t duk_loginfo(duk_context *duk) {
return 0;
}
duk_ret_t duk_make_sprite(duk_context *duk) {
int go = duk_to_int(duk, 0);
const char *path = duk_to_string(duk, 1);
cpVect pos = duk2vec2(duk, 2);
struct sprite *sp = make_sprite(get_gameobject_from_id(go));
sprite_loadtex(sp, path);
sp->pos[0] = pos.x;
sp->pos[1] = pos.y;
return 0;
}
duk_ret_t duk_set_body(duk_context *duk) {
int cmd = duk_to_int(duk, 0);
@ -272,6 +261,10 @@ duk_ret_t duk_set_body(duk_context *duk) {
go->flipy = duk_to_boolean(duk, 2);
break;
case 7:
cpBodySetMass(go->body, duk_to_number(duk, 2));
break;
}
return 0;
@ -312,17 +305,44 @@ duk_ret_t duk_q_body(duk_context *duk) {
return 0;
}
duk_ret_t duk_sprite(duk_context *duk) {
int cmd = duk_to_int(duk, 0);
int id = duk_to_int(duk, 1);
switch (cmd) {
case 0:
}
}
duk_ret_t duk_make_sprite(duk_context *duk) {
int go = duk_to_int(duk, 0);
const char *path = duk_to_string(duk, 1);
cpVect pos = duk2vec2(duk, 2);
int sprite = make_sprite(go);
struct sprite *sp = id2sprite(sprite);
sprite_loadtex(sp, path);
sp->pos[0] = pos.x;
sp->pos[1] = pos.y;
duk_push_int(duk, sprite);
return 1;
}
duk_ret_t duk_make_box2d(duk_context *duk) {
int go = duk_to_int(duk, 0);
cpVect size = duk2vec2(duk, 1);
cpVect offset = duk2vec2(duk, 2);
struct phys2d_box *box = Make2DBox(get_gameobject_from_id(go));
struct phys2d_box *box = Make2DBox(go);
box->w = size.x;
box->h = size.y;
box->offset[0] = offset.x;
box->offset[1] = offset.y;
phys2d_boxinit(box, get_gameobject_from_id(go));
phys2d_applybox(box);
return 0;
}
@ -332,12 +352,12 @@ duk_ret_t duk_make_circle2d(duk_context *duk) {
double radius = duk_to_number(duk, 1);
cpVect offset = duk2vec2(duk, 2);
struct phys2d_circle *circle = Make2DCircle(get_gameobject_from_id(go));
struct phys2d_circle *circle = Make2DCircle(go);
circle->radius = radius;
circle->offset[0] = offset.x;
circle->offset[1] = offset.y;
phys2d_circleinit(circle, get_gameobject_from_id(go));
phys2d_applycircle(circle);
return 0;
}
@ -369,6 +389,8 @@ duk_ret_t duk_anim(duk_context *duk) {
return 0;
}
duk_ret_t duk_anim_cmd(duk_context *duk) {
return 0;
}
@ -389,6 +411,9 @@ void ffi_load()
DUK_FUNC(make_gameobject, 7);
DUK_FUNC(set_body, 3);
DUK_FUNC(q_body, 2);
DUK_FUNC(sprite, 3);
DUK_FUNC(sys_cmd, 1);
DUK_FUNC(win_make, 3);

View file

@ -2,7 +2,6 @@
#include "shader.h"
#include "sprite.h"
#include "registry.h"
#include "2dphysics.h"
#include "script.h"
#include "input.h"
@ -16,6 +15,7 @@
#include "stb_ds.h"
struct gameobject *gameobjects = NULL;
struct gameobject *first = NULL;
const int nameBuf[MAXNAME] = { 0 };
const int prefabNameBuf[MAXNAME] = { 0 };
@ -26,6 +26,12 @@ struct gameobject *get_gameobject_from_id(int id)
return &gameobjects[id];
}
struct gameobject *id2go(int id) {
if (id < 0) return NULL;
return &gameobjects[id];
}
int id_from_gameobject(struct gameobject *go) {
for (int i = 0; i < arrlen(gameobjects); i++) {
if (&gameobjects[i] == go) return i;
@ -44,6 +50,7 @@ void gameobject_apply(struct gameobject *go)
static void gameobject_setpickcolor(struct gameobject *go)
{
/*
float r = ((go->editor.id & 0x000000FF) >> 0) / 255.f;
float g = ((go->editor.id & 0x0000FF00) >> 8) / 255.f;
float b = ((go->editor.id & 0x00FF0000) >> 16) / 255.f;
@ -51,57 +58,54 @@ static void gameobject_setpickcolor(struct gameobject *go)
go->editor.color[0] = r;
go->editor.color[1] = g;
go->editor.color[2] = b;
*/
}
int MakeGameobject()
{
if (gameobjects == NULL) arrsetcap(gameobjects, 50);
struct gameobject go = {
.editor.id = arrlen(gameobjects),
.scale = 1.f,
.bodytype = CP_BODY_TYPE_STATIC,
.mass = 1.f
};
gameobject_setpickcolor(&go);
strncpy(go.editor.mname, "New object", MAXNAME);
go.body = cpSpaceAddBody(space, cpBodyNew(go.mass, 1.f));
struct gameobject *tar;
int retid;
if (!first) {
arrput(gameobjects, go);
tar = &arrlast(gameobjects);
retid = arrlen(gameobjects)-1;
tar->id = retid;
} else {
cpBodySetUserData(go.body, &arrlast(gameobjects));
return arrlen(gameobjects)-1;
retid = first->id;
struct gameobject *next = first->next;
YughInfo("Created in slot %d.", retid);
tar = first;
*first = go;
first->id = retid;
first = next;
}
void gameobject_addcomponent(struct gameobject *go, struct component_interface *c)
{
struct component new;
new.ref = c;
new.data = c->make(go);
new.go = go;
arrput(go->components, new);
cpBodySetUserData(go.body, tar);
YughInfo("Made game object with ID ===== %d", retid);
return retid;
}
void gameobject_delete(int id)
{
YughInfo("Deleting gameobject with id %d.", id);
struct gameobject *go = &gameobjects[id];
for (int i = 0; i < arrlen(go->components); i++) {
comp_delete(&go->components[i]);
arrdel(go->components, i);
}
struct gameobject *go = id2go(id);
cpSpaceRemoveBody(space, go->body);
go->next = first;
first = go;
arrdelswap(gameobjects, id);
}
void gameobject_delcomponent(struct gameobject *go, int n)
{
comp_delete(&go->components[n]);
arrdel(go->components, n);
YughInfo("Did it to %d.", first->id);
}
void gameobject_save(struct gameobject *go, FILE * file)
@ -126,6 +130,7 @@ void gameobject_save(struct gameobject *go, FILE * file)
int gameobject_makefromprefab(char *path)
{
/*
FILE *fprefab = fopen(path, "rb");
if (fprefab == NULL) {
YughError("Could not find prefab %s.", path);
@ -143,6 +148,7 @@ int gameobject_makefromprefab(char *path)
arrlast(gameobjects).editor.id = arrlen(gameobjects)-1;
return arrlen(gameobjects)-1;
*/
}
void gameobject_init(struct gameobject *go, FILE * fprefab)
@ -180,6 +186,7 @@ void gameobject_init(struct gameobject *go, FILE * fprefab)
void gameobject_saveprefab(struct gameobject *go)
{
/*
char prefabfname[60] = { '\0' };
strncat(prefabfname, go->editor.prefabName, MAXNAME);
strncat(prefabfname, EXT_PREFAB, 10);
@ -188,6 +195,7 @@ void gameobject_saveprefab(struct gameobject *go)
fclose(pfile);
findPrefabs();
*/
}
@ -212,6 +220,7 @@ void gameobject_revertprefab(struct gameobject *go)
void toggleprefab(struct gameobject *go)
{
/*
go->editor.prefabSync = !go->editor.prefabSync;
if (go->editor.prefabSync) {
@ -220,11 +229,7 @@ void toggleprefab(struct gameobject *go)
} else {
go->editor.prefabName[0] = '\0';
}
}
int gameobject_ncomponents(struct gameobject *go)
{
return arrlen(go->components);
*/
}
void gameobject_move(struct gameobject *go, cpVect vec)
@ -261,6 +266,7 @@ void gameobject_setpos(struct gameobject *go, cpVect vec) {
void object_gui(struct gameobject *go)
{
/*
float temp_pos[2];
temp_pos[0] = cpBodyGetPosition(go->body).x;
temp_pos[1] = cpBodyGetPosition(go->body).y;
@ -321,16 +327,20 @@ void object_gui(struct gameobject *go)
if (n >= 0)
gameobject_delcomponent(go, n);
*/
}
void gameobject_draw_debugs() {
/*
YughInfo("DRAWING DEBUG");
for (int i = 0; i < arrlen(gameobjects); i++) {
YughInfo("Drawing this many ... %d", arrlen(gameobjects[i].components));
for (int j = 0; j < arrlen(gameobjects[i].components); j++) {
struct component *c = &gameobjects[i].components[j];
comp_draw_debug(c);
}
}
*/
}

View file

@ -7,7 +7,6 @@
#include <stdbool.h>
#include <chipmunk/chipmunk.h>
#include "2dphysics.h"
#include "registry.h"
struct shader;
struct sprite;
@ -25,7 +24,10 @@ struct editor {
};
struct gameobject {
union {
cpBodyType bodytype;
struct gameobject *next;
};
float scale;
float mass;
float f; /* friction */
@ -33,30 +35,22 @@ struct gameobject {
int flipx; /* 1 or -1 */
int flipy;
cpBody *body;
struct component *components;
int id;
struct phys_cbs cbs;
struct editor editor;
};
extern struct gameobject *gameobjects;
int MakeGameobject();
void gameobject_apply(struct gameobject *go);
void init_gameobjects();
void gameobject_delete(int id);
void clear_gameobjects();
int number_of_gameobjects();
void set_n_gameobjects(int n);
void toggleprefab(struct gameobject *go);
struct gameobject *get_gameobject_from_id(int id);
struct gameobject *id2go(int id);
int id_from_gameobject(struct gameobject *go);
void gameobject_save(struct gameobject *go, FILE * file);
void gameobject_addcomponent(struct gameobject *go, struct component_interface *c);
void gameobject_delcomponent(struct gameobject *go, int n);
void gameobject_loadcomponent(struct gameobject *go, int id);
int gameobject_ncomponents(struct gameobject *go);
void gameobject_saveprefab(struct gameobject *go);
int gameobject_makefromprefab(char *path);

View file

@ -24,6 +24,7 @@ void save_level(char name[MAXNAME])
void load_level(char name[MAXNAME])
{
/*
FILE *lfile = fopen(name, "rb");
if (!lfile) return;
@ -43,6 +44,7 @@ void load_level(char name[MAXNAME])
}
fclose(lfile);
*/
}
void new_level()

View file

@ -1,87 +0,0 @@
#include "registry.h"
#include "gameobject.h"
#include "2dphysics.h"
#include "editor.h"
#include "sprite.h"
#include "stb_ds.h"
struct component *components;
struct component_interface *interfaces;
void registry_init()
{
register_component("Sprite",
sizeof(struct sprite),
make_sprite,
sprite_delete,
sprite_io,
NULL,
sprite_gui,
sprite_init);
register_component("2D Circle Collider",
sizeof(struct phys2d_circle),
Make2DCircle,
phys2d_circledel,
NULL,
phys2d_dbgdrawcircle,
circle_gui,
phys2d_circleinit);
register_component("2D Segment", sizeof(struct phys2d_segment), Make2DSegment, phys2d_segdel, NULL, phys2d_dbgdrawseg, segment_gui, phys2d_seginit);
register_component("2D Box", sizeof(struct phys2d_box), Make2DBox, phys2d_boxdel, NULL, phys2d_dbgdrawbox, box_gui, phys2d_boxinit);
register_component("2D Polygon", sizeof(struct phys2d_poly), Make2DPoly, phys2d_polydel, NULL, phys2d_dbgdrawpoly, poly_gui,phys2d_polyinit);
register_component("2D Edge", sizeof(struct phys2d_edge), Make2DEdge, phys2d_edgedel, NULL, phys2d_dbgdrawedge, edge_gui, phys2d_edgeinit);
}
void register_component(const char *name, size_t size,
void (*make)(struct gameobject * go),
void (*delete)(void *data),
void (*io)(void *data, FILE *f, int read),
void(*draw_debug)(void *data),
void(*draw_gui)(void *data),
void(*init)(void *data, struct gameobject * go))
{
struct component_interface c;
c.name = name;
c.make = make;
c.io = io;
c.draw_debug = draw_debug;
c.draw_gui = draw_gui;
c.delete = delete;
c.init = init;
arrput(interfaces, c);
}
struct component comp_make(struct component_interface *interface)
{
struct component c;
c.data = interface->make(NULL);
c.ref = interface;
return c;
}
void comp_draw_debug(struct component *c) {
c->ref->draw_debug(c->data);
}
void comp_draw_gui(struct component *c) {
c->ref->draw_gui(c->data);
}
void comp_delete(struct component *c)
{
c->ref->delete(c->data);
}
void comp_init(struct component *c, struct gameobject *go)
{
c->ref->init(c->data, go);
}
void comp_io(struct component *c, FILE *f, int read)
{
c->ref->io(c->data, f, read);
}

View file

@ -1,46 +0,0 @@
#ifndef REGISTRY_H
#define REGISTRY_H
#include <stddef.h>
#include <stdio.h>
#include "config.h"
struct gameobject;
struct component_interface {
const char *name;
void *(*make)(struct gameobject * go); /* Called to create the component */
void (*io)(void *data, FILE *f, int read);
void (*draw_debug)(void *data); /* Draw debugging info in editor */
void (*draw_gui)(void *data);
void (*init)(void *data, struct gameobject * go);
void (*delete)(void *data);
};
struct component {
void *data;
struct gameobject *go;
struct component_interface *ref;
};
struct component comp_make(struct component_interface *interface);
void comp_draw_debug(struct component *c);
void comp_draw_gui(struct component *c);
void comp_delete(struct component *c);
void comp_init(struct component *c, struct gameobject *go);
void comp_io(struct component *c, FILE *f, int read);
void comp_update(struct component *c, struct gameobject *go);
void registry_init();
void register_component(const char *name, size_t size,
void (*make)(struct gameobject * go),
void (*delete)(void *data),
void (*io)(void *data, FILE *f, int read),
void(*draw_debug)(void *data),
void(*draw_gui)(void *data),
void(*init)(void *data, struct gameobject * go));
#endif

View file

@ -15,27 +15,42 @@
struct TextureOptions TEX_SPRITE = { 1, 0, 0 };
struct sprite *sprites;
struct sprite *first;
static uint32_t VBO;
struct sprite *make_sprite(struct gameobject *go)
int make_sprite(int go)
{
if (arrcap(sprites) == 0)
arrsetcap(sprites, 100);
YughInfo("Making sprite with gameobject %d", go);
struct sprite sprite = {
.color = {1.f, 1.f, 1.f},
.size = {1.f, 1.f},
.tex = texture_loadfromfile("ph.png") };
sprite_init(&sprite, go);
arrput(sprites, sprite);
.tex = texture_loadfromfile("ph.png"),
.go = go,
.next = NULL };
return &arrlast(sprites);
int ret;
if (!first) {
YughInfo("Making a brand new sprite.");
arrput(sprites, sprite);
arrlast(sprites).id = arrlen(sprites)-1;
return arrlen(sprites)-1;
} else {
YughInfo("Reusing a sprite slot.");
int slot = first->id;
struct sprite *next = first->next;
*first = sprite;
first->id = slot;
first = next;
return slot;
}
}
void sprite_init(struct sprite *sprite, struct gameobject *go)
{
sprite->go = go;
struct sprite *id2sprite(int id) {
return &sprites[id];
}
void sprite_io(struct sprite *sprite, FILE *f, int read)
@ -57,25 +72,20 @@ void sprite_io(struct sprite *sprite, FILE *f, int read)
}
}
void sprite_delete(struct sprite *sprite)
void sprite_delete(int id)
{
YughInfo("Attempting to delete sprite, address is %p.", sprite);
YughInfo("Number of sprites is %d.", arrlen(sprites));
for (int i = 0; i < arrlen(sprites); i++) {
YughInfo("Address of try sprite is %p.", &sprites[i]);
if (&sprites[i] == sprite) {
YughInfo("Deleted a sprite.");
arrdel(sprites, i);
return;
}
}
struct sprite *sp = id2sprite(id);
sp->go = -1;
sp->next = first;
first = sp;
}
void sprite_draw_all()
{
//shader_use(spriteShader);
for (int i = 0; i < arrlen(sprites); i++)
sprite_draw(&sprites[i]);
for (int i = 0; i < arrlen(sprites); i++) {
if (sprites[i].go >= 0) sprite_draw(&sprites[i]);
}
}
void sprite_loadtex(struct sprite *sprite, const char *path)
@ -155,19 +165,21 @@ void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], flo
void sprite_draw(struct sprite *sprite)
{
struct gameobject *go = id2go(sprite->go);
if (sprite->tex) {
cpVect cpos = cpBodyGetPosition(sprite->go->body);
cpVect cpos = cpBodyGetPosition(go->body);
float pos[2] = {cpos.x, cpos.y};
if (sprite->tex->opts.animation) {
float size[2];
//size[0] = sprite->tex->anim.dimensions[0] * sprite->go->scale;
//size[1] = sprite->tex->anim.dimensions[1] * sprite->go->scale;
tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, anim_get_rect(&sprite->anim));
//size[0] = sprite->tex->anim.dimensions[0] * go->scale;
//size[1] = sprite->tex->anim.dimensions[1] * go->scale;
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, anim_get_rect(&sprite->anim));
} else {
float size[2] = { sprite->size[0] * sprite->go->scale * sprite->go->flipx, sprite->size[1] * sprite->go->scale * sprite->go->flipy };
float size[2] = { sprite->size[0] * go->scale * go->flipx, sprite->size[1] * go->scale * go->flipy };
tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos, tex_get_rect(sprite->tex));
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, tex_get_rect(sprite->tex));
}
}
}

View file

@ -10,22 +10,21 @@ struct datastream;
struct gameobject;
struct Texture;
struct timer;
struct sprite {
mfloat_t pos[2];
mfloat_t size[2];
float rotation;
mfloat_t color[3];
int go;
int id;
struct TexAnimation anim;
struct gameobject *go;
struct Texture *tex;
struct sprite *next;
};
struct sprite *make_sprite(struct gameobject *go);
void sprite_delete(struct sprite *sprite);
void sprite_init(struct sprite *sprite, struct gameobject *go);
int make_sprite(int go);
struct sprite *id2sprite(int id);
void sprite_delete(int id);
void sprite_io(struct sprite *sprite, FILE *f, int read);
void sprite_loadtex(struct sprite *sprite, const char *path);
void sprite_settex(struct sprite *sprite, struct Texture *tex);