Skip to content

Instantly share code, notes, and snippets.

@JDeeth
Last active April 8, 2018 10:08
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/a56f765ec48bdde7e0a43ee566bc45b0 to your computer and use it in GitHub Desktop.
Save JDeeth/a56f765ec48bdde7e0a43ee566bc45b0 to your computer and use it in GitHub Desktop.
Static library
// IIRC it's a bit vague when this DataRef registers and gets destructed
PPL::DataRef<std::string> sim_build{"sim/version/sim_build_string"};
PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc) {
char name[] = "BadExample";
strcpy(outName, name); strcpy(outSig, name); strcpy(outDesc, name);
return 1;
}
PLUGIN_API void XPluginStop() {}
PLUGIN_API void XPluginDisable() {}
PLUGIN_API int XPluginEnable() {
XPLMSpeakString(sim_build.c_str());
return 1;
}
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID, long, void *) {}
#include <memory>
std::unique_ptr<PPL::DataRef<std::string>> sim_build;
PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc) {
char name[] = "BetterExample";
strcpy(outName, name); strcpy(outSig, name); strcpy(outDesc, name);
// With this pattern, the dataref is only registered when XPluginStart is called
sim_build = std::make_unique<PPL::DataRef<std::string>>("sim/version/sim_build_string");
return 1;
}
PLUGIN_API void XPluginStop() {}
PLUGIN_API void XPluginDisable() {}
PLUGIN_API int XPluginEnable() {
// there's probably a better way to get the string out of the DataRef out of the unique_ptr
std::string s{*foo.get()};
XPLMSpeakString(s.c_str());
return 1;
}
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID, long, void *) {}
#include <memory>
// more usually, the datarefs I'm accessing are part of a class or struct that does something
struct Foo {
PPL::DataRef<std::string> sim_build{"sim/version/sim_build_string"};
PPL::DataRef<int> xpdr_blink{"sim/cockpit/radios/transponder_code"};
PPL::DataRef<float> ias_kts{
"sim/cockpit2/gauges/indicators/airspeed_kts_pilot"};
void speak() {
std::stringstream s;
s << ias_kts << "kts, " << std::string(sim_build);
XPLMSpeakString(s.str().c_str());
}
};
std::unique_ptr<Foo> foo;
PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc) {
char name[] = "BetterExample";
strcpy(outName, name); strcpy(outSig, name); strcpy(outDesc, name);
// this makes it much cleaner to create everything
foo = std::make_unique<Foo>();
return 1;
}
PLUGIN_API void XPluginStop() {}
PLUGIN_API void XPluginDisable() {}
PLUGIN_API int XPluginEnable() {
foo->speak();
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