This commit is contained in:
John Alanbrook 2022-08-29 03:34:33 +00:00
parent c56e2841b6
commit 81986d6bad
8 changed files with 113 additions and 22 deletions

View file

@ -42,6 +42,11 @@ void init_phys2dshape(struct phys2d_shape *shape, struct mGameObject *go)
phys2d_shape_apply(shape);
}
void phys2d_shape_del(struct phys2d_shape *shape)
{
cpSpaceRemoveShape(space, shape->shape);
}
struct phys2d_circle *Make2DCircle(struct mGameObject *go)
{
struct phys2d_circle *new = malloc(sizeof(struct phys2d_circle));
@ -61,6 +66,11 @@ void phys2d_circleinit(struct phys2d_circle *circle, struct mGameObject *go)
init_phys2dshape(&circle->shape, go);
}
void phys2d_circledel(struct phys2d_circle *c)
{
phys2d_shape_del(&c->shape);
}
void circle_gui(struct phys2d_circle *circle)
{
nk_property_float(ctx, "Radius", 1.f, &circle->radius, 10000.f, 1.f, 1.f);
@ -98,6 +108,11 @@ void phys2d_seginit(struct phys2d_segment *seg, struct mGameObject *go)
init_phys2dshape(&seg->shape, go);
}
void phys2d_segdel(struct phys2d_segment *seg)
{
phys2d_shape_del(&seg->shape);
}
void segment_gui(struct phys2d_segment *seg)
{
nk_property_float2(ctx, "a", 0.f, seg->a, 1.f, 0.01f, 0.01f);
@ -130,6 +145,11 @@ void phys2d_boxinit(struct phys2d_box *box, struct mGameObject *go)
phys2d_applybox(box);
}
void phys2d_boxdel(struct phys2d_box *box)
{
phys2d_shape_del(&box->shape);
}
void box_gui(struct phys2d_box *box)
{
nk_property_float(ctx, "Width", 0.f, &box->w, 1000.f, 1.f, 1.f);
@ -164,6 +184,11 @@ void phys2d_polyinit(struct phys2d_poly *poly, struct mGameObject *go)
phys2d_applypoly(poly);
}
void phys2d_polydel(struct phys2d_poly *poly)
{
phys2d_shape_del(&poly->shape);
}
void phys2d_polyaddvert(struct phys2d_poly *poly)
{
poly->n++;
@ -215,6 +240,11 @@ void phys2d_edgeinit(struct phys2d_edge *edge, struct mGameObject *go)
phys2d_applyedge(edge);
}
void phys2d_edgedel(struct phys2d_edge *edge)
{
phys2d_shape_del(&edge->shape);
}
void phys2d_edgeshapeapply(struct phys2d_shape *mshape, cpShape * shape)
{
cpShapeSetFriction(shape, mshape->go->f);

View file

@ -53,26 +53,29 @@ struct phys2d_poly {
};
struct phys2d_circle *Make2DCircle(struct mGameObject *go);
void phys2d_circleinit(struct phys2d_circle *circle,
struct mGameObject *go);
void phys2d_circleinit(struct phys2d_circle *circle, struct mGameObject *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 mGameObject *go);
void phys2d_seginit(struct phys2d_segment *seg, struct mGameObject *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 mGameObject *go);
void phys2d_boxinit(struct phys2d_box *box, struct mGameObject *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 mGameObject *go);
void phys2d_polyinit(struct phys2d_poly *poly, struct mGameObject *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);
@ -80,6 +83,7 @@ void poly_gui(struct phys2d_poly *poly);
struct phys2d_edge *Make2DEdge(struct mGameObject *go);
void phys2d_edgeinit(struct phys2d_edge *edge, struct mGameObject *go);
void phys2d_edgedel(struct phys2d_edge *edge);
void phys2d_applyedge(struct phys2d_edge *edge);
void phys2d_edgeshapeapply(struct phys2d_shape *mshape, cpShape * shape);
void phys2d_dbgdrawedge(struct phys2d_edge *edge);

View file

@ -494,9 +494,10 @@ void editor_project_gui() {
}
*/
NK_MENU_START(level)
nk_layout_row_dynamic(ctx,25,3);
nuke_nel(1);
nk_labelf(ctx, "Current level: %s", current_level[0] == '\0' ? "No level loaded." : current_level);
nuke_nel(3);
if (nk_button_label(ctx, "New")) {
new_level();
current_level[0] = '\0';
@ -514,10 +515,10 @@ void editor_project_gui() {
levelname[0] = '\0';
get_levels();
}
nuke_nel(1);
nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, levelname, MAXNAME - 1, nk_filter_default);
nk_layout_row_dynamic(ctx,25,1);
vec_walk(levels, editor_level_btn);
NK_MENU_END()
@ -649,13 +650,14 @@ void editor_project_gui() {
NK_FORCE_END()
NK_FORCE(prefab)
nuke_nel(1);
nk_layout_row_dynamic(ctx, 25, 1);
vec_walk(prefabs, editor_prefab_btn);
NK_FORCE_END()
NK_MENU_START(assets)
nk_layout_row_dynamic(ctx,25,1);
nuke_nel(1);
editor.asset_srch = nk_edit_string_zero_terminated(ctx, NK_EDIT_SIMPLE, asset_search_buffer, 100, nk_filter_ascii);
@ -990,6 +992,7 @@ void get_levels() { fill_extensions(levels, DATA_PATH, EXT_LEVEL); }
void editor_prefab_btn(char *prefab) {
if (nk_button_label(ctx, prefab)) {
printf("making prefab\n");
gameobject_makefromprefab(prefab);
/*GameObject* newprefab = (GameObject*)createPrefab(*prefab); */
/*cam_inverse_goto(&camera, &newprefab->transform); */

View file

@ -36,6 +36,7 @@ static void gameobject_setpickcolor(struct mGameObject *go)
struct mGameObject *MakeGameobject()
{
printf("Making gameobject\n");
struct mGameObject go = {
.editor.id = arrlen(gameobjects),
.transform.scale = 1.f,
@ -75,6 +76,7 @@ void gameobject_delete(int id)
void gameobject_delcomponent(struct mGameObject *go, int n)
{
go->components[n].delete(go->components[n].data);
arrdel(go->components, n);
}
@ -98,9 +100,13 @@ void gameobject_save(struct mGameObject *go, FILE * file)
int n = arrlen(go->components);
fwrite(&n, sizeof(n), 1, file);
for (int i = 0; i < arrlen(go->components); i++) {
fwrite(go->components[i].id, sizeof(int), 1, file);
for (int i = 0; i < n; i++) {
fwrite(&go->components[i].id, sizeof(int), 1, file);
if (go->components[i].io == NULL)
fwrite(go->components[i].data, go->components[i].datasize, 1, file);
else
go->components[i].io(go->components[i].data, file, 0);
}
}
@ -118,9 +124,6 @@ void gameobject_makefromprefab(char *path)
gameobject_init(new, fprefab);
fclose(fprefab);
new->editor.id = arrlen(gameobjects);
arrput(gameobjects, *new);
}
void gameobject_init(struct mGameObject *go, FILE * fprefab)
@ -138,7 +141,12 @@ void gameobject_init(struct mGameObject *go, FILE * fprefab)
struct component *newc = &go->components[i];
newc->go = go;
newc->data = malloc(newc->datasize);
if (newc->io == NULL)
fread(newc->data, newc->datasize, 1, fprefab);
else
newc->io(newc->data, fprefab, 1);
newc->init(newc->data, go);
}
}

View file

@ -21,16 +21,34 @@ void registry_init()
REGISTER_COMP(2d_edge);
*/
register_component("Sprite", sizeof(struct mSprite), MakeSprite, sprite_delete, NULL, sprite_gui, sprite_init);
register_component("2D Circle Collider", sizeof(struct phys2d_circle), Make2DCircle, NULL, phys2d_dbgdrawcircle, circle_gui, phys2d_circleinit);
register_component("2D Segment", sizeof(struct phys2d_segment), NULL, Make2DSegment, phys2d_dbgdrawseg, segment_gui, phys2d_seginit);
register_component("2D Box", sizeof(struct phys2d_box), Make2DBox, NULL, phys2d_dbgdrawbox, box_gui, phys2d_boxinit);
register_component("2D Polygon", sizeof(struct phys2d_poly), Make2DPoly, NULL, phys2d_dbgdrawpoly, poly_gui,phys2d_polyinit);
register_component("2D Edge", sizeof(struct phys2d_edge), Make2DEdge, NULL, phys2d_dbgdrawedge, edge_gui, phys2d_edgeinit);
register_component("Sprite",
sizeof(struct mSprite),
MakeSprite,
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), phys2d_segdel, NULL, Make2DSegment, 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 mGameObject * go, struct component * c),
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 mGameObject * go))
@ -38,10 +56,12 @@ void register_component(const char *name, size_t size,
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;
}

