mudgangster

Log | Files | Refs

commit d19f8e62f5ad84d7cf3469e706f202ea09be57f5
parent d9c2c73208093b0efdf64d39854e3c9988ddacdf
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon,  3 Sep 2018 17:51:38 +0300

Don't allocate a massive scrollback bufer for the chat window

Diffstat:
src/textbox.cc | 13+++++++------
src/textbox.h | 3++-
src/ui.cc | 4++--
3 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/textbox.cc b/src/textbox.cc @@ -7,11 +7,12 @@ #include "common.h" #include <stdio.h> -void textbox_init( TextBox * tb ) { +void textbox_init( TextBox * tb, size_t scrollback ) { *tb = { }; // TODO: this is kinda crap - tb->text.lines = ( Line * ) calloc( sizeof( Line ), SCROLLBACK_SIZE ); + tb->text.lines = ( Line * ) calloc( sizeof( Line ), scrollback ); tb->text.num_lines = 1; + tb->text.max_lines = scrollback; } void textbox_term( TextBox * tb ) { @@ -29,7 +30,7 @@ void textbox_setsize( TextBox * tb, int width, int height ) { } void textbox_add( TextBox * tb, const char * str, unsigned int len, Colour fg, Colour bg, bool bold ) { - Line * line = &tb->text.lines[ ( tb->text.head + tb->text.num_lines ) % SCROLLBACK_SIZE ]; + Line * line = &tb->text.lines[ ( tb->text.head + tb->text.num_lines ) % tb->text.max_lines ]; size_t remaining = MAX_LINE_LENGTH - line->len; size_t n = min( strlen( str ), remaining ); @@ -45,13 +46,13 @@ void textbox_add( TextBox * tb, const char * str, unsigned int len, Colour fg, C } void textbox_newline( TextBox * tb ) { - if( tb->text.num_lines < SCROLLBACK_SIZE ) { + if( tb->text.num_lines < tb->text.max_lines ) { tb->text.num_lines++; return; } tb->text.head++; - Line * line = &tb->text.lines[ ( tb->text.head + tb->text.num_lines ) % SCROLLBACK_SIZE ]; + Line * line = &tb->text.lines[ ( tb->text.head + tb->text.num_lines ) % tb->text.max_lines ]; line->len = 0; } @@ -76,7 +77,7 @@ void textbox_draw( const TextBox * tb ) { size_t tb_cols = tb->width / Style.font.width; while( rows_drawn < tb_rows && lines_drawn < tb->text.num_lines ) { - const Line & line = tb->text.lines[ ( tb->text.head + tb->text.num_lines - tb->scroll_offset - lines_drawn ) % SCROLLBACK_SIZE ]; + const Line & line = tb->text.lines[ ( tb->text.head + tb->text.num_lines - tb->scroll_offset - lines_drawn ) % tb->text.max_lines ]; size_t line_rows = 1 + line.len / tb_cols; if( line.len > 0 && line.len % tb_cols == 0 ) diff --git a/src/textbox.h b/src/textbox.h @@ -32,6 +32,7 @@ struct Text { Line * lines; size_t head; size_t num_lines; + size_t max_lines; }; struct TextBox { @@ -49,7 +50,7 @@ struct TextBox { void page_down(); }; -void textbox_init( TextBox * tb ); +void textbox_init( TextBox * tb, size_t scrollback ); void textbox_term( TextBox * tb ); void textbox_setpos( TextBox * tb, int x, int y ); diff --git a/src/ui.cc b/src/ui.cc @@ -344,8 +344,8 @@ void initStyle() { } void ui_init() { - textbox_init( &UI.textMain ); - textbox_init( &UI.textChat ); + textbox_init( &UI.textMain, SCROLLBACK_SIZE ); + textbox_init( &UI.textChat, CHAT_ROWS ); UI.display = XOpenDisplay( NULL ); UI.screen = XDefaultScreen( UI.display ); UI.width = -1;