From: Christian Heller Date: Sun, 25 Oct 2020 06:18:09 +0000 (+0100) Subject: Make map rendering slightly more efficient. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/test.html?a=commitdiff_plain;h=609edc98f5c2c8785f90355d54847d24d0e49eec;p=plomrogue2-experiments Make map rendering slightly more efficient. --- diff --git a/new2/rogue_chat.html b/new2/rogue_chat.html index 3d8e4c3..f584ddd 100644 --- a/new2/rogue_chat.html +++ b/new2/rogue_chat.html @@ -26,13 +26,14 @@ let terminal = { set_font: function(type='normal') { this.ctx.font = type + ' ' + this.charHeight + 'px monospace'; }, + write_cheap: function(start_y, start_x, msg) { + this.ctx.fillText(msg, start_x*this.charWidth, start_y*this.charHeight); + }, + set_color: function(color) { + this.ctx.fillStyle = color; + }, write: function(start_y, start_x, msg, foreground_color='white') { - if (foreground_color === 'black') { - this.ctx.fillStyle = 'white'; - } else { - this.ctx.fillStyle = 'black'; - } - this.ctx.fillText(msg, start_x*this.charWidth, start_y*this.charHeight); + this.ctx.fillStyle = 'black'; this.ctx.fillRect(start_x*this.charWidth, start_y*this.charHeight, this.charWidth*msg.length, this.charHeight); this.ctx.fillStyle = foreground_color; @@ -99,22 +100,28 @@ let tui = { } }, draw_map: function() { - terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2); + terminal.drawBox(0, 0, terminal.rows, terminal.cols / 2, 'black'); + let last_c = ''; + let color = 'white'; for (let i = 0, y = 0, x = 0; i < game.map.length; i++, x++) { if (x >= game.map_size[1]) { x = 0; y += 1; }; let c = game.map[i]; - let color = 'white'; - if (c == '.') { - color = '#ffaa00'; - } else if (c == '~') { - color = '#5555ff'; - } else if (c == 'X') { - color = '#55ff00'; + if (c != last_c) { + last_c = c; + color = 'white'; + if (c == '.') { + color = '#ffaa00'; + } else if (c == '~') { + color = '#5555ff'; + } else if (c == 'X') { + color = '#55ff00'; + } + terminal.set_color(color); } - terminal.write(y, x, game.map[i], color); + terminal.write_cheap(y, x, c); } for (const t in game.things) { terminal.write(game.things[t][0], game.things[t][1], '@');