This commit is contained in:
John Alanbrook 2023-01-03 23:13:31 +00:00
parent 7ce8cd22ad
commit 740077fae3
11 changed files with 57 additions and 155 deletions

View file

@ -17,8 +17,6 @@
#include "registry.h" #include "registry.h"
register_component(0, 0, 0, 0, 0, 0, 0, 0, 0);
cpSpace *space = NULL; cpSpace *space = NULL;
float phys2d_gravity = -50.f; float phys2d_gravity = -50.f;

View file

@ -1,78 +0,0 @@
#include "static_actor.h"
//ADDMAKE(StaticActor);
static struct mStaticActor *models[100];
static int numModels = 0;
static struct mStaticActor *shadow_casters[100];
static int numShadowCasters = 0;
struct mStaticActor *curActor = NULL;
void staticactor_draw_dbg_color_pick(struct shader *s)
{
for (int i = 0; i < numModels; i++) {
shader_setvec3(s, "PickingColor", models[i]->obj.editor.color);
setup_model_transform(&models[i]->obj.transform, s, 1.f);
//models[i]->obj.draw(s);
}
}
void staticactor_draw_models(struct shader *s)
{
for (int i = 0; i < numModels; i++) {
setup_model_transform(&models[i]->obj.transform, s, 1.f);
draw_model(models[i]->model, s);
}
}
void staticactor_draw_shadowcasters(struct shader *s)
{
for (int i = 0; i < numShadowCasters; i++) {
setup_model_transform(&shadow_casters[i]->obj.transform, s, 1.f);
//models[i]->obj.draw(s);
}
}
/*
void StaticActor::serialize(FILE * file)
{
GameObject::serialize(file);
SerializeBool(file, &castShadows);
Serializecstr(file, currentModelPath);
}
void StaticActor::deserialize(FILE * file)
{
GameObject::deserialize(file);
DeserializeBool(file, &castShadows);
Deserializecstr(file, currentModelPath, MAXPATH);
curActor = this;
set_new_model(currentModelPath);
}
*/
struct mStaticActor *MakeStaticActor(const char *modelPath)
{
struct mStaticActor *newsa =
(struct mStaticActor *) malloc(sizeof(struct mStaticActor));
newsa->model = GetExistingModel(modelPath);
models[numModels++] = newsa;
return newsa;
}
/*
Serialize *make_staticactor()
{
StaticActor *nactor = (StaticActor *) malloc(sizeof(StaticActor));
return nactor;
}
*/
#include "nuke.h"

View file

@ -1,24 +0,0 @@
#ifndef STATIC_ACTOR_H
#define STATIC_ACTOR_H
#include "gameobject.h"
#include "model.h"
#include "shader.h"
struct mStaticActor {
struct gameobject obj;
struct model *model;
char *modelPath;
char currentModelPath[MAXPATH];
bool castShadows;
};
void staticactor_draw_dbg_color_pick(struct shader *s);
void staticactor_draw_models(struct shader *s);
void staticactor_draw_shadowcasters(struct shader *s);
struct mStaticActor *MakeStaticActor();
void staticactor_gui(struct mStaticActor *sa);
extern struct mStaticActor *curActor;
#endif

View file

