From c16a0332a5a1ebf004ec2c6b310bbe616db2ebb2 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 22 May 2023 05:08:08 +0000 Subject: [PATCH] render update --- source/engine/debug/debugdraw.c | 56 +++++++++++++++++++++---------- source/engine/editor/ed_project.c | 1 - source/engine/ffi.c | 4 ++- source/engine/font.c | 3 +- source/engine/input.c | 4 +-- source/engine/render.h | 10 ++++++ source/engine/yugine.c | 7 ++-- source/scripts/editor.js | 2 -- source/scripts/engine.js | 2 +- source/shaders/textfrag.glsl | 3 ++ 10 files changed, 63 insertions(+), 29 deletions(-) diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index f75677a..d9d7e33 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -1,6 +1,7 @@ #include "debugdraw.h" #include "openglrender.h" +#include "render.h" #include "shader.h" #include "log.h" @@ -25,6 +26,7 @@ struct point_vertex { float radius; }; static int point_c = 0; +static struct point_vertex point_b[1000]; static sg_shader line_shader; static sg_pipeline line_pipe; @@ -37,6 +39,8 @@ struct line_vert { }; static int line_c = 0; static int line_v = 0; +static struct line_vert line_b[1000]; +static uint16_t line_bi[1000]; static sg_pipeline grid_pipe; static sg_bindings grid_bind; @@ -53,6 +57,8 @@ struct poly_vertex { float uv[2]; struct rgba color; }; +static struct poly_vertex poly_b[1000]; +static uint32_t poly_bi[1000]; static sg_pipeline circle_pipe; static sg_bindings circle_bind; @@ -64,35 +70,58 @@ struct circle_vertex { float radius; struct rgba color; }; +static struct circle_vertex circle_b[1000]; void debug_flush() { + if (circle_count != 0) { sg_apply_pipeline(circle_pipe); sg_apply_bindings(&circle_bind); sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(projection)); - + sg_update_buffer(circle_bind.vertex_buffers[0], &(sg_range){ + .ptr = circle_b, + .size = sizeof(struct circle_vertex)*circle_count + }); sg_draw(0,4,circle_count); circle_count = 0; + } + if (poly_c != 0) { sg_apply_pipeline(poly_pipe); sg_apply_bindings(&poly_bind); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection)); + sg_update_buffer(poly_bind.vertex_buffers[0], &(sg_range){ + .ptr = poly_b, .size = sizeof(struct poly_vertex)*poly_v}); + sg_update_buffer(poly_bind.index_buffer, &(sg_range){ + .ptr = poly_bi, .size = sizeof(uint32_t)*poly_c}); sg_draw(0,poly_c,1); poly_c = 0; poly_v = 0; + } + if (point_c != 0) { sg_apply_pipeline(point_pipe); sg_apply_bindings(&point_bind); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection)); + sg_update_buffer(point_bind.vertex_buffers[0], &(sg_range){ + .ptr = point_b, + .size = sizeof(struct point_vertex)*point_c}); sg_draw(0,point_c,1); point_c = 0; + } + if (line_c != 0) { sg_apply_pipeline(line_pipe); sg_apply_bindings(&line_bind); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection)); + sg_update_buffer(line_bind.vertex_buffers[0], &(sg_range){ + .ptr = line_b, .size = sizeof(struct line_vert)*line_v}); + sg_update_buffer(line_bind.index_buffer, &(sg_range){ + .ptr = line_bi, .size = sizeof(uint16_t)*line_c}); sg_draw(0,line_c,1); line_c = 0; line_v = 0; + } } static sg_shader_uniform_block_desc projection_ubo = { @@ -102,15 +131,6 @@ static sg_shader_uniform_block_desc projection_ubo = { } }; -sg_blend_state blend_trans = { - .enabled = true, - .src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA, - .dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, - .src_factor_alpha = SG_BLENDFACTOR_SRC_ALPHA, - .src_factor_alpha = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA -}; - - void debugdraw_init() { point_shader = sg_make_shader(&(sg_shader_desc){ @@ -320,8 +340,8 @@ void draw_line(cpVect *a_points, int a_n, struct rgba color, float seg_len) .size = sizeof(uint16_t)*i_c }; - sg_append_buffer(line_bind.vertex_buffers[0], &vr); - sg_append_buffer(line_bind.index_buffer, &ir); + memcpy(line_b+line_v, v, sizeof(struct line_vert)*n); + memcpy(line_bi+line_c, idxs, sizeof(uint16_t)*i_c); line_c += i_c; line_v += n; @@ -443,8 +463,8 @@ void draw_edge(cpVect *points, int n, struct rgba color, int thickness, int clos .size = sizeof(struct poly_vertex)*mesh->num_vertices }; - sg_append_buffer(poly_bind.vertex_buffers[0], &vvt); - sg_append_buffer(poly_bind.index_buffer, &it); + memcpy(poly_b+poly_v, vertices, sizeof(struct poly_vertex)*mesh->num_vertices); + memcpy(poly_bi+poly_c, mesh->triangle_indices, sizeof(uint32_t)*mesh->num_triangles*3); poly_c += mesh->num_triangles*3; poly_v += mesh->num_vertices; @@ -459,7 +479,7 @@ void draw_circle(int x, int y, float radius, int pixels, struct rgba color, int cv.pos[1] = y; cv.radius = radius; cv.color = color; - sg_append_buffer(circle_bind.vertex_buffers[0], SG_RANGE_REF(cv)); + memcpy(circle_b+circle_count, &cv, sizeof(struct circle_vertex)); circle_count++; } @@ -520,7 +540,7 @@ void draw_cppoint(struct cpVect point, float r, struct rgba color) .radius = r }; - sg_append_buffer(point_bind.vertex_buffers[0], SG_RANGE_REF(p)); + memcpy(point_b+point_c, &p, sizeof(struct point_vertex)); point_c++; } @@ -572,8 +592,8 @@ void draw_poly(cpVect *points, int n, struct rgba color) .size = sizeof(struct poly_vertex)*n }; - sg_append_buffer(poly_bind.vertex_buffers[0], &ppp); - sg_append_buffer(poly_bind.index_buffer, &trip); + memcpy(poly_b+poly_v, polyverts, sizeof(struct poly_vertex)*n); + memcpy(poly_bi+poly_c, tridxs, sizeof(uint32_t)*3*tric); poly_c += tric*3; poly_v += n; diff --git a/source/engine/editor/ed_project.c b/source/engine/editor/ed_project.c index ca719fe..45f7fd9 100644 --- a/source/engine/editor/ed_project.c +++ b/source/engine/editor/ed_project.c @@ -2,7 +2,6 @@ #include #include -#include #include "editor.h" void editor_init_project(struct gameproject *gp) diff --git a/source/engine/ffi.c b/source/engine/ffi.c index b14a0ae..3f6dd4e 100644 --- a/source/engine/ffi.c +++ b/source/engine/ffi.c @@ -118,11 +118,13 @@ int js_arrlen(JSValue v) { } struct rgba js2color(JSValue v) { + JSValue ja = js_arridx(v,3); + unsigned char a = JS_IsUndefined(ja) ? 255 : js2int(ja); struct rgba color = { .r = js2int(js_arridx(v,0)), .g = js2int(js_arridx(v,1)), .b = js2int(js_arridx(v,2)), - .a = js2int(js_arridx(v,3)), + .a = a, }; return color; } diff --git a/source/engine/font.c b/source/engine/font.c index 9828633..bed3296 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -107,7 +107,8 @@ void font_init(struct shader *textshader) { }, .buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE }, - .primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP, + .primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP, + .colors[0].blend = blend_trans, }); float text_verts[8] = { diff --git a/source/engine/input.c b/source/engine/input.c index 9944856..16f4866 100644 --- a/source/engine/input.c +++ b/source/engine/input.c @@ -426,9 +426,7 @@ void input_poll(double wait) { mouseWheelX = 0; mouseWheelY = 0; - glfwPollEvents(); - - // glfwWaitEventsTimeout(wait); + glfwWaitEventsTimeout(wait); for (int i = 0; i < arrlen(downkeys); i++) call_input_down(&downkeys[i]); diff --git a/source/engine/render.h b/source/engine/render.h index 7cbe3e8..0b1a017 100644 --- a/source/engine/render.h +++ b/source/engine/render.h @@ -4,6 +4,7 @@ #define GLFW_INCLUDE_NONE #include +#include "sokol/sokol_gfx.h" struct uv_n { unsigned short u; @@ -15,4 +16,13 @@ struct st_n { struct uv_n t; }; +static sg_blend_state blend_trans = { + .enabled = true, + .src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA, + .dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, + .src_factor_alpha = SG_BLENDFACTOR_SRC_ALPHA, + .src_factor_alpha = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA +}; + + #endif diff --git a/source/engine/yugine.c b/source/engine/yugine.c index f96f613..8c4cb8c 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -211,8 +211,11 @@ int main(int argc, char **args) { double elapsed = glfwGetTime() - lastTick; deltaT = elapsed; lastTick = glfwGetTime(); - double wait = fmax(0, renderMS - elapsed); - input_poll(wait); + //double wait = fmax(0, renderMS - elapsed); + if (sim_playing()) + input_poll(fmax(0, renderMS-elapsed)); + else + input_poll(1000); window_all_handle_events(); framems[framei++] = elapsed; diff --git a/source/scripts/editor.js b/source/scripts/editor.js index 87b1835..33e6ca4 100644 --- a/source/scripts/editor.js +++ b/source/scripts/editor.js @@ -1312,8 +1312,6 @@ var editor = { "PAUSED" : "STOPPED", [0, 0], 1); - gui_text("FPS " + this.fps, [0, 540], 1); - var clvl = this.edit_level; var ypos = 200; var lvlcolor = Color.white; diff --git a/source/scripts/engine.js b/source/scripts/engine.js index 5f2f916..9f061e2 100644 --- a/source/scripts/engine.js +++ b/source/scripts/engine.js @@ -128,7 +128,7 @@ var Color = { var GUI = { text(str, pos, size, color, wrap) { size = size ? size : 1; - color = color ? color : [255,255,255]; + color = color ? color : [255,255,255,255]; wrap = wrap ? wrap : 500; var h = ui_text(str, pos, size, color, wrap); diff --git a/source/shaders/textfrag.glsl b/source/shaders/textfrag.glsl index 8cb4101..fcade80 100644 --- a/source/shaders/textfrag.glsl +++ b/source/shaders/textfrag.glsl @@ -6,9 +6,12 @@ out vec4 color; uniform sampler2D text; +float osize = 1.0; + void main() { float lettera = texture(text,TexCoords).r; + // color = vec4(1.f, 1.f, 1.f, texture(text, TexCoords).r); if (lettera <= 0.1f)