|
2 | 2 | // FlyWithLua Plugin for X-Plane 12 |
3 | 3 | // ---------------------------------- |
4 | 4 |
|
5 | | -#define PLUGIN_VERSION_NO "2.8.12" |
| 5 | +#define PLUGIN_VERSION_NO "2.8.13" |
6 | 6 | #define PLUGIN_VERSION_BUILD __DATE__ " " __TIME__ |
7 | 7 | #define PLUGIN_VERSION PLUGIN_VERSION_NO " build " PLUGIN_VERSION_BUILD |
8 | 8 |
|
|
170 | 170 | * v2.8.10 [changed] Removed X-Plane LuaJIT alloc because it is not needed and was causing issues with Linux. |
171 | 171 | * v2.8.11 [changed] Fixed missing Throttle 9 from SaveInitalAssignments.ini Thanks XPJavelin from x-plane.org |
172 | 172 | * 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 |
175 | 175 | * Markus (Teddii): |
176 | 176 | * v2.1.20 [changed] bug fixed in Luahid_open() and Luahid_open_path(), setting last HID device index back if no device was found |
177 | 177 | * [changed] extended logMsg() with logType=logToAll|logToDevCon|logToSqkBox. If XSquawkBox is not connected logMsg() will fall back to DevCon |
178 | 178 | * [changed] overworked all logMsg() and XSBSpeakString() calls - so there are no more doubled strings in the code |
179 | 179 | * [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 | + * |
181 | 182 | */ |
182 | 183 |
|
183 | 184 | /* Configure Code:Blocks to compile FlyWithLua on Windows |
|
316 | 317 | //#endif |
317 | 318 | // include the extern command provided by the LUA team |
318 | 319 | #include <lua.hpp> |
| 320 | +using namespace flywithlua; // Resolve namespace issues when compiling |
319 | 321 |
|
320 | 322 | /// This symbol comes from statically linked LuaXML_lib library. |
321 | 323 | extern "C" int luaopen_LuaXML_lib(lua_State* L); |
@@ -2896,6 +2898,76 @@ static int LuaDoEveryFrame(lua_State* L) |
2896 | 2898 | return 0; |
2897 | 2899 | } |
2898 | 2900 |
|
| 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 | + |
2899 | 2971 | static int LuaDoOften(lua_State* L) |
2900 | 2972 | { |
2901 | 2973 | if (!lua_isstring(L, 1)) |
@@ -6024,6 +6096,7 @@ void RegisterCoreCFunctionsToLua(lua_State* L) |
6024 | 6096 | lua_register(L, "do_on_mouse_wheel", LuaDoEveryMouseWheel); |
6025 | 6097 | lua_register(L, "do_every_draw", LuaDoEveryDrawCallback); |
6026 | 6098 | lua_register(L, "do_every_frame", LuaDoEveryFrame); |
| 6099 | + lua_register(L, "do_every_frame_after", LuaDoEveryFrameAfter); // Callback after FlightModel |
6027 | 6100 | lua_register(L, "do_often", LuaDoOften); |
6028 | 6101 | lua_register(L, "do_sometimes", LuaDoSometimes); |
6029 | 6102 | lua_register(L, "add_macro", LuaAddMacro); |
@@ -7361,6 +7434,15 @@ PLUGIN_API void XPluginDisable(void) |
7361 | 7434 | XPLMUnregisterFlightLoopCallback(MyFastLoopCallback, nullptr); |
7362 | 7435 | XPLMUnregisterFlightLoopCallback(MySlowLoopCallback, nullptr); |
7363 | 7436 | 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 | + |
7364 | 7446 |
|
7365 | 7447 | // write to Log.txt |
7366 | 7448 | logMsg(logToDevCon, "FlyWithLua Info: FlyWithLua plugin disabled."); |
@@ -7416,6 +7498,8 @@ PLUGIN_API int XPluginEnable(void) |
7416 | 7498 | nullptr); /* refcon not used. */ |
7417 | 7499 |
|
7418 | 7500 | 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. |
7419 | 7503 |
|
7420 | 7504 | // create the FlyWithLua menu inside the plugin menu |
7421 | 7505 | if (FlyWithLuaMenuItem < 0) |
|
0 commit comments