plane icon Welcome to Microsoft Flight Simulator’s SDK Q&A Platform!

You have questions regarding the SDK? DevMode Tools? SimConnect? You would like to submit an idea for future improvements, seek help or exchange knowledge? You’re in the right place.


Please take a moment to read the platform’s guidelines before you get started!


question

JB3DG avatar image
JB3DG asked JB3DG commented

GDI+ DrawString with \n new lines

It appears the \n character is treated as a space when drawing text instead of applying a Y shift. Is there any way around this? I would hate to have to do separate DrawString calls every time I use a new line in a single character buffer.

In addition, I notice the length parameter of the DrawString call is not respected. It seems to render the entire string till null termination no matter what I put in the length parameter. This hinders applications where I want to use custom spacing between each character.

wasmgdi+
1 comment
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

JB3DG avatar image JB3DG commented ·
0 Likes 0 ·
ricarenado avatar image
ricarenado answered JB3DG commented

I know it may sound stupid but did you try \r\n?

1 comment
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

JB3DG avatar image JB3DG commented ·
No joy. Just a double space
0 Likes 0 ·
RoscoHead avatar image
RoscoHead answered JB3DG commented

I don't think DrawString has ever supported hard-coded line breaks. You might be able to fake it by adding enough spaces where you want the newline so it wraps, but that's a bit of a hack (and might not work). I think multiple calls are probably the safest way.

1 comment
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

JB3DG avatar image JB3DG commented ·
If you mean DrawString in MSFS yeah I can see this bug slipping through, although if you mean DrawString in GDI+ in native C++ environment I know that is not the case as this is display code I ported from a P3D project. And wrap isn't working either. So I would like Asobo to look deeper at fixing this (it's not hard to do, my own proprietary DX based 2D graphics engine that I use in P3D does it nicely).
0 Likes 0 ·
HansHartmann avatar image
HansHartmann answered JB3DG commented

You can use nvgTextBox instead of DrawString. I know, it's not \n but you can define a "breakRowWidth" and use this to create a multi-line text output.

MSFS GDI+ and nanovg can be used concurrently without any problems.

4 comments
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

JB3DG avatar image JB3DG commented ·
Thanks. I would hope Asobo fixes this though as it is far more convenient from a programming perspective to simply use \n for newlines in strings.
0 Likes 0 ·
JB3DG avatar image JB3DG JB3DG commented ·
@HansHartmann last I tried to load fonts things didn't go all that well. Can you show an example of loading fonts and combining GDI+ and nvg?
0 Likes 0 ·
HansHartmann avatar image HansHartmann JB3DG commented ·

I don't see a problem with fonts.

However, I recommend to load fonts first and only start drawing after the NVG_FONT.face_id change from -1 to something useful. In the following example, I skip the draw function until fontsInitialized is true.

if (!fontsInitialized)
{
if (this->nvgctx == nullptr)
return;

this->fonts[0] = NVG_FONT(this->nvgctx, "ARIAL", "./SimObjects/Airplanes/Common/fonts/arial.ttf", 36.0f);
this->fonts[1] = NVG_FONT(this->nvgctx, "ARIAL", "./SimObjects/Airplanes/Common/fonts/arial.ttf", 42.0f);
this->fonts[2] = NVG_FONT(this->nvgctx, "ARIAL", "./SimObjects/Airplanes/Common/fonts/arial.ttf", 32.0f);

for (auto & font : this->fonts)
{
if (font.faceId == -1)
fontsInitialized = false;
}

fontsInitialized = true;
}

Hope this helps.

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 5 attachments (including images) can be used with a maximum of 19.1 MiB each and 23.8 MiB total.