Replaced vec.h with stb_ds.h arrays

This commit is contained in:
John Alanbrook 2022-08-26 14:19:17 +00:00
parent ff4168d279
commit 02d4396010
16 changed files with 210 additions and 177 deletions

View file

@ -91,7 +91,7 @@ INCLUDE = $(BIN)include
LINK = $(LIBPATH) $(LINKER_FLAGS) $(LELIBS)
engine: $(yuginec:.%.c=$(objprefix)%.o) $(ENGINE)
engine: $(yuginec:.%.c=$(objprefix)%.o) $(ENGINE) tags
@echo Linking engine
$(CLINK) $< $(LINK) -o $@
@echo Finished build

View file

@ -6,11 +6,11 @@
#include "stdlib.h"
#include "iir.h"
#include "log.h"
#include "vec.h"
#include "stb_ds.h"
#define PI 3.14159265
struct vec filters;
struct dsp_filter *filters;
struct dsp_filter make_dsp(void *data, void (*in)(void *data, short *out, int n)) {
struct dsp_filter new;
@ -19,7 +19,7 @@ struct dsp_filter make_dsp(void *data, void (*in)(void *data, short *out, int n)
return new;
if (filters.len == 0) {
if (arrlen(filters) == 0) {
}
}

View file

@ -763,9 +763,9 @@ startobjectgui:
void editor_render() { editor_project_gui(); }
void pickGameObject(int pickID) {
if (pickID >= 0 && pickID < gameobjects->len) {
if (pickID >= 0 && pickID < arrlen(gameobjects)) {
selected_index = pickID;
selectedobject = (struct mGameObject *)vec_get(gameobjects, pickID);
selectedobject = &gameobjects[pickID];
} else {
selected_index = -1;
selectedobject = NULL;
@ -974,8 +974,8 @@ void editor_makenewobject() {}
int obj_gui_hierarchy(struct mGameObject *selected) {
for (int i = 0; i < gameobjects->len; i++) {
struct mGameObject *go = (struct mGameObject *)vec_get(gameobjects, i);
for (int i = 0; i < arrlen(gameobjects); i++) {
struct mGameObject *go = &gameobjects[i];
if (nk_select_label(ctx, go->editor.mname, NK_TEXT_LEFT, go == selected)) {
if (go != selected)

View file

@ -32,7 +32,7 @@
#include "resources.h"
#include "timer.h"
#include "script.h"
#include "vec.h"
#include "sound.h"
// TODO: Init on the heap
@ -61,10 +61,8 @@ void engine_init()
resources_init();
script_init();
registry_init();
init_gameobjects();
prefabs = vec_make(MAXNAME, 25);
stbi_set_flip_vertically_on_load(1);
//stbi_set_flip_vertically_on_load(1);
phys2d_init();
gui_init();
sound_init();

View file

@ -5,26 +5,22 @@
#include "registry.h"
#include "2dphysics.h"
#include "script.h"
#include "vec.h"
#include "input.h"
#include <string.h>
#include <chipmunk/chipmunk.h>
#include "resources.h"
#include "nuke.h"
struct vec *gameobjects = NULL;
#include "stb_ds.h"
struct mGameObject *gameobjects = NULL;
const int nameBuf[MAXNAME] = { 0 };
const int prefabNameBuf[MAXNAME] = { 0 };
void init_gameobjects()
{
gameobjects = vec_make(sizeof(struct mGameObject), 100);
}
struct mGameObject *get_gameobject_from_id(int id)
{
return vec_get(gameobjects, id - 1);
return &gameobjects[id];
}
static void gameobject_setpickcolor(struct mGameObject *go)
@ -40,36 +36,46 @@ static void gameobject_setpickcolor(struct mGameObject *go)
struct mGameObject *MakeGameobject()
{
struct mGameObject *go = vec_add(gameobjects, NULL);
go->editor.id = gameobjects->len - 1;
go->transform.scale = 1.f;
gameobject_setpickcolor(go);
strncpy(go->editor.mname, "New object", MAXNAME);
go->scale = 1.f;
go->bodytype = CP_BODY_TYPE_STATIC;
go->mass = 1.f;
go->body = cpSpaceAddBody(space, cpBodyNew(go->mass, 1.f));
struct mGameObject go = {
.editor.id = arrlen(gameobjects),
.transform.scale = 1.f,
.scale = 1.f,
.bodytype = CP_BODY_TYPE_STATIC,
.mass = 1.f
};
go->components = vec_make(sizeof(struct component), 10);
gameobject_setpickcolor(&go);
strncpy(go.editor.mname, "New object", MAXNAME);
go.body = cpSpaceAddBody(space, cpBodyNew(go.mass, 1.f));
return go;
arrput(gameobjects, go);
return &arrlast(gameobjects);
}
void gameobject_addcomponent(struct mGameObject *go, struct component *c)
{
struct component *newc = vec_add(go->components, c);
arrput(go->components, *c);
struct component *newc = &arrlast(go->components);
newc->go = go;
newc->data = newc->make(newc->go);
}
void gameobject_delete(int id)
{
vec_delete(gameobjects, id);
struct mGameObject *go = &gameobjects[id];
for (int i = 0; i < arrlen(go->components); i++) {
free(go->components[i].data);
}
arrfree(go->components);
arrdelswap(gameobjects, id);
}
void gameobject_delcomponent(struct mGameObject *go, int n)
{
vec_del_order(go->components, n);
arrdel(go->components, n);
}
void setup_model_transform(struct mTransform *t, struct mShader *s, float scale)
@ -88,10 +94,10 @@ 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->data, c->datasize, 1, file);
fwrite(arrlen(go->components), sizeof(int), 1, file);
for (int i = 0; i < arrlen(go->components); i++) {
fwrite(go->components[i].id, sizeof(int), 1, file);
fwrite(go->components[i].data, go->components[i].datasize, 1, file);
}
}
@ -103,30 +109,34 @@ void gameobject_makefromprefab(char *path)
}
struct mGameObject *new = MakeGameobject();
struct vec *hold = new->components;
fread(new, sizeof(*new), 1, fprefab);
new->components = hold;
new->editor.id = gameobjects->len - 1;
new->components = NULL;
gameobject_init(new, fprefab);
fclose(fprefab);
new->editor.id = arrlen(gameobjects);
arrput(gameobjects, *new);
}
void gameobject_init(struct mGameObject *go, FILE * fprefab)
{
go->body = cpSpaceAddBody(space, cpBodyNew(go->mass, 1.f));
vec_load(go->components, fprefab);
int comp_n;
fread(&comp_n, sizeof(int), 1, fprefab);
arrsetlen(go->components, comp_n);
int n;
for (int i = 0; i < go->components->len; i++) {
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);
newc->init(newc->data, go);
for (int i = 0; i < comp_n; i++) {
fread(&n, sizeof(int), 1, fprefab);
arrput(go->components, components[n]);
struct component *newc = &arrlast(go->components);
newc->go = go;
newc->data = malloc(newc->datasize);
fread(newc->data, newc->datasize, 1, fprefab);
newc->init(newc->data, go);
}
}
@ -176,9 +186,8 @@ void toggleprefab(struct mGameObject *go)
void gameobject_update(struct mGameObject *go)
{
if (go->script) {
if (go->script)
script_run(go->script);
}
}
void gameobject_move(struct mGameObject *go, float xs, float ys)
@ -197,7 +206,8 @@ void gameobject_rotate(struct mGameObject *go, float as)
}
void update_gameobjects() {
vec_walk(gameobjects, &gameobject_update);
for (int i = 0; i < arrlen(gameobjects); i++)
gameobject_update(&gameobjects[i]);
}
@ -245,8 +255,8 @@ void object_gui(struct mGameObject *go)
for (int i = 0; i < go->components->len; i++) {
struct component *c = vec_get(go->components, i);
for (int i = 0; i < arrlen(go->components); i++) {
struct component *c = &go->components[i];
if (c->draw_debug)
c->draw_debug(c->data);

View file

@ -11,10 +11,6 @@
struct mShader;
struct mSprite;
struct component;
struct vec;
extern struct mGameObject *updateGO;
extern struct vec *gameobjects;
struct editor {
mfloat_t color[3];
@ -36,10 +32,12 @@ struct mGameObject {
cpBody *body;
float f; /* friction */
float e; /* elasticity */
struct vec *components;
struct component *components;
char *script;
};
extern struct mGameObject *gameobjects;
struct mGameObject *MakeGameobject();
void init_gameobjects();
void gameobject_delete(int id);

View file

@ -1,5 +1,6 @@
#include "input.h"
#include "vec.h"
#include "stb_ds.h"
int32_t mouseWheelX = 0;
int32_t mouseWheelY = 0;
@ -11,7 +12,7 @@ int quit = 0;
static double c_xpos;
static double c_ypos;
static struct vec downkeys;
static int *downkeys = NULL;
static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos)
{
@ -55,44 +56,48 @@ void input_poll(double wait)
//editor_input(&e);
vec_walk(&downkeys, call_input_down);
}
int same_key(int *key1, int *key2) {
return *key1 == *key2;
for (int i = 0; i < arrlen(downkeys); i++)
call_input_down(downkeys[i]);
}
void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods)
{
if (downkeys.data == NULL) {
downkeys = vec_init(sizeof(key), 10);
}
char keystr[50] = {'\0'};
strcat(keystr, "input_");
strcat(keystr, glfwGetKeyName(key, 0));
switch (action) {
case GLFW_PRESS:
strcat(keystr, "_pressed");
int *foundkey = vec_find(&downkeys, same_key, &key);
if (foundkey == NULL) {
vec_add(&downkeys, &key);
int found = 0;
for (int i = 0; i < arrlen(downkeys); i++) {
if (downkeys[i] == key)
goto SCRIPTCALL;
}
arrput(downkeys, key);
break;
case GLFW_RELEASE:
strcat(keystr, "_released");
int found = vec_find_n(&downkeys, same_key, &key);
if (found != -1) {
vec_delete(&downkeys, found);
for (int i = 0; i < arrlen(downkeys); i++) {
if (downkeys[i] == key) {
arrdelswap(downkeys, i);
goto SCRIPTCALL;
}
}
break;
case GLFW_REPEAT:
strcat(keystr, "_rep");
break;
}
SCRIPTCALL:
script_call(keystr);
}
@ -108,12 +113,23 @@ void cursor_show()
int action_down(int scancode)
{
int *foundkey = vec_find(&downkeys, same_key, &scancode);
return foundkey != NULL;
for (int i = 0; i < arrlen(downkeys); i++) {
if (downkeys[i] == scancode)
return 1;
}
return 0;
}
int action_up(int scancode)
{
int *foundkey = vec_find(&downkeys, same_key, &scancode);
return foundkey == NULL;
int found = 0;
for (int i = 0; i < arrlen(downkeys); i++) {
if (downkeys[i] == scancode) {
found = 1;
break;
}
}
return !found;
}

View file

@ -1,10 +1,11 @@
#include "level.h"
#include <stdio.h>
#include <string.h>
#include "vec.h"
#include "gameobject.h"
#include "resources.h"
#include "stb_ds.h"
void save_level(char name[MAXNAME])
{
FILE *lfile = res_open(name, "wb+");
@ -12,41 +13,44 @@ void save_level(char name[MAXNAME])
if (!lfile) return;
int objs = gameobjects->len;
int objs = arrlen(gameobjects);
fwrite(&objs, sizeof(objs), 1, lfile);
for (int i = 0; i < objs; i++) {
gameobject_save(vec_get(gameobjects, i), lfile);
}
for (int i = 0; i < objs; i++)
gameobject_save(&gameobjects[i], lfile);
fclose(lfile);
}
void load_level(char name[MAXNAME])
{
FILE *lfile = fopen(name, "rb");
if (!lfile) return;
new_level();
int objs;
fread(&objs, sizeof(objs), 1, lfile);
vec_clear(gameobjects);
arraddn(gameobjects, objs);
for (int i = 0; i < objs; i++) {
struct mGameObject *go = vec_add(gameobjects, NULL);
struct mGameObject *go = &gameobjects[i];
fread(go, sizeof(struct mGameObject), 1, lfile);
go->components = vec_make(1,1);
go->components = NULL;
gameobject_init(go, lfile);
}
fclose(lfile);
}
void new_level()
{
vec_clear(gameobjects);
for (int i = 0; i < arrlen(gameobjects); i++)
gameobject_delete(i);
arrfree(gameobjects);
}

View file

@ -105,6 +105,8 @@ void openglInit()
animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl");
textShader = MakeShader("textvert.glsl", "textfrag.glsl");
shader_use(textShader);
shader_setint(textShader, "text", 0);
@ -114,7 +116,6 @@ void openglInit()
sprite_initialize();
debugdraw_init();
//glEnable(GL_STENCIL_TEST);

View file

@ -33,6 +33,8 @@ char pathbuf[MAXPATH];
void resources_init()
{
prefabs = vec_make(MAXNAME, 25);
DATA_PATH = malloc(MAXPATH);
getcwd(DATA_PATH, MAXPATH);
strncat(DATA_PATH, "/", MAXPATH);

View file

@ -7,26 +7,24 @@
#include <stdlib.h>
#include "log.h"
#include "resources.h"
#include "vec.h"
#include "stb_ds.h"
#include "timer.h"
#define SHADER_BUF 10000
struct vec shaders;
static struct mShader *shaders;
struct mShader *MakeShader(const char *vertpath, const char *fragpath)
{
if (shaders.data == NULL) shaders = vec_init(sizeof(struct mShader), 10);
if (arrcap(shaders) == 0)
arrsetcap(shaders, 20);
struct mShader init = { 0, vertpath, fragpath };
struct mShader *new = vec_add(&shaders, NULL);
memcpy(new, &init, sizeof(*new));
shader_compile(new);
return new;
}
struct mShader *CreateShader(const char *vert, const char *frag)
{
return NULL;
struct mShader init = {
.vertpath = vertpath,
.fragpath = fragpath };
shader_compile(&init);
arrput(shaders, init);
return &arrlast(shaders);
}
int shader_compile_error(GLuint shader)
@ -90,6 +88,8 @@ GLuint load_shader_from_file(const char *path, int type)
void shader_compile(struct mShader *shader)
{
printf("Making shader with %s and %s.\n", shader->vertpath, shader->fragpath);
GLuint vert = load_shader_from_file(shader->vertpath, GL_VERTEX_SHADER);
GLuint frag = load_shader_from_file(shader->fragpath, GL_FRAGMENT_SHADER);
@ -162,5 +162,5 @@ void shader_setUBO(struct mShader *shader, const char *name, unsigned int index)
void shader_compile_all()
{
vec_walk(&shaders, shader_compile);
arrwalk(shaders, shader_compile);
}

View file

@ -11,7 +11,6 @@ struct mShader {
void shader_compile_all();
struct mShader *MakeShader(const char *vertpath, const char *fragpath);
struct mShader *CreateShader(const char *vert, const char *frag);
void shader_compile(struct mShader *shader);
void shader_use(struct mShader *shader);

View file

@ -9,41 +9,28 @@
#include "datastream.h"
#include "gameobject.h"
#include <string.h>
#include "vec.h"
#include "stb_ds.h"
static struct mGameObject *gui_go = NULL;
/*
static struct mShader *spriteShader = NULL;
static struct mShader *animSpriteShader = NULL;
*/
struct TextureOptions TEX_SPRITE = { 1, 0, 0 };
struct vec sprites;
struct mSprite *sprites;
static uint32_t quadVAO;
struct mSprite *MakeSprite(struct mGameObject *go)
{
// TODO: Init this once and never check again
if (sprites.data == NULL) sprites = vec_init(sizeof(struct mSprite), 10);
struct mSprite sprite = {
.color = {1.f, 1.f, 1.f},
.size = {1.f, 1.f},
.tex = texture_loadfromfile("ph.png"),
.index = arrlen(sprites) };
struct mSprite *sprite = vec_add(&sprites, NULL);
sprite->color[0] = 1.f;
sprite->color[1] = 1.f;
sprite->color[2] = 1.f;
sprite->pos[0] = 0.f;
sprite->pos[1] = 0.f;
sprite->size[0] = 1.f;
sprite->size[1] = 1.f;
sprite->tex = texture_loadfromfile("ph.png");
sprite_init(sprite, go);
sprite->index = sprites.last;
return sprite;
sprite_init(&sprite, go);
arrput(sprites, sprite);
return &arrlast(sprites);
}
void sprite_init(struct mSprite *sprite, struct mGameObject *go)
@ -53,8 +40,8 @@ void sprite_init(struct mSprite *sprite, struct mGameObject *go)
void sprite_draw_all()
{
shader_use(spriteShader);
vec_walk(&sprites, sprite_draw);
//shader_use(spriteShader);
arrwalk(sprites, sprite_draw);
}
void sprite_loadtex(struct mSprite *sprite, const char *path)

View file

@ -86,3 +86,18 @@ void timer_settime(struct timer *t, double interval) {
// TODO: timer_settime reacts to elapsed time
}
void *arrfind(void *arr, int (*valid)(void *arr, void *cmp), void *cmp)
{
for (int i = 0; i < arrlen(arr); i++) {
if (valid(&arr[i], cmp))
return &arr[i];
}
return NULL;
}
void arrwalk(void *arr, void (*fn)(void *data))
{
for (int i = 0; i < arrlen(arr); i++)
fn(&arr[i]);
}

View file

@ -21,4 +21,10 @@ void timer_stop(struct timer *t);
void timer_update(double s);
void timer_settime(struct timer *t, double interval);
void *arrfind(void *arr, int (*valid)(void *arr, void *cmp), void *cmp);
void arrwalk(void *arr, void (*fn)(void *data));
#endif

View file

@ -4,14 +4,16 @@
#include "log.h"
#include <stdlib.h>
#include <stdio.h>
#include <vec.h>
#include "input.h"
#include "script.h"
#include "nuke.h"
#include "stb_ds.h"
struct mSDLWindow *mainwin;
static struct vec windows;
static struct mSDLWindow *windows = NULL;
struct Texture *icon = NULL;
int is_win(struct mSDLWindow *s, GLFWwindow *w)
@ -26,25 +28,25 @@ void window_size_callback(GLFWwindow *w)
void window_iconify_callback(GLFWwindow *w, int iconified)
{
struct mSDLWindow *win = vec_find(&windows, is_win, w);
struct mSDLWindow *win = arrfind(windows, is_win, w);
win->iconified = iconified;
}
void window_focus_callback(GLFWwindow *w, int focused)
{
struct mSDLWindow *win = vec_find(&windows, is_win, w);
struct mSDLWindow *win = arrfind(windows, is_win, w);
win->keyboardFocus = focused;
}
void window_maximize_callback(GLFWwindow *w, int maximized)
{
struct mSDLWindow *win = vec_find(&windows, is_win, w);
struct mSDLWindow *win = arrfind(windows, is_win, w);
win->minimized = !maximized;
}
void window_framebuffer_size_cb(GLFWwindow *w, int width, int height)
{
struct mSDLWindow *win = vec_find(&windows, is_win, w);
struct mSDLWindow *win = arrfind(windows, is_win, w);
win->width = width;
win->height = height;
window_makecurrent(win);
@ -60,51 +62,46 @@ void window_close_callback(GLFWwindow *w)
struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height, uint32_t flags)
{
struct mSDLWindow *w;
if (arrcap(windows) == 0)
arrsetcap(windows, 5);
GLFWwindow *sharewin = mainwin == NULL ? NULL : mainwin->window;
struct mSDLWindow w = {
.width = width,
.height = height,
.id = arrlen(windows),
.window = glfwCreateWindow(width, height, name, NULL, sharewin) };
if (!w.window) {
YughError("Couldn't make GLFW window\n", 1);
return NULL;
}
if (windows.data == NULL) {
windows = vec_init(sizeof(struct mSDLWindow), 5);
w = vec_add(&windows, NULL);
mainwin = w;
} else {
w = vec_add(&windows, NULL);
}
if (icon) window_seticon(&w, icon);
GLFWwindow *sharewin = mainwin ? NULL : mainwin->window;
w->width = width;
w->height = height;
YughInfo("Number of windows: %d.\n", windows.len);
w->id = windows.len-1;
w->window = glfwCreateWindow(width, height, name, NULL, sharewin);
if (!w->window) {
YughError("Couldn't make GLFW window\n", 1);
return w;
}
if (icon) window_seticon(w, icon);
glfwMakeContextCurrent(w->window);
glfwMakeContextCurrent(w.window);
gladLoadGL(glfwGetProcAddress);
glfwSwapInterval(1);
// Set callbacks
glfwSetWindowCloseCallback(w->window, window_close_callback);
glfwSetWindowSizeCallback(w->window, window_size_callback);
glfwSetFramebufferSizeCallback(w->window, window_framebuffer_size_cb);
glfwSetWindowFocusCallback(w->window, window_focus_callback);
glfwSetKeyCallback(w->window, win_key_callback);
glfwSetWindowCloseCallback(w.window, window_close_callback);
glfwSetWindowSizeCallback(w.window, window_size_callback);
glfwSetFramebufferSizeCallback(w.window, window_framebuffer_size_cb);
glfwSetWindowFocusCallback(w.window, window_focus_callback);
glfwSetKeyCallback(w.window, win_key_callback);
nuke_init(w);
nuke_init(&w);
w->nuke_cb = 0;
w->gui_cb = 0;
arrput(windows, w);
return w;
if (arrlen(windows) == 1)
mainwin = &windows[0];
return &arrlast(windows);
}
void window_set_icon(const char *png)
@ -115,11 +112,11 @@ void window_set_icon(const char *png)
void window_destroy(struct mSDLWindow *w)
{
glfwDestroyWindow(w->window);
vec_delete(&windows, w->id);
arrdelswap(windows, w->id);
}
struct mSDLWindow *window_i(int index) {
return vec_get(&windows, index);
return &windows[index];
}
void window_handle_event(struct mSDLWindow *w)
@ -207,7 +204,7 @@ void window_handle_event(struct mSDLWindow *w)
void window_all_handle_events()
{
vec_walk(&windows, window_handle_event);
arrwalk(windows, window_handle_event);
}
void window_makefullscreen(struct mSDLWindow *w)
@ -285,5 +282,5 @@ void window_render(struct mSDLWindow *w) {
}
void window_renderall() {
vec_walk(&windows, window_render);
arrwalk(windows, window_render);
}