From 5823947fb42e94a4011f13cc7c5b42f68df27666 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Wed, 17 Aug 2022 05:01:51 +0000 Subject: [PATCH] engine --- source/engine/2dphysics.c | 44 +++++++++++++++++--------------------- source/engine/2dphysics.h | 2 +- source/engine/editor.c | 8 +++---- source/engine/gameobject.c | 17 ++++++++------- source/engine/nuke.c | 18 +++++++++++----- source/engine/nuke.h | 2 ++ source/engine/registry.c | 26 +++++++--------------- source/engine/sprite.c | 2 +- 8 files changed, 58 insertions(+), 61 deletions(-) diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index aff3b2a7..97ea91d6 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -5,6 +5,11 @@ #include "mathc.h" #include "nuke.h" +#include "debugdraw.h" +#include "gameobject.h" +#include +#include + cpBody *ballBody = NULL; cpSpace *space = NULL; float phys2d_gravity = -50.f; @@ -49,13 +54,10 @@ struct phys2d_circle *Make2DCircle(struct mGameObject *go) return new; } -void phys2d_circleinit(struct phys2d_circle *circle, - struct mGameObject *go) +void phys2d_circleinit(struct phys2d_circle *circle, struct mGameObject *go) { - circle->shape.shape = - cpSpaceAddShape(space, - cpCircleShapeNew(go->body, circle->radius, - cpvzero)); + printf("Initing a circle\n"); + circle->shape.shape = cpSpaceAddShape(space, cpCircleShapeNew(go->body, circle->radius, cpvzero)); init_phys2dshape(&circle->shape, go); } @@ -67,6 +69,15 @@ void circle_gui(struct phys2d_circle *circle) phys2d_applycircle(circle); } +void phys2d_dbgdrawcircle(struct phys2d_circle *circle) +{ + 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 mGameObject *go) { struct phys2d_segment *new = malloc(sizeof(struct phys2d_segment)); @@ -83,10 +94,7 @@ struct phys2d_segment *Make2DSegment(struct mGameObject *go) void phys2d_seginit(struct phys2d_segment *seg, struct mGameObject *go) { - seg->shape.shape = - cpSpaceAddShape(space, - cpSegmentShapeNew(go->body, cpvzero, cpvzero, - seg->thickness)); + seg->shape.shape = cpSpaceAddShape(space, cpSegmentShapeNew(go->body, cpvzero, cpvzero, seg->thickness)); init_phys2dshape(&seg->shape, go); } @@ -127,7 +135,6 @@ void box_gui(struct phys2d_box *box) nk_property_float(ctx, "Width", 0.f, &box->w, 1000.f, 1.f, 1.f); nk_property_float(ctx, "Height", 0.f, &box->h, 1000.f, 1.f, 1.f); nk_property_float2(ctx, "Offset", 0.f, &box->offset, 1.f, 0.01f, 0.01f); - nk_property_float(ctx, "Radius", 0.f, &box->r, 100.f, 1.f, 0.1f); phys2d_applybox(box); } @@ -255,10 +262,7 @@ void edge_gui(struct phys2d_edge *edge) phys2d_applyedge(edge); } -#include "debugdraw.h" -#include "gameobject.h" -#include -#include + void phys2d_applycircle(struct phys2d_circle *circle) { @@ -332,15 +336,7 @@ void phys2d_applyedge(struct phys2d_edge *edge) } } -void phys2d_dbgdrawcircle(struct phys2d_circle *circle) -{ - 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); -} + void phys2d_dbgdrawseg(struct phys2d_segment *seg) { diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index c952dbc3..75ba2c38 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -32,8 +32,8 @@ struct phys2d_segment { struct phys2d_box { float w; float h; - float r; float offset[2]; + float r; struct phys2d_shape shape; }; diff --git a/source/engine/editor.c b/source/engine/editor.c index 1bf87e13..bfe2334c 100644 --- a/source/engine/editor.c +++ b/source/engine/editor.c @@ -963,8 +963,9 @@ void game_stop() { physOn = 0; } void game_pause() { physOn = 0; } void sprite_gui(struct mSprite *sprite) { + nuke_nel(2); nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", sprite->tex->path); - // ImGui::SameLine(); + if (nk_button_label(ctx, "Load texture") && selected_asset != NULL) { sprite_loadtex(sprite, selected_asset->filename); @@ -978,10 +979,9 @@ void sprite_gui(struct mSprite *sprite) { // if (ImGui::ImageButton ((void *) (intptr_t) sprite->tex->id, ImVec2(50, // 50))) { } - nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, - 0.01f); + nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, 0.01f); - nk_layout_row_dynamic(ctx, 25, 3); + nuke_nel(3); if (nk_button_label(ctx, "C")) { sprite->pos[0] = -0.5f; sprite->pos[1] = -0.5f; diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 51e3c546..b30cd0bb 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -89,10 +89,9 @@ void gameobject_save(struct mGameObject *go, FILE * file) fwrite(go, sizeof(*go), 1, file); vec_store(go->components, file); - for (int i = 0; i < go->components->len; i++) { struct component *c = vec_get(go->components, i); - fwrite(c, c->datasize, 1, file); + fwrite(c->data, c->datasize, 1, file); } } @@ -122,8 +121,8 @@ void gameobject_init(struct mGameObject *go, FILE * fprefab) vec_load(go->components, fprefab); for (int i = 0; i < go->components->len; i++) { - struct component *newc = vec_set(go->components, i, &components[((struct component *) vec_get(go->components, i))->id]); - + vec_set(go->components, i, &components[((struct component *) vec_get(go->components, i))->id]); + struct component *newc = vec_get(go->components, i); newc->go = go; newc->data = malloc(newc->datasize); fread(newc->data, newc->datasize, 1, fprefab); @@ -210,19 +209,22 @@ void object_gui(struct mGameObject *go) draw_point(temp_pos[0], temp_pos[1], 3); - nk_property_float2(ctx, "Position", 0.f, temp_pos, 1.f, 0.01f, 0.01f); + nk_property_float2(ctx, "Position", -1000000.f, temp_pos, 1000000.f, 1.f, 0.5f); cpVect tvect = { temp_pos[0], temp_pos[1] }; cpBodySetPosition(go->body, tvect); float mtry = cpBodyGetAngle(go->body); float modtry = fmodf(mtry * RAD2DEGS, 360.f); + if (modtry < 0.f) + modtry += 360.f; + float modtry2 = modtry; nk_property_float(ctx, "Angle", -1000.f, &modtry, 1000.f, 0.5f, 0.5f); modtry -= modtry2; cpBodySetAngle(go->body, mtry + (modtry * DEG2RADS)); - nk_property_float(ctx, "Scale", 0.f, &go->scale, 1000.f, 0.01f, 0.001f); + nk_property_float(ctx, "Scale", 0.f, &go->scale, 1000.f, 0.01f, go->scale * 0.01f); nk_layout_row_dynamic(ctx, 25, 3); nk_radio_button_label(ctx, "Static", &go->bodytype, CP_BODY_TYPE_STATIC); @@ -242,8 +244,7 @@ void object_gui(struct mGameObject *go) int n = -1; for (int i = 0; i < go->components->len; i++) { - struct component *c = - (struct component *) vec_get(go->components, i); + struct component *c = vec_get(go->components, i); if (c->draw_debug) c->draw_debug(c->data); diff --git a/source/engine/nuke.c b/source/engine/nuke.c index a5b294eb..216439ae 100644 --- a/source/engine/nuke.c +++ b/source/engine/nuke.c @@ -47,19 +47,27 @@ void nk_property_float3(struct nk_context *ctx, const char *label, float min, fl nk_layout_row_dynamic(ctx, 25, 1); nk_label(ctx, label, NK_TEXT_LEFT); nk_layout_row_dynamic(ctx, 25, 3); - nk_property_float(ctx, "X", min, &val[0], max, step, dragstep); - nk_property_float(ctx, "Y", min, &val[1], max, step, dragstep); - nk_property_float(ctx, "Z", min, &val[2], max, step, dragstep); + nk_property_float(ctx, "#X", min, &val[0], max, step, dragstep); + nk_property_float(ctx, "#Y", min, &val[1], max, step, dragstep); + nk_property_float(ctx, "#Z", min, &val[2], max, step, dragstep); } void nk_property_float2(struct nk_context *ctx, const char *label, float min, float *val, float max, float step, float dragstep) { nk_layout_row_dynamic(ctx, 25, 1); nk_label(ctx, label, NK_TEXT_LEFT); nk_layout_row_dynamic(ctx, 25, 2); - nk_property_float(ctx, "X", min, &val[0], max, step, dragstep); - nk_property_float(ctx, "Y", min, &val[1], max, step, dragstep); + nk_property_float(ctx, "#X", min, &val[0], max, step, dragstep); + nk_property_float(ctx, "#Y", min, &val[1], max, step, dragstep); } void nk_radio_button_label(struct nk_context *ctx, const char *label, int *val, int cmp) { if (nk_option_label(ctx, label, (bool)*val == cmp)) *val = cmp; +} + +void nuke_nel(int cols) { + nk_layout_row_dynamic(ctx, 25, cols); +} + +void nuke_label(const char *s) { + } \ No newline at end of file diff --git a/source/engine/nuke.h b/source/engine/nuke.h index c0e87f5b..9994acc4 100644 --- a/source/engine/nuke.h +++ b/source/engine/nuke.h @@ -15,5 +15,7 @@ void nuke_end(); void nk_property_float3(struct nk_context *ctx, const char *label, float min, float *val, float max, float step, float dragstep); void nk_property_float2(struct nk_context *ctx, const char *label, float min, float *val, float max, float step, float dragstep); void nk_radio_button_label(struct nk_context *ctx, const char *label, int *val, int cmp); +void nuke_nel(int cols); +void nuke_label(const char *s); #endif \ No newline at end of file diff --git a/source/engine/registry.c b/source/engine/registry.c index 973ea25f..021e72e0 100644 --- a/source/engine/registry.c +++ b/source/engine/registry.c @@ -8,7 +8,7 @@ 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, +#define REGISTER_COMP(NAME) register_component(#NAME, sizeof(struct NAME), make_NAME, dbgdraw_NAME, NAME_gui, NAME_init, void registry_init() { @@ -21,26 +21,16 @@ void registry_init() REGISTER_COMP(2d_edge); */ - register_component("Sprite", sizeof(struct mSprite), &MakeSprite, NULL, - &sprite_gui, &sprite_init); - register_component("2D Circle Collider", sizeof(struct phys2d_circle), - &Make2DCircle, &phys2d_dbgdrawcircle, &circle_gui, - &phys2d_circleinit); - register_component("2D Segment", sizeof(struct phys2d_segment), - &Make2DSegment, &phys2d_dbgdrawseg, &segment_gui, - &phys2d_seginit); - register_component("2D Box", sizeof(struct phys2d_box), &Make2DBox, - &phys2d_dbgdrawbox, &box_gui, &phys2d_boxinit); - register_component("2D Polygon", sizeof(struct phys2d_poly), - &Make2DPoly, &phys2d_dbgdrawpoly, &poly_gui, - &phys2d_polyinit); - register_component("2D Edge", sizeof(struct phys2d_edge), &Make2DEdge, - &phys2d_dbgdrawedge, &edge_gui, &phys2d_edgeinit); + register_component("Sprite", sizeof(struct mSprite), MakeSprite, NULL, sprite_gui, sprite_init); + register_component("2D Circle Collider", sizeof(struct phys2d_circle), Make2DCircle, phys2d_dbgdrawcircle, circle_gui, phys2d_circleinit); + register_component("2D Segment", sizeof(struct phys2d_segment), Make2DSegment, phys2d_dbgdrawseg, segment_gui, phys2d_seginit); + register_component("2D Box", sizeof(struct phys2d_box), Make2DBox, phys2d_dbgdrawbox, box_gui, phys2d_boxinit); + register_component("2D Polygon", sizeof(struct phys2d_poly), Make2DPoly, phys2d_dbgdrawpoly, poly_gui,phys2d_polyinit); + register_component("2D Edge", sizeof(struct phys2d_edge), Make2DEdge, phys2d_dbgdrawedge, edge_gui, phys2d_edgeinit); } void register_component(const char *name, size_t size, - void (*make)(struct mGameObject * go, - struct component * c), + void (*make)(struct mGameObject * go, struct component * c), void(*draw_debug)(void *data), void(*draw_gui)(void *data), void(*init)(void *data, struct mGameObject * go)) diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 196b8312..00529e61 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -40,7 +40,7 @@ struct mSprite *MakeSprite(struct mGameObject *go) sprite->pos[1] = 0.f; sprite->size[0] = 1.f; sprite->size[1] = 1.f; - sprite->tex = NULL; + sprite->tex = texture_loadfromfile("ph.png"); sprite_init(sprite, go); sprite->index = sprites.last; return sprite;