View file

@ -2,6 +2,8 @@
#define REGISTRY_H
#include <stddef.h>
#include <stdio.h>
#include "config.h"
struct mGameObject;
@ -9,6 +11,7 @@ struct mGameObject;
struct component {
const char *name;
void *(*make)(struct mGameObject * go);
void (*io)(void *data, FILE *f, int read);
void *data;
struct mGameObject *go;
void (*draw_debug)(void *data);
@ -31,6 +34,7 @@ void registry_init();
void register_component(const char *name, size_t size,
void (*make)(struct mGameObject * go, struct component * c),
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 mGameObject * go));

View file

@ -31,13 +31,33 @@ struct mSprite *MakeSprite(struct mGameObject *go)
sprite_init(&sprite, go);
arrput(sprites, sprite);
return &arrlast(sprites);
}
void sprite_init(struct mSprite *sprite, struct mGameObject *go)
{
sprite->go = go;
}
void sprite_delete(struct mSprite *sprite)
.3+
void sprite_io(struct mSprite *sprite, FILE *f, int read)
{
char path[100];
if (read) {
fgets(path,100, f);
fread(sprite, sizeof(*sprite), 1, f);
sprite_loadtex(sprite, path);
} else {
fputs(tex_get_path(sprite->tex), f);
fwrite(sprite, sizeof(*sprite), 1, f);
}
}
void sprite_delete(struct mSprite *sprite)
{
for (int i = 0; i < arrlen(sprites); i++)
if (&sprites[i] == sprite) {
arrdel(sprites, i);
return;
}
}
void sprite_draw_all()

View file

@ -1,6 +1,7 @@
#ifndef SPRITE_H
#define SPRITE_H
#include <stdio.h>
#include "timer.h"
#include "mathc.h"
@ -19,7 +20,6 @@ struct Anim2D {
};
struct mSprite {
struct Texture *tex;
mfloat_t pos[2];
mfloat_t size[2];
float rotation;
@ -28,11 +28,13 @@ struct mSprite {
struct Anim2D anim;
struct mGameObject *go;
struct Texture *tex;
};
struct mSprite *MakeSprite(struct mGameObject *go);
void sprite_delete(struct mSprite *sprite);
void sprite_init(struct mSprite *sprite, struct mGameObject *go);
void sprite_io(struct mSprite *sprite, FILE *f, int read);
void sprite_loadtex(struct mSprite *sprite, const char *path);
void sprite_loadanim(struct mSprite *sprite, const char *path, struct Anim2D anim);
void sprite_settex(struct mSprite *sprite, struct Texture *tex);