mudgangster

Log | Files | Refs

commit 1ee814ca8a45bf263399b8f8fcc5b96db506ba67
parent de769d8937e2f2ea18761ad25d6566a4b0b2f6f6
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon,  3 Sep 2018 18:05:39 +0300

Reduce glyph memory usage to 66%, or 13% of original

Diffstat:
src/common.h | 2++
src/textbox.cc | 34++++++++++++++++++++++++++++------
src/textbox.h | 8++++----
3 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/src/common.h b/src/common.h @@ -90,6 +90,8 @@ extern StyleDefs Style; #define STRL( x ) ( x ), sizeof( x ) - 1 +#define STATIC_ASSERT( p ) static_assert( p, #p ) + template< typename T > constexpr T min( T a, T b ) { return a < b ? a : b; diff --git a/src/textbox.cc b/src/textbox.cc @@ -7,6 +7,28 @@ #include "common.h" #include <stdio.h> +static uint8_t pack_style( Colour fg, Colour bg, bool bold ) { + STATIC_ASSERT( NUM_COLOURS * NUM_COLOURS * 2 < UINT8_MAX ); + + uint32_t style = 0; + + style = checked_cast< uint32_t >( fg ); + style = style * NUM_COLOURS + checked_cast< uint32_t >( bg ); + style = style * 2 + checked_cast< uint32_t >( bold ); + + return checked_cast< uint8_t >( style ); +} + +static void unpack_style( uint8_t style, int * fg, int * bg, int * bold ) { + *bold = style % 2; + style /= 2; + + *bg = style % NUM_COLOURS; + style /= NUM_COLOURS; + + *fg = style; +} + void textbox_init( TextBox * tb, size_t scrollback ) { *tb = { }; // TODO: this is kinda crap @@ -37,8 +59,7 @@ void textbox_add( TextBox * tb, const char * str, unsigned int len, Colour fg, C for( size_t i = 0; i < n; i++ ) { Glyph & glyph = line->glyphs[ line->len + i ]; glyph.ch = str[ i ]; - glyph.fgbg = checked_cast< uint8_t >( fg ) | ( checked_cast< uint8_t >( bg ) << 4 ); - glyph.bold = checked_cast< uint8_t >( bold ); + glyph.style = pack_style( fg, bg, bold ); }; line->len += n; @@ -92,17 +113,18 @@ void textbox_draw( const TextBox * tb ) { if( top < 0 ) continue; + int fg, bg, bold; + unpack_style( glyph.style, &fg, &bg, &bold ); + // bg - int bg = glyph.fgbg >> 4; int top_spacing = SPACING / 2; int bot_spacing = SPACING - top_spacing; XSetForeground( UI.display, UI.gc, bg == SYSTEM ? Style.Colours.system : Style.colours[ 0 ][ bg ] ); XFillRectangle( UI.display, doublebuf, UI.gc, left, top - top_spacing, Style.font.width, Style.font.height + bot_spacing ); // fg - int fg = glyph.fgbg & 0xF; - XSetFont( UI.display, UI.gc, ( glyph.bold ? Style.fontBold : Style.font ).font->fid ); - XSetForeground( UI.display, UI.gc, fg == SYSTEM ? Style.Colours.system : Style.colours[ glyph.bold ][ fg ] ); + XSetFont( UI.display, UI.gc, ( bold ? Style.fontBold : Style.font ).font->fid ); + XSetForeground( UI.display, UI.gc, fg == SYSTEM ? Style.Colours.system : Style.colours[ bold ][ fg ] ); XDrawString( UI.display, doublebuf, UI.gc, left, top + Style.font.ascent + SPACING, &glyph.ch, 1 ); } diff --git a/src/textbox.h b/src/textbox.h @@ -2,6 +2,7 @@ #include <stdint.h> +// TODO: probably don't need most of these enum Colour { BLACK, RED, @@ -12,17 +13,16 @@ enum Colour { CYAN, WHITE, SYSTEM, - NONE, + + NUM_COLOURS, }; constexpr size_t MAX_LINE_LENGTH = 2048; constexpr size_t SCROLLBACK_SIZE = 1 << 16; // TODO -// TODO: pack these better struct Glyph { char ch; - uint8_t fgbg; - uint8_t bold; + uint8_t style; }; struct Line {