From 2731d01bc1c3ab17ac80e7cb027e4fe1d42db5da Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sun, 15 Jan 2023 15:53:50 +0000 Subject: [PATCH] Music and sound --- source/engine/2dphysics.c | 13 ------------- source/engine/editor/editor.c | 2 +- source/engine/ffi.c | 24 ++++++++++++++++++++++-- source/engine/font.c | 8 ++------ source/engine/font.h | 4 ++-- source/engine/sound.c | 11 ++++++----- source/engine/sound.h | 2 +- source/engine/sprite.h | 4 ++-- source/engine/texture.c | 24 ++++++++++++------------ source/engine/texture.h | 27 ++++++++++++++------------- 10 files changed, 62 insertions(+), 57 deletions(-) diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index f4410f0..ceed162 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -481,19 +481,6 @@ static cpBool script_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) { return 1; } -static cpBool s7_phys_cb_presolve(cpArbiter *arb, cpSpace *space, void *data) { - -} - -static void s7_phys_cb_postsolve(cpArbiter *arb, cpSpace *space, void *data) { - -} - -static void s7_phys_cb_separate(cpArbiter *Arb, cpSpace *space, void *data) { - //struct gameobject *go = data; - //script_call_sym(go->cbs->separate); -} - void phys2d_add_handler_type(int cmd, int go, struct callee c) { cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, go); diff --git a/source/engine/editor/editor.c b/source/engine/editor/editor.c index 65bdcd2..2d58d96 100644 --- a/source/engine/editor/editor.c +++ b/source/engine/editor/editor.c @@ -95,7 +95,7 @@ static int grid1_draw = true; static int grid2_draw = true; static float tex_scale = 1.f; -static struct TexAnimation tex_gui_anim = {0}; +static struct anim2d tex_gui_anim = {0}; char current_level[MAXNAME] = {'\0'}; char levelname[MAXNAME] = {'\0'}; diff --git a/source/engine/ffi.c b/source/engine/ffi.c index 5814ec5..ae47664 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -15,6 +15,7 @@ #include "anim.h" #include "yugine.h" #include "nuke.h" +#include "sound.h" #include "font.h" cpVect duk2vec2(duk_context *duk, int p) { @@ -118,6 +119,22 @@ duk_ret_t duk_cmd(duk_context *duk) { play_song(duk_to_string(duk, 1), duk_to_string(duk, 2)); break; + case 14: + play_sound(make_sound(duk_to_string(duk, 1))); + break; + + case 15: + + break; + + case 16: + + break; + + case 17: + + break; + } return 0; @@ -347,7 +364,10 @@ duk_ret_t duk_make_sprite(duk_context *duk) { duk_ret_t duk_make_anim2d(duk_context *duk) { int go = duk_to_int(duk, 0); const char *path = duk_to_string(duk, 1); - cpVect pos = duk2vec2(duk, 2); + int frames = duk_to_int(duk, 2); + int fps = duk_to_int(duk, 3); + + return 0; } @@ -437,7 +457,7 @@ void ffi_load() DUK_FUNC(win_make, 3); DUK_FUNC(make_sprite, 3); - DUK_FUNC(make_anim2d, 3); + DUK_FUNC(make_anim2d, 4); DUK_FUNC(make_box2d, 3); DUK_FUNC(make_circle2d, 3); DUK_FUNC(cmd, DUK_VARARGS); diff --git a/source/engine/font.c b/source/engine/font.c index 537317e..e169d8c 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -22,7 +22,7 @@ static uint32_t VAO = 0; struct sFont *font; static struct shader *shader; -char *slurp_file(const char *filename) { +unsigned char *slurp_file(const char *filename) { FILE *f = fopen(filename, "rb"); if (!f) return NULL; @@ -31,13 +31,12 @@ char *slurp_file(const char *filename) { long fsize = ftell(f); fseek(f, 0, SEEK_SET); unsigned char *slurp = malloc(fsize); - size_t r = fread(slurp, fsize, 1, f); fclose(f); return slurp; } -char *slurp_text(const char *filename) { +unsigned char *slurp_text(const char *filename) { FILE *f = fopen(filename, "r'"); if (!f) return NULL; @@ -117,9 +116,6 @@ struct sFont *MakeFont(const char *fontfile, int height) YughError("Failed to make font %s", fontfile); } - - - float scale = stbtt_ScaleForPixelHeight(&fontinfo, height); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, &newfont->texID); glBindTexture(GL_TEXTURE_2D, newfont->texID); diff --git a/source/engine/font.h b/source/engine/font.h index 0505f25..ece091d 100644 --- a/source/engine/font.h +++ b/source/engine/font.h @@ -31,7 +31,7 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct void text_settype(struct sFont *font); void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw); -char *slurp_file(const char *filename); -char *slurp_text(const char *filename); +unsigned char *slurp_file(const char *filename); +unsigned char *slurp_text(const char *filename); #endif diff --git a/source/engine/sound.c b/source/engine/sound.c index 5239a68..3dfac30 100644 --- a/source/engine/sound.c +++ b/source/engine/sound.c @@ -118,7 +118,6 @@ void print_devices() void sound_init() { - PaError err = Pa_Initialize(); check_pa_err(err); @@ -151,18 +150,20 @@ void audio_close() //Mix_CloseAudio(); } -struct wav make_sound(const char *wav) +static struct wav mwav; + +struct wav *make_sound(const char *wav) { - struct wav mwav; - mwav.data = drwav_open_file_and_read_pcm_frames_s16("sounds/alert.wav", &mwav.ch, &mwav.samplerate, &mwav.frames, NULL); + mwav.data = drwav_open_file_and_read_pcm_frames_s16(wav, &mwav.ch, &mwav.samplerate, &mwav.frames, NULL); if (mwav.samplerate != SAMPLERATE) { + YughInfo("Changing samplerate of %s.", wav); mwav = change_samplerate(mwav, 48000); } mwav.gain = 1.f; - return mwav; + return &mwav; } struct soundstream *soundstream_make() diff --git a/source/engine/sound.h b/source/engine/sound.h index 50078da..43e5afb 100644 --- a/source/engine/sound.h +++ b/source/engine/sound.h @@ -61,7 +61,7 @@ void audio_close(); void sound_fillbuf(struct sound *s, short *buf, int n); -struct wav make_sound(const char *wav); +struct wav *make_sound(const char *wav); void wav_norm_gain(struct wav *w, double lv); struct sound *play_sound(struct wav *wav); diff --git a/source/engine/sprite.h b/source/engine/sprite.h index 46d9f8d..23b0122 100644 --- a/source/engine/sprite.h +++ b/source/engine/sprite.h @@ -8,7 +8,6 @@ struct datastream; struct gameobject; -struct Texture; struct sprite { mfloat_t pos[2]; @@ -17,11 +16,12 @@ struct sprite { mfloat_t color[3]; int go; int id; - struct TexAnimation anim; + struct anim2d anim; struct Texture *tex; int next; }; + int make_sprite(int go); struct sprite *id2sprite(int id); void sprite_delete(int id); diff --git a/source/engine/texture.c b/source/engine/texture.c index 6d7d8e0..66d5185 100644 --- a/source/engine/texture.c +++ b/source/engine/texture.c @@ -134,38 +134,38 @@ void tex_gpu_reload(struct Texture *tex) //tex_gpu_load(tex); } -void tex_incr_anim(struct TexAnimation *tex_anim) +void tex_incr_anim(struct anim2d *tex_anim) { anim_incr(tex_anim); - if (!tex_anim->loop && tex_anim->frame == arrlen(tex_anim->anim->st_frames)) + if (!tex_anim->anim->loop && tex_anim->frame == arrlen(tex_anim->anim->st_frames)) anim_pause(tex_anim); } -void anim_incr(struct TexAnimation *anim) +void anim_incr(struct anim2d *anim) { anim->frame = (anim->frame + 1) % arrlen(anim->anim->st_frames); //tex_anim_calc_uv(anim); } -void anim_decr(struct TexAnimation *anim) +void anim_decr(struct anim2d *anim) { anim->frame = (anim->frame + arrlen(anim->anim->st_frames) - 1) % arrlen(anim->anim->st_frames); //tex_anim_calc_uv(anim); } -struct glrect anim_get_rect(struct TexAnimation *anim) +struct glrect anim_get_rect(struct anim2d *anim) { return anim->anim->st_frames[anim->frame]; } -void anim_setframe(struct TexAnimation *anim, int frame) +void anim_setframe(struct anim2d *anim, int frame) { anim->frame = frame; //tex_anim_calc_uv(anim); } -void tex_anim_set(struct TexAnimation *anim) +void tex_anim_set(struct anim2d *anim) { if (anim->playing) { timer_remove(anim->timer); @@ -203,7 +203,7 @@ void tex_bind(struct Texture *tex) glBindTexture(GL_TEXTURE_2D_ARRAY, tex->id); } -void anim_play(struct TexAnimation *anim) +void anim_play(struct anim2d *anim) { if (anim->playing) return; @@ -221,7 +221,7 @@ void anim_play(struct TexAnimation *anim) timer_start(anim->timer); } -void anim_stop(struct TexAnimation *anim) +void anim_stop(struct anim2d *anim) { if (!anim->playing) return; @@ -233,7 +233,7 @@ void anim_stop(struct TexAnimation *anim) //tex_anim_calc_uv(anim); } -void anim_pause(struct TexAnimation *anim) +void anim_pause(struct anim2d *anim) { if (!anim->playing) return; @@ -242,12 +242,12 @@ void anim_pause(struct TexAnimation *anim) timer_pause(anim->timer); } -void anim_fwd(struct TexAnimation *anim) +void anim_fwd(struct anim2d *anim) { anim_incr(anim); } -void anim_bkwd(struct TexAnimation *anim) +void anim_bkwd(struct anim2d *anim) { anim_decr(anim); } diff --git a/source/engine/texture.h b/source/engine/texture.h index a0a45f1..e5cfafa 100644 --- a/source/engine/texture.h +++ b/source/engine/texture.h @@ -30,13 +30,13 @@ struct uvrect { }; /* Tracks a playing animation */ -struct TexAnimation { +struct anim2d { int frame; int playing; int pausetime; struct timer *timer; struct TexAnim *anim; - int loop; + }; /* Describes an animation on a particular texture */ @@ -44,6 +44,7 @@ struct TexAnim { struct glrect *st_frames; /* Dynamic array of frames of animation */ int ms; struct Texture *tex; + int loop; }; struct TextureOptions { @@ -70,20 +71,20 @@ void tex_bind(struct Texture *tex); // Bind to gl context char * tex_get_path(struct Texture *tex); // Get image path for texture -void anim_play(struct TexAnimation *anim); -void anim_setframe(struct TexAnimation *anim, int frame); -void anim_stop(struct TexAnimation *anim); -void anim_pause(struct TexAnimation *anim); -void anim_fwd(struct TexAnimation *anim); -void anim_bkwd(struct TexAnimation *anim); -void anim_incr(struct TexAnimation *anim); -void anim_decr(struct TexAnimation *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); -void tex_incr_anim(struct TexAnimation *tex_anim); -void tex_anim_set(struct TexAnimation *anim); +void tex_incr_anim(struct anim2d *tex_anim); +void tex_anim_set(struct anim2d *anim); struct glrect tex_get_rect(struct Texture *tex); -struct glrect anim_get_rect(struct TexAnimation *anim); +struct glrect anim_get_rect(struct anim2d *anim); int anim_frames(struct TexAnim *a);