WARNING: Advanced C++ (take excedrin now)
*Deep breath*
Ok... I'm creating a class which will represent a line of text on a display. The goal is, of course, encapsulation and a desire to be able to use it like this:
The DisplayLine class has a constructor that looks like this:
You'll note the brush has to be initialized like the above due to being a const.
Now, with the above, I can do this:
And this will compile fine. However, it crashes in flight sim due to:
Now, if I uncomment and use sFormat above (vs. pStrFormat), it works fine. Problem is, then I couldn't allow it to be adjusted. I'm baffled why this doesn't work. Any ideas fellow gurus?
*Deep breath*
Ok... I'm creating a class which will represent a line of text on a display. The goal is, of course, encapsulation and a desire to be able to use it like this:
Code:
pTitleLine = new DisplayLine(rect1, pageTitle); // Create title
pTitleLine->AlignCenter(); // Center text
pTitleLine->Draw(pGraphics);
The DisplayLine class has a constructor that looks like this:
Code:
DisplayLine ::DisplayLine (RectF rect, string str)
: strBrush(Color((int)(brightness * 2.55), 0, 255, 0))
{
lineRect = rect; // RectF for DisplayString()
lineText = str; // string to display
pStrFormat = new StringFormat();
}
You'll note the brush has to be initialized like the above due to being a const.
Now, with the above, I can do this:
Code:
void DisplayLine ::Draw(Graphics *pGraphics)
{
Font font_big (L"FIXEDSYS", 18, FontStyleBold, UnitPixel);
Font font_small (L"FIXEDSYS", 12, FontStyleBold, UnitPixel);
//StringFormat sFormat = new StringFormat();
SolidBrush gbrush(Color((int)(brightness * 2.55), 0, 255, 0));
//sFormat.SetAlignment(StringAlignmentCenter);
//sFormat.SetLineAlignment(StringAlignmentFar);
wchar_t buf[20+1] = { 0 };
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lineText.c_str(), (int)lineText.length(), buf, 20);
pGraphics->DrawString(buf, -1, &font_big, lineRect, pStrFormat, &gbrush);
}
And this will compile fine. However, it crashes in flight sim due to:
Code:
pTitleLine->AlignCenter(); // Crashes FS
// Coded like this:
void DisplayLine::AlignCenter()
{
pStrFormat->SetAlignment(StringAlignmentCenter);
}
Now, if I uncomment and use sFormat above (vs. pStrFormat), it works fine. Problem is, then I couldn't allow it to be adjusted. I'm baffled why this doesn't work. Any ideas fellow gurus?