From 4579a7f94e3ac323a0b69d05fdb3eea9b7e6b999 Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Mon, 24 Jun 2024 00:31:08 +0200 Subject: more efficient rendering Signed-off-by: Lizzy Fleckenstein --- src/client.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/client.c b/src/client.c index 964e91f..593e9ac 100644 --- a/src/client.c +++ b/src/client.c @@ -49,9 +49,13 @@ void gfx_clear_effects(client *c) fprintf(c->termbuf, "\e[0m"); } -void gfx_set_color(client *c, color col, bool bg) +void gfx_set_color(client *c, color col, bool bg, bool *has_old, color *old) { + if (*has_old && old->r == col.r && old->g == col.g && old->b == col.b) + return; fprintf(c->termbuf, "\e[%u;2;%u;%u;%um", bg ? 48 : 38, col.r, col.g, col.b); + *has_old = true; + *old = col; } void gfx_clear(client *c) @@ -143,28 +147,38 @@ void gfx_render(client *c, uint64_t dtime) fprintf(c->termbuf, "%.*s\n", PSTR(c->server_motd)); for (size_t i = 0; i < c->players.len; i++) fprintf(c->termbuf, "%.*s\n", PSTR(c->players.data[i].name)); + + bool has_bg = false; + bool has_fg = false; + color bg; + color fg; + for (size_t y = 0; y < c->map.bounds.size.y; y++) { for (size_t x = 0; x < c->map.bounds.size.y; x++) { uvec2 off = UVEC2(x, y); vec2 pos = vec2_add(c->map.bounds.pos, CVEC2(off)); node_display dis = gfx_render_node(c, pos, chunk_index(c->map, off)); - gfx_set_color(c, dis.bg, true); + gfx_set_color(c, dis.bg, true, &has_bg, &bg); if (vec2_eq(pos, c->player_pos)) { fprintf(c->termbuf, "🙂"); } else if (dis.texture) { - gfx_set_color(c, dis.fg, false); + gfx_set_color(c, dis.fg, false, &has_fg, &fg); fprintf(c->termbuf, "%s", dis.texture); } else { fprintf(c->termbuf, " "); } } gfx_clear_effects(c); + has_bg = false; + has_fg = false; fprintf(c->termbuf, "\n"); } fflush(c->termbuf); - fwrite(c->termbuf_buf, 1, ftell(c->termbuf), stdout); - fflush(stdout); + fprintf(stderr, "size: %ld\n", ftell(c->termbuf)); + write(STDOUT_FILENO, c->termbuf_buf, ftell(c->termbuf)); + //fwrite(c->termbuf_buf, 1, ftell(c->termbuf), stdout); + //fflush(stdout); } void free_players(client *c) @@ -189,7 +203,7 @@ void client_exit(client *c, int ret) peer_free(&c->conn); tcsetattr(STDIN_FILENO, TCSANOW, &c->oldtio); - fclose(c->termbuf); + if (c->termbuf) fclose(c->termbuf); free(c->termbuf_buf); gfx_alt_buffer(false); -- cgit v1.2.3