Example #1
0
CBitmap *CreateStringBitmap (
	const char *s, int nKey, uint nKeyColor, int *nTabs, int bCentered, int nMaxWidth, int bForce)
{
	int			origColor = CCanvas::Current ()->FontColor (0).index;//to allow easy reseting to default string color with colored strings -MPM
	int			i, x, y, hx, hy, w, h, aw, cw, spacing, nTab, nChars, bHotKey;
	CBitmap		*bmP, *bmfP;
	tRgbaColorb	hc, kc, *pc;
	ubyte			*pf;
	CPalette		*palP = NULL;
	tRgbColorb	*colorP;
	ubyte			c;
	const char	*textP, *text_ptr1, *nextRowP;
	int			letter;
	CFont*		fontP = fontManager.Current ();

#if 0
if (!(bForce || (gameOpts->menus.nStyle && gameOpts->menus.bFastMenus)))
	return NULL;
#endif
fontP->StringSizeTabbed (s, w, h, aw, nTabs, nMaxWidth);
if (!(w && h))
	return NULL;
if (bForce >= 0) {
	for (i = 1; i < w; i <<= 2)
		;
	w = i;
	for (i = 1; i < h; i <<= 2)
		;
	h = i;
	}
if (!(bmP = CBitmap::Create (0, w, h, 4))) 
	return NULL;
if (!bmP->Buffer ()) {
	delete bmP;
	return NULL;
	}
bmP->SetName ("String Bitmap");
bmP->Clear ();
bmP->AddFlags (BM_FLAG_TRANSPARENT);
nextRowP = s;
y = 0;
nTab = 0;
nChars = 0;
while (nextRowP) {
	text_ptr1 = nextRowP;
	nextRowP = NULL;
	textP = text_ptr1;
#if 0
#	if DBG
	if (bCentered)
		x = (w - fontManager.Current ()->GetLineWidth (textP)) / 2;
	else
		x = 0;
#	else
	x = bCentered ? (w - fontManager.Current ()->GetLineWidth (textP)) / 2 : 0;
#	endif
#else
x = 0;
#endif
	while ((c = *textP)) {
		if (c == '\n') {
			nextRowP = textP + 1;
			y += fontP->Height () + 2;
			nTab = 0;
			break;
			}
		if (c == '\t') {
			textP++;
			if (nTabs && (nTab < 6)) {
				int	w, h, aw;

				fontManager.Current ()->StringSize (textP, w, h, aw);
				x = LHX (nTabs [nTab++]);
				if (!gameStates.multi.bSurfingNet)
					x += nMaxWidth - w;
				for (i = 1; i < w; i <<= 2)
					;
				w = i;
				for (i = 1; i < h; i <<= 2)
					;
				h = i;
				}
			continue;
			}
		letter = c - fontP->MinChar ();
		fontP->GetCharWidth (c, textP [1], cw, spacing);
		if (c <= 0x06) {	//not in font, draw as space
			textP = ScanEmbeddedColors (c, textP, origColor, 128, 2);
			continue;
			}
		if (!fontManager.Current ()->InFont (letter)) {
			x += spacing;
			textP++;
			continue;
			}
		if ((bHotKey = ((nKey < 0) && isalnum (c)) || (nKey && ((int) c == nKey))))
			nKey = 0;
		bmfP = (bHotKey && (fontManager.Current () != SMALL_FONT)) ? SELECTED_FONT->Bitmaps () + letter : fontP->Bitmaps () + letter;
		palP = bmfP->Parent () ? bmfP->Parent ()->Palette () : bmfP->Palette ();
		nChars++;
		i = nKeyColor * 3;
		kc.red = RGBA_RED (nKeyColor);
		kc.green = RGBA_GREEN (nKeyColor);
		kc.blue = RGBA_BLUE (nKeyColor);
		kc.alpha = 255;
		if (fontP->Flags () & FT_COLOR) {
			for (hy = 0; hy < bmfP->Height (); hy++) {
				pc = reinterpret_cast<tRgbaColorb*> (bmP->Buffer ()) + (y + hy) * w + x;
				pf = bmfP->Buffer () + hy * bmfP->RowSize ();
				for (hx = bmfP->Width (); hx; hx--, pc++, pf++)
					if ((c = *pf) != TRANSPARENCY_COLOR) {
						colorP = palP->Color () + c;
						pc->red = colorP->red * 4;
						pc->green = colorP->green * 4;
						pc->blue = colorP->blue * 4;
						pc->alpha = 255;
						}
#if DBG
					else
						c = c;
#endif
				}
			}
		else {
			if (CCanvas::Current ()->FontColor (0).index < 0)
				memset (&hc, 0xff, sizeof (hc));
			else {
				if (CCanvas::Current ()->FontColor (0).rgb) {
					hc = CCanvas::Current ()->FontColor (0).color;
					}
				else {
					colorP = palP->Color () + CCanvas::Current ()->FontColor (0).index;
					hc.red = colorP->red * 4;
					hc.green = colorP->green * 4;
					hc.blue = colorP->blue * 4;
					}
				hc.alpha = 255;
				}
			for (hy = 0; hy < bmfP->Height (); hy++) {
				pc = reinterpret_cast<tRgbaColorb*> (bmP->Buffer ()) + (y + hy) * w + x;
				pf = bmfP->Buffer () + hy * bmfP->RowSize ();
				for (hx = bmfP->Width (); hx; hx--, pc++, pf++)
					if (*pf != TRANSPARENCY_COLOR)
						*pc = bHotKey ? kc : hc;
				}
			}
		x += spacing;
		textP++;
		}
	}
bmP->SetPalette (palP);
bmP->SetTranspType (-1);
return bmP;
}