• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

C++ Tutorial - Interpolator, a non-linearity class

Messages
240
Country
switzerland
This tutorial will discuss a way to create non-linearity tables, to be used in any situation when we need some value to be derived from another variable, and can be expressed with a table.

This approach could be used in place of SDK non-linearity tables, but if you work entirely in GDI and can't use them, a custom approach is needed. The advantage of using a C++ class, is that is entirely generic, and you can use it everywhere in your code, to simulate any kind of relationship between variables tied to each other, not just graphical objects.

In this example, we are going to simulate a fake "vibration" simulation, tied to the engine RPM. Here's the link to the Source code:

http://www.mediafire.com/download/g93gb616i6pveb8/Interpolator_sample.zip

It includes code from the previous CNamedVar sample, and add some bits.

The new files added to the project are:

interpolater.h
interpolater.cpp


They are the implementation of the InterpTable class. You can have a look at those files to understand how the class is made.

Using the class is very simple, you first declare an object variable of the InterpTable type:

Code:
InterpTable MyTable;

Then, somewhere at your code startup, you fill up the objects with entries that make up the table, like this:

Code:
MyTable.addEntry( 0.0, 0.0 );
MyTable.addEntry( 60.0, 1.5 );
MyTable.addEntry( 80.0, 4.0 );
MyTable.addEntry( 100.0, 4.5 );

This table has 4 entries and it will work in a way that, when supplied with a number from 0 to 60, the result will be linearly interpolated from 0 to 1.5, then from 1.5 to 4.0 with a value between 60 and 80, and from 4.0 to 4.5 if the value goes from 80 to 100.

If you supply with a value less than the minimum ( less than 0 ), it will return 0, and if you supply it with more than 100, it will return 4.5.

To "use" the object, just call the interpolate member function over it, passing a value, like this:

Code:
double MyDerivedValue = MyTable.interpolate( 65.0 );

In this case, since we passed 65 to it, we'll receive a value slightly higher than 1.5, according to the table of values created before.

Let's see how we used it in the tutorial. The declaration will happen, as in the previous examples, in the constructor of the CSimulation class in the CSimulation.h header file. This means our table will be inside the CSimulation Singleton, together with the other variables, user or sim based.

So, we added this code to the CSimulation constructor:

Code:
CMultipleVar<SIMVAR_GENERAL_ENG_PCT_MAX_RPM, Units::percent, 1> EnginePctRPM;

// Declare an Interpolation (non-linearity) table to simulate something based on engine rpm
InterpTable tab_vc_vib_rpm;

We also added a new CFSVar, to read the engine max percent rpm from FSX, which is named EnginePctRPM.

Next, we fill the table with values, in the CSimulation class constructor BODY, the one that starts after the curly brackets:

Code:
CSimulation()
    : mStarted(false),
    MyVar1("MyVar1", 4, 2, 10, true),
    MyVar2("MyVar2", 3, 1, 9, true)

{
   // Fill the interpolation table with values
   tab_vc_vib_rpm.addEntry(60.0, 0.0);
   tab_vc_vib_rpm.addEntry(70.0, 0.3);
   tab_vc_vib_rpm.addEntry(80.0, 0.6);
   tab_vc_vib_rpm.addEntry(90.0, 0.8);
   tab_vc_vib_rpm.addEntry(100.0, 1.2);
}

In the CSimulation.cpp file, we add the update for the engine pct rpm variable in the CSimulation::Update() function, the one that updates at 6hz should be fine for this application.

Code:
void CSimulation::Update6hz(void)
{
    PlaneLatitude.Update();
    PlaneLongitude.Update();
    PlaneAltitude.Update();
    Throttle.Update(1);

    EnginePctRPM.Update(1);
}

Finally, we go into the actual gauge that "draws" stuff on screen, the GDIPlus_Gauge.cpp file, in the PANEL_SERVICE_PRE_DRAW cycle of the callback routine:

Code:
StringCbPrintf(mpszDest, mcbDest, L"LAT: %6.3f°\nLON: %6.3f°\nALT: %8.0f ft.\n\nHeading: %6.3f°\nMyVar1: %.2f\nMyVar2: %.2f\nENG PCT MAX: %.2f\nENG VIB: %.2f",
   Sim->PlaneLatitude.Get(),
   Sim->PlaneLongitude.Get(),
   Sim->PlaneAltitude.Get(),
   Sim->Heading.Get(),
   Sim->MyVar1.Get(),
   Sim->MyVar2.Get(),

   Sim->EnginePctRPM.Get( 1 ),
   Sim->tab_vc_vib_rpm.interpolate( Sim->EnginePctRPM.Get( 1 ) )
  );

The last two lines have been added from the previous example and, for clarity, we first print the original value of the EnginePctRPM variable, using Get(1) instead of Update(1) since we already updated it at 6hz and don't need to update it again at the fps rate, and THEN we print the derived value, calling the interpolate member function over the tab_vc_vib_rpm object, passing the EnginePctRPM in order to calculate our fictional "vibration" simulation, derived from the engine RPM.

In this case, we'll obtain a value ranging from 0 to 1.2, depending on the actual max pct rpm of the engine, that will be interpolated according to our table. If you compile the sample, you'll see this is exactly.

Of course, you can easily use those tables to calculate values to drive animations. You could tie an animation, for example for a 3d needle of a 3d VC gauge, to an L: variable, and set it using this method to calculate an angle to rotate the needle, based on some other variable, so you don't need any XML code to handle non-linearity, your needle will have the usual "dumb" code definition in the modeldef.xml file, that won't do anything other than linking the animation to the L: variable, and all the work to decide how the needle is supposed to be rotated, will be in your C++ code.