@ -679,7 +679,8 @@ void editor_project_gui() {
startobjectgui: startobjectgui:
if (selectedobject) { if (selectedobject) {
draw_point(selectedobject->transform.position[0], selectedobject->transform.position[1], 5); cpVect pos = cpBodyGetPosition(selectedobject->body);
draw_point(pos.x, pos.y, 5);
NK_FORCE(gameobject) NK_FORCE(gameobject)
@ -711,9 +712,9 @@ startobjectgui:
// nuke_label("Components"); // nuke_label("Components");
nuke_nel(3); nuke_nel(3);
for (int i = 0; i < ncomponent; i++) { for (int i = 0; i < gameobject_ncomponents(selectedobject); i++) {
if (nuke_btn(components[i].name)) { if (nuke_btn(selectedobject->components[i].ref->name)) {
gameobject_addcomponent(selectedobject, &components[i]); gameobject_addcomponent(selectedobject, &selectedobject->components[i]);
} }
} }

View file

@ -12,7 +12,7 @@
#include "openglrender.h" #include "openglrender.h"
#include <stb_truetype.h> #include "stb_truetype.h"
#include "stb_rect_pack.h" #include "stb_rect_pack.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h" #include "stb_image_write.h"
@ -20,8 +20,6 @@
static uint32_t VBO = 0; static uint32_t VBO = 0;
static uint32_t VAO = 0; static uint32_t VAO = 0;
unsigned char ttf_buffer[1<<25];
struct sFont *font; struct sFont *font;
static struct shader *shader; static struct shader *shader;
@ -61,7 +59,14 @@ struct sFont *MakeFont(const char *fontfile, int height)
char fontpath[256]; char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile); snprintf(fontpath, 256, "fonts/%s", fontfile);
fread(ttf_buffer, 1, 1<<25, fopen(fontpath, "rb"));
FILE *f = fopen(fontpath, "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char *ttf_buffer = malloc(fsize+1);
fread(ttf_buffer, fsize, 1, f);
fclose(f);
unsigned char *bitmap = malloc(packsize*packsize); unsigned char *bitmap = malloc(packsize*packsize);
@ -73,25 +78,30 @@ struct sFont *MakeFont(const char *fontfile, int height)
stbtt_PackFontRange(&pc, ttf_buffer, 0, height, 32, 95, glyphs); stbtt_PackFontRange(&pc, ttf_buffer, 0, height, 32, 95, glyphs);
stbtt_PackEnd(&pc); stbtt_PackEnd(&pc);
stbi_write_png("packedfont.png", packsize, packsize, 1, bitmap, sizeof(char)*packsize); //stbi_write_png("packedfont.png", packsize, packsize, 1, bitmap, sizeof(char)*packsize);
stbtt_fontinfo fontinfo; stbtt_fontinfo fontinfo;
if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0))) { if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0))) {
YughError("Failed to make font %s", fontfile); YughError("Failed to make font %s", fontfile);
} }
float scale = stbtt_ScaleForPixelHeight(&fontinfo, height); float scale = stbtt_ScaleForPixelHeight(&fontinfo, height);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &newfont->texID); glGenTextures(1, &newfont->texID);
glBindTexture(GL_TEXTURE_2D, newfont->texID); glBindTexture(GL_TEXTURE_2D, newfont->texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, packsize, packsize, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, packsize, packsize, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap);
//glGenerateMipmap(GL_TEXTURE_2D); //glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
free(ttf_buffer);
free(bitmap);
for (unsigned char c = 32; c < 127; c++) { for (unsigned char c = 32; c < 127; c++) {
stbtt_packedchar glyph = glyphs[c-32]; stbtt_packedchar glyph = glyphs[c-32];

View file

@ -27,7 +27,7 @@ struct gameobject *get_gameobject_from_id(int id)
int id_from_gameobject(struct gameobject *go) { int id_from_gameobject(struct gameobject *go) {
for (int i = 0; i < arrlen(gameobjects); i++) { for (int i = 0; i < arrlen(gameobjects); i++) {
if (&gameobjects[i] == i) return i; if (&gameobjects[i] == go) return i;
} }
return -1; return -1;
@ -51,7 +51,6 @@ int MakeGameobject()
YughInfo("Making new gameobject"); YughInfo("Making new gameobject");
struct gameobject go = { struct gameobject go = {
.editor.id = arrlen(gameobjects), .editor.id = arrlen(gameobjects),
.transform.scale = 1.f,
.scale = 1.f, .scale = 1.f,
.bodytype = CP_BODY_TYPE_STATIC, .bodytype = CP_BODY_TYPE_STATIC,
.mass = 1.f .mass = 1.f
@ -71,10 +70,10 @@ int MakeGameobject()
void gameobject_addcomponent(struct gameobject *go, struct component_interface *c) void gameobject_addcomponent(struct gameobject *go, struct component_interface *c)
{ {
struct component new; struct component new;
new.interface = c; new.ref = c;
new.data = c->make(go); new.data = c->make(go);
new.go = go; new.go = go;
arrput(go->components, c); arrput(go->components, new);
} }
void gameobject_delete(int id) void gameobject_delete(int id)
@ -82,7 +81,7 @@ void gameobject_delete(int id)
YughInfo("Deleting gameobject with id %d.", id); YughInfo("Deleting gameobject with id %d.", id);
struct gameobject *go = &gameobjects[id]; struct gameobject *go = &gameobjects[id];
for (int i = 0; i < arrlen(go->components); i++) { for (int i = 0; i < arrlen(go->components); i++) {
go->components[i].delete(go->components[i].data); comp_delete(&go->components[i]);
arrdel(go->components, i); arrdel(go->components, i);
} }
@ -93,24 +92,13 @@ void gameobject_delete(int id)
void gameobject_delcomponent(struct gameobject *go, int n) void gameobject_delcomponent(struct gameobject *go, int n)
{ {
go->components[n].delete(go->components[n].data); comp_delete(&go->components[n]);
arrdel(go->components, n); arrdel(go->components, n);
} }
void setup_model_transform(struct mTransform *t, struct shader *s, float scale)
{
mfloat_t modelT[16] = { 0.f };
mfloat_t matbuff[16] = { 0.f };
memcpy(modelT, UNITMAT4, sizeof(modelT));
mat4_translate_vec3(modelT, t->position);
mat4_multiply(modelT, modelT, mat4_rotation_quat(matbuff, t->rotation));
mat4_scale_vec3f(modelT, scale);
shader_setmat4(s, "model", modelT);
}
void gameobject_save(struct gameobject *go, FILE * file) void gameobject_save(struct gameobject *go, FILE * file)
{ {
/*
fwrite(go, sizeof(*go), 1, file); fwrite(go, sizeof(*go), 1, file);
YughInfo("Number of components is %d.", arrlen(go->components)); YughInfo("Number of components is %d.", arrlen(go->components));
@ -125,6 +113,7 @@ void gameobject_save(struct gameobject *go, FILE * file)
else else
go->components[i].io(go->components[i].data, file, 0); go->components[i].io(go->components[i].data, file, 0);
} }
*/
} }
int gameobject_makefromprefab(char *path) int gameobject_makefromprefab(char *path)
@ -150,6 +139,7 @@ int gameobject_makefromprefab(char *path)
void gameobject_init(struct gameobject *go, FILE * fprefab) void gameobject_init(struct gameobject *go, FILE * fprefab)
{ {
/*
go->body = cpSpaceAddBody(space, cpBodyNew(go->mass, 1.f)); go->body = cpSpaceAddBody(space, cpBodyNew(go->mass, 1.f));
cpBodySetType(go->body, go->bodytype); cpBodySetType(go->body, go->bodytype);
cpBodySetUserData(go->body, go); cpBodySetUserData(go->body, go);
@ -173,6 +163,7 @@ void gameobject_init(struct gameobject *go, FILE * fprefab)
newc->init(newc->data, go); newc->init(newc->data, go);
} }
*/
} }
@ -221,6 +212,11 @@ void toggleprefab(struct gameobject *go)
} }
} }
int gameobject_ncomponents(struct gameobject *go)
{
return arrlen(go->components);
}
void gameobject_move(struct gameobject *go, cpVect vec) void gameobject_move(struct gameobject *go, cpVect vec)
{ {
cpVect p = cpBodyGetPosition(go->body); cpVect p = cpBodyGetPosition(go->body);
@ -300,15 +296,13 @@ void object_gui(struct gameobject *go)
for (int i = 0; i < arrlen(go->components); i++) { for (int i = 0; i < arrlen(go->components); i++) {
struct component *c = &go->components[i]; struct component *c = &go->components[i];
if (c->draw_debug) comp_draw_debug(c);
c->draw_debug(c->data);
nuke_nel(5); nuke_nel(5);
if (nuke_btn("Del")) n = i; if (nuke_btn("Del")) n = i;
if (nuke_push_tree_id(c->name, i)) { if (nuke_push_tree_id(c->ref->name, i)) {
c->draw_gui(c->data); comp_draw_gui(c);
nuke_tree_pop(); nuke_tree_pop();
} }
@ -324,8 +318,7 @@ void gameobject_draw_debugs() {
for (int i = 0; i < arrlen(gameobjects); i++) { for (int i = 0; i < arrlen(gameobjects); i++) {
for (int j = 0; j < arrlen(gameobjects[i].components); j++) { for (int j = 0; j < arrlen(gameobjects[i].components); j++) {
struct component *c = &gameobjects[i].components[j]; struct component *c = &gameobjects[i].components[j];
comp_draw_debug(c);
if (c->draw_debug) c->draw_debug(c->data);
} }
} }
} }

View file

@ -3,11 +3,11 @@
#include <stdio.h> #include <stdio.h>
#include "mathc.h" #include "mathc.h"
#include "transform.h"
#include "config.h" #include "config.h"
#include <stdbool.h> #include <stdbool.h>
#include <chipmunk/chipmunk.h> #include <chipmunk/chipmunk.h>
#include "2dphysics.h" #include "2dphysics.h"
#include "registry.h"
struct shader; struct shader;
struct sprite; struct sprite;
@ -29,16 +29,15 @@ struct go_temp {
}; };
struct gameobject { struct gameobject {
struct mTransform transform;
struct editor editor;
cpBodyType bodytype; cpBodyType bodytype;
float scale; float scale;
float mass; float mass;
cpBody *body;
float f; /* friction */ float f; /* friction */
float e; /* elasticity */ float e; /* elasticity */
cpBody *body;
struct component *components; struct component *components;
struct phys_cbs *cbs; struct phys_cbs *cbs;
struct editor editor;
}; };
extern struct gameobject *gameobjects; extern struct gameobject *gameobjects;
@ -49,16 +48,16 @@ void gameobject_delete(int id);
void clear_gameobjects(); void clear_gameobjects();
int number_of_gameobjects(); int number_of_gameobjects();
void set_n_gameobjects(int n); void set_n_gameobjects(int n);
void setup_model_transform(struct mTransform *t, struct shader *s, float scale);
void toggleprefab(struct gameobject *go); void toggleprefab(struct gameobject *go);
struct gameobject *get_gameobject_from_id(int id); struct gameobject *get_gameobject_from_id(int id);
int id_from_gameobject(struct gameobject *go); int id_from_gameobject(struct gameobject *go);
void gameobject_save(struct gameobject *go, FILE * file); void gameobject_save(struct gameobject *go, FILE * file);
void gameobject_addcomponent(struct gameobject *go, struct component *c); void gameobject_addcomponent(struct gameobject *go, struct component_interface *c);
void gameobject_delcomponent(struct gameobject *go, int n); void gameobject_delcomponent(struct gameobject *go, int n);
void gameobject_loadcomponent(struct gameobject *go, int id); void gameobject_loadcomponent(struct gameobject *go, int id);
int gameobject_ncomponents(struct gameobject *go);
void gameobject_saveprefab(struct gameobject *go); void gameobject_saveprefab(struct gameobject *go);
int gameobject_makefromprefab(char *path); int gameobject_makefromprefab(char *path);

View file

@ -4,7 +4,6 @@
#include "shader.h" #include "shader.h"
#include "font.h" #include "font.h"
#include "config.h" #include "config.h"
#include "static_actor.h"
#include "gameobject.h" #include "gameobject.h"
#include "camera.h" #include "camera.h"
#include "window.h" #include "window.h"

View file

@ -76,12 +76,12 @@ void comp_delete(struct component *c)
c->ref->delete(c->data); c->ref->delete(c->data);
} }
void comp_init(struct component *c) void comp_init(struct component *c, struct gameobject *go)
{ {
c->ref->init(c->data); c->ref->init(c->data, go);
} }
void comp_io(struct component *c, int read) void comp_io(struct component *c, FILE *f, int read)
{ {
c->ref->io(c->data, read); c->ref->io(c->data, f, read);
} }

View file

@ -28,15 +28,15 @@ struct component comp_make(struct component_interface *interface);
void comp_draw_debug(struct component *c); void comp_draw_debug(struct component *c);
void comp_draw_gui(struct component *c); void comp_draw_gui(struct component *c);
void comp_delete(struct component *c); void comp_delete(struct component *c);
void comp_init(struct component *c); void comp_init(struct component *c, struct gameobject *go);
void comp_io(struct component *c, int read); void comp_io(struct component *c, FILE *f, int read);
void comp_update(struct component *c, struct gameobject *go); void comp_update(struct component *c, struct gameobject *go);
void registry_init(); void registry_init();
void register_component(const char *name, size_t size, 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 (*delete)(void *data),
void (*io)(void *data, FILE *f, int read), void (*io)(void *data, FILE *f, int read),
void(*draw_debug)(void *data), void(*draw_debug)(void *data),

View file

@ -66,12 +66,14 @@ GLuint load_shader_from_file(const char *path, int type)
if (!path) if (!path)
perror(spath), exit(1); perror(spath), exit(1);
char buf[SHADER_BUF] = {'\0'}; char *buf;
long int fsize; long int fsize;
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
fsize = ftell(f); fsize = ftell(f);
buf = malloc(fsize+1);
rewind(f); rewind(f);
fread(buf, fsize, 1, f); size_t r = fread(buf, sizeof(char), fsize, f);
buf[r] = '\0';
fclose(f); fclose(f);
@ -85,6 +87,8 @@ GLuint load_shader_from_file(const char *path, int type)
return 0; return 0;
} }
free(buf);
return id; return id;
} }