From eb3e576521ce7dfd763935bf5eaa13e0f3ee5674 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 8 May 2023 00:47:49 +0000 Subject: [PATCH] instanced text --- source/engine/debug/debugdraw.c | 6 +++++- source/engine/debug/debugdraw.h | 2 ++ source/engine/font.c | 29 +++++++++++++++++++++-------- source/engine/openglrender.c | 12 +++++++----- source/engine/sprite.c | 16 ++++++++++++---- source/engine/sprite.h | 2 +- 6 files changed, 48 insertions(+), 19 deletions(-) diff --git a/source/engine/debug/debugdraw.c b/source/engine/debug/debugdraw.c index 650a096..ed5aab5 100644 --- a/source/engine/debug/debugdraw.c +++ b/source/engine/debug/debugdraw.c @@ -28,9 +28,13 @@ typedef struct { float res[2]; } circle_ubo; +void debug_flush() +{ + +} + void debugdraw_init() { -// circleShader = MakeShader("shaders/circlevert.glsl", "shaders/circlefrag.glsl"); sg_shader csg = sg_make_shader(&(sg_shader_desc){ .vs.source = slurp_text("shaders/circlevert.glsl"), .fs.source = slurp_text("shaders/circlefrag.glsl"), diff --git a/source/engine/debug/debugdraw.h b/source/engine/debug/debugdraw.h index fe12a4f..a3e4377 100644 --- a/source/engine/debug/debugdraw.h +++ b/source/engine/debug/debugdraw.h @@ -17,6 +17,8 @@ void draw_point(int x, int y, float r, float *color); void draw_cppoint(struct cpVect point, float r, struct color color); void draw_poly(float *points, int n, float *color); +void debug_flush(); + void debugdraw_flush(); /* This is called once per frame to draw all queued elements */ diff --git a/source/engine/font.c b/source/engine/font.c index e082119..da11c4f 100644 --- a/source/engine/font.c +++ b/source/engine/font.c @@ -100,11 +100,12 @@ void font_init(struct shader *textshader) { .buffers[2].step_func = SG_VERTEXSTEP_PER_INSTANCE, }, // .primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP, - .label = "text pipeline" + .label = "text pipeline", + .index_type = SG_INDEXTYPE_UINT16 }); bind_text.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ - .size = sizeof(float)*24*3*1024*1024, + .size = sizeof(float)*16*500, .type = SG_BUFFERTYPE_VERTEXBUFFER, .usage = SG_USAGE_STREAM, .label = "text buffer" @@ -117,6 +118,13 @@ void font_init(struct shader *textshader) { .label = "text color buffer" }); + bind_text.index_buffer = sg_make_buffer(&(sg_buffer_desc){ + .size = sizeof(uint16_t)*6*500, + .type = SG_BUFFERTYPE_INDEXBUFFER, + .usage = SG_USAGE_STREAM, + .label = "text index buffer" + }); + font = MakeFont("LessPerfectDOSVGA.ttf", 16); bind_text.fs_images[0] = font->texID; } @@ -210,7 +218,6 @@ void draw_char_box(struct Character c, float cursor[2], float scale, float color void text_flush() { - sg_apply_pipeline(pipe_text); sg_apply_bindings(&bind_text); sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection)); @@ -227,16 +234,14 @@ void fill_charverts(float *verts, float cursor[2], float scale, struct Character float xpos = cursor[0] + (c.Bearing[0]+offset[0]) * scale; float ypos = cursor[1] - (c.Bearing[1]+offset[1]) * scale; - float v[24] = { + float v[16] = { xpos, ypos, c.rect.s0, c.rect.t1, xpos+w, ypos, c.rect.s1, c.rect.t1, xpos, ypos + h, c.rect.s0, c.rect.t0, - xpos, ypos + h, c.rect.s0, c.rect.t0, - xpos+w, ypos, c.rect.s1, c.rect.t1, xpos + w, ypos + h, c.rect.s1, c.rect.t0 }; - memcpy(verts, v, sizeof(float)*24); + memcpy(verts, v, sizeof(float)*16); } static int drawcaret = 0; @@ -246,7 +251,7 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct float shadowcolor[3] = {0.f, 0.f, 0.f}; float shadowcursor[2]; - float verts[24]; + float verts[16]; float offset[2] = {-1, 1}; fill_charverts(verts, cursor, scale, c, offset); @@ -257,8 +262,16 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct curchar++; /* SET COLOR ? */ + uint16_t pts[6] = { + 0, 1, 2, + 2, 1, 3 + }; + + for (int i = 0; i < 6; i++) pts[i] += curchar*4; + sg_append_buffer(bind_text.vertex_buffers[0], SG_RANGE_REF(verts)); sg_append_buffer(bind_text.vertex_buffers[1], SG_RANGE_REF(color)); + sg_append_buffer(bind_text.index_buffer, SG_RANGE_REF(pts)); return; diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index af7ce10..8351fb1 100644 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -75,7 +75,7 @@ void openglInit() exit(1); } - donquixote = slurp_text("quixote.txt"); +// donquixote = slurp_text("quixote.txt"); ////// MAKE SHADERS spriteShader = MakeShader("spritevert.glsl", "spritefrag.glsl"); @@ -142,14 +142,14 @@ void openglRender(struct window *window) 0, window->height, -1.f, 1.f); -// sprite_draw_all(); -// gui_draw_img("pill1.png", 200, 200); + sprite_draw_all(); + gui_draw_img("pill1.png", 200, 200); float a[2] = {100,100}; float w[3] = {1.f,1.f,1.f}; -// renderText("TEST RENDER", a, 1.f, w, 0,-1); + renderText("TEST RENDER", a, 1.f, w, 0,-1); float b[2] = {50,50}; -// renderText(donquixote, b, 1.f, w, 0,-1); + renderText("TEST @@@@ RENDER", b, 1.f, w, 0,-1); /* UI Elements & Debug elements */ // glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); @@ -174,4 +174,6 @@ void openglRender(struct window *window) sg_end_pass(); sg_commit(); + + sprite_flush(); } diff --git a/source/engine/sprite.c b/source/engine/sprite.c index 6adda9d..1339958 100644 --- a/source/engine/sprite.c +++ b/source/engine/sprite.c @@ -62,6 +62,13 @@ struct sprite *id2sprite(int id) { return &sprites[id]; } +static sprite_count = 0; + +void sprite_flush() +{ + sprite_count = 0; +} + void sprite_io(struct sprite *sprite, FILE *f, int read) { char path[100]; @@ -83,7 +90,6 @@ void sprite_io(struct sprite *sprite, FILE *f, int read) void sprite_draw_all() { - YughWarn("Applying sprite pipeline"); sg_apply_pipeline(pip_sprite); static struct sprite **layers[5]; @@ -149,7 +155,7 @@ void sprite_initialize() }); bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ - .size = sizeof(float)*16, + .size = sizeof(float)*16*500, .type = SG_BUFFERTYPE_VERTEXBUFFER, .usage = SG_USAGE_STREAM, .label = "sprite vertex buffer", @@ -183,7 +189,8 @@ void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], flo }; bind_sprite.fs_images[0] = tex->id; - sg_update_buffer(bind_sprite.vertex_buffers[0], SG_RANGE_REF(vertices)); + sg_append_buffer(bind_sprite.vertex_buffers[0], SG_RANGE_REF(vertices)); + sg_apply_bindings(&bind_sprite); @@ -192,7 +199,8 @@ void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], flo for (int i = 0; i < 3; i++) c[i] = color[i]; sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, SG_RANGE_REF(c)); - sg_draw(0,4,1); + sg_draw(sprite_count*4,4,1); + sprite_count++; } void sprite_draw(struct sprite *sprite) diff --git a/source/engine/sprite.h b/source/engine/sprite.h index a909242..2fb089f 100644 --- a/source/engine/sprite.h +++ b/source/engine/sprite.h @@ -38,7 +38,7 @@ void sprite_draw(struct sprite *sprite); void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float rotate, mfloat_t color[3]); void sprite_draw_all(); unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite); - +void sprite_flush(); void gui_draw_img(const char *img, float x, float y);