commit de769d8937e2f2ea18761ad25d6566a4b0b2f6f6
parent d19f8e62f5ad84d7cf3469e706f202ea09be57f5
Author: Michael Savage <mikejsavage@gmail.com>
Date: Mon, 3 Sep 2018 17:57:21 +0300
Reduce glyph memory usage to 20%
Diffstat:
3 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/src/common.h b/src/common.h
@@ -1,5 +1,8 @@
#pragma once
+#include <stdint.h>
+#include <assert.h>
+
#include <X11/Xlib.h>
#include "config.h"
@@ -96,3 +99,10 @@ template< typename T >
constexpr T max( T a, T b ) {
return a > b ? a : b;
}
+
+template< typename To, typename From >
+inline To checked_cast( const From & from ) {
+ To result = To( from );
+ assert( From( result ) == from );
+ return result;
+}
diff --git a/src/textbox.cc b/src/textbox.cc
@@ -37,9 +37,8 @@ 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.fg = fg;
- glyph.bg = bg;
- glyph.bold = bold;
+ glyph.fgbg = checked_cast< uint8_t >( fg ) | ( checked_cast< uint8_t >( bg ) << 4 );
+ glyph.bold = checked_cast< uint8_t >( bold );
};
line->len += n;
@@ -94,16 +93,16 @@ void textbox_draw( const TextBox * tb ) {
continue;
// bg
+ int bg = glyph.fgbg >> 4;
int top_spacing = SPACING / 2;
int bot_spacing = SPACING - top_spacing;
- XSetForeground( UI.display, UI.gc,
- glyph.bg == SYSTEM ? Style.Colours.system : Style.colours[ 0 ][ glyph.bg ] );
+ 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,
- glyph.fg == SYSTEM ? Style.Colours.system : Style.colours[ glyph.bold ][ glyph.fg ] );
+ XSetForeground( UI.display, UI.gc, fg == SYSTEM ? Style.Colours.system : Style.colours[ glyph.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
@@ -1,5 +1,7 @@
#pragma once
+#include <stdint.h>
+
enum Colour {
BLACK,
RED,
@@ -19,8 +21,8 @@ constexpr size_t SCROLLBACK_SIZE = 1 << 16; // TODO
// TODO: pack these better
struct Glyph {
char ch;
- Colour fg, bg;
- bool bold;
+ uint8_t fgbg;
+ uint8_t bold;
};
struct Line {