From 3018a1a1886ae7220060149b89d05f46db656128 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 10 Mar 2023 19:13:48 +0000 Subject: [PATCH] Many fixes --- source/engine/2dphysics.c | 77 +++++++++++++++++++++++---------- source/engine/2dphysics.h | 7 ++- source/engine/debug/debugdraw.c | 19 ++------ source/engine/debug/debugdraw.h | 2 +- source/engine/debug/log.h | 2 +- source/engine/ffi.c | 63 +++++++++++++++++++++------ source/engine/font.c | 62 +++++++++++++++++++++----- source/engine/font.h | 3 +- source/engine/gameobject.c | 21 ++++----- source/engine/gameobject.h | 1 + source/engine/input.c | 15 ++++++- source/engine/input.h | 3 ++ source/engine/nuke.c | 6 ++- source/engine/nuke.h | 1 + source/engine/openglrender.c | 2 +- source/engine/sound.c | 17 +++----- source/engine/sound/mix.c | 26 ++++++++++- source/engine/yugine.c | 15 +++---- source/engine/yugine.h | 3 +- source/shaders/textfrag.glsl | 3 +- 20 files changed, 243 insertions(+), 105 deletions(-) diff --git a/source/engine/2dphysics.c b/source/engine/2dphysics.c index 0e391e30..1f1eca19 100644 --- a/source/engine/2dphysics.c +++ b/source/engine/2dphysics.c @@ -30,6 +30,13 @@ float dynamic_color[3] = {255/255, 70/255, 46/255}; float kinematic_color[3] = {255/255, 206/255,71/255}; float static_color[3] = {0.22f, 0.271f, 1.f}; +unsigned int category_masks[32]; + +void set_cat_mask(int cat, unsigned int mask) +{ + category_masks[cat] = mask; +} + void color2float(struct color color, float *fcolor) { fcolor[0] = (float)color.r/255; @@ -565,9 +572,9 @@ void phys2d_dbgdrawedge(struct phys2d_edge *edge) void shape_enabled(struct phys2d_shape *shape, int enabled) { if (enabled) - cpShapeSetFilter(shape->shape, CP_SHAPE_FILTER_ALL); + cpShapeSetFilter(shape->data, CP_SHAPE_FILTER_ALL); else - cpShapeSetFilter(shape->shape, CP_SHAPE_FILTER_NONE); + cpShapeSetFilter(shape->data, CP_SHAPE_FILTER_NONE); } int shape_is_enabled(struct phys2d_shape *shape) @@ -606,6 +613,15 @@ void register_collide(void *sym) { } +struct hit_call { + cpVect norm; + struct callee c; + int hit; +}; + +struct hit_call *frame_hits; + + void duk_call_phys_cb(cpVect norm, struct callee c, int hit) { duk_push_heapptr(duk, c.fn); @@ -619,6 +635,9 @@ void duk_call_phys_cb(cpVect norm, struct callee c, int hit) duk_push_int(duk, hit); duk_put_prop_literal(duk, obj, "hit"); +/* vect2duk(cpArbiterGetSurfaceVelocity(arb)); + duk_put_prop_literal(duk, obj, "velocity"); +*/ duk_call_method(duk,1); // if (duk_pcall_method(duk, 1)) @@ -626,9 +645,28 @@ void duk_call_phys_cb(cpVect norm, struct callee c, int hit) duk_pop(duk); } +void push_phys_cb(cpVect norm, struct callee c, int hit) +{ + struct hit_call newhit; + newhit.norm = norm; + newhit.c = c; + newhit.hit = hit; + + arrpush(frame_hits, newhit); +} + +void fire_hits() +{ + if (arrlen(frame_hits) == 0) return; + + for (int i = 0; i < arrlen(frame_hits); i++) + duk_call_phys_cb(frame_hits[i].norm, frame_hits[i].c, frame_hits[i].hit); + + arrfree(frame_hits); +} + static cpBool script_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) { - cpBody *body1; cpBody *body2; cpArbiterGetBodies(arb, &body1, &body2); @@ -642,26 +680,19 @@ static cpBool script_phys_cb_begin(cpArbiter *arb, cpSpace *space, void *data) struct gameobject *go = id2go(g1); struct gameobject *go2 = id2go(g2); + struct phys2d_shape *pshape1 = cpShapeGetUserData(shape1); + struct phys2d_shape *pshape2 = cpShapeGetUserData(shape2); + cpVect norm1 = cpArbiterGetNormal(arb); + cpVect vel1 = cpArbiterGetSurfaceVelocity(arb); for (int i = 0; i < arrlen(go->shape_cbs); i++) - duk_call_phys_cb(norm1, go->shape_cbs[i].cbs.begin, g2); - + if (go->shape_cbs[i].shape == pshape1) + duk_call_phys_cb(norm1, go->shape_cbs[i].cbs.begin, g2); + if (go->cbs.begin.obj) duk_call_phys_cb(norm1, go->cbs.begin, g2); - return; - - cpVect norm2 = norm1; - norm2.x *= -1; - norm2.y *= -1; - - for (int i = 0; i < arrlen(go2->shape_cbs); i++) - duk_call_phys_cb(norm2, go2->shape_cbs[i].cbs.begin, g1); - - if (go2->cbs.begin.obj) - duk_call_phys_cb(norm2, go2->cbs.begin, g1); - return 1; } @@ -673,14 +704,16 @@ void phys2d_rm_go_handlers(int go) handler->separateFunc = NULL; } +void phys2d_setup_handlers(int go) +{ + cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, go); + handler->userData = go; + handler->beginFunc = script_phys_cb_begin; +} + void phys2d_add_handler_type(int cmd, int go, struct callee c) { - cpCollisionHandler *handler = cpSpaceAddWildcardHandler(space, go); - - handler->userData = go; - switch (cmd) { case 0: - handler->beginFunc = script_phys_cb_begin; id2go(go)->cbs.begin = c; break; diff --git a/source/engine/2dphysics.h b/source/engine/2dphysics.h index bf145a68..3c99d244 100644 --- a/source/engine/2dphysics.h +++ b/source/engine/2dphysics.h @@ -112,10 +112,12 @@ struct phys_cbs { }; struct shape_cb { - cpShape *shape; + struct phys2d_shape *shape; struct phys_cbs cbs; }; +void fire_hits(); + void phys2d_add_handler_type(int cmd, int go, struct callee c); void register_collide(void *sym); void phys2d_rm_go_handlers(int go); @@ -136,9 +138,12 @@ void color2float(struct color, float *fcolor); struct color float2color(float *fcolor); void shape_gui(struct phys2d_shape *shape); +void phys2d_setup_handlers(int go); void phys2d_reindex_body(cpBody *body); cpVect world2go(struct gameobject *go, cpVect worldpos); cpVect go2world(struct gameobject *go, cpVect gopos); +extern unsigned int category_masks[32]; +void set_cat_mask(int cat, unsigned int mask); #endif diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 68da9bc1..e02bbf95 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -255,24 +255,13 @@ void draw_grid(int width, int span) void draw_point(int x, int y, float r, float *color) { - shader_use(circleShader); - - float verts[] = { x, y }; - - glBindBuffer(GL_ARRAY_BUFFER, circleVBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_DYNAMIC_DRAW); - glPointSize(r); - - glBindVertexArray(circleVAO); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); - glDrawArrays(GL_POINTS, 0, 4); + draw_circle(x,y,r,r,color,1); } -void draw_cppoint(struct cpVect point, float r) +void draw_cppoint(struct cpVect point, float r, struct color color) { - float white[3] = {1.f, 1.f, 1.f}; - draw_point(point.x, point.y, r, white); + float col[3] = {(float)color.r/255, (float)color.g/255, (float)color.b/255}; + draw_point(point.x, point.y, r, col); } void draw_points(struct cpVect *points, int n, float size, float *color) diff --git a/source/engine/debug/debugdraw.h b/source/engine/debug/debugdraw.h index f30dbd02..dd66512f 100644 --- a/source/engine/debug/debugdraw.h +++ b/source/engine/debug/debugdraw.h @@ -13,7 +13,7 @@ void draw_grid(int width, int span); void draw_rect(int x, int y, int w, int h, float *color); void draw_box(struct cpVect c, struct cpVect wh, struct color color); void draw_point(int x, int y, float r, float *color); -void draw_cppoint(struct cpVect point, float r); +void draw_cppoint(struct cpVect point, float r, struct color color); void draw_poly(float *points, int n, float *color); diff --git a/source/engine/debug/log.h b/source/engine/debug/log.h index ff91bf05..b9b1656b 100644 --- a/source/engine/debug/log.h +++ b/source/engine/debug/log.h @@ -3,7 +3,7 @@ #include -#define ERROR_BUFFER 2048 +#define ERROR_BUFFER 1024*1024 #define LOG_INFO 0 #define LOG_WARN 1 diff --git a/source/engine/ffi.c b/source/engine/ffi.c index eeeedbca..7414b157 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -105,16 +105,13 @@ cpBitmask duk2bitmask(duk_context *duk, int p) for (int i = 0; i < len; i++) { duk_get_prop_index(duk, p, i); - int val = duk_to_int(duk, -1); + int val = duk_to_boolean(duk, -1); duk_pop(duk); + if (!val) continue; - if (val > 10) continue; - - mask |= 1<layer = duk_to_int(duk,2); + return 0; + + case 76: + set_cat_mask(duk_to_int(duk,1), duk2bitmask(duk,2)); + return 0; + + case 77: + input_to_game(); + break; + + case 78: + input_to_nuke(); + break; + + case 79: + duk_push_boolean(duk, phys_stepping()); + return 1; } return 0; @@ -846,7 +879,7 @@ duk_ret_t duk_register(duk_context *duk) { void gameobject_add_shape_collider(int go, struct callee c, struct phys2d_shape *shape) { struct shape_cb shapecb; - shapecb.shape = shape->shape; + shapecb.shape = shape; shapecb.cbs.begin = c; arrpush(id2go(go)->shape_cbs, shapecb); } @@ -860,11 +893,12 @@ duk_ret_t duk_register_collide(duk_context *duk) { switch(cmd) { case 0: - phys2d_add_handler_type(cmd, go, c); + id2go(go)->cbs.begin = c; break; case 1: gameobject_add_shape_collider(go, c, duk_get_pointer(duk,4)); + YughInfo("Adding gameobject %d shape collider for shape %p", go, duk_get_pointer(duk,4)); break; case 2: @@ -1045,6 +1079,10 @@ duk_ret_t duk_q_body(duk_context *duk) { case 5: duk_push_number(duk, cpBodyGetMass(go->body)); return 1; + + case 6: + duk_push_number(duk, cpBodyGetMoment(go->body)); + return 1; } return 0; @@ -1322,9 +1360,8 @@ void ffi_load() DUK_FUNC(gui_text, DUK_VARARGS); DUK_FUNC(ui_text, 4); + DUK_FUNC(cursor_text,5); DUK_FUNC(gui_img, 2); - - DUK_FUNC(anim, 2); } diff --git a/source/engine/font.c b/source/engine/font.c index 7f24268e..0d8eca3a 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -164,6 +164,20 @@ struct sFont *MakeFont(const char *fontfile, int height) static int curchar = 0; static float *buffdraw; +void draw_char_box(struct Character c, float cursor[2], float scale, float color[3]) +{ + int x, y, w, h; + + x = cursor[0]; + y = cursor[1]; + w = 8*scale; + h = 14; + x += w/2.f; + y += h/2.f; + + draw_rect(x,y,w,h,color); +} + void fill_charverts(float *verts, float cursor[2], float scale, struct Character c, float *offset) { float w = c.Size[0] * scale; @@ -182,6 +196,8 @@ void fill_charverts(float *verts, float cursor[2], float scale, struct Character memcpy(verts, v, sizeof(float)*16); } +static int drawcaret = 0; + void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct shader *shader, float color[3]) { float shadowcolor[3] = {0.f, 0.f, 0.f}; @@ -191,11 +207,24 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct float offset[2] = {-1, 1}; fill_charverts(verts, cursor, scale, c, offset); + curchar++; /* Check if the vertex is off screen */ if (verts[5] < 0 || verts[10] < 0 || verts[0] > window_i(0)->width || verts[1] > window_i(0)->height) return; + if (drawcaret == curchar) { + draw_char_box(c, cursor, scale, color); + shader_use(shader); + shader_setvec3(shader, "textColor", color); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, font->texID); + glBindVertexArray(VAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + + } + + shader_setvec3(shader, "textColor", shadowcolor); glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); @@ -224,7 +253,7 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STREAM_DRAW); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - curchar++; + } void text_settype(struct sFont *mfont) @@ -232,41 +261,52 @@ void text_settype(struct sFont *mfont) font = mfont; } -void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw) +void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw, int caret) { - shader_use(shader); - shader_setvec3(shader, "textColor", color); - int len = strlen(text); + drawcaret = caret; mfloat_t cursor[2] = { 0.f }; cursor[0] = pos[0]; cursor[1] = pos[1]; - + shader_use(shader); + shader_setvec3(shader, "textColor", color); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, font->texID); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, len*16*sizeof(float)*2, NULL, GL_STREAM_DRAW); /* x2 on the size for the outline pass */ - - const unsigned char *line, *wordstart; - line = (unsigned char*)text; + + const unsigned char *line, *wordstart, *drawstart; + line = drawstart = (unsigned char*)text; curchar = 0; + float *usecolor = color; + float caretcolor[3] = {0.4,0.98,0.75}; + while (*line != '\0') { switch (*line) { case '\n': + sdrawCharacter(font->Characters[*line], cursor, scale, shader, usecolor); cursor[1] -= scale * font->height; + cursor[0] = pos[0]; line++; break; case ' ': - sdrawCharacter(font->Characters[*line], cursor, scale, shader, color); + sdrawCharacter(font->Characters[*line], cursor, scale, shader, usecolor); cursor[0] += font->Characters[*line].Advance * scale; line++; break; + + case '\t': + sdrawCharacter(font->Characters[*line], cursor, scale, shader, usecolor); + cursor[0] += font->Characters[*line].Advance * scale; + line++; + break; + default: wordstart = line; @@ -283,7 +323,7 @@ void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3 } while (wordstart < line) { - sdrawCharacter(font->Characters[*wordstart], cursor, scale, shader, color); + sdrawCharacter(font->Characters[*wordstart], cursor, scale, shader, usecolor); cursor[0] += font->Characters[*wordstart].Advance * scale; wordstart++; } diff --git a/source/engine/font.h b/source/engine/font.h index 1079356f..5b74cfb5 100644 --- a/source/engine/font.h +++ b/source/engine/font.h @@ -13,6 +13,7 @@ struct Character { mfloat_t Bearing[2]; // Offset from baseline to left/top of glyph unsigned int Advance; // Horizontal offset to advance to next glyph struct glrect rect; + }; struct sFont { @@ -27,7 +28,7 @@ void font_frame(struct window *w); struct sFont *MakeFont(const char *fontfile, int height); void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct shader *shader, float color[3]); void text_settype(struct sFont *font); -void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw); +void renderText(const char *text, mfloat_t pos[2], float scale, mfloat_t color[3], float lw,int caret); unsigned char *slurp_file(const char *filename); char *slurp_text(const char *filename); diff --git a/source/engine/gameobject.c b/source/engine/gameobject.c index 696260c3..bb0a55e1 100644 --- a/source/engine/gameobject.c +++ b/source/engine/gameobject.c @@ -95,11 +95,13 @@ void go_shape_apply(cpBody *body, cpShape *shape, struct gameobject *go) { cpShapeSetFriction(shape, go->f); cpShapeSetElasticity(shape, go->e); -// cpShapeSetSensor(shape, go->sensor); cpShapeSetCollisionType(shape, go2id(go)); - -// cpShapeSetFilter(shape, go->filter); + cpShapeFilter filter; + filter.group = go2id(go); + filter.categories = 1<layer; + filter.mask = category_masks[go->layer]; + cpShapeSetFilter(shape, filter); } void go_shape_moi(cpBody *body, cpShape *shape, struct gameobject *go) @@ -170,10 +172,8 @@ int MakeGameobject() *id2go(retid) = go; } - go.filter.group = retid; - go.filter.mask = CP_ALL_CATEGORIES; - go.filter.categories = CP_ALL_CATEGORIES; cpBodySetUserData(go.body, (int)retid); + phys2d_setup_handlers(retid); return retid; } @@ -205,26 +205,21 @@ void gameobject_delete(int id) id2go(id)->next = first; first = id; - if (cpSpaceIsLocked(space)) { - YughInfo("Space is simulating; adding %d to queue ...", id); + if (cpSpaceIsLocked(space)) arrpush(go_toclean, id); - } else gameobject_clean(id); } void gameobjects_cleanup() { - for (int i = 0; i < arrlen(go_toclean); i++) { - YughInfo("Cleaning object %d", go_toclean[i]); + for (int i = 0; i < arrlen(go_toclean); i++) gameobject_clean(go_toclean[i]); - } arrsetlen(go_toclean, 0); return; int clean = first; - YughInfo("Initiating a clean"); while (clean >= 0 && id2go(clean)->body) { gameobject_clean(clean); diff --git a/source/engine/gameobject.h b/source/engine/gameobject.h index 0c2c963f..c69030a1 100644 --- a/source/engine/gameobject.h +++ b/source/engine/gameobject.h @@ -22,6 +22,7 @@ struct gameobject { int flipx; /* 1 or -1 */ int flipy; int sensor; + unsigned int layer; cpShapeFilter filter; cpBody *body; /* NULL if this object is dead */ int id; diff --git a/source/engine/input.c b/source/engine/input.c index 814820e6..d03a7f0f 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -128,12 +128,24 @@ void char_cb(GLFWwindow *w, unsigned int codepoint) } } +static GLFWcharfun nukechar; + void input_init() { glfwSetCursorPosCallback(mainwin->window, cursor_pos_cb); glfwSetScrollCallback(mainwin->window, scroll_cb); glfwSetMouseButtonCallback(mainwin->window, mb_cb); - glfwSetCharCallback(mainwin->window, char_cb); + nukechar = glfwSetCharCallback(mainwin->window, char_cb); +} + +void input_to_nuke() +{ + glfwSetCharCallback(mainwin->window, nukechar); +} + +void input_to_game() +{ + glfwSetCharCallback(mainwin->window, char_cb); } void call_input_signal(char *signal) { @@ -309,7 +321,6 @@ void input_poll(double wait) glfwWaitEventsTimeout(wait); - //editor_input(&e); for (int i = 0; i < arrlen(downkeys); i++) call_input_down(&downkeys[i]); } diff --git a/source/engine/input.h b/source/engine/input.h index b60c074b..e68de938 100644 --- a/source/engine/input.h +++ b/source/engine/input.h @@ -38,4 +38,7 @@ struct inputaction void set_pawn(void *pawn); void remove_pawn(void *pawn); +void input_to_nuke(); +void input_to_game(); + #endif diff --git a/source/engine/nuke.c b/source/engine/nuke.c index af9d5eb9..63f1c9a2 100644 --- a/source/engine/nuke.c +++ b/source/engine/nuke.c @@ -31,7 +31,6 @@ void nuke_init(struct window *win) { ctx = nk_glfw3_init(&nkglfw, win->window, NK_GLFW3_INSTALL_CALLBACKS); - struct nk_font_atlas *atlas; nk_glfw3_font_stash_begin(&nkglfw, &atlas); struct nk_font *noto = nk_font_atlas_add_from_file(atlas, "fonts/teenytinypixels.tff", 14, 0); @@ -66,6 +65,11 @@ struct nk_rect nuke_win_get_bounds() { return nk_window_get_bounds(ctx); } +void nuke_row(int height) +{ + nk_layout_row_dynamic(ctx, height, 1); +} + void nuke_property_float3(const char *label, float min, float *val, float max, float step, float dragstep) { nk_layout_row_dynamic(ctx, 25, 1); nk_label(ctx, label, NK_TEXT_LEFT); diff --git a/source/engine/nuke.h b/source/engine/nuke.h index 77c43ed5..c99922b5 100644 --- a/source/engine/nuke.h +++ b/source/engine/nuke.h @@ -23,6 +23,7 @@ void nuke_property_int(const char *lbl, int min, int *val, int max, int step); void nuke_radio_btn(const char *lbl, int *val, int cmp); void nuke_checkbox(const char *lbl, int *val); void nuke_nel(int cols); +void nuke_row(int height); void nuke_label(const char *s); void nuke_prop_float(const char *label, float min, float *val, float max, float step, float dragstep); void nuke_edit_str(char *str); diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index 01ec6997..5df318b4 100644 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -186,7 +186,7 @@ void openglRender(struct window *window) glDisable(GL_DEPTH_TEST); //// DEBUG if (debugDrawPhysics) - gameobject_draw_debugs(); + gameobject_draw_debugs(); ////// TEXT && GUI glBindBuffer(GL_UNIFORM_BUFFER, projUBO); diff --git a/source/engine/sound.c b/source/engine/sound.c index 43c5dffd..dad7adfb 100644 --- a/source/engine/sound.c +++ b/source/engine/sound.c @@ -139,17 +139,15 @@ void print_devices() void sound_init() { - return; mixer_init(); - PaError err = Pa_Initialize(); - check_pa_err(err); + PaError err = Pa_Initialize(); + check_pa_err(err); err = Pa_OpenDefaultStream(&stream_def, 0, CHANNELS, paInt16, SAMPLERATE, BUF_FRAMES, patestCallback, NULL); check_pa_err(err); err = Pa_StartStream(stream_def); check_pa_err(err); - } struct wav *make_sound(const char *wav) @@ -160,10 +158,9 @@ struct wav *make_sound(const char *wav) struct wav mwav; 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 from %d to %d.", wav, mwav.samplerate, SAMPLERATE); - mwav = change_samplerate(mwav, SAMPLERATE); +// mwav = change_samplerate(mwav, SAMPLERATE); } if (mwav.ch != CHANNELS) { @@ -205,10 +202,12 @@ void kill_oneshot(struct sound *s) free(s); } -void play_oneshot(struct wav *wav) { +void play_oneshot(struct wav *wav) { struct sound *new = malloc(sizeof(*new)); new->data = wav; new->bus = first_free_bus(dsp_filter(new, sound_fillbuf)); + YughInfo("Playing sound ..."); + YughInfo("Bus is on? %d", new->bus->on); new->playing=1; new->loop=0; new->frame = 0; @@ -224,7 +223,6 @@ struct sound *play_sound(struct wav *wav) new->playing = 1; return new; - } int sound_playing(const struct sound *s) @@ -292,7 +290,6 @@ void sound_fillbuf(struct sound *s, short *buf, int n) short *in = s->data->data; for (int i = 0; i < n; i++) { for (int j = 0; j < CHANNELS; j++) buf[i*CHANNELS+j] = in[s->frame+j] * gainmult; - s->frame++; if (s->frame == s->data->frames) { @@ -309,8 +306,6 @@ void mp3_fillbuf(struct sound *s, short *buf, int n) } - - void soundstream_fillbuf(struct soundstream *s, short *buf, int n) { int max = s->buf->write - s->buf->read; diff --git a/source/engine/sound/mix.c b/source/engine/sound/mix.c index a1c23bab..69faf45e 100644 --- a/source/engine/sound/mix.c +++ b/source/engine/sound/mix.c @@ -37,8 +37,17 @@ void mixer_init() { } struct bus *first_free_bus(struct dsp_filter in) { - if (!initted) return NULL; assert(initted); + + for (int i = 0; i < 255; i++) + if (!bus[i].on) { + bus[i].on = 1; + bus[i].in = in; + YughInfo("Returning bus %d", i); + return &bus[i]; + } + + return NULL; if (first == -1) return NULL; int ret = first; @@ -58,6 +67,10 @@ struct bus *first_free_bus(struct dsp_filter in) { void bus_free(struct bus *b) { + YughInfo("Freeing bus %d", b->id); + b->on = 0; + return; + if (first_on == b->id) first_on = b->next; if (b->next != -1) bus[b->next].prev = b->prev; if (b->prev != -1) bus[b->prev].next = b->next; @@ -69,9 +82,18 @@ void bus_free(struct bus *b) void bus_fill_buffers(short *master, int n) { int curbus = first_on; - if (curbus == -1) return; +// if (curbus == -1) return; memset(master, 0, BUF_FRAMES*CHANNELS*sizeof(short)); + for (int i = 0; i < 255; i++) { + if (!bus[i].on) continue; + dsp_run(bus[i].in, bus[i].buf, BUF_FRAMES); + for (int j = 0; j < BUF_FRAMES*CHANNELS; j++) + master[j] += bus[i].buf[j] * master_volume; + } + + return; + while (curbus != -1) { int nextbus = bus[curbus].next; /* Save this in case busses get changed during fill */ dsp_run(bus[curbus].in, bus[curbus].buf, BUF_FRAMES); diff --git a/source/engine/yugine.c b/source/engine/yugine.c index fed16333..60a903fc 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -47,6 +47,7 @@ double updateMS = 1/60.f; static int ed = 1; static int sim_play = 0; static double lastTick; +static int phys_step = 0; static float timescale = 1.f; @@ -174,15 +175,10 @@ int main(int argc, char **args) { window_set_icon("icon.png"); - if (ed) { - editor_init(MakeSDLWindow("Editor", 600, 600, 0)); - } else { - script_dofile("game.js"); - } + script_dofile("game.js"); input_init(); openglInit(); - sim_stop(); while (!want_quit()) { double elapsed = glfwGetTime() - lastTick; deltaT = elapsed; @@ -199,12 +195,14 @@ int main(int argc, char **args) { timer_update(elapsed); physlag += elapsed; call_updates(elapsed * timescale); - while (physlag >= physMS) { + phys_step = 1; physlag -= physMS; phys2d_update(physMS * timescale); call_physics(physMS * timescale); + fire_hits(); if (sim_play == SIM_STEP) sim_pause(); + phys_step = 0; } } @@ -216,7 +214,6 @@ int main(int argc, char **args) { } gameobjects_cleanup(); - } return 0; @@ -249,6 +246,8 @@ void sim_stop() { sim_play = SIM_STOP; } +int phys_stepping() { return phys_step; } + void sim_step() { if (sim_paused()) { YughInfo("Step"); diff --git a/source/engine/yugine.h b/source/engine/yugine.h index 24bf0818..a16cc3b6 100644 --- a/source/engine/yugine.h +++ b/source/engine/yugine.h @@ -7,9 +7,10 @@ void sim_start(); void sim_pause(); void sim_stop(); void sim_step(); +int phys_stepping(); void set_timescale(float val); int frame_fps(); -#endif \ No newline at end of file +#endif diff --git a/source/shaders/textfrag.glsl b/source/shaders/textfrag.glsl index 3519e1f7..694e96ec 100644 --- a/source/shaders/textfrag.glsl +++ b/source/shaders/textfrag.glsl @@ -4,11 +4,12 @@ out vec4 color; uniform sampler2D text; uniform vec3 textColor; +uniform bool invert; void main() { vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); color = vec4(textColor, 1.0) * sampled; if (color.a <= 0.1f) - discard; + discard; }