sdlterm/src/sdlterm.cc
changeset 50 c5b8b9d2da95
parent 49 1611c462c3e3
child 51 dce7325109c1
--- a/sdlterm/src/sdlterm.cc	Sat Jan 05 16:47:30 2013 +0100
+++ b/sdlterm/src/sdlterm.cc	Sat Jan 05 18:44:56 2013 +0100
@@ -4,6 +4,14 @@
 #include <algorithm>
 
 
+void ColorMap::index_to_rgb(int index, SDL_Color &color)
+{
+	color.r = _map[index][0];
+	color.g = _map[index][1];
+	color.b = _map[index][2];
+}
+
+
 SDL_Surface *GlyphCache::lookup_glyph(Uint16 ch)
 {
 	auto iter = _glyph_map.find(ch);
@@ -32,7 +40,7 @@
 
 
 GlyphRenderer::GlyphRenderer()
- : _font_regular(NULL), _font_bold(NULL)
+ : _font_regular(NULL), _font_bold(NULL), _cell_width(0), _cell_height(0), _cache(), _colormap()
 {
 	if (TTF_Init() == -1)
 	{
@@ -111,9 +119,11 @@
 }
 
 
-SDL_Surface *GlyphRenderer::render_glyph(Uint16 ch)
+SDL_Surface *GlyphRenderer::render_cell(Uint16 ch, Uint16 attr)
 {
 	SDL_Surface *cell_surface;
+	TTF_Font *font;
+	SDL_Color fgcolor, bgcolor;
 
 	// try cache
 	cell_surface = _cache.lookup_glyph(ch);
@@ -122,8 +132,11 @@
 		return cell_surface;
 	}
 
-	TTF_Font *font = _font_regular;
-	SDL_Color color={0xff,0xff,0xff}, bgcolor={0,0,0};
+	// load attributes
+	_colormap.index_to_rgb(attr & 0x000F, fgcolor);
+	_colormap.index_to_rgb((attr & 0x00F0) >> 4, bgcolor);
+	Style style = (Style) ((attr & 0xFF00) >> 8);
+	font = (style == Style::BOLD) ? _font_bold : _font_regular;
 
 	// create surface for whole cell and fill it with bg color
 	cell_surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
@@ -139,7 +152,7 @@
 	// render glyph, blit it onto cell surface
 	if (ch)
 	{
-		SDL_Surface *glyph_surface = TTF_RenderGlyph_Shaded(font, ch, color, bgcolor);
+		SDL_Surface *glyph_surface = TTF_RenderGlyph_Shaded(font, ch, fgcolor, bgcolor);
 		int minx, maxy;
 		TTF_GlyphMetrics(font, ch, &minx, NULL, NULL, &maxy, NULL);
 		dst_rect.x = minx;
@@ -203,7 +216,7 @@
 {
 	auto front_iter = _cells_front.begin();
 	auto back_iter = _cells_back.begin();
-	SDL_Surface *glyph_surface;
+	SDL_Surface *cell_surface;
 	SDL_Rect dst_rect;
 	for (int y = 0; y < _height; y++)
 	{
@@ -213,8 +226,8 @@
 			{
 				dst_rect.x = x * _cell_width;
 				dst_rect.y = y * _cell_height;
-				glyph_surface = _render.render_glyph(front_iter->ch);
-				SDL_BlitSurface(glyph_surface, NULL, _screen_surface, &dst_rect);
+				cell_surface = _render.render_cell(front_iter->ch, front_iter->attr);
+				SDL_BlitSurface(cell_surface, NULL, _screen_surface, &dst_rect);
 				*back_iter = *front_iter;
 			}
 			front_iter++;
@@ -230,17 +243,22 @@
 {
 	_cell_width = _render.get_cell_width();
 	_cell_height = _render.get_cell_height();
+	if (!_cell_width || !_cell_height)
+		return;
+
 	_width = _pixel_width / _cell_width;
 	_height = _pixel_height / _cell_height;
+	if (!_width || !_height)
+		return;
 
 	int num_cells = _width * _height;
-	_cells_front.resize(num_cells);
-	_cells_back.resize(num_cells);
+	_cells_front.resize(num_cells, TerminalCell());
+	_cells_back.resize(num_cells, TerminalCell());
 }
 
 
 Terminal::Terminal()
- : _screen(), _mousemove_last_x(-1)
+ : _screen(), _attr(7), _mousemove_last_x(-1)
 {
     if (SDL_Init(SDL_INIT_VIDEO) == -1)
     {
@@ -327,7 +345,6 @@
 		case SDLK_TAB:			return "tab";
 		case SDLK_RETURN:		return "enter";
 		case SDLK_KP_ENTER:		return "enter";
-		case SDLK_PAUSE:	    return "pause";
 		case SDLK_ESCAPE:		return "escape";
 		case SDLK_DELETE:		return "delete";
 		case SDLK_INSERT:		return "insert";
@@ -351,6 +368,9 @@
 		case SDLK_F10:			return "f10";
 		case SDLK_F11:			return "f11";
 		case SDLK_F12:			return "f12";
+		case SDLK_PRINT:		return "print";
+		case SDLK_SCROLLOCK:	return "scrllock";
+		case SDLK_PAUSE:	    return "pause";
 		default: return NULL;
 	}
 }