component rework

This commit is contained in:
John Alanbrook 2023-01-03 15:06:36 +00:00
parent 09036d075d
commit 7ce8cd22ad
6 changed files with 78 additions and 62 deletions

View file

@ -15,6 +15,10 @@
#include "log.h"
#include "registry.h"
register_component(0, 0, 0, 0, 0, 0, 0, 0, 0);
cpSpace *space = NULL;
float phys2d_gravity = -50.f;
@ -88,13 +92,26 @@ void circle_gui(struct phys2d_circle *circle)
phys2d_applycircle(circle);
}
void phys2d_dbgdrawcpcirc(cpCircleShape *c)
{
cpVect pos = cpBodyGetPosition(cpShapeGetBody(c));
cpVect offset = cpCircleShapeGetOffset(c);
float radius = cpCircleShapeGetRadius(c);
float d = sqrt(pow(offset.x, 2.f) + pow(offset.y, 2.f));
float a = atan2(offset.y, offset.x) + cpBodyGetAngle(cpShapeGetBody(c));
draw_circle(pos.x + (d * cos(a)), pos.y + (d*sin(a)), radius, 1);
}
void phys2d_dbgdrawcircle(struct phys2d_circle *circle)
{
phys2d_dbgdrawcpcirc((cpCircleShape *)circle->shape.shape);
/*
cpVect p = cpBodyGetPosition(circle->shape.go->body);
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);
draw_circle(p.x + (d * cos(a)), p.y + (d * sin(a)), cpCircleShapeGetRadius(circle->shape.shape), 1);
*/
}
struct phys2d_segment *Make2DSegment(struct gameobject *go)

View file

@ -68,12 +68,13 @@ int MakeGameobject()
return arrlen(gameobjects)-1;
}
void gameobject_addcomponent(struct gameobject *go, struct component *c)
void gameobject_addcomponent(struct gameobject *go, struct component_interface *c)
{
arrput(go->components, *c);
struct component *newc = &arrlast(go->components);
newc->go = go;
newc->data = newc->make(newc->go);
struct component new;
new.interface = c;
new.data = c->make(go);
new.go = go;
arrput(go->components, c);
}
void gameobject_delete(int id)

View file

@ -541,7 +541,9 @@ s7_pointer s7_make_circ2d(s7_scheme *sc, s7_pointer args) {
circle->offset[0] = offset.x;
circle->offset[1] = offset.y;
phys2d_applycircle(circle);
phys2d_circleinit(circle, get_gameobject_from_id(go));
return s7_make_integer(sc, get_gameobject_from_id(go));
}
#define S7_FUNC(NAME, ARGS) s7_define_function(s7, #NAME, s7_ ##NAME, ARGS, 0, 0, "")

View file

@ -3,25 +3,14 @@
#include "2dphysics.h"
#include "editor.h"
#include "sprite.h"
#include "stb_ds.h"
struct component components[MAXNAME] = { 0 };
int ncomponent = 0;
#define REGISTER_COMP(NAME) register_component(#NAME, sizeof(struct NAME), make_NAME, dbgdraw_NAME, NAME_gui, NAME_init,
struct component *components;
struct component_interface *interfaces;
void registry_init()
{
/*
REGISTER_COMP(sprite);
REGISTER_COMP(2d_circle);
REGISTER_COMP(2d_segment);
REGISTER_COMP(2d_box);
REGISTER_COMP(2d_polygon);
REGISTER_COMP(2d_edge);
*/
register_component("Sprite",
sizeof(struct sprite),
make_sprite,
@ -40,41 +29,59 @@ void registry_init()
circle_gui,
phys2d_circleinit);
register_component("2D Segment", sizeof(struct phys2d_segment), phys2d_segdel, NULL, Make2DSegment, phys2d_dbgdrawseg, segment_gui, phys2d_seginit);
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, struct component * c),
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 *c = &components[ncomponent++];
c->name = name;
c->make = make;
c->io = io;
c->draw_debug = draw_debug;
c->draw_gui = draw_gui;
c->init = init;
c->data = NULL;
c->delete = delete;
c->id = ncomponent - 1;
c->datasize = size;
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->draw_debug(c->data);
c->ref->draw_debug(c->data);
}
void comp_draw_gui(struct component *c) {
c->draw_gui(c->data);
c->ref->draw_gui(c->data);
}
void c_draw_debug(struct compref *c) {
c->ref->draw_debug(c->data);
void comp_delete(struct component *c)
{
c->ref->delete(c->data);
}
void comp_init(struct component *c)
{
c->ref->init(c->data);
}
void comp_io(struct component *c, int read)
{
c->ref->io(c->data, read);
}

View file

@ -8,30 +8,29 @@
struct gameobject;
struct compref {
void *data;
struct component *ref;
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 {
const char *name;
void *(*make)(struct gameobject * go); /* Called to create the component */
void (*io)(void *data, FILE *f, int read); /* Pulls data from a component file into the component */
void *data;
struct gameobject *go;
void (*draw_debug)(void *data); /* Draw debugging info in editor */
void (*draw_gui)(void *data); /* Use to draw GUI for editing the component in editor */
void (*delete)(void *data); /* Deletes and cleans up component */
int id;
int datasize;
void (*init)(void *data, struct gameobject * go); /* Inits the component */
struct component_interface *ref;
};
extern struct component components[MAXNAME];
extern int ncomponent;
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);
void comp_io(struct component *c, int read);
void comp_update(struct component *c, struct gameobject *go);

View file

@ -14,8 +14,6 @@
#include "yugine.h"
#include "2dphysics.h"
#include "parson.h"
#if ED
#include "editor.h"
#endif
@ -152,14 +150,6 @@ int main(int argc, char **args) {
engine_init();
JSON_Value *rv = json_value_init_object();
JSON_Object *ro = json_value_get_object(rv);
json_object_set_string(ro, "name", "yugine");
json_object_set_number(ro, "age", 30);
char *serialized = json_serialize_to_file(rv, "test2.json");
json_value_free(rv);
const GLFWvidmode *vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
YughInfo("Refresh rate is %d", vidmode->refreshRate);