Skip to content

Instantly share code, notes, and snippets.

@JDeeth
Last active May 8, 2018 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JDeeth/1d6db5d8a81c49f0b00543718d92841f to your computer and use it in GitHub Desktop.
Save JDeeth/1d6db5d8a81c49f0b00543718d92841f to your computer and use it in GitHub Desktop.
X-Plane flight loop callback demo
// complete plugin using PPL
#include <cstring>
#include <memory>
#include <XPLMPlugin.h>
#include <XPLMUtilities.h>
#include <processor.h>
class Foo : PPL::Processor {
// callback function inherited from Processor
virtual float callback(float, float, int) override {
// do whatever this plugin's made to do
if (tick)
XPLMSpeakString("No luck catching them swans then");
else
XPLMSpeakString("It's just the one swan actually");
tick = !tick;
// call again after 10 seconds
return 10;
}
bool tick;
};
// using unique_ptr and make_unique is an easy way to make sure everything gets
// set up nice and safely at a precise point during XPluginStart
static std::unique_ptr<Foo> foo;
PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc) {
char name[] = "flcb";
strcpy(outName, name);
strcpy(outSig, name);
strcpy(outDesc, name);
foo = std::make_unique<Foo>();
return 1;
}
PLUGIN_API void XPluginStop(void) {}
PLUGIN_API void XPluginDisable(void) {}
PLUGIN_API int XPluginEnable(void) {
return 1;
}
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID, long, void*) {}
// complete plugin using SDK directly
#include <cstring>
#include <XPLMPlugin.h>
#include <XPLMUtilities.h>
#include <XPLMProcessing.h>
// declare the values, datarefs, commands etc
static bool tick;
// callback functions need this signature
float callback(float, float, int, void*) {
// do whatever this plugin's made to do
if (tick)
XPLMSpeakString("No luck catching them swans then");
else
XPLMSpeakString("It's just the one swan actually");
tick = !tick;
// call this function again after 10 seconds
return 10;
}
PLUGIN_API int XPluginStart(char* outName, char* outSig, char* outDesc) {
char name[] = "flcb";
strcpy(outName, name);
strcpy(outSig, name);
strcpy(outDesc, name);
XPLMRegisterFlightLoopCallback(callback, 10, nullptr);
return 1;
}
PLUGIN_API void XPluginStop(void) {
// if you forget this the UNIVERSE EXPLODES possibly
XPLMUnregisterFlightLoopCallback(callback, nullptr);
}
PLUGIN_API void XPluginDisable(void) {}
PLUGIN_API int XPluginEnable(void) {
return 1;
}
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID, long, void*) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment