instanced text

This commit is contained in:
John Alanbrook 2023-05-08 00:47:49 +00:00
parent 7fc02062c0
commit eb3e576521
6 changed files with 48 additions and 19 deletions

View file

@ -28,9 +28,13 @@ typedef struct {
float res[2]; float res[2];
} circle_ubo; } circle_ubo;
void debug_flush()
{
}
void debugdraw_init() void debugdraw_init()
{ {
// circleShader = MakeShader("shaders/circlevert.glsl", "shaders/circlefrag.glsl");
sg_shader csg = sg_make_shader(&(sg_shader_desc){ sg_shader csg = sg_make_shader(&(sg_shader_desc){
.vs.source = slurp_text("shaders/circlevert.glsl"), .vs.source = slurp_text("shaders/circlevert.glsl"),
.fs.source = slurp_text("shaders/circlefrag.glsl"), .fs.source = slurp_text("shaders/circlefrag.glsl"),

View file

@ -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_cppoint(struct cpVect point, float r, struct color color);
void draw_poly(float *points, int n, float *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 */ void debugdraw_flush(); /* This is called once per frame to draw all queued elements */

View file

@ -100,11 +100,12 @@ void font_init(struct shader *textshader) {
.buffers[2].step_func = SG_VERTEXSTEP_PER_INSTANCE, .buffers[2].step_func = SG_VERTEXSTEP_PER_INSTANCE,
}, },
// .primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP, // .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){ 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, .type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM, .usage = SG_USAGE_STREAM,
.label = "text buffer" .label = "text buffer"
@ -117,6 +118,13 @@ void font_init(struct shader *textshader) {
.label = "text color buffer" .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); font = MakeFont("LessPerfectDOSVGA.ttf", 16);
bind_text.fs_images[0] = font->texID; 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() void text_flush()
{ {
sg_apply_pipeline(pipe_text); sg_apply_pipeline(pipe_text);
sg_apply_bindings(&bind_text); sg_apply_bindings(&bind_text);
sg_apply_uniforms(SG_SHADERSTAGE_VS,0,SG_RANGE_REF(projection)); 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 xpos = cursor[0] + (c.Bearing[0]+offset[0]) * scale;
float ypos = cursor[1] - (c.Bearing[1]+offset[1]) * 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, ypos, c.rect.s0, c.rect.t1,
xpos+w, ypos, c.rect.s1, 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, 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 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; 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 shadowcolor[3] = {0.f, 0.f, 0.f};
float shadowcursor[2]; float shadowcursor[2];
float verts[24]; float verts[16];
float offset[2] = {-1, 1}; float offset[2] = {-1, 1};
fill_charverts(verts, cursor, scale, c, offset); fill_charverts(verts, cursor, scale, c, offset);
@ -257,8 +262,16 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct
curchar++; curchar++;
/* SET COLOR ? */ /* 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[0], SG_RANGE_REF(verts));
sg_append_buffer(bind_text.vertex_buffers[1], SG_RANGE_REF(color)); sg_append_buffer(bind_text.vertex_buffers[1], SG_RANGE_REF(color));
sg_append_buffer(bind_text.index_buffer, SG_RANGE_REF(pts));
return; return;

View file

@ -75,7 +75,7 @@ void openglInit()
exit(1); exit(1);
} }
donquixote = slurp_text("quixote.txt"); // donquixote = slurp_text("quixote.txt");
////// MAKE SHADERS ////// MAKE SHADERS
spriteShader = MakeShader("spritevert.glsl", "spritefrag.glsl"); spriteShader = MakeShader("spritevert.glsl", "spritefrag.glsl");
@ -142,14 +142,14 @@ void openglRender(struct window *window)
0, 0,
window->height, -1.f, 1.f); window->height, -1.f, 1.f);
// sprite_draw_all(); sprite_draw_all();
// gui_draw_img("pill1.png", 200, 200); gui_draw_img("pill1.png", 200, 200);
float a[2] = {100,100}; float a[2] = {100,100};
float w[3] = {1.f,1.f,1.f}; 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}; 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 */ /* UI Elements & Debug elements */
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
@ -174,4 +174,6 @@ void openglRender(struct window *window)
sg_end_pass(); sg_end_pass();
sg_commit(); sg_commit();
sprite_flush();
} }

View file

@ -62,6 +62,13 @@ struct sprite *id2sprite(int id) {
return &sprites[id]; return &sprites[id];
} }
static sprite_count = 0;
void sprite_flush()
{
sprite_count = 0;
}
void sprite_io(struct sprite *sprite, FILE *f, int read) void sprite_io(struct sprite *sprite, FILE *f, int read)
{ {
char path[100]; char path[100];
@ -83,7 +90,6 @@ void sprite_io(struct sprite *sprite, FILE *f, int read)
void sprite_draw_all() void sprite_draw_all()
{ {
YughWarn("Applying sprite pipeline");
sg_apply_pipeline(pip_sprite); sg_apply_pipeline(pip_sprite);
static struct sprite **layers[5]; static struct sprite **layers[5];
@ -149,7 +155,7 @@ void sprite_initialize()
}); });
bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(float)*16, .size = sizeof(float)*16*500,
.type = SG_BUFFERTYPE_VERTEXBUFFER, .type = SG_BUFFERTYPE_VERTEXBUFFER,
.usage = SG_USAGE_STREAM, .usage = SG_USAGE_STREAM,
.label = "sprite vertex buffer", .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; 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); 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]; for (int i = 0; i < 3; i++) c[i] = color[i];
sg_apply_uniforms(SG_SHADERSTAGE_FS, 0, SG_RANGE_REF(c)); 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) void sprite_draw(struct sprite *sprite)

View file

@ -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 video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float rotate, mfloat_t color[3]);
void sprite_draw_all(); void sprite_draw_all();
unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite); unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite);
void sprite_flush();
void gui_draw_img(const char *img, float x, float y); void gui_draw_img(const char *img, float x, float y);