This commit is contained in:
John Alanbrook 2022-08-17 05:01:51 +00:00
parent 7aef7afc90
commit 5823947fb4
8 changed files with 58 additions and 61 deletions

View file

@ -5,6 +5,11 @@
#include "mathc.h" #include "mathc.h"
#include "nuke.h" #include "nuke.h"
#include "debugdraw.h"
#include "gameobject.h"
#include <math.h>
#include <chipmunk/chipmunk_unsafe.h>
cpBody *ballBody = NULL; cpBody *ballBody = NULL;
cpSpace *space = NULL; cpSpace *space = NULL;
float phys2d_gravity = -50.f; float phys2d_gravity = -50.f;
@ -49,13 +54,10 @@ struct phys2d_circle *Make2DCircle(struct mGameObject *go)
return new; return new;
} }
void phys2d_circleinit(struct phys2d_circle *circle, void phys2d_circleinit(struct phys2d_circle *circle, struct mGameObject *go)
struct mGameObject *go)
{ {
circle->shape.shape = printf("Initing a circle\n");
cpSpaceAddShape(space, circle->shape.shape = cpSpaceAddShape(space, cpCircleShapeNew(go->body, circle->radius, cpvzero));
cpCircleShapeNew(go->body, circle->radius,
cpvzero));
init_phys2dshape(&circle->shape, go); init_phys2dshape(&circle->shape, go);
} }
@ -67,6 +69,15 @@ void circle_gui(struct phys2d_circle *circle)
phys2d_applycircle(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 *Make2DSegment(struct mGameObject *go)
{ {
struct phys2d_segment *new = malloc(sizeof(struct phys2d_segment)); 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) void phys2d_seginit(struct phys2d_segment *seg, struct mGameObject *go)
{ {
seg->shape.shape = seg->shape.shape = cpSpaceAddShape(space, cpSegmentShapeNew(go->body, cpvzero, cpvzero, seg->thickness));
cpSpaceAddShape(space,
cpSegmentShapeNew(go->body, cpvzero, cpvzero,
seg->thickness));
init_phys2dshape(&seg->shape, go); 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, "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_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_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); phys2d_applybox(box);
} }
@ -255,10 +262,7 @@ void edge_gui(struct phys2d_edge *edge)
phys2d_applyedge(edge); phys2d_applyedge(edge);
} }
#include "debugdraw.h"
#include "gameobject.h"
#include <math.h>
#include <chipmunk/chipmunk_unsafe.h>
void phys2d_applycircle(struct phys2d_circle *circle) 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) void phys2d_dbgdrawseg(struct phys2d_segment *seg)
{ {

View file

@ -32,8 +32,8 @@ struct phys2d_segment {
struct phys2d_box { struct phys2d_box {
float w; float w;
float h; float h;
float r;
float offset[2]; float offset[2];
float r;
struct phys2d_shape shape; struct phys2d_shape shape;
}; };

View file

@ -963,8 +963,9 @@ void game_stop() { physOn = 0; }
void game_pause() { physOn = 0; } void game_pause() { physOn = 0; }
void sprite_gui(struct mSprite *sprite) { void sprite_gui(struct mSprite *sprite) {
nuke_nel(2);
nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", sprite->tex->path); nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", sprite->tex->path);
// ImGui::SameLine();
if (nk_button_label(ctx, "Load texture") && selected_asset != NULL) { if (nk_button_label(ctx, "Load texture") && selected_asset != NULL) {
sprite_loadtex(sprite, selected_asset->filename); 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, // if (ImGui::ImageButton ((void *) (intptr_t) sprite->tex->id, ImVec2(50,
// 50))) { // 50))) {
} }
nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, 0.01f);
0.01f);
nk_layout_row_dynamic(ctx, 25, 3); nuke_nel(3);
if (nk_button_label(ctx, "C")) { if (nk_button_label(ctx, "C")) {
sprite->pos[0] = -0.5f; sprite->pos[0] = -0.5f;
sprite->pos[1] = -0.5f; sprite->pos[1] = -0.5f;

View file

@ -89,10 +89,9 @@ void gameobject_save(struct mGameObject *go, FILE * file)
fwrite(go, sizeof(*go), 1, file); fwrite(go, sizeof(*go), 1, file);
vec_store(go->components, file); vec_store(go->components, file);
for (int i = 0; i < go->components->len; i++) { for (int i = 0; i < go->components->len; i++) {
struct component *c = vec_get(go->components, 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); vec_load(go->components, fprefab);
for (int i = 0; i < go->components->len; i++) { 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->go = go;
newc->data = malloc(newc->datasize); newc->data = malloc(newc->datasize);
fread(newc->data, newc->datasize, 1, fprefab); 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); 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] }; cpVect tvect = { temp_pos[0], temp_pos[1] };
cpBodySetPosition(go->body, tvect); cpBodySetPosition(go->body, tvect);
float mtry = cpBodyGetAngle(go->body); float mtry = cpBodyGetAngle(go->body);
float modtry = fmodf(mtry * RAD2DEGS, 360.f); float modtry = fmodf(mtry * RAD2DEGS, 360.f);
if (modtry < 0.f)
modtry += 360.f;
float modtry2 = modtry; float modtry2 = modtry;
nk_property_float(ctx, "Angle", -1000.f, &modtry, 1000.f, 0.5f, 0.5f); nk_property_float(ctx, "Angle", -1000.f, &modtry, 1000.f, 0.5f, 0.5f);
modtry -= modtry2; modtry -= modtry2;
cpBodySetAngle(go->body, mtry + (modtry * DEG2RADS)); 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_layout_row_dynamic(ctx, 25, 3);
nk_radio_button_label(ctx, "Static", &go->bodytype, CP_BODY_TYPE_STATIC); 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; int n = -1;
for (int i = 0; i < go->components->len; i++) { for (int i = 0; i < go->components->len; i++) {
struct component *c = struct component *c = vec_get(go->components, i);
(struct component *) vec_get(go->components, i);
if (c->draw_debug) if (c->draw_debug)
c->draw_debug(c->data); c->draw_debug(c->data);

View file

@ -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_layout_row_dynamic(ctx, 25, 1);
nk_label(ctx, label, NK_TEXT_LEFT); nk_label(ctx, label, NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 25, 3); nk_layout_row_dynamic(ctx, 25, 3);
nk_property_float(ctx, "X", min, &val[0], 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, "#Y", min, &val[1], max, step, dragstep);
nk_property_float(ctx, "Z", min, &val[2], 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) { 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_layout_row_dynamic(ctx, 25, 1);
nk_label(ctx, label, NK_TEXT_LEFT); nk_label(ctx, label, NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 25, 2); nk_layout_row_dynamic(ctx, 25, 2);
nk_property_float(ctx, "X", min, &val[0], 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, "#Y", min, &val[1], max, step, dragstep);
} }
void nk_radio_button_label(struct nk_context *ctx, const char *label, int *val, int cmp) { 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; 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) {
}

View file

@ -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_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_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 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 #endif

View file

@ -8,7 +8,7 @@ struct component components[MAXNAME] = { 0 };
int ncomponent = 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() void registry_init()
{ {
@ -21,26 +21,16 @@ void registry_init()
REGISTER_COMP(2d_edge); REGISTER_COMP(2d_edge);
*/ */
register_component("Sprite", sizeof(struct mSprite), &MakeSprite, NULL, register_component("Sprite", sizeof(struct mSprite), MakeSprite, NULL, sprite_gui, sprite_init);
&sprite_gui, &sprite_init); register_component("2D Circle Collider", sizeof(struct phys2d_circle), Make2DCircle, phys2d_dbgdrawcircle, circle_gui, phys2d_circleinit);
register_component("2D Circle Collider", sizeof(struct phys2d_circle), register_component("2D Segment", sizeof(struct phys2d_segment), Make2DSegment, phys2d_dbgdrawseg, segment_gui, phys2d_seginit);
&Make2DCircle, &phys2d_dbgdrawcircle, &circle_gui, register_component("2D Box", sizeof(struct phys2d_box), Make2DBox, phys2d_dbgdrawbox, box_gui, phys2d_boxinit);
&phys2d_circleinit); register_component("2D Polygon", sizeof(struct phys2d_poly), Make2DPoly, phys2d_dbgdrawpoly, poly_gui,phys2d_polyinit);
register_component("2D Segment", sizeof(struct phys2d_segment), register_component("2D Edge", sizeof(struct phys2d_edge), Make2DEdge, phys2d_dbgdrawedge, edge_gui, phys2d_edgeinit);
&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 register_component(const char *name, size_t size,
void (*make)(struct mGameObject * go, void (*make)(struct mGameObject * go, struct component * c),
struct component * c),
void(*draw_debug)(void *data), void(*draw_debug)(void *data),
void(*draw_gui)(void *data), void(*draw_gui)(void *data),
void(*init)(void *data, struct mGameObject * go)) void(*init)(void *data, struct mGameObject * go))

View file

@ -40,7 +40,7 @@ struct mSprite *MakeSprite(struct mGameObject *go)
sprite->pos[1] = 0.f; sprite->pos[1] = 0.f;
sprite->size[0] = 1.f; sprite->size[0] = 1.f;
sprite->size[1] = 1.f; sprite->size[1] = 1.f;
sprite->tex = NULL; sprite->tex = texture_loadfromfile("ph.png");
sprite_init(sprite, go); sprite_init(sprite, go);
sprite->index = sprites.last; sprite->index = sprites.last;
return sprite; return sprite;