How to change a compass (SDK Gauges Aircraft example)

Note: I assume you know how to run Developer mode on in MSFS2020.

Note: If you don’t have Microsoft Visual Studio Community 2019 (VS) shut down MSFS2020, then download and install VS. Enable all C++ options in the installation screen. Then, start MSFS2020 again and reinstall your MSFS2020 SDK. The reinstall will add libraries and some templates to VS that you will need to recompile the example code.

Note: the airplane modified in this tutorial is only the Gauges Aircraft example from the SDK. The change in the layout of the compass is done for this airplane only. The SDK will not allow you to change instruments in any other airplane types, unless you have the package Source code to build them.

Purpose

The C++ Gauges example in the SDK allows you to draw cockpit instruments and handle mouse events.
This tutorial modifies the Compass instrument in the SDK Gauges Aircraft cockpit panel.

1. Make your first flight with the SDK Gauges Aircraft.

To use the example airplane, you’ll have to enter Developer mode in MSFS2020 and open the project:

C:\MSFS SDK\Samples\Aircraft\GaugeAircraft\GaugeAircraftProject.xml

This will take a while, wait for it. If all went well, the Project Editor appears. Issue a Build, then close the Project Editor window (x) and go World Map in MSFS. You can now choose an airplane that looks like this one…

CompassAircraft

Take a short flight first… the SDK dialogs work best when Bing has been properly initialized.

When you hop inside the aircraft, you have a cockpit panel that looks as follows:

The purpose of this tutorial is to show how to change a small detail. You won’t need understanding of the complete Gauges API to run this. I will show how to do a very minor change to the left instrument, which is the compass.

Let’s go to Visual Studio now. Always make sure you leave the simulator in Main Menu, this will allow Visual Studio to change files.

When you fully installed the SDK and C++ examples are installed in the default path, the three example instruments are found in the following C++ solution:

C:\MSFS SDK\Samples\Aircraft\GaugeAircraft\Sources\Code\SampleWasmModule.sln

Double click the solution SampleWasmModule.sln, and using Solution Explorer on the right, navigate to Compass.CPP at line number 153.

You see how the instrument C++ initialzes the wind directions on the compass, that is N, E, S and W.

	switch (i)
					{
					case 0:
						strcpy(cNumber, "N");
						break;
					case 90:
						strcpy(cNumber, "E");
						break;
					case 180:
						strcpy(cNumber, "S");
						break;
					case 270:
						strcpy(cNumber, "W");
						break;
					default:
						sprintf(cNumber, "%i", i / 10);
						break;
					}

For this tutorial, we’re going to change this code, to let the compass show NZOW (in Dutch) instead of NSEW (English)

	switch (i)
					{
					case 0:
						strcpy(cNumber, "N");
						break;
					case 90:
						strcpy(cNumber, "O");
						break;
					case 180:
						strcpy(cNumber, "Z");
						break;
					case 270:
						strcpy(cNumber, "W");
						break;
					default:
						sprintf(cNumber, "%i", i / 10);
						break;
					}

Testing the change

After doing above change in the code, choose Visual Studio menu Build, click Rebuild Solution. This will update a Web Assembly object, that is the .wasm file needed by MS2020 to run the gauges. When you compile the project in the default SDK samples directory, the .wasm file is automatically copied into our Gauges Aircraft directory also. All is set for a Project Build in the Simulator now.

Go back to the Simulator, select the Project Editor, perform the Build.

CompassBuildAircraft

This can take some time… Now close the Project Editor and go to the World Map. Check airplane choice and pick the Gauges Aicraft, if it is not already selected. You’ll now see this panel:

The “S” in the compass is modified to “Z” and “E” has become “O”. You now have a Dutch compass !

The cockpit panel that comes with the Gauges example is unflyable, because some key data is invisible.

To be able to fly this thing, I put ground speed and altitude from ground into the compass :smiley_cat:

I changed the sCompassVars record to include these variables… note that the extra eNumber field is used for these variables as well,

struct sCompassVars
{
	ENUM m_eDegrees;
	ENUM m_eNumber;
	ENUM m_ePlaneHeadingDegreesTrue;
	ENUM m_eGroundSpeed; // ..
	ENUM m_eAltAboveGround;
	int m_iFont;
};

In the PANEL_SERVICE_PRE_INSTALL event, initialize access to these variables:

g_CompassVars.m_eNumber = get_units_enum("NUMBER");
g_CompassVars.m_eGroundSpeed = get_aircraft_var_enum("GROUND VELOCITY");
g_CompassVars.m_eAltAboveGround = get_aircraft_var_enum("PLANE ALT ABOVE GROUND");

To represent the text in the PANEL_SERVICE_PRE_DRAW event, do this at the start of the event…

FLOAT64 fGroundSpeed = aircraft_varget(g_CompassVars.m_eGroundSpeed, g_CompassVars.m_eNumber, 0);
FLOAT64 fAltAboveGround = aircraft_varget(g_CompassVars.m_eAltAboveGround, g_CompassVars.m_eNumber, 0);

… and after the small white plane drawing, do this

nvgTranslate(nvgctx, 0, -fR * 0.035f); // up
nvgFillColor(nvgctx, nvgRGB(255, 0, 0));
char cCenterText[20];
fAltAboveGround = fAltAboveGround / 0.3048; // Feet				
snprintf(cCenterText, sizeof(cCenterText), "%.0f   %.0f", fGroundSpeed, fAltAboveGround);
nvgTextAlign(nvgctx, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
float dR1 = -fR * 0.04f;
nvgFontSize(nvgctx, fR * 0.16f);
nvgText(nvgctx, 0, 0, cCenterText, nullptr);
nvgResetTransform(nvgctx);
nvgTranslate(nvgctx, p_draw_data->winWidth * 0.5f, p_draw_data->winHeight * 0.5f);

Flying 81 knots at 1642 feet now looks like,

AltitudeAirspeed

Reference aircraft variables handling: see prepar3d.com, variables and programming gauges

2 Likes