bug fixes; remove texture anims

This commit is contained in:
2023-12-18 23:12:05 +00:00
parent 444fb98125
commit a1aff79d5c
21 changed files with 144 additions and 435 deletions

View File

@@ -11,27 +11,14 @@
static gameobject **gameobjects;
gameobject *body2go(cpBody *body) { return cpBodyGetUserData(body); }
gameobject *shape2go(cpShape *shape)
{
return ((struct phys2d_shape *)cpShapeGetUserData(shape))->go;
}
gameobject *shape2go(cpShape *shape) { return ((struct phys2d_shape *)cpShapeGetUserData(shape))->go; }
HMM_Vec2 go_pos(gameobject *go)
{
cpVect p = cpBodyGetPosition(go->body);
return (HMM_Vec2){p.x, p.y};
}
HMM_Vec2 go_worldpos(gameobject *go)
{
HMM_Vec2 ret;
ret.cp = cpBodyGetPosition(go->body);
return ret;
}
float go_angle(gameobject *go) { return go_worldangle(go); }
float go_worldangle(gameobject *go) { return cpBodyGetAngle(go->body); }
float go2angle(gameobject *go) { return cpBodyGetAngle(go->body); }
float go_angle(gameobject *go) { return cpBodyGetAngle(go->body); }
transform3d go2t3(gameobject *go)
{
@@ -212,8 +199,6 @@ void gameobject_clean(gameobject *go) {
/* Really more of a "mark for deletion" ... */
void gameobject_free(gameobject *go) {
if (!go) return;
YughWarn("FREEING A GAMEOBJECT");
JS_FreeValue(js, go->ref);
for (int i = arrlen(gameobjects)-1; i >= 0; i--)
if (gameobjects[i] == go) {

View File

@@ -69,11 +69,9 @@ HMM_Mat4 t3d_go2world(gameobject *go);
HMM_Mat4 t3d_world2go(gameobject *go);
HMM_Vec2 go_pos(gameobject *go);
HMM_Vec2 go_worldpos(gameobject *go);
//float go_angle(gameobject *go);
float go_worldangle(gameobject *go);
float go2angle(gameobject *go);
void gameobject_setpos(gameobject *go, cpVect vec);
float go_angle(gameobject *go);
void gameobject_setangle(gameobject *go, float angle);
gameobject *body2go(cpBody *body);
gameobject *shape2go(cpShape *shape);
@@ -83,10 +81,6 @@ void go_shape_apply(cpBody *body, cpShape *shape, gameobject *go);
/* Tries a few methods to select a gameobject; if none is selected returns -1 */
gameobject *pos2gameobject(HMM_Vec2 pos);
void gameobject_move(gameobject *go, HMM_Vec2 vec);
void gameobject_rotate(gameobject *go, float as);
void gameobject_setangle(gameobject *go, float angle);
void gameobject_setpos(gameobject *go, cpVect vec);
void gameobject_draw_debug(gameobject *go);
void gameobject_draw_debugs();
#endif

View File

@@ -403,8 +403,6 @@ JSValue duk_spline_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst
samples = bezier_cb_ma_v2(points, param);
break;
}
arrfree(points);
@@ -638,7 +636,6 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
break;
case 30:
sprite_setanim(id2sprite(js2int(argv[1])), js2ptr(argv[2]), js2int(argv[3]));
break;
case 31:
@@ -784,6 +781,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
break;
case 63:
set_cam_body(NULL);
break;
case 64:
@@ -996,7 +994,7 @@ JSValue duk_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *argv)
break;
case 113:
js2gameobject(argv[1])->ref = argv[2];//JS_DupValue(js,argv[2]);
js2gameobject(argv[1])->ref = argv[2];
break;
case 114:
@@ -1511,7 +1509,6 @@ JSValue duk_sys_cmd(JSContext *js, JSValueConst this, int argc, JSValueConst *ar
}
JSValue duk_make_gameobject(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
YughWarn("MAKING GAMOBJECT");
return gameobject2js(MakeGameobject());
}
@@ -1648,19 +1645,6 @@ JSValue duk_make_sprite(JSContext *js, JSValueConst this, int argc, JSValueConst
return sprite;
}
/* Make anim from texture */
JSValue duk_make_anim2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
const char *path = JS_ToCString(js, argv[0]);
int frames = js2int(argv[1]);
int fps = js2int(argv[2]);
struct TexAnim *anim = anim2d_from_tex(path, frames, fps);
JS_FreeCString(js, path);
return ptr2js(anim);
}
JSValue duk_make_box2d(JSContext *js, JSValueConst this, int argc, JSValueConst *argv) {
gameobject *go = js2gameobject(argv[0]);
HMM_Vec2 size = js2vec2(argv[1]);
@@ -1888,7 +1872,6 @@ void ffi_load() {
DUK_FUNC(sys_cmd, 1)
DUK_FUNC(make_sprite, 1)
DUK_FUNC(make_anim2d, 3)
DUK_FUNC(spline_cmd, 6)
DUK_FUNC(make_box2d, 3)

View File

@@ -466,13 +466,8 @@ void render_winsize()
}
static cpBody *camera = NULL;
void set_cam_body(cpBody *body) {
camera = body;
}
cpVect cam_pos() {
return camera ? cpBodyGetPosition(camera) : cpvzero;
}
void set_cam_body(cpBody *body) { camera = body; }
cpVect cam_pos() { return camera ? cpBodyGetPosition(camera) : cpvzero; }
static float zoom = 1.f;
float cam_zoom() { return zoom; }

View File

@@ -9,6 +9,8 @@
#include "stb_ds.h"
#include "smbPitchShift.h"
#include "pthread.h"
#define PI 3.14159265
dsp_node *masterbus = NULL;
@@ -22,7 +24,6 @@ void iir_free(struct dsp_iir *iir)
free(iir);
}
void interleave(soundbyte *a, soundbyte *b, soundbyte *stereo, int frames)
{
for (int i = 0; i < frames; i++) {
@@ -117,10 +118,7 @@ void scale_soundbytes(soundbyte *a, float scale, int samples)
for (int i = 0; i < samples; i++) a[i] *= scale;
}
void zero_soundbytes(soundbyte *a, int samples)
{
memset(a, 0, sizeof(soundbyte)*samples);
}
void zero_soundbytes(soundbyte *a, int samples) { memset(a, 0, sizeof(soundbyte)*samples); }
void set_soundbytes(soundbyte *a, soundbyte *b, int samples)
{
@@ -151,13 +149,15 @@ dsp_node *make_node(void *data, void (*proc)(void *data, soundbyte *out, int sam
void node_free(dsp_node *node)
{
YughWarn("FREEING A NODE");
if (node == masterbus) return; /* Simple check to not delete the masterbus */
pthread_mutex_lock(&soundrun);
unplug_node(node);
if (node->data)
if (node->data_free) node->data_free(node->data);
else free(node->data);
free(node);
free(node);
pthread_mutex_unlock(&soundrun);
}
void dsp_node_free(dsp_node *node) { node_free(node); }

View File

@@ -8,6 +8,9 @@
#include "string.h"
#include "time.h"
#include <stdlib.h>
#include "pthread.h"
pthread_mutex_t soundrun = PTHREAD_MUTEX_INITIALIZER;
#include "samplerate.h"
@@ -105,9 +108,10 @@ void change_samplerate(struct wav *w, int rate) {
w->samplerate = rate;
}
void push_sound(soundbyte *buffer, int frames, int chan)
{
void push_sound(soundbyte *buffer, int frames, int chan) {
pthread_mutex_lock(&soundrun);
set_soundbytes(buffer, dsp_node_out(masterbus), frames*chan);
pthread_mutex_unlock(&soundrun);
}
void filter_mod(pocketmod_context *mod, soundbyte *buffer, int frames)

View File

@@ -3,8 +3,10 @@
#include "script.h"
#include "samplerate.h"
#include "pthread.h"
typedef float soundbyte;
extern pthread_mutex_t soundrun;
struct dsp_node;

View File

@@ -56,7 +56,7 @@ int make_sprite(gameobject *go) {
.t = t2d_unit,
.color = color_white,
.emissive = {0,0,0,0},
.tex = texture_loadfromfile(NULL),
.tex = texture_pullfromfile(NULL),
.go = go,
.layer = 0,
.next = -1,
@@ -83,27 +83,7 @@ struct sprite *id2sprite(int id) {
static int sprite_count = 0;
void sprite_flush() {
sprite_count = 0;
}
void sprite_io(struct sprite *sprite, FILE *f, int read) {
char path[100];
if (read) {
// fscanf(f, "%s", &path);
for (int i = 0; i < 100; i++) {
path[i] = fgetc(f);
if (path[i] == '\0') break;
}
fread(sprite, sizeof(*sprite), 1, f);
sprite_loadtex(sprite, path, ST_UNIT);
} else {
fputs(tex_get_path(sprite->tex), f);
fputc('\0', f);
fwrite(sprite, sizeof(*sprite), 1, f);
}
}
void sprite_flush() { sprite_count = 0; }
int sprite_sort(int *a, int *b)
{
@@ -140,7 +120,7 @@ void sprite_loadtex(struct sprite *sprite, const char *path, struct glrect frame
YughWarn("NO SPRITE!");
return;
}
sprite->tex = texture_loadfromfile(path);
sprite->tex = texture_pullfromfile(path);
sprite_setframe(sprite, &frame);
}
@@ -244,19 +224,12 @@ void sprite_draw(struct sprite *sprite) {
HMM_Mat3 sm = transform2d2mat(sprite->t);
tex_draw(sprite->tex, HMM_MulM3(m, sm), sprite->frame, sprite->color, 0, (HMM_Vec2){0,0}, 0, sprite->emissive);
}
void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame) {
if (!sprite) return;
sprite->tex = anim->tex;
sprite->frame = anim->st_frames[frame];
}
void gui_draw_img(const char *img, transform2d t, int wrap, HMM_Vec2 wrapoffset, float wrapscale, struct rgba color) {
sg_apply_pipeline(pip_sprite);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(hudproj));
struct Texture *tex = texture_loadfromfile(img);
struct Texture *tex = texture_pullfromfile(img);
tex_draw(tex, transform2d2mat(t), tex_get_rect(tex), color, wrap, wrapoffset, wrapscale, (struct rgba){0,0,0,0});
}
@@ -264,7 +237,7 @@ void slice9_draw(const char *img, HMM_Vec2 pos, HMM_Vec2 dimensions, struct rgba
{
sg_apply_pipeline(slice9_pipe);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(hudproj));
struct Texture *tex = texture_loadfromfile(img);
struct Texture *tex = texture_pullfromfile(img);
struct glrect r = tex_get_rect(tex);

View File

@@ -28,7 +28,6 @@ void sprite_enabled(int id, int e);
void sprite_io(struct sprite *sprite, FILE *f, int read);
void sprite_loadtex(struct sprite *sprite, const char *path, struct glrect rect);
void sprite_settex(struct sprite *sprite, struct Texture *tex);
void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame);
void sprite_setframe(struct sprite *sprite, struct glrect *frame);
void sprite_initialize();
void sprite_draw(struct sprite *sprite);

View File

@@ -29,10 +29,7 @@ static struct {
} *texhash = NULL;
struct Texture *tex_default;
struct Texture *texture_notex() {
return texture_pullfromfile("icons/no_tex.gif");
}
struct Texture *texture_notex() { return texture_pullfromfile("icons/no_tex.gif"); }
unsigned int next_pow2(unsigned int v)
{
@@ -201,9 +198,7 @@ struct Texture *texture_pullfromfile(const char *path) {
return tex;
}
void texture_sync(const char *path) {
YughWarn("Need to implement texture sync.");
}
void texture_sync(const char *path) { YughWarn("Need to implement texture sync."); }
char *tex_get_path(struct Texture *tex) {
for (int i = 0; i < shlen(texhash); i++) {
@@ -289,94 +284,9 @@ struct Texture *texture_fromdata(void *raw, long size)
struct Texture *texture_loadfromfile(const char *path) {
struct Texture *new = texture_pullfromfile(path);
/*
if (new->id == 0) {
glGenTextures(1, &new->id);
//tex_gpu_load(new);
YughInfo("Loaded texture path %s", path);
}
*/
return new;
}
void tex_gpu_reload(struct Texture *tex) {
tex_gpu_free(tex);
// tex_gpu_load(tex);
}
void anim_calc(struct anim2d *anim) {
anim->size[0] = anim->anim->tex->width * st_s_w(anim->anim->st_frames[anim->frame]);
anim->size[1] = anim->anim->tex->height * st_s_h(anim->anim->st_frames[anim->frame]);
}
void anim_incr(struct anim2d *anim) {
anim->frame = (anim->frame + 1) % arrlen(anim->anim->st_frames);
if (!anim->anim->loop && anim->frame == arrlen(anim->anim->st_frames))
anim_pause(anim);
anim_calc(anim);
}
void anim_decr(struct anim2d *anim) {
anim->frame = (anim->frame + arrlen(anim->anim->st_frames) - 1) % arrlen(anim->anim->st_frames);
anim_calc(anim);
}
struct glrect anim_get_rect(struct anim2d *anim) {
return anim->anim->st_frames[anim->frame];
}
void anim_setframe(struct anim2d *anim, int frame) {
anim->frame = frame;
anim_calc(anim);
}
struct TexAnim *anim2d_from_tex(const char *path, int frames, int fps) {
struct TexAnim *anim = malloc(sizeof(*anim));
anim->tex = texture_loadfromfile(path);
texanim_fromframes(anim, frames);
anim->ms = (float)1 / fps;
return anim;
}
void texanim_fromframes(struct TexAnim *anim, int frames) {
if (anim->st_frames) {
free(anim->st_frames);
}
arrsetlen(anim->st_frames, frames);
float width = (float)1 / frames;
for (int i = 0; i < frames; i++) {
anim->st_frames[i].s0 = width * i;
anim->st_frames[i].s1 = width * (i + 1);
anim->st_frames[i].t0 = 0.f;
anim->st_frames[i].t1 = 1.f;
}
}
void tex_gpu_free(struct Texture *tex) {
/*
if (tex->id != 0) {
glDeleteTextures(1, &tex->id);
tex->id = 0;
}
*/
}
int anim_frames(struct TexAnim *a) {
return arrlen(a->st_frames);
}
struct glrect tex_get_rect(struct Texture *tex) {
return ST_UNIT;
}
struct glrect tex_get_rect(struct Texture *tex) { return ST_UNIT; }
HMM_Vec2 tex_get_dimensions(struct Texture *tex) {
if (!tex) return (HMM_Vec2){0,0};
@@ -386,69 +296,6 @@ HMM_Vec2 tex_get_dimensions(struct Texture *tex) {
return d;
}
void tex_bind(struct Texture *tex) {
/* glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex->id);
glBindTexture(GL_TEXTURE_2D_ARRAY, tex->id);
*/
}
float st_s_w(struct glrect st) { return (st.s1 - st.s0); }
/********************** ANIM2D ****************/
void anim_load(struct anim2d *anim, const char *path) {
anim->anim = &texture_pullfromfile(path)->anim;
anim->anim->tex->opts.animation = 1;
anim_stop(anim);
anim_play(anim);
}
void anim_play(struct anim2d *anim) {
// if (anim->playing)
// return;
// if (anim->frame == anim_frames(anim->anim))
// anim->frame = 0;
// anim->playing = 1;
// if (anim->timer == NULL)
// anim->timer = id2timer(timer_make(1.f / anim->anim->ms, anim_incr, anim, 0, 0));
// else
// timerr_settime(anim->timer, 1.f / anim->anim->ms);
// timer_start(anim->timer);
}
void anim_stop(struct anim2d *anim) {
if (!anim->playing)
return;
anim->playing = 0;
anim->frame = 0;
anim->pausetime = 0;
timer_stop(anim->timer);
}
void anim_pause(struct anim2d *anim) {
if (!anim->playing)
return;
anim->playing = 0;
timer_pause(anim->timer);
}
void anim_fwd(struct anim2d *anim) {
anim_incr(anim);
}
void anim_bkwd(struct anim2d *anim) {
anim_decr(anim);
}
float st_s_w(struct glrect st) {
return (st.s1 - st.s0);
}
float st_s_h(struct glrect st) {
return (st.t1 - st.t0);
}
float st_s_h(struct glrect st) { return (st.t1 - st.t0); }

View File

@@ -33,25 +33,6 @@ struct uvrect {
int v1;
};
/* Tracks a playing animation */
/* Objects should keep this, and just change what TexAnim they are pointing to */
struct anim2d {
int frame;
int playing;
int pausetime;
struct timer *timer;
struct TexAnim *anim;
float size[2]; /* Current size of animation in pixels*/
};
/* Describes an animation on a particular texture */
struct TexAnim {
struct Texture *tex;
struct glrect *st_frames; /* Dynamic array of frames of animation */
int ms;
int loop;
};
struct TextureOptions {
int sprite;
int mips;
@@ -59,7 +40,6 @@ struct TextureOptions {
int animation;
int wrapx;
int wrapy;
};
/* Represents an actual texture on the GPU */
@@ -69,7 +49,6 @@ struct Texture {
uint16_t height;
unsigned char *data;
struct TextureOptions opts;
struct TexAnim anim;
int frames;
};
@@ -79,30 +58,15 @@ struct Image {
};
struct Texture *texture_pullfromfile(const char *path); // Create texture from image
struct Texture *texture_loadfromfile(const char *path); // Create texture & load to gpu
struct Texture *texture_fromdata(void *raw, long size);
/* Hot reloads a texture, if needed */
void texture_sync(const char *path);
struct Texture *str2tex(const char *path);
void tex_gpu_reload(struct Texture *tex); // gpu_free then gpu_load
void tex_gpu_free(struct Texture *tex); // Remove texture data from gpu
void tex_bind(struct Texture *tex); // Bind to gl context
char * tex_get_path(struct Texture *tex); // Get image path for texture
struct TexAnim *anim2d_from_tex(const char *path, int frames, int fps);
void texanim_fromframes(struct TexAnim *anim, int frames);
void anim_load(struct anim2d *anim, const char *path); /* Load and start new animation */
void anim_calc(struct anim2d *anim);
void anim_play(struct anim2d *anim);
void anim_setframe(struct anim2d *anim, int frame);
void anim_stop(struct anim2d *anim);
void anim_pause(struct anim2d *anim);
void anim_fwd(struct anim2d *anim);
void anim_bkwd(struct anim2d *anim);
void anim_incr(struct anim2d *anim);
void anim_decr(struct anim2d *anim);
int gif_nframes(const char *path);
struct glrect tex_get_rect(struct Texture *tex);

View File

@@ -304,7 +304,7 @@ dam->update_activity(dam, &da, NULL, NULL);
if (argc > i+1) argsize++;
}
char cmdstr[argsize];
char cmdstr[argsize+1];
cmdstr[0] = '\0';
for (int i = 0; i < argc; i++) {