Skip to content

Commit fbe0e47

Browse files
authored
Merge pull request #122 from smoothchat/patch-5
New callback which runs in the AFTER flightloop phase of x-plane
2 parents dc48ed9 + 5e9ddc5 commit fbe0e47

1 file changed

Lines changed: 88 additions & 4 deletions

File tree

src/FlyWithLua.cpp

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FlyWithLua Plugin for X-Plane 12
33
// ----------------------------------
44

5-
#define PLUGIN_VERSION_NO "2.8.12"
5+
#define PLUGIN_VERSION_NO "2.8.13"
66
#define PLUGIN_VERSION_BUILD __DATE__ " " __TIME__
77
#define PLUGIN_VERSION PLUGIN_VERSION_NO " build " PLUGIN_VERSION_BUILD
88

@@ -170,14 +170,15 @@
170170
* v2.8.10 [changed] Removed X-Plane LuaJIT alloc because it is not needed and was causing issues with Linux.
171171
* v2.8.11 [changed] Fixed missing Throttle 9 from SaveInitalAssignments.ini Thanks XPJavelin from x-plane.org
172172
* v2.8.12 [changed] Fixed issue with do_on_mouse_click now is only called once per mouse click Thanks apn from x-plane.org
173-
*
174-
*
173+
* v2.8.13 [Added] do_every_frame_after() callback to run in FlightLoop Phase 1 (after physics processing, thus eliminating
174+
* graphic jitter
175175
* Markus (Teddii):
176176
* v2.1.20 [changed] bug fixed in Luahid_open() and Luahid_open_path(), setting last HID device index back if no device was found
177177
* [changed] extended logMsg() with logType=logToAll|logToDevCon|logToSqkBox. If XSquawkBox is not connected logMsg() will fall back to DevCon
178178
* [changed] overworked all logMsg() and XSBSpeakString() calls - so there are no more doubled strings in the code
179179
* [fixed] fixed some copy/pasted logMessages in LuaAddMacro(), LuaLastButton(), LuaSetArray()
180-
* [fixed] fixed a bug in function LuaSpeakString()
180+
* [fixed] fixed a bug in function LuaSpeakString()
181+
*
181182
*/
182183

183184
/* Configure Code:Blocks to compile FlyWithLua on Windows
@@ -316,6 +317,7 @@
316317
//#endif
317318
// include the extern command provided by the LUA team
318319
#include <lua.hpp>
320+
using namespace flywithlua; // Resolve namespace issues when compiling
319321

320322
/// This symbol comes from statically linked LuaXML_lib library.
321323
extern "C" int luaopen_LuaXML_lib(lua_State* L);
@@ -2896,6 +2898,76 @@ static int LuaDoEveryFrame(lua_State* L)
28962898
return 0;
28972899
}
28982900

2901+
// --- MULTI-CALLBACK VERSION OF: FlightLoop Callback AfterFlightModel ---
2902+
2903+
static XPLMFlightLoopID g_DoEveryFrameAfter_ID = nullptr;
2904+
static std::vector<std::string> do_every_frame_after_code;
2905+
2906+
// Called every frame after flight model is updated
2907+
float Do_Every_Frame_After(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop,
2908+
int inCounter, void* inRefcon)
2909+
{
2910+
if (!LuaIsRunning) return -1.0f;
2911+
2912+
CopyDataRefsToLua();
2913+
2914+
for (const auto& code : do_every_frame_after_code)
2915+
{
2916+
if (luaL_loadstring(FWLLua, code.c_str()) == LUA_OK)
2917+
{
2918+
if (lua_pcall(FWLLua, 0, 0, 0) != LUA_OK)
2919+
{
2920+
const char* err = lua_tostring(FWLLua, -1);
2921+
logMsg(logToDevCon, std::string("Error in do_every_frame_after(): ") + err);
2922+
lua_pop(FWLLua, 1);
2923+
}
2924+
}
2925+
else
2926+
{
2927+
const char* err = lua_tostring(FWLLua, -1);
2928+
logMsg(logToDevCon, std::string("Syntax error in do_every_frame_after(): ") + err);
2929+
lua_pop(FWLLua, 1);
2930+
}
2931+
}
2932+
2933+
CopyDataRefsToXPlane();
2934+
2935+
return -1.0f; // every frame
2936+
}
2937+
2938+
void Register_Do_Every_Frame_After()
2939+
{
2940+
if (g_DoEveryFrameAfter_ID != nullptr)
2941+
return;
2942+
2943+
XPLMCreateFlightLoop_t loop_params{};
2944+
loop_params.structSize = sizeof(XPLMCreateFlightLoop_t);
2945+
loop_params.phase = xplm_FlightLoop_Phase_AfterFlightModel;
2946+
loop_params.callbackFunc = Do_Every_Frame_After;
2947+
loop_params.refcon = nullptr;
2948+
2949+
g_DoEveryFrameAfter_ID = XPLMCreateFlightLoop(&loop_params);
2950+
XPLMScheduleFlightLoop(g_DoEveryFrameAfter_ID, -1.0, 0);
2951+
}
2952+
2953+
static int LuaDoEveryFrameAfter(lua_State* L)
2954+
{
2955+
if (!lua_isstring(L, 1))
2956+
{
2957+
logMsg(logToDevCon, "FlyWithLua Error: do_every_frame_after() needs a string of Lua code.");
2958+
LuaIsRunning = false;
2959+
return 0;
2960+
}
2961+
2962+
std::string lua_code = lua_tostring(L, 1);
2963+
do_every_frame_after_code.push_back(lua_code);
2964+
2965+
return 0;
2966+
}
2967+
2968+
// --- End DoEveryFrameAfter routines
2969+
2970+
28992971
static int LuaDoOften(lua_State* L)
29002972
{
29012973
if (!lua_isstring(L, 1))
@@ -6024,6 +6096,7 @@ void RegisterCoreCFunctionsToLua(lua_State* L)
60246096
lua_register(L, "do_on_mouse_wheel", LuaDoEveryMouseWheel);
60256097
lua_register(L, "do_every_draw", LuaDoEveryDrawCallback);
60266098
lua_register(L, "do_every_frame", LuaDoEveryFrame);
6099+
lua_register(L, "do_every_frame_after", LuaDoEveryFrameAfter); // Callback after FlightModel
60276100
lua_register(L, "do_often", LuaDoOften);
60286101
lua_register(L, "do_sometimes", LuaDoSometimes);
60296102
lua_register(L, "add_macro", LuaAddMacro);
@@ -7361,6 +7434,15 @@ PLUGIN_API void XPluginDisable(void)
73617434
XPLMUnregisterFlightLoopCallback(MyFastLoopCallback, nullptr);
73627435
XPLMUnregisterFlightLoopCallback(MySlowLoopCallback, nullptr);
73637436
XPLMUnregisterFlightLoopCallback(MyEveryFrameLoopCallback, nullptr);
7437+
7438+
// Unregister and clean up the AfterFlightModel flight loop
7439+
if (g_DoEveryFrameAfter_ID != nullptr)
7440+
{
7441+
XPLMDestroyFlightLoop(g_DoEveryFrameAfter_ID);
7442+
g_DoEveryFrameAfter_ID = nullptr;
7443+
do_every_frame_after_code.clear();
7444+
}
7445+
73647446

73657447
// write to Log.txt
73667448
logMsg(logToDevCon, "FlyWithLua Info: FlyWithLua plugin disabled.");
@@ -7416,6 +7498,8 @@ PLUGIN_API int XPluginEnable(void)
74167498
nullptr); /* refcon not used. */
74177499

74187500
XPLMRegisterDrawCallback(FWLDrawWindowCallback, xplm_Phase_Window, 0, (void*) "FWLWindowDrawer");
7501+
7502+
Register_Do_Every_Frame_After(); // Provide a callback that runs in FlightLoop Phase After mode.
74197503

74207504
// create the FlyWithLua menu inside the plugin menu
74217505
if (FlyWithLuaMenuItem < 0)

0 commit comments

Comments
 (0)