X-Git-Url: https://eleni.mutantstargoat.com/git/?p=winnie;a=blobdiff_plain;f=src%2Ftext.cc;h=b79e6f83d0815c506175177bc7275e17d5060517;hp=ca0bf0bc648928ab76b707ff28f7040d625c9d88;hb=5984d5479693fd7519674a5bc40ebf804f8d0a46;hpb=ed66dbc4e6c017dad675a83dbcd7619b1b5d1e7d diff --git a/src/text.cc b/src/text.cc index ca0bf0b..b79e6f8 100644 --- a/src/text.cc +++ b/src/text.cc @@ -10,29 +10,38 @@ static int draw_glyph(Pixmap *pixmap, int x, int y, char c); -static FT_Library ft_lib; -static FT_Face ft_face; +struct Text { + FT_Library ft_lib; + FT_Face ft_face; + int text_x, text_y; + int text_color[3]; +}; -static int text_x, text_y; -static int text_color[3] = {255, 255, 255}; +Text *text; bool init_text() { - if(FT_Init_FreeType(&ft_lib)) { + if(!(text = (Text*)malloc(sizeof *text))) { + return false; + } + + if(FT_Init_FreeType(&text->ft_lib)) { fprintf(stderr, "Failed to initialize the FreeType library!\n"); return false; } - if(FT_New_Face(ft_lib, FONT_PATH, 0, &ft_face)) { + if(FT_New_Face(text->ft_lib, FONT_PATH, 0, &text->ft_face)) { fprintf(stderr, "Failed to load font: %s\n", FONT_PATH); return false; } - if(FT_Set_Char_Size(ft_face, 0, FONT_SIZE * 64, DPI, DPI)) { + if(FT_Set_Char_Size(text->ft_face, 0, FONT_SIZE * 64, DPI, DPI)) { fprintf(stderr, "Failed to set font size\n"); return false; } + set_text_color(255, 255, 255); + return true; } @@ -43,35 +52,35 @@ void draw_text(const char *txt, Pixmap *pixmap) } while(*txt != 0) { - text_x += draw_glyph(pixmap, text_x, text_y, *txt); + text->text_x += draw_glyph(pixmap, text->text_x, text->text_y, *txt); txt++; } } void set_text_position(int x, int y) { - text_x = x; - text_y = y; + text->text_x = x; + text->text_y = y; } void set_text_color(int r, int g, int b) { - text_color[0] = r; - text_color[1] = g; - text_color[2] = b; + text->text_color[0] = r; + text->text_color[1] = g; + text->text_color[2] = b; } static int draw_glyph(Pixmap *pixmap, int x, int y, char c) { - if(FT_Load_Char(ft_face, c, FT_LOAD_RENDER)) { + if(FT_Load_Char(text->ft_face, c, FT_LOAD_RENDER)) { return 0; } - x += ft_face->glyph->bitmap_left; - y -= ft_face->glyph->bitmap_top; + x += text->ft_face->glyph->bitmap_left; + y -= text->ft_face->glyph->bitmap_top; - FT_Bitmap *ft_bmp = &ft_face->glyph->bitmap; + FT_Bitmap *ft_bmp = &text->ft_face->glyph->bitmap; unsigned char *bmp_ptr = ft_bmp->buffer; unsigned char *pxm_ptr = pixmap->get_image() + (pixmap->get_width() * y + x) * 4; @@ -93,9 +102,9 @@ static int draw_glyph(Pixmap *pixmap, int x, int y, char c) if(bmp_ptr[j] && dest_x >= clipping_rect.x) { int a = (int)bmp_ptr[j]; - pxm_ptr[4 * j] = (a * text_color[0] + pxm_ptr[4 * j] * (255 - a)) / 255; - pxm_ptr[4 * j + 1] = (a * text_color[1] + pxm_ptr[4 * j + 1] * (255 - a)) / 255; - pxm_ptr[4 * j + 2] = (a * text_color[2] + pxm_ptr[4 * j + 2] * (255 - a)) / 255; + pxm_ptr[4 * j] = (a * text->text_color[0] + pxm_ptr[4 * j] * (255 - a)) / 255; + pxm_ptr[4 * j + 1] = (a * text->text_color[1] + pxm_ptr[4 * j + 1] * (255 - a)) / 255; + pxm_ptr[4 * j + 2] = (a * text->text_color[2] + pxm_ptr[4 * j + 2] * (255 - a)) / 255; } } } @@ -104,5 +113,5 @@ static int draw_glyph(Pixmap *pixmap, int x, int y, char c) bmp_ptr += ft_bmp->pitch; } - return ft_face->glyph->advance.x >> 6; + return text->ft_face->glyph->advance.x >> 6; }