Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Instance drawing #14

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fefe3b0
Added necessary types for XPLMInstance and created XPlaneInstance. St…
Nov 9, 2018
eb051e1
Merge branch 'master' into InstanceDrawing
Nov 13, 2018
0eaf212
Add back XPInstance and the factory from the XPSceneryObject
Nov 14, 2018
2c8f513
Make InstanceDrawing working, including the test harness.
Nov 21, 2018
829c8aa
Merge branch 'upstream/master' into InstanceDrawing
Nov 21, 2018
aeb99ca
Clean up the GraphicsTestPlugin. Add xpnetcfg.json to .gitignore
Nov 21, 2018
6f905fb
Trying to figure out how to pass a reference to DrawInfos
Nov 24, 2018
b64b2d7
Fix the drawing an object at multiple locations
Nov 25, 2018
30df4cb
Rename and move some structures that directly map to the X-Plane API.
Nov 25, 2018
9cf6c1d
Improve documentatino for XPDrawInfo
Nov 28, 2018
c2fe9b2
Document XPProbeResult enum
Nov 28, 2018
6a80edd
Add documentation to XPDrawingPhase
Nov 28, 2018
410fcee
Cosmetic change
Nov 28, 2018
becea96
Remove unintended line break
Nov 28, 2018
dead143
Cleaned up opening brackets of some blocks according to coding standard
Nov 28, 2018
e03c9bb
Clean up opening brackets for functions according to coding standard
Dec 1, 2018
007afe0
- Add Instance API to the available APIs
Dec 2, 2018
ec81927
For instance creation, change IEnumerable<string> for datarefs to str…
Dec 2, 2018
4801011
Instantiate 10 instances of the same object instead of just one instance
Dec 2, 2018
784a86e
Add documentation to XPlaneInstance
Dec 2, 2018
dd1de73
Remove some unnecessary comments and do cosmetic changes
Dec 2, 2018
ee20683
Add documentation to XPlaneScenery
Dec 6, 2018
8e3107c
Merge remote-tracking branch 'upstream/master' into InstanceDrawing
Dec 6, 2018
fd7f955
Merge branch 'master' into InstanceDrawing
mbrachner May 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ PublishProfiles
plugin
package
.DS_Store
*.user
*.user
/XPNetPluginTestHost/xpnetcfg.json
52 changes: 49 additions & 3 deletions XPLMTestHarness/XPLMTestHarness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "XPLMScenery.h"
#include "XPLMGraphics.h"
#include <map>
#include <list>
#include <tuple>
#include <vector>
#include <string>
Expand Down Expand Up @@ -118,6 +119,9 @@ static commandmap configuredCommands;
static map<XPLMFlightLoop_f, flightloop> registeredFlightLoops;
static map<tuple<XPLMDrawCallback_f, XPLMDrawingPhase, int>, drawcallback> registeredDrawCallbacks;

static unsigned int instanceCounter = 0;
static map<XPLMInstanceRef, std::list<string>> registeredInstances;

template <typename T>
void SetDataRef(const string name, const T& value, datarefmap<T>& container)
{
Expand Down Expand Up @@ -557,6 +561,11 @@ XPLM_API void XPLMDrawObjects(
int earth_relative)
{
std::cout << "XPLMTestHarness: Drawing object " << inObject << std::endl;
for (int c = 0; c < inCount; c++)
{
std::cout << "XPLMTestHarness: Position #" << c << ": ("
<< inLocations[c].x << ", " << inLocations[c].y << ", " << inLocations[c].z << ")" << std::endl;
}
}

XPLM_API void XPLMUnloadObject(
Expand Down Expand Up @@ -608,6 +617,43 @@ XPLM_API void XPLMLocalToWorld(
*outLatitude = inZ;
}

XPLM_API XPLMInstanceRef XPLMCreateInstance(XPLMObjectRef obj, const char ** datarefs)
{
instanceCounter++;
std::cout << "XPLMTestHarness: Creating instance for object " << obj << std::endl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer to have the test harness just store and provide data, without logging to the console. The data can then be verified/asserted/logged either over in the test plugin (C#) or in XPNetPluginTestHost.cpp. If you didn't notice, there were no log messages here in the harness prior to your push last month. One of the directions I want to move with XPNet is to turn the test host into an actual automated unit test suite, which would mean that the places where it currently says, "look for log message XYZ to see if the test succeeded" would turn into tests that are checked for pass/fail. In preparation for that, all of the test output was isolated to XPNetPluginTestHost.cpp, and the harness here just tried/tries to be a simple stand-in for X-Plane. It's OK to do testing by checking logging output currently (that's how the other tests work now as well), but the logging and checking should be done in the test plugin or the test host, not the test harness, to reduce the amount of rework we'll have to do to make those tests automated. (If you're not sure how to do what I'm talking about, or are unfamiliar with automated unit testing, let me know and we'll discuss).

unsigned int i = 0;
std::list<string> dRefList;
for (i = 0; datarefs[i] != NULL; i++) {
mbrachner marked this conversation as resolved.
Show resolved Hide resolved
std::cout << "XPLMTestHarness: DataRef " << datarefs[i] << std::endl;
dRefList.push_back(datarefs[i]);
}
XPLMInstanceRef instRef = reinterpret_cast<XPLMInstanceRef>(static_cast<uintptr_t>(instanceCounter));
registeredInstances.emplace(instRef, dRefList);

return instRef;
}

XPLM_API void XPLMDestroyInstance(XPLMInstanceRef instance)
{
std::cout << "XPLMTestHarness: Destroying instance " << instance << std::endl;
registeredInstances.erase(instance);
}

XPLM_API void XPLMInstanceSetPosition(XPLMInstanceRef instance, const XPLMDrawInfo_t * new_position, const float * data)
{
std::cout << "XPLMTestHarness: Instance set position for instance " << instance << std::endl;
auto it = registeredInstances.find(instance);
if (it != registeredInstances.end()) {
auto datarefs = it->second;
unsigned int i = 0;
for (string dref : datarefs) {
std::cout << "XPLMTestHarness: DataRef " << dref << " set to value " << data[i++] << std::endl;
}
}
}



XPLM_API void XPHarnessInvokeFlightLoop(float elapsedSinceLastCall, float elapsedTimeSinceLastFlightLoop, int counter)
{
// Before invoking clean up all the unregistered flight loops.
Expand All @@ -627,11 +673,11 @@ XPLM_API void XPHarnessInvokeFlightLoop(float elapsedSinceLastCall, float elapse
XPLM_API void XPHarnessInvokeDrawCallback()
{
// Before invoking clean up all the unregistered draw callbacks.
auto it = registeredFlightLoops.begin();
while (it != registeredFlightLoops.end())
auto it = registeredDrawCallbacks.begin();
while (it != registeredDrawCallbacks.end())
{
if (it->second.deleted)
it = registeredFlightLoops.erase(it);
it = registeredDrawCallbacks.erase(it);
else
++it;
}
Expand Down
1 change: 1 addition & 0 deletions XPLMTestHarness/XPLMTestHarness.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "XPLMDataAccess.h"
#include "XPLMProcessing.h"
#include "XPLMDisplay.h"
#include "XPLMInstance.h"
#include "XPLMUtilities.h"

typedef enum {
Expand Down
35 changes: 29 additions & 6 deletions XPNet.CLR/Display/XPlaneDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace XPNet
/// Delegate for .NET code that wants to be called as part of the render loop.
/// </summary>
public delegate int DrawDelegate(
XPLMDrawingPhase inPhase,
XPDrawingPhase inPhase,
int inIsBefore
);

Expand All @@ -16,12 +16,12 @@ int inIsBefore
/// </summary>
public interface IXPlaneDisplay
{
IXPDrawingLoopHook RegisterDrawHook(DrawDelegate drawDelegate, XPLMDrawingPhase inPhase, int inWantsBefore);
IXPDrawingLoopHook RegisterDrawHook(DrawDelegate drawDelegate, XPDrawingPhase inPhase, int inWantsBefore);
}

internal class XPlaneDisplay : IXPlaneDisplay
{
public IXPDrawingLoopHook RegisterDrawHook(DrawDelegate drawDelegate, XPLMDrawingPhase inPhase, int inWantsBefore)
public IXPDrawingLoopHook RegisterDrawHook(DrawDelegate drawDelegate, XPDrawingPhase inPhase, int inWantsBefore)
{
return new XPDrawingLoopHook(drawDelegate, inPhase, inWantsBefore);
}
Expand All @@ -40,10 +40,10 @@ internal class XPDrawingLoopHook : IXPDrawingLoopHook
private readonly DrawDelegate m_loopDelegate;
private readonly XPLMDrawCallback_f m_hookDelegate;

private readonly XPLMDrawingPhase m_inPhase;
private readonly XPDrawingPhase m_inPhase;
private readonly int m_inWantsBefore;

public unsafe XPDrawingLoopHook(DrawDelegate drawCallbackDelegate, XPLMDrawingPhase inPhase, int inWantsBefore)
public unsafe XPDrawingLoopHook(DrawDelegate drawCallbackDelegate, XPDrawingPhase inPhase, int inWantsBefore)
{
m_loopDelegate = drawCallbackDelegate;
m_inPhase = inPhase;
Expand All @@ -62,7 +62,7 @@ public unsafe void Dispose()
PluginBridge.ApiFunctions.XPLMUnregisterDrawCallback(m_hookDelegate, m_inPhase, m_inWantsBefore, null);
}

private unsafe int XPLMDrawHook(XPLMDrawingPhase inPhase, int inIsBefore, void* inRefcon)
private unsafe int XPLMDrawHook(XPDrawingPhase inPhase, int inIsBefore, void* inRefcon)
{
try
{
Expand All @@ -75,6 +75,29 @@ private unsafe int XPLMDrawHook(XPLMDrawingPhase inPhase, int inIsBefore, void*
return 1; // If an exception happens in the draw hook, then let X-Plane draw
}
}
}

/// <summary>
/// Describes the phase in which X-Plane is currently drawing
/// </summary>
public enum XPDrawingPhase : int
{
// MAINT: This needs to be kept in sync with the
// XPLMDrawingPhase enumerable from the X-Plane API
FirstScene = 0,
Terrain = 5,
Airports = 10,
Vectors = 15,
Objects = 20,
Airplanes = 25,
LastScene = 30,
FirstCockpit = 35,
Panel = 40,
Gauges = 45,
Window = 50,
LastCockpit = 55,
LocalMap3D = 100,
LocalMap2D = 101,
LocalMapProfile = 102
}
}
54 changes: 54 additions & 0 deletions XPNet.CLR/Instance/XPlaneInstance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace XPNet
{
public interface IXPlaneInstance
mbrachner marked this conversation as resolved.
Show resolved Hide resolved
{
void CreateInstance();
void DestroyInstance();
}

internal class XPlaneInstance : IXPlaneInstance
{
public void CreateInstance()
{
throw new NotImplementedException();
}

public void DestroyInstance()
{
throw new NotImplementedException();
}
}

public interface IXPInstance : IDisposable
{
void SetPosition(XPDrawInfo xPLMDrawInfo_t, IEnumerable<float> v);
}

internal unsafe class XPInstance : IXPInstance
{
readonly void* m_instanceRef;
mbrachner marked this conversation as resolved.
Show resolved Hide resolved

public XPInstance(void* instanceRef)
{
m_instanceRef = instanceRef;
}

public void Dispose()
{
PluginBridge.ApiFunctions.XPLMDestroyInstance(m_instanceRef);
}

public void SetPosition(XPDrawInfo xPLMDrawInfo_t, IEnumerable<float> v)
{
var floatArray = v.ToArray();
fixed (float* p = &floatArray[0])
{
PluginBridge.ApiFunctions.XPLMInstanceSetPosition(m_instanceRef, xPLMDrawInfo_t, p);
}
}
}
}
Loading