But the approach is really generic: you can use it to simulate new systems. For example, in the MS F/A-18, I used a couple of tables (in addition to all 3d needles having non-linearity tables made this way) to derive the engine nozzle position based on the engine rpm, with two different tables used, depending if afterburners were active or not.
 
Last edited:
Very nice tutorial, Umberto.

Just a question, I see you inlined the interpolate function, that is a public member of the InterpTable class. I thought these kind of functions are automatically inlined by the compiler, which in fact may or may not actually inline them (?). Do you think making them inline will always improve the result, or it's like a "just in case" alternative?. I ask because I'm fond of inlines though I don't have a clear reason for being so :confused:

Tom

PS: Have you worked on shared cockpit code? Do you have any example on how to deal with shared events and stream data?
 
The inline keyword was probably redundant in this case, because when a function is defined in the class declaration, the compiler will automatically inline it, if possible. At least, this is what VC++ does, I believe is optional (or depending by the optimization switches) in other compilers.

When you define a function outside the class declaration in the .CPP file, using the MyClass::MyFunction() form, if you want to inline it, you must tell the compiler, because it won't do it automatically.

Usually, you want to inline the shortest functions, because the added code length of having "unwrapped" the function, is less costly than the code overhead of adding a function call to it (with the related pops and pushes on the stack), this was probably a borderline case, if it was a more complex function, I wouldn't probably inline it.
 
I tried to compile GDIPlus_Gauge.
VS Express 2010 got the error:

Solution folders are are not supported in this version of the application
Solution 'Solution Items' will be displayed as unavailable
Clicking OK gave the message:
One or more projects in the solution were not loaded correctly.
Please the Output Window for details

The Output Window reads
Some of the properties associated with the solution could not be read.
and no code folders are shown, from which I assume VS Express can't be used.
VS Express 2012 gave the message:
Error 1 error MSB8020: The builds tools for v120 (Platform Toolset = 'v120') cannot be found. To build using the v120 build tools, either click the Project menu or right-click the solution, and then select "Update VC++ Projects...". Install v120 to build using the v120 build tools. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.Cpp.Platform.targets 44 5 GDIPlus_Gauge
When I click the Project menu or right-click the solution, the menu "Update VC++ Projects..." does not appear in either .

Do I need to install VS Express 2013?
 
Do I need to install VS Express 2013?

I guess so, the project was made with VS 2013, not sure it it's backward compatible with VS 2012, apparently it isn't. Searching around, it seems that VS 2013 Express should support Solution Folders, previous Express versions didn't.
 
I think that must be so. Platform Toolset ='v120 also appears to be for VS Express 2014 also.
 
I think that must be so. Platform Toolset ='v120 also appears to be for VS Express 2014 also.


You have a version that has not yet been released ;)
 
As far as I am aware VS (Express) 2014 doesn't exist. VS 2013 Update 2 anticipated release is Spring 2014
 
Do I need to install VS Express 2013?

I guess you could build the .dll in other version (ie VC++ Express 2005,2010, etc) just using the sources provided with the example (.cpp, .h, etc.) A bit of exta work but not too complicated.

Tom
 
I have run VS Express 2.13 but got message:
Unable start program "C:\fsx\fsx.exe"
The system cannot find the path specified
It then gives the error:
Error 1 error MSB3073: The command "copy "C:\Users\Gerry\Documents\Visual Studio 2013\Projects\GDI Tutorials\Debug\GDIPlus_Gauge.dll" "c:\FSX\Gauges"
:VCEnd" exited with code 1. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppCommon.targets 132 5 GDIPlus_Gauge

It appears to looking for "C:\fsx\fsx.exe" and "c:\FSX\Gauges". This isn't the location of my FSX folder. How should I change it - I can't Find and Replace in the files?
 
It appears to looking for "C:\fsx\fsx.exe"

This is not really an error, and is only need to DEBUG your gauge while FSX is running. Of course, you can change it to your FSX folder, in the Debug settings of the Project.

and "c:\FSX\Gauges". This isn't the location of my FSX folder. How should I change it - I can't Find and Replace in the files?

This, instead, is the path to copy the compiled gauge after it has been compiled. It's a POST-BUILD step so, you can edit it to your actual FSX\Gagues path, by editing the "Post-build step" in the Project settings.
 
I'm still getting this error:
Error 1 error MSB3073: The command "copy "C:\Users\Gerry\Documents\Visual Studio 2013\Projects\GDI Tutorials\Debug\GDIPlus_Gauge.dll" C:\Microsoft Games\Microsoft Flight Simulator X\Gauges"
:VCEnd" exited with code 1. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppCommon.targets 132 5 GDIPlus_Gauge
The paths seem to not be recognised by Windows Explorer?

C:\Users\Gerry\Documents\Visual Studio 2013\Projects\GDI Tutorials\Debug

C:\Microsoft Games\Microsoft Flight Simulator X\Gauges
 
Last edited:
Shouldn't it be

C:\Users\Gerry\My Documents\Visual Studio 2013\Projects\GDI Tutorials\Debug

not

C:\Users\Gerry\Documents\Visual Studio 2013\Projects\GDI Tutorials\Debug
 
Use this instead:

copy "$(TargetPath)" "C:\Microsoft Games\Microsoft Flight Simulator X\Gauges"

Note the open/closed double quotes over both parameters.
 
Back
Top