I am developing a very simple application which displays random shapes and text. I want the text to have different colors, and font types. Right now I am using Direct2D to create rectangles and the text. Unfortunately, the Direct2D libraries such as d2d1WriteFactory don't seem to be as fast as the GDI DrawText function.
For example, using Direct2D, you would do something like the following to create an array of characters. Note that there are 2 ways to do this, using something called a TextLayout, or a different function called DrawText.
bool USE_TEXT_LAYOUT = true;
p_textColor->SetColor(font_color);
p_dWriteFactory->CreateTextFormat(
font_name,
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
font_size,
L"", &p_textFormat
);
if (USE_TEXT_LAYOUT){
int width = text_position.right - text_position.left;
int height = text_position.bottom - text_position.top;
p_dWriteFactory->CreateTextLayout(
char_array, char_array_size, p_textFormat, width, height, &p_textLayout );
p_renderTarget->DrawTextLayout(
D2D1::Point2F(text_position.left, text_position.bottom),
p_textLayout,
p_textColor,
D2D1_DRAW_TEXT_OPTIONS_NONE);
}
else {
p_renderTarget->DrawText(
char_array,
char_array_size - 1,
p_textFormat,
text_position),
p_textColor);
}
On the other hand, if you want to stick to GDI libraries, you would do the following which seems to be faster:
int char_num = int(255*rand()/RAND_MAX);
WCHAR text_char[2];
text_char[0] = (WCHAR)char_num;
text_char[1] = 0;
G_LOG_FONT.lfHeight = font_height;
G_LOG_FONT.lfWidth = font_width;
StringCchCopy(G_LOG_FONT.lfFaceName, 9, font_name);
int font_red_byte = int(255*rand()/RAND_MAX);
int font_green_byte = int(255*rand()/RAND_MAX);
int font_blue_byte = int(255*rand()/RAND_MAX);
int font_size = max(font_width, font_height);
DWORD font_color = RGB(font_red_byte, font_green_byte, font_blue_byte);
COLORREF text_color = font_color;
COLORREF background_color = RGB(font_green_byte, font_red_byte, font_blue_byte);
SetTextColor(h_dc, text_color);
SetBkColor(h_dc, background_color);
SetBkMode(h_dc, TRANSPARENT);
G_H_FONT = CreateFontIndirect(&G_LOG_FONT);
SelectObject(h_dc, G_H_FONT);
DrawText(h_dc, (LPCWSTR)text_char, -1, &position, DT_NOCLIP |
DT_NOPREFIX | DT_CALCRECT | DT_CENTER | DT_VCENTER);
DrawText(h_dc, (LPCWSTR)text_char, -1, &position, DT_NOPREFIX |
DT_NOCLIP | DT_CENTER | DT_VCENTER);
DeleteObject(G_H_FONT);
Does anyone know any other approach that would be faster for displaying multicolor strings than these functions?