From 8fee290f7a77a09ae11cf8a42e444332bb86f20e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20M=C3=BCnzel?= Date: Tue, 27 Mar 2018 04:23:15 +0200 Subject: [PATCH 01/22] always eat the mouse click in the 2d window --- src/overlaygauge.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/overlaygauge.cpp b/src/overlaygauge.cpp index 968de14..7cc751d 100644 --- a/src/overlaygauge.cpp +++ b/src/overlaygauge.cpp @@ -406,26 +406,26 @@ int OverlayGauge::handle2dClickCallback(XPLMWindowID window_id, int x, int y, XP /// Test for the mouse in the window if (vr_enabled_ == 0) { - if (coordInRect(x, y, Left, Top, Left+40, Top-40)) - { - XPLMTakeKeyboardFocus(0); - window_has_keyboard_focus_ = false; - setVisible(false); - } - else if (coordInRect(x, y, Right-40, Top, Right, Top-40)) - { - XPLMSetWindowPositioningMode(window2d_id_, xplm_WindowPopOut, -1); - } - else if (!handleNonDragClick(x_rel, y_rel, false)) - { - dX = x - Left; - dY = y - Top; - window_is_dragging_ = true; - } + if (coordInRect(x, y, Left, Top, Left+40, Top-40)) + { + XPLMTakeKeyboardFocus(0); + window_has_keyboard_focus_ = false; + setVisible(false); + } + else if (coordInRect(x, y, Right-40, Top, Right, Top-40)) + { + XPLMSetWindowPositioningMode(window2d_id_, xplm_WindowPopOut, -1); + } + else if (!handleNonDragClick(x_rel, y_rel, false)) + { + dX = x - Left; + dY = y - Top; + window_is_dragging_ = true; + } } else { - return handleNonDragClick(x_rel, y_rel, false); + handleNonDragClick(x_rel, y_rel, false); } break; case xplm_MouseDrag: From 98ab208e6ccf92963df2b7fe1828511ff222eb03 Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Mon, 2 Apr 2018 19:31:22 +0200 Subject: [PATCH 02/22] child can override update for the per-frame callback. This is the better place to do shit rather than in the wantRedraw override. Changing window params from the draw callback can crash in VR, so don't do it. --- src/overlaygauge.cpp | 5 +++++ src/overlaygauge.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/overlaygauge.cpp b/src/overlaygauge.cpp index 968de14..2e9c669 100644 --- a/src/overlaygauge.cpp +++ b/src/overlaygauge.cpp @@ -198,6 +198,7 @@ bool OverlayGauge::isVisible() const void OverlayGauge::frame() { + update(); visible_2d_ = XPLMGetWindowIsVisible(window2d_id_); if (!wantVRifAvailable()) return; @@ -269,6 +270,10 @@ bool OverlayGauge::wantVRifAvailable() const return true; } +void OverlayGauge::update() +{ +} + void OverlayGauge::draw2dWindowCallback(XPLMWindowID) { if (visible_2d_) diff --git a/src/overlaygauge.h b/src/overlaygauge.h index e24816c..76e4a09 100644 --- a/src/overlaygauge.h +++ b/src/overlaygauge.h @@ -88,6 +88,7 @@ class OverlayGauge virtual int handleMouseWheel(int x, int y, int wheel, int clicks); virtual float instrumentBrightness() const; virtual bool wantVRifAvailable() const; + virtual void update(); static int draw3dCallback(XPLMDrawingPhase phase, int is_before, void* refcon); From 793713391bd616e1ef91c7a8a3345abdbe522be5 Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Tue, 17 Apr 2018 00:50:12 +0200 Subject: [PATCH 03/22] move the OSD to the VR headset if we are in VR right now. Since the OSD is considered a "transient" thing only needed to alert the user to something that has gone wrong, or notification-like update to something that just happened, it has no logic to go in or out of VR mode. It stays in VR or in 2D simply where it was created. --- src/onscreendisplay.cpp | 6 ++++++ src/onscreendisplay.h | 6 ++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/onscreendisplay.cpp b/src/onscreendisplay.cpp index 412d308..5113ebb 100644 --- a/src/onscreendisplay.cpp +++ b/src/onscreendisplay.cpp @@ -35,6 +35,7 @@ using namespace PPL; OnScreenDisplay::OnScreenDisplay(int width, int height, const std::string& title): screen_w_("sim/graphics/view/window_width"), screen_h_("sim/graphics/view/window_height"), + vr_enabled_("sim/graphics/VR/enabled"), title_(title) { left_ = (screen_w_ - width)/2; @@ -54,6 +55,11 @@ OnScreenDisplay::OnScreenDisplay(int width, int height, const std::string& title XPSetWidgetProperty(widget_id_, xpProperty_MainWindowType, xpMainWindowStyle_Translucent); XPSetWidgetProperty(widget_id_, xpProperty_Object, reinterpret_cast(this)); XPAddWidgetCallback(widget_id_, widgetCallback); + window_id_ = XPGetWidgetUnderlyingWindow(widget_id_); + if (vr_enabled_ == 1) + { + XPLMSetWindowPositioningMode(window_id_, xplm_WindowVR, -1); + } } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/onscreendisplay.h b/src/onscreendisplay.h index 5b44280..ad28783 100644 --- a/src/onscreendisplay.h +++ b/src/onscreendisplay.h @@ -29,6 +29,7 @@ #define ONSCREENDISPLAY_H #include "XPWidgetDefs.h" +#include "XPLMDisplay.h" #include "dataref.h" namespace PPL { @@ -47,9 +48,10 @@ class OnScreenDisplay { static int widgetCallback(XPWidgetMessage inMessage, XPWidgetID inWidget, intptr_t param1, intptr_t param2); private: - DataRef screen_w_; - DataRef screen_h_; + DataRef screen_w_, screen_h_; + DataRef vr_enabled_; XPWidgetID widget_id_; + XPLMWindowID window_id_; int top_, left_, bottom_, right_; std::string title_; }; From 8f61eb7f4ce6e2cebfe04173f04a8c546d5b7ff8 Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Wed, 23 May 2018 22:33:17 -0400 Subject: [PATCH 04/22] oops I killed bmp texture loading in v2. Fixed. --- src/texture.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/texture.cpp b/src/texture.cpp index c9a7cca..092f8f2 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -153,11 +153,8 @@ Texture::Texture(const std::string& file_name) XPLMBindTexture2d(m_id, 0); #endif -#if APL - glGenerateMipmap(GL_TEXTURE_2D); -#else - gluBuild2DMipmaps(GL_TEXTURE_2D, 3, m_imagedata.Width, m_imagedata.Height, GL_RGB, GL_UNSIGNED_BYTE, &m_imagedata.pData[0]); -#endif + GLuint type = GL_RGB; // 24bit bmp only supported for now + glTexImage2D(GL_TEXTURE_2D, 0, type, m_imagedata.Width, m_imagedata.Height, 0, type, GL_UNSIGNED_BYTE, &m_imagedata.pData[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); From a35431646f467b62de73178c3edd16e45eca1fba Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Fri, 25 May 2018 19:39:20 -0400 Subject: [PATCH 05/22] experimental CMake build on Mac. --- CMakeLists.txt | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..00d42ed --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,42 @@ +cmake_minimum_required (VERSION 3.8) +project(PPL VERSION 2.0.0 DESCRIPTION "Plugin Patterns Library for X-Plane 11") + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib) + +if(UNIX AND NOT APPLE) +set(LINUX TRUE) +endif() + +if(APPLE) +set(CMAKE_OSX_DEPLOYMENT_TARGET 10.11) +set(CMAKE_OSX_SYSROOT "macosx") +set(PLATFORM_CORE_DEFINITIONS + APL=1 + LIN=0 + IBM=0) +else() +if(LINUX) +endif() +endif() + +set(DEFINITIONS + XPLM200=1 + XPLM210=1 + XPLM300=1 + XPLM301=1 + ${PLATFORM_CORE_DEFINITIONS}) + +include_directories(/usr/X11/include/freetype2 +${CMAKE_SOURCE_DIR}/../SDK/CHeaders/XPLM +${CMAKE_SOURCE_DIR}/../SDK/CHeaders/Widgets +${CMAKE_SOURCE_DIR}/include/simpleini +) + +file(GLOB SOURCES "src/*.cpp") + + +add_library(PPL STATIC ${SOURCES}) +target_compile_definitions(PPL PRIVATE ${DEFINITIONS}) From 04fb1e6dc203864bec99495a25d15fa703f19df7 Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Fri, 25 May 2018 19:39:38 -0400 Subject: [PATCH 06/22] never used that, it can go away --- ppl.pro | 5 ----- src/fontmgr.cpp | 17 ----------------- src/pluginpath.cpp | 16 ---------------- src/texture.cpp | 14 +------------- 4 files changed, 1 insertion(+), 51 deletions(-) diff --git a/ppl.pro b/ppl.pro index 214d87a..e78c315 100644 --- a/ppl.pro +++ b/ppl.pro @@ -19,11 +19,6 @@ OBJECTS_DIR = objects DESTDIR = lib TARGET = ppl -standalone { - DEFINES += BUILD_FOR_STANDALONE - TARGET = pplstandalone -} - macx { DEFINES += APL=1 IBM=0 LIN=0 QMAKE_CXXFLAGS += -Wextra -Wfloat-equal -pedantic diff --git a/src/fontmgr.cpp b/src/fontmgr.cpp index 66e6e49..5f71d1a 100644 --- a/src/fontmgr.cpp +++ b/src/fontmgr.cpp @@ -35,10 +35,7 @@ #include #include #endif - -#ifndef BUILD_FOR_STANDALONE #include "XPLMGraphics.h" -#endif using namespace PPL; @@ -227,14 +224,8 @@ FontHandle FontMgr::loadFont(const char* inFontPath, const char * inStartMem, co unsigned char* textureData = new unsigned char[info->tex_height * info->tex_width]; // Create the texture number that we'll be tied to -#ifdef BUILD_FOR_STANDALONE - glGenTextures(1, (GLuint*)&info->tex_id); - // Now we bind to it - glBindTexture(GL_TEXTURE_2D, info->tex_id); -#else XPLMGenerateTextureNumbers(&info->tex_id, 1); XPLMBindTexture2d(info->tex_id, 0); -#endif // We have to 0 out the memory or we'll get artifacts when the glyphs are cut memset(textureData, 0, info->tex_height * info->tex_width); @@ -445,11 +436,7 @@ void FontMgr::displayTexture( if(!inFont) return; -#ifdef BUILD_FOR_STANDALONE - glBindTexture(GL_TEXTURE_2D, inFont->tex_id); -#else XPLMBindTexture2d(inFont->tex_id, 0); -#endif glColor3f(1.0, 0.0, 0.0); glBegin(GL_QUADS); glTexCoord2f(0.0, 1.0); glVertex2f(0, 0); @@ -499,11 +486,7 @@ void FontMgr::drawRange( return; float l, b, r, t, scale; -#ifdef BUILD_FOR_STANDALONE - glBindTexture(GL_TEXTURE_2D, inFont->tex_id); -#else XPLMBindTexture2d(inFont->tex_id, 0); -#endif // Determine how much to scale the font to make it right the height scale = (inTop - inBottom) / inFont->line_height; diff --git a/src/pluginpath.cpp b/src/pluginpath.cpp index 69e748f..0e40d1d 100644 --- a/src/pluginpath.cpp +++ b/src/pluginpath.cpp @@ -42,37 +42,25 @@ std::string PluginPath::plugin_directory = ""; std::string PluginPath::prependXPlanePath(const std::string& file) { -#ifdef BUILD_FOR_STANDALONE - return file; -#else char path[512]; XPLMGetSystemPath(path); std::string absolute_path(path); absolute_path.append(file); return absolute_path; -#endif } std::string PluginPath::prependPluginPath(const std::string& file) { -#ifdef BUILD_FOR_STANDALONE - return file; -#else std::string path = "/Resources/plugins/"; path.append(plugin_directory).append("/").append(file); return prependXPlanePath(path); -#endif } std::string PluginPath::prependPluginResourcesPath(const std::string& file) { std::string res_path("Resources/"); res_path.append(file); -#ifdef BUILD_FOR_STANDALONE - return res_path; -#else return prependPluginPath(res_path); -#endif } /////////////////////////////////////////////////////////////////////////////// @@ -80,9 +68,6 @@ std::string PluginPath::prependPluginResourcesPath(const std::string& file) std::string PluginPath::prependPlanePath(const std::string& file) { -#ifdef BUILD_FOR_STANDALONE - return file; -#else char name[512]; char path[512]; XPLMGetNthAircraftModel(0, name, path); @@ -91,7 +76,6 @@ std::string PluginPath::prependPlanePath(const std::string& file) absolute_path = absolute_path.substr(0, pos); absolute_path.append(file); return absolute_path; -#endif } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/texture.cpp b/src/texture.cpp index 092f8f2..b50c0c5 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -29,10 +29,8 @@ #include #include #include "texture.h" -#ifndef BUILD_FOR_STANDALONE #include "XPLMUtilities.h" #include "XPLMGraphics.h" -#endif #if IBM #include @@ -143,15 +141,9 @@ Texture::Texture(const std::string& file_name) swapRedBlue(); - -#ifdef BUILD_FOR_STANDALONE - glGenTextures(1, (GLuint*)&m_id); - glBindTexture(GL_TEXTURE_2D, m_id); -#else /// Do the opengl stuff using XPLM functions for a friendly Xplane existence. XPLMGenerateTextureNumbers(&m_id, 1); XPLMBindTexture2d(m_id, 0); -#endif GLuint type = GL_RGB; // 24bit bmp only supported for now glTexImage2D(GL_TEXTURE_2D, 0, type, m_imagedata.Width, m_imagedata.Height, 0, type, GL_UNSIGNED_BYTE, &m_imagedata.pData[0]); @@ -224,14 +216,10 @@ Texture::Texture(const std::string& file_name) { type=GL_RGB; // If So Set The 'type' To GL_RGB } -#ifdef BUILD_FOR_STANDALONE - glGenTextures(1, (GLuint*)&m_id); - glBindTexture(GL_TEXTURE_2D, m_id); -#else /// Do the opengl stuff using XPLM functions for a friendly Xplane existence. XPLMGenerateTextureNumbers(&m_id, 1); XPLMBindTexture2d(m_id, 0); -#endif + glTexImage2D(GL_TEXTURE_2D, 0, type, m_imagedata.Width, m_imagedata.Height, 0, type, GL_UNSIGNED_BYTE, &m_imagedata.pData[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); From 2b52eb91b917b00a9394bf480e26b30e2c981f64 Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Fri, 25 May 2018 21:41:52 -0400 Subject: [PATCH 07/22] restored optional mipmap building --- src/texture.cpp | 10 +++++++++- src/texture.h | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/texture.cpp b/src/texture.cpp index b50c0c5..ed6ac85 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -81,7 +81,7 @@ void SwapEndian(int32_t *){} #endif -Texture::Texture(const std::string& file_name) +Texture::Texture(const std::string& file_name, bool build_mipmaps) { if (file_name.rfind(".bmp") != std::string::npos) { @@ -229,6 +229,14 @@ Texture::Texture(const std::string& file_name) { throw std::runtime_error("The texture file is neither a BMP nor a TGA. Other fileformats are not supported."); } + if (build_mipmaps) + { +#if APL + glGenerateMipmap(GL_TEXTURE_2D); +#else + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, m_imagedata.Width, m_imagedata.Height, GL_ALPHA, GL_UNSIGNED_BYTE, &m_imagedata.pData[0])); +#endif + } } Texture::~Texture() diff --git a/src/texture.h b/src/texture.h index 3ac8a69..b1cbdaa 100644 --- a/src/texture.h +++ b/src/texture.h @@ -76,7 +76,7 @@ class Texture #pragma pack(pop, ident) - Texture(const std::string& file_name); + Texture(const std::string& file_name, bool build_mipmaps = false); ~Texture(); Texture(const Texture&) = delete; Texture& operator=(const Texture&) = delete; From 059afc34b7978c5f87f46fa176e6d1ccc9495b25 Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Tue, 29 May 2018 13:48:32 -0400 Subject: [PATCH 08/22] more progress building PPL with CMake. --- CMakeLists.txt | 67 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 00d42ed..7cf20d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ -cmake_minimum_required (VERSION 3.8) +cmake_minimum_required (VERSION 3.0) project(PPL VERSION 2.0.0 DESCRIPTION "Plugin Patterns Library for X-Plane 11") -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 14) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib) @@ -11,14 +11,34 @@ set(LINUX TRUE) endif() if(APPLE) -set(CMAKE_OSX_DEPLOYMENT_TARGET 10.11) -set(CMAKE_OSX_SYSROOT "macosx") -set(PLATFORM_CORE_DEFINITIONS - APL=1 - LIN=0 - IBM=0) + set(CMAKE_OSX_DEPLOYMENT_TARGET 10.12) + set(PLATFORM_CORE_DEFINITIONS + APL=1 + LIN=0 + IBM=0) + set(PLATFORM_INCLUDE_DIRECTORIES + /usr/X11/include/freetype2) else() if(LINUX) + set(PLATFORM_CORE_DEFINITIONS + APL=0 + LIN=1 + IBM=0) + set(PLATFORM_INCLUDE_DIRECTORIES + /usr/include/freetype2) +else() + set(PLATFORM_CORE_DEFINITIONS + APL=0 + LIN=0 + IBM=1 + FREETYPE2_STATIC + _USE_MATH_DEFINES + NOMINMAX + WIN32_LEAN_AND_MEAN + GLEW_STATIC=1) + set(PLATFORM_INCLUDE_DIRECTORIES + include/glew/include, + include/openal-soft/include) endif() endif() @@ -29,14 +49,33 @@ set(DEFINITIONS XPLM301=1 ${PLATFORM_CORE_DEFINITIONS}) -include_directories(/usr/X11/include/freetype2 -${CMAKE_SOURCE_DIR}/../SDK/CHeaders/XPLM -${CMAKE_SOURCE_DIR}/../SDK/CHeaders/Widgets -${CMAKE_SOURCE_DIR}/include/simpleini -) +include_directories(${CMAKE_SOURCE_DIR}/../SDK/CHeaders/XPLM + ${CMAKE_SOURCE_DIR}/../SDK/CHeaders/Widgets + ${CMAKE_SOURCE_DIR}/include/simpleini + ${PLATFORM_INCLUDE_DIRECTORIES}) + -file(GLOB SOURCES "src/*.cpp") +set(SOURCES + src/pluginpath.cpp + src/settings.cpp + src/dataref.cpp + src/messagewindow.cpp + src/onscreendisplay.cpp + src/owneddata.cpp + src/logichandler.cpp + src/texture.cpp + src/overlaygauge.cpp + src/log.cpp + src/logwriter.cpp + src/menuitem.cpp + src/processor.cpp + src/vertexbuffer.cpp + src/alsoundbuffer.cpp + src/alcontextmanager.cpp + src/alcontextchanger.cpp + src/fontmgr.cpp) add_library(PPL STATIC ${SOURCES}) +target_compile_options(PPL PRIVATE -Wall -Wextra -Wfloat-equal -pedantic) target_compile_definitions(PPL PRIVATE ${DEFINITIONS}) From b252acb8b65ab0b485840b066d91a1d9f6b65ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20M=C3=BCnzel?= Date: Tue, 29 May 2018 13:54:38 -0400 Subject: [PATCH 09/22] build with CMake on Windows --- CMakeLists.txt | 13 ++++++++++--- src/texture.cpp | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cf20d8..ce45ae1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,12 @@ if(UNIX AND NOT APPLE) set(LINUX TRUE) endif() +if(UNIX) + set(PLATFORM_COMPILE_OPTIONS -Wall -Wextra -Wfloat-equal -pedantic) +else() + set(PLATFORM_COMPILE_OPTIONS /W4 /wd4996) +endif() + if(APPLE) set(CMAKE_OSX_DEPLOYMENT_TARGET 10.12) set(PLATFORM_CORE_DEFINITIONS @@ -37,8 +43,9 @@ else() WIN32_LEAN_AND_MEAN GLEW_STATIC=1) set(PLATFORM_INCLUDE_DIRECTORIES - include/glew/include, - include/openal-soft/include) + include/glew/include + include/openal-soft/include + include/freetype2/include) endif() endif() @@ -77,5 +84,5 @@ set(SOURCES add_library(PPL STATIC ${SOURCES}) -target_compile_options(PPL PRIVATE -Wall -Wextra -Wfloat-equal -pedantic) +target_compile_options(PPL PRIVATE ${PLATFORM_COMPILE_OPTIONS}) target_compile_definitions(PPL PRIVATE ${DEFINITIONS}) diff --git a/src/texture.cpp b/src/texture.cpp index ed6ac85..68ab8df 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -234,7 +234,7 @@ Texture::Texture(const std::string& file_name, bool build_mipmaps) #if APL glGenerateMipmap(GL_TEXTURE_2D); #else - gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, m_imagedata.Width, m_imagedata.Height, GL_ALPHA, GL_UNSIGNED_BYTE, &m_imagedata.pData[0])); + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, m_imagedata.Width, m_imagedata.Height, GL_ALPHA, GL_UNSIGNED_BYTE, &m_imagedata.pData[0]); #endif } } From 25f40351fb6dd83362f3ffc99976992665820462 Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Tue, 29 May 2018 16:19:36 -0400 Subject: [PATCH 10/22] added debug and release configurations to CMake --- CMakeLists.txt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ce45ae1..4366069 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,9 +2,15 @@ cmake_minimum_required (VERSION 3.0) project(PPL VERSION 2.0.0 DESCRIPTION "Plugin Patterns Library for X-Plane 11") set(CMAKE_CXX_STANDARD 14) -set(CMAKE_INCLUDE_CURRENT_DIR ON) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_VERBOSE_MAKEFILE ON) -set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_SOURCE_DIR}/lib) +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_SOURCE_DIR}/lib) + +if(NOT CMAKE_DEBUG_POSTFIX) + set(CMAKE_DEBUG_POSTFIX d) +endif() if(UNIX AND NOT APPLE) set(LINUX TRUE) @@ -46,6 +52,8 @@ else() include/glew/include include/openal-soft/include include/freetype2/include) + set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG} -Zi -MTd) + set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE} -MT) endif() endif() From e9988b1e3a2819bdc2e6fec291aedd8e597e15fc Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Tue, 29 May 2018 23:05:00 -0400 Subject: [PATCH 11/22] bring in XPLM 3.0.1 SDK as a git submodule --- .gitmodules | 4 ++++ include/SDK | 1 + 2 files changed, 5 insertions(+) create mode 160000 include/SDK diff --git a/.gitmodules b/.gitmodules index 442a597..a67376a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,7 @@ [submodule "include/simpleini"] path = include/simpleini url = https://github.com/brofield/simpleini.git +[submodule "include/SDK"] + path = include/SDK + url = https://github.com/PhilippMuenzel/SDK.git + diff --git a/include/SDK b/include/SDK new file mode 160000 index 0000000..bac3c36 --- /dev/null +++ b/include/SDK @@ -0,0 +1 @@ +Subproject commit bac3c362811f9ea0a77720ce05d147c2d00df7d3 From 5e8862af2cbb227f466c0b694f41c953abe8d6b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20M=C3=BCnzel?= Date: Tue, 29 May 2018 23:36:10 -0400 Subject: [PATCH 12/22] fixed debug and release settings for Visual Studio --- CMakeLists.txt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4366069..c0a654f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ endif() if(UNIX) set(PLATFORM_COMPILE_OPTIONS -Wall -Wextra -Wfloat-equal -pedantic) else() - set(PLATFORM_COMPILE_OPTIONS /W4 /wd4996) + set(PLATFORM_COMPILE_OPTIONS /W3 /wd4996) endif() if(APPLE) @@ -52,8 +52,8 @@ else() include/glew/include include/openal-soft/include include/freetype2/include) - set(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG} -Zi -MTd) - set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE} -MT) + set(PLATFORM_DEBUG_OPTIONS "-MTd") + set(PLATFORM_RELEASE_OPTIONS "-MT") endif() endif() @@ -92,5 +92,7 @@ set(SOURCES add_library(PPL STATIC ${SOURCES}) -target_compile_options(PPL PRIVATE ${PLATFORM_COMPILE_OPTIONS}) +target_compile_options(PPL PUBLIC ${PLATFORM_COMPILE_OPTIONS}) +target_compile_options(PPL PUBLIC "$<$:${PLATFORM_DEBUG_OPTIONS}>") +target_compile_options(PPL PUBLIC "$<$:${PLATFORM_RELEASE_OPTIONS}>") target_compile_definitions(PPL PRIVATE ${DEFINITIONS}) From 45d388b9eb799de87a9f8c507841d670e8576efe Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Tue, 21 Aug 2018 13:16:25 +0200 Subject: [PATCH 13/22] now that we pull in the SDK via submodule, we should actually use it... Duh! --- ppl.pro | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ppl.pro b/ppl.pro index e78c315..47c0926 100644 --- a/ppl.pro +++ b/ppl.pro @@ -9,8 +9,8 @@ CONFIG -= thread qt VERSION = 2.0.0 INCLUDEPATH += include/simpleini -INCLUDEPATH += ../SDK/CHeaders/XPLM -INCLUDEPATH += ../SDK/CHeaders/Widgets +INCLUDEPATH += include/SDK/CHeaders/XPLM +INCLUDEPATH += include/SDK/CHeaders/Widgets # Defined to use X-Plane SDK 2.0, 2.1, 3.0 and 3.01 capabilities - no backward compatibility before 11.20 DEFINES += XPLM200 XPLM210 XPLM300 XPLM301 From 37867c2d6231f541d219fefb27dcebf14f11e729 Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Mon, 17 Sep 2018 10:01:17 +0200 Subject: [PATCH 14/22] updated glew, openAL --- include/glew | 2 +- include/openal-soft | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/glew b/include/glew index f0067bb..3a8eff7 160000 --- a/include/glew +++ b/include/glew @@ -1 +1 @@ -Subproject commit f0067bb1151d36b37bd43948ce5a399844afb313 +Subproject commit 3a8eff77da3658c13fbd3634c943d5251d76322c diff --git a/include/openal-soft b/include/openal-soft index ce60760..96aacac 160000 --- a/include/openal-soft +++ b/include/openal-soft @@ -1 +1 @@ -Subproject commit ce6076091bac3c00cd10803916e8911495580bd0 +Subproject commit 96aacac10ca852fc30fd7f72f3e3c6ddbe02858c From 6155497d36b520832fd5fc164c7cc0d31222b30e Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Tue, 18 Sep 2018 11:49:48 +0200 Subject: [PATCH 15/22] updated simpleini --- include/simpleini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/simpleini b/include/simpleini index 2af65fc..fe082fa 160000 --- a/include/simpleini +++ b/include/simpleini @@ -1 +1 @@ -Subproject commit 2af65fcc504f8242752755e836709762ef7ce062 +Subproject commit fe082fa81f4a55ddceb55056622136be616b3c6f From cb85bbd83964d357705b72776ff4b53a9bcc066a Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Tue, 4 Dec 2018 16:06:13 -0500 Subject: [PATCH 16/22] compile warning free with clang 6. --- ppl.pro | 2 +- src/dataref.cpp | 14 ++--- src/dataref.h | 6 +- src/logichandler.cpp | 4 +- src/logichandler.h | 3 + src/menuitem.cpp | 2 +- src/messagewindow.cpp | 4 +- src/messagewindow.h | 2 +- src/onscreendisplay.cpp | 4 +- src/overlaygauge.cpp | 10 ++-- src/owneddata.cpp | 124 ++++++++++++++++++++-------------------- src/owneddata.h | 2 +- src/pluginpath.h | 6 +- src/texture.cpp | 4 +- src/vertexbuffer.cpp | 14 ++--- 15 files changed, 102 insertions(+), 99 deletions(-) diff --git a/ppl.pro b/ppl.pro index e78c315..f288d56 100644 --- a/ppl.pro +++ b/ppl.pro @@ -35,7 +35,7 @@ win32 { DEFINES += APL=0 IBM=1 LIN=0 #disable the deprecated warnings that make writing standards-compliant code impossible QMAKE_CXXFLAGS += -wd4996 - QMAKE_CXXFLAGS_DEBUG = -Zi -MTd + QMAKE_CXXFLAGS_DEBUG = -Zi -MTd QMAKE_CXXFLAGS_RELEASE = -MT DEFINES += _USE_MATH_DEFINES NOMINMAX WIN32_LEAN_AND_MEAN GLEW_STATIC=1 } diff --git a/src/dataref.cpp b/src/dataref.cpp index 57785d4..d38fb7e 100644 --- a/src/dataref.cpp +++ b/src/dataref.cpp @@ -179,7 +179,7 @@ DataRef::operator double() const template <> DataRef >::operator std::vector() const { - cache_.resize(XPLMGetDatavf(m_data_ref, NULL, 0, 0)); + cache_.resize(XPLMGetDatavf(m_data_ref, nullptr, 0, 0)); XPLMGetDatavf(m_data_ref, &cache_[0], 0, cache_.size()); return cache_; } @@ -187,7 +187,7 @@ DataRef >::operator std::vector() const template <> DataRef >::operator std::vector() const { - cache_.resize(XPLMGetDatavi(m_data_ref, NULL, 0, 0)); + cache_.resize(XPLMGetDatavi(m_data_ref, nullptr, 0, 0)); XPLMGetDatavi(m_data_ref, &cache_[0], 0, cache_.size()); return cache_; } @@ -195,7 +195,7 @@ DataRef >::operator std::vector() const template <> DataRef::operator std::string() const { - cache_.resize(XPLMGetDatab(m_data_ref, NULL, 0, 0)); + cache_.resize(XPLMGetDatab(m_data_ref, nullptr, 0, 0)); XPLMGetDatab(m_data_ref, &cache_[0], 0, cache_.size()); return std::string(cache_.data(), strnlen(cache_.data(), cache_.size())); } @@ -273,7 +273,7 @@ dataref_trait >::BasicType DataRef >::operator template<> dataref_trait::BasicType DataRef::operator[](std::size_t index) const { - cache_.resize(XPLMGetDatab(m_data_ref, NULL, 0, 0)); // can't use convert to std::string method here, because we might want the raw data with embedded null bytes. + cache_.resize(XPLMGetDatab(m_data_ref, nullptr, 0, 0)); // can't use convert to std::string method here, because we might want the raw data with embedded null bytes. XPLMGetDatab(m_data_ref, &cache_[0], 0, cache_.size()); return cache_[index]; } @@ -419,7 +419,7 @@ void DataRef::reserve(std::size_t i) template<> void DataRef >::reserve() { - std::size_t i = XPLMGetDatavi(m_data_ref, NULL, 0, 0); + std::size_t i = XPLMGetDatavi(m_data_ref, nullptr, 0, 0); cache_.resize(i); m_history.resize(i); } @@ -427,14 +427,14 @@ void DataRef >::reserve() template<> void DataRef >::reserve() { - std::size_t i = XPLMGetDatavf(m_data_ref, NULL, 0, 0); + std::size_t i = XPLMGetDatavf(m_data_ref, nullptr, 0, 0); cache_.resize(i); m_history.resize(i); } template<> void DataRef::reserve() { - std::size_t i = XPLMGetDatab(m_data_ref, NULL, 0, 0); + std::size_t i = XPLMGetDatab(m_data_ref, nullptr, 0, 0); cache_.resize(i); m_history.resize(i); } diff --git a/src/dataref.h b/src/dataref.h index 69c8974..44b6308 100644 --- a/src/dataref.h +++ b/src/dataref.h @@ -279,14 +279,14 @@ DataRef::DataRef(const std::string& identifier, RWType writeability, bool share, bool publish_in_dre): - m_data_ref(0), + m_data_ref(nullptr), m_read_write(writeability), shared_(false), identifier_(identifier) { try { - if (share && XPLMFindDataRef(identifier.c_str()) == 0) + if (share && XPLMFindDataRef(identifier.c_str()) == nullptr) shareDataRef(identifier, publish_in_dre); lookUp(identifier); checkDataType(); @@ -435,7 +435,7 @@ void DataRef::publishInDRE() { XPLMPluginID PluginID = XPLMFindPluginBySignature(DRE_PLUGIN_SINATURE); if (PluginID != XPLM_NO_PLUGIN_ID) - XPLMSendMessageToPlugin(PluginID, DRE_MSG_ADD_DATAREF, (void*)identifier_.c_str()); + XPLMSendMessageToPlugin(PluginID, DRE_MSG_ADD_DATAREF, static_cast(const_cast(identifier_.c_str()))); } diff --git a/src/logichandler.cpp b/src/logichandler.cpp index aa90572..73085a7 100644 --- a/src/logichandler.cpp +++ b/src/logichandler.cpp @@ -34,7 +34,7 @@ using namespace PPL; -float HandlerCallbackInit(float, float, int, void* inRefCon) +float PPL::HandlerCallbackInit(float, float, int, void* inRefCon) { LogicHandler* handler = static_cast(inRefCon); handler->initializeAtStart(); @@ -44,7 +44,7 @@ float HandlerCallbackInit(float, float, int, void* inRefCon) /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// -float HandlerCallbackProcess(float, float, int, void* inRefCon) +float PPL::HandlerCallbackProcess(float, float, int, void* inRefCon) { LogicHandler* handler = static_cast(inRefCon); if (!handler->isSuspended()) diff --git a/src/logichandler.h b/src/logichandler.h index 3e45964..ff9cfd7 100644 --- a/src/logichandler.h +++ b/src/logichandler.h @@ -124,6 +124,9 @@ class LogicHandler { }; +float HandlerCallbackInit(float, float, int, void* inRefCon); +float HandlerCallbackProcess(float, float, int, void* inRefCon); + } #endif diff --git a/src/menuitem.cpp b/src/menuitem.cpp index 62d7357..027ce2a 100644 --- a/src/menuitem.cpp +++ b/src/menuitem.cpp @@ -33,7 +33,7 @@ using namespace PPL; MenuItem::MenuItem(const std::string& title) { - m_item_id = XPLMAppendMenuItem(XPLMFindPluginsMenu(), title.c_str(), NULL, 1); + m_item_id = XPLMAppendMenuItem(XPLMFindPluginsMenu(), title.c_str(), nullptr, 1); m_menu_id = XPLMCreateMenu(title.c_str(), XPLMFindPluginsMenu(), m_item_id, menuHandler, this); } diff --git a/src/messagewindow.cpp b/src/messagewindow.cpp index 6176c98..41bcad8 100644 --- a/src/messagewindow.cpp +++ b/src/messagewindow.cpp @@ -201,7 +201,7 @@ int MessageWindow::processMessages(XPWidgetMessage inMessage, intptr_t, intptr_t int MessageWindow::widgetCallback(XPWidgetMessage inMessage, XPWidgetID inWidget, intptr_t param1, intptr_t param2) { - MessageWindow* widget = reinterpret_cast(XPGetWidgetProperty(inWidget, xpProperty_Object, NULL)); + MessageWindow* widget = reinterpret_cast(XPGetWidgetProperty(inWidget, xpProperty_Object, nullptr)); if (widget) { return widget->processMessages(inMessage, param1, param2); @@ -250,7 +250,7 @@ void MessageWindow::createSurroundingBox() 1, m_title.c_str(), 1, - 0, + nullptr, xpWidgetClass_MainWindow); XPSetWidgetProperty(m_box_widget, xpProperty_MainWindowHasCloseBoxes, 1); XPSetWidgetProperty(m_box_widget, xpProperty_MainWindowType, xpMainWindowStyle_MainWindow); diff --git a/src/messagewindow.h b/src/messagewindow.h index d9b1a4a..8fd1df0 100644 --- a/src/messagewindow.h +++ b/src/messagewindow.h @@ -122,7 +122,7 @@ class MessageWindow * @param param1 * @param param2 */ - int processMessages(XPWidgetMessage, intptr_t, intptr_t); + int processMessages(XPWidgetMessage message, intptr_t param1, intptr_t param2); /** * static widget callback to register in X-Plane's widget logic diff --git a/src/onscreendisplay.cpp b/src/onscreendisplay.cpp index 5113ebb..7d33762 100644 --- a/src/onscreendisplay.cpp +++ b/src/onscreendisplay.cpp @@ -50,7 +50,7 @@ OnScreenDisplay::OnScreenDisplay(int width, int height, const std::string& title 1, title_.c_str(), 1, - 0, + nullptr, xpWidgetClass_MainWindow); XPSetWidgetProperty(widget_id_, xpProperty_MainWindowType, xpMainWindowStyle_Translucent); XPSetWidgetProperty(widget_id_, xpProperty_Object, reinterpret_cast(this)); @@ -89,7 +89,7 @@ int OnScreenDisplay::processMessages(XPWidgetMessage inMessage, intptr_t, intptr int OnScreenDisplay::widgetCallback(XPWidgetMessage inMessage, XPWidgetID inWidget, intptr_t param1, intptr_t param2) { - OnScreenDisplay* display = reinterpret_cast(XPGetWidgetProperty(inWidget, xpProperty_Object, 0)); + OnScreenDisplay* display = reinterpret_cast(XPGetWidgetProperty(inWidget, xpProperty_Object, nullptr)); if (display) { return display->processMessages(inMessage, param1, param2); diff --git a/src/overlaygauge.cpp b/src/overlaygauge.cpp index 7c8d06e..24c74ab 100644 --- a/src/overlaygauge.cpp +++ b/src/overlaygauge.cpp @@ -103,7 +103,7 @@ OverlayGauge::OverlayGauge(int left2d, int top2d, int width2d, int height2d, int generateTex((int*)(&gauge_texture_), 1); bindTex(gauge_texture_, 0); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_3d_, height_3d_, 0, - GL_RGBA, GL_UNSIGNED_BYTE, 0); + GL_RGBA, GL_UNSIGNED_BYTE, nullptr); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -179,7 +179,7 @@ void OverlayGauge::setVisible(bool b) { if (!b) { - XPLMTakeKeyboardFocus(0); + XPLMTakeKeyboardFocus(nullptr); window_has_keyboard_focus_ = false; } visible_2d_ = b; @@ -250,7 +250,7 @@ void OverlayGauge::toggleKeyboardFocus() { if (window_has_keyboard_focus_) { - XPLMTakeKeyboardFocus(0); + XPLMTakeKeyboardFocus(nullptr); window_has_keyboard_focus_ = false; } else @@ -324,7 +324,7 @@ void OverlayGauge::draw2dWindowCallback(XPLMWindowID) { static float color[] = { 1.f, 0.5f, 0.f}; static char str[] = "K"; - XPLMDrawString(color, left + 20, top - 25, str, 0, xplmFont_Proportional); + XPLMDrawString(color, left + 20, top - 25, str, nullptr, xplmFont_Proportional); } } } @@ -413,7 +413,7 @@ int OverlayGauge::handle2dClickCallback(XPLMWindowID window_id, int x, int y, XP { if (coordInRect(x, y, Left, Top, Left+40, Top-40)) { - XPLMTakeKeyboardFocus(0); + XPLMTakeKeyboardFocus(nullptr); window_has_keyboard_focus_ = false; setVisible(false); } diff --git a/src/owneddata.cpp b/src/owneddata.cpp index 65d41e5..bac0d33 100644 --- a/src/owneddata.cpp +++ b/src/owneddata.cpp @@ -36,10 +36,10 @@ template <> void OwnedData::registerRead() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Int, 0, - readFunc, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, this, NULL ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + readFunc, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, this, nullptr); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -49,10 +49,10 @@ template <> void OwnedData::registerRead() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Float, 0, - NULL, NULL, readFunc, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, this, NULL ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, readFunc, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, this, nullptr); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -62,10 +62,10 @@ template <> void OwnedData::registerRead() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Double, 0, - NULL, NULL, NULL, NULL, readFunc, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, this, NULL); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, nullptr, nullptr, readFunc, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, this, nullptr); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -75,10 +75,10 @@ template <> void OwnedData::registerRead() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Data, 0, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, readFuncStr, NULL, this, NULL ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, readFuncStr, nullptr, this, nullptr); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -88,10 +88,10 @@ template <> void OwnedData >::registerRead() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_FloatArray, 0, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - readFuncVF, NULL, NULL, NULL, this, NULL ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + readFuncVF, nullptr, nullptr, nullptr, this, nullptr); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -102,10 +102,10 @@ template <> void OwnedData::registerWrite() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Int, 1, - NULL, writeFunc, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, this ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, writeFunc, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, this); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -115,10 +115,10 @@ template <> void OwnedData::registerWrite() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Float, 1, - NULL, NULL, NULL, writeFunc, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, this ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, nullptr, writeFunc, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, this); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -128,10 +128,10 @@ template <> void OwnedData::registerWrite() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Double, 1, - NULL, NULL, NULL, NULL, NULL, writeFunc, NULL, NULL, - NULL, NULL, NULL, NULL, NULL, this ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, nullptr, nullptr, nullptr, writeFunc, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, nullptr, this); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -141,10 +141,10 @@ template <> void OwnedData::registerWrite() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Data, 1, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, writeFuncStr, NULL, this ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, writeFuncStr, nullptr, this); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -154,10 +154,10 @@ template <> void OwnedData >::registerWrite() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_FloatArray, 1, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, writeFuncVF, NULL, NULL, NULL, this ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, writeFuncVF, nullptr, nullptr, nullptr, this); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -168,10 +168,10 @@ template <> void OwnedData::registerReadWrite() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Int, 1, - readFunc, writeFunc, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, this, this ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + readFunc, writeFunc, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, this, this); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -181,10 +181,10 @@ template <> void OwnedData::registerReadWrite() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Float, 1, - NULL, NULL, readFunc, writeFunc, NULL, NULL, NULL, NULL, - NULL, NULL, NULL, NULL, this, this ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, readFunc, writeFunc, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, this, this); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -194,10 +194,10 @@ template <> void OwnedData::registerReadWrite() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Double, 1, - NULL, NULL, NULL, NULL, readFunc, writeFunc, NULL, NULL, - NULL, NULL, NULL, NULL, this, this ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, nullptr, nullptr, readFunc, writeFunc, nullptr, nullptr, + nullptr, nullptr, nullptr, nullptr, this, this); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -207,10 +207,10 @@ template <> void OwnedData::registerReadWrite() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_Data, 1, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - NULL, NULL, readFuncStr, writeFuncStr, this, this ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + nullptr, nullptr, readFuncStr, writeFuncStr, this, this); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -220,10 +220,10 @@ template <> void OwnedData >::registerReadWrite() { m_data_ref = XPLMRegisterDataAccessor( m_data_ref_identifier.c_str(), xplmType_FloatArray, 1, - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, - readFuncVF, writeFuncVF, NULL, NULL, this, this ); - if (m_data_ref == 0) - throw DataRefNotPublishedException(""); + nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, + readFuncVF, writeFuncVF, nullptr, nullptr, this, this); + if (m_data_ref == nullptr) + throw DataRefNotPublishedException(m_data_ref_identifier); } /////////////////////////////////////////////////////////////////////////////// @@ -234,7 +234,7 @@ int PPL::readFuncStr(void* inRefCon, void* outValue, int inOffset, int inMaxLeng { OwnedData* p_owned_data = static_cast*>(inRefCon); std::size_t length = p_owned_data->value().length(); - if (outValue == NULL) + if (outValue == nullptr) return length; std::size_t maxlen = std::min(std::size_t(inMaxLength), length); strncpy(static_cast(outValue), p_owned_data->value().substr(inOffset, maxlen).c_str(), maxlen); @@ -258,7 +258,7 @@ int PPL::readFuncVF(void* inRefCon, float* outValues, int inOffset, int inMaxLen { std::size_t max_length = static_cast(inMaxLength); OwnedData >* p_owned_data = static_cast >*>(inRefCon); - if (outValues == NULL) + if (outValues == nullptr) return p_owned_data->value().size(); int end = (p_owned_data->value().size() < max_length) ? p_owned_data->value().size() : max_length; memcpy(outValues, &p_owned_data->value()[inOffset], sizeof(float)*end); diff --git a/src/owneddata.h b/src/owneddata.h index 80ce849..5483ffa 100644 --- a/src/owneddata.h +++ b/src/owneddata.h @@ -84,7 +84,7 @@ class OwnedData{ bool publish_in_dre = false, DataCallback_f callback = 0): m_data_ref_identifier(identifier), - m_data_ref(0), + m_data_ref(nullptr), m_value(T()), m_callback(callback) { diff --git a/src/pluginpath.h b/src/pluginpath.h index a53ddac..f9ed96f 100644 --- a/src/pluginpath.h +++ b/src/pluginpath.h @@ -60,7 +60,7 @@ class PluginPath * @return the absolute path * @exception PathSetupError is thrown if path conversion fails */ - static std::string prependPluginPath(const std::string&); + static std::string prependPluginPath(const std::string& path); /** * prepend the absolute path to the Resources subdirectory of the @@ -69,7 +69,7 @@ class PluginPath * @return the absolute path * @exception PathSetupError is thrown if path conversion fails */ - static std::string prependPluginResourcesPath(const std::string&); + static std::string prependPluginResourcesPath(const std::string& path); /** * prepend the absolute path to the directory of the currently loaded @@ -78,7 +78,7 @@ class PluginPath * @return the absolute path * @exception PathSetupError is thrown if path conversion fails */ - static std::string prependPlanePath(const std::string&); + static std::string prependPlanePath(const std::string& path); /** * set the name of the directory where fat plugin resides diff --git a/src/texture.cpp b/src/texture.cpp index 68ab8df..39d2291 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -164,12 +164,12 @@ Texture::Texture(const std::string& file_name, bool build_mipmaps) FILE *file = fopen(file_name.c_str(), "rb"); // Open The TGA File - if( file==NULL || // Does File Even Exist? + if( file==nullptr || // Does File Even Exist? fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || // Are There 12 Bytes To Read? memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want? fread(header,1,sizeof(header),file)!=sizeof(header)) // If So Read Next 6 Header Bytes { - if (file == NULL) // Did The File Even Exist? *Added Jim Strong* + if (file == nullptr) // Did The File Even Exist? *Added Jim Strong* throw std::runtime_error("File could not be opened: "+file_name); else // Otherwise { diff --git a/src/vertexbuffer.cpp b/src/vertexbuffer.cpp index a544eaa..7d89ba1 100644 --- a/src/vertexbuffer.cpp +++ b/src/vertexbuffer.cpp @@ -56,7 +56,7 @@ std::size_t VertexBuffer::beginSpecifyVerts(std::size_t num_vertices, volatile f case STATIC: usage = GL_STATIC_DRAW; break; case STREAMING: usage = GL_STREAM_DRAW; break; } - glBufferData(GL_ARRAY_BUFFER, num_vertices * stride_floats_ * sizeof(GLfloat), NULL, usage); + glBufferData(GL_ARRAY_BUFFER, num_vertices * stride_floats_ * sizeof(GLfloat), nullptr, usage); GLfloat* bp = static_cast(glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY)); *out_verts = bp; @@ -66,7 +66,7 @@ std::size_t VertexBuffer::beginSpecifyVerts(std::size_t num_vertices, volatile f if(num_nrm_) *out_normals = bp; else - *out_normals = NULL; + *out_normals = nullptr; } bp += num_nrm_; if(out_texes) @@ -74,7 +74,7 @@ std::size_t VertexBuffer::beginSpecifyVerts(std::size_t num_vertices, volatile f if(num_tx_) *out_texes = bp; else - *out_texes = NULL; + *out_texes = nullptr; } bp += num_tx_; if(out_texes2) @@ -82,7 +82,7 @@ std::size_t VertexBuffer::beginSpecifyVerts(std::size_t num_vertices, volatile f if(num_tx2_) *out_texes2 = bp; else - *out_texes2 = NULL; + *out_texes2 = nullptr; } bp += num_tx2_; if(out_colors) @@ -90,7 +90,7 @@ std::size_t VertexBuffer::beginSpecifyVerts(std::size_t num_vertices, volatile f if(num_col_) *out_colors = bp; else - *out_colors = NULL; + *out_colors = nullptr; } return stride_floats_; } @@ -111,9 +111,9 @@ void VertexBuffer::setupForDraw() glEnableClientState(GL_TEXTURE_COORD_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, vbo_); - float* p = NULL; + float* p = nullptr; - int stride = stride_floats_*sizeof(GLfloat); + std::size_t stride = stride_floats_*sizeof(GLfloat); glVertexPointer(num_vrt_, GL_FLOAT, stride, p); p += num_vrt_; From 47f758019bc769293e74b67476fd3ea7cbc93830 Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Tue, 4 Dec 2018 16:16:10 -0500 Subject: [PATCH 17/22] git-ignore compilation products --- .gitignore | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 8887ed6..9b53814 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ -.directory -Makefile -objects/ -ppl.pro.user +.directory +Makefile +objects/ +ppl.pro.user +lib/libppl.a +lib/libppld.a From 0a9d129cb95cbaef5e964b4119350ca7b7e7fe0d Mon Sep 17 00:00:00 2001 From: Philipp Ringler CFI Date: Mon, 24 Oct 2022 22:11:37 -0400 Subject: [PATCH 18/22] added dataref names to lookup exceptions for writability, the lookup exception now carries the dataref name for the exception's what() --- src/dataref.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dataref.h b/src/dataref.h index 44b6308..ace6b4b 100644 --- a/src/dataref.h +++ b/src/dataref.h @@ -459,7 +459,7 @@ void DataRef::checkWriteabilityIsValid() { if(m_read_write == WriteOnly || m_read_write == ReadWrite) if (!XPLMCanWriteDataRef(m_data_ref)) - throw NotWriteableException("Declared to be writeable, but X-Plane says it is read-only."); + throw NotWriteableException(identifier_+ " declared to be writeable, but X-Plane says it is read-only."); } /////////////////////////////////////////////////////////////////////////////// @@ -468,7 +468,7 @@ void DataRef::checkWriteabilityIsValid() template void DataRef::checkDataType() { - throw IncompatibleTypeException("No type defined."); + throw IncompatibleTypeException(identifier_+" no type defined."); } /////////////////////////////////////////////////////////////////////////////// From 1d4e5dfbcdf218ba5ec5ac7f0c570535195fae9d Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Fri, 27 Oct 2023 16:27:20 -0400 Subject: [PATCH 19/22] updated to XPLM 4.0.1 --- include/SDK | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/SDK b/include/SDK index bac3c36..7329d3b 160000 --- a/include/SDK +++ b/include/SDK @@ -1 +1 @@ -Subproject commit bac3c362811f9ea0a77720ce05d147c2d00df7d3 +Subproject commit 7329d3b3c9211192d7f231aa40300a9a592a7fc2 From 676654c550e536577be1d18acbde2b50466fde9b Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Fri, 27 Oct 2023 16:27:44 -0400 Subject: [PATCH 20/22] updated to Freetype 2.13.2 --- include/freetype2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/freetype2 b/include/freetype2 index f9b1871..920c550 160000 --- a/include/freetype2 +++ b/include/freetype2 @@ -1 +1 @@ -Subproject commit f9b1871ded4df726ed962e5eae535c9918705a3d +Subproject commit 920c5502cc3ddda88f6c7d85ee834ac611bb11cc From 793d15388973769907619b3c1346bd42e8ad6f02 Mon Sep 17 00:00:00 2001 From: Philipp Ringler Date: Fri, 27 Oct 2023 16:28:31 -0400 Subject: [PATCH 21/22] updared CMake for arm/x86 build for X-Plane 12 so you can make native plugins for apple silicon --- CMakeLists.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c0a654f..3d3b66a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required (VERSION 3.0) -project(PPL VERSION 2.0.0 DESCRIPTION "Plugin Patterns Library for X-Plane 11") +project(PPL VERSION 2.0.0 DESCRIPTION "Plugin Patterns Library for X-Plane 11/12") -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_VERBOSE_MAKEFILE ON) @@ -23,13 +23,14 @@ else() endif() if(APPLE) - set(CMAKE_OSX_DEPLOYMENT_TARGET 10.12) + set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15) + set(CMAKE_OSX_ARCHITECTURES $(ARCHS_STANDARD)) set(PLATFORM_CORE_DEFINITIONS APL=1 LIN=0 IBM=0) set(PLATFORM_INCLUDE_DIRECTORIES - /usr/X11/include/freetype2) + include/freetype2/include) else() if(LINUX) set(PLATFORM_CORE_DEFINITIONS From b22d61b4b03be6fed7e8c3d6c0ec3361ae8626d7 Mon Sep 17 00:00:00 2001 From: Julia DeMille Date: Thu, 9 Nov 2023 13:36:13 -0600 Subject: [PATCH 22/22] Commands API updated and robustified. Added the commands files to CMake, made the API more modern (in my personal opinion), and added more error checking to avoid UB. Signed-off-by: Julia DeMille --- CMakeLists.txt | 9 ++- src/command.cpp | 167 +++++++++++++++++++++++++++++------------ src/command.h | 192 ++++++++++++++++++++++++++---------------------- 3 files changed, 228 insertions(+), 140 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d3b66a..728ffda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required (VERSION 3.0) -project(PPL VERSION 2.0.0 DESCRIPTION "Plugin Patterns Library for X-Plane 11/12") +project(PPL VERSION 2.1.0 DESCRIPTION "Plugin Patterns Library for X-Plane 11/12") -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_VERBOSE_MAKEFILE ON) @@ -65,8 +65,8 @@ set(DEFINITIONS XPLM301=1 ${PLATFORM_CORE_DEFINITIONS}) -include_directories(${CMAKE_SOURCE_DIR}/../SDK/CHeaders/XPLM - ${CMAKE_SOURCE_DIR}/../SDK/CHeaders/Widgets +include_directories(${CMAKE_SOURCE_DIR}/include/SDK/CHeaders/XPLM + ${CMAKE_SOURCE_DIR}/include/SDK/CHeaders/Widgets ${CMAKE_SOURCE_DIR}/include/simpleini ${PLATFORM_INCLUDE_DIRECTORIES}) @@ -86,6 +86,7 @@ set(SOURCES src/menuitem.cpp src/processor.cpp src/vertexbuffer.cpp + src/command.cpp src/alsoundbuffer.cpp src/alcontextmanager.cpp src/alcontextchanger.cpp diff --git a/src/command.cpp b/src/command.cpp index 527d2dd..8b03476 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -1,78 +1,151 @@ -#include "command.h" - -/* Copyright (c) 2018, Jack Deeth github@jackdeeth.org.uk +// Copyright (c) 2023, Jack Deeth , Julia DeMille // All rights reserved // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, -this -// list of conditions and the following disclaimer. +// this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE -FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // The views and conclusions contained in the software and documentation are -those -// of the authors and should not be interpreted as representing official -policies, -// either expressed or implied, of the FreeBSD Project. -*/ +// those of the authors and should not be interpreted as representing official +// policies, either expressed or implied, of the FreeBSD Project. + +#include "command.h" +#include "XPLMDataAccess.h" +#include "XPLMUtilities.h" +#include "log.h" +#include + +using namespace std::string_literals; -PPL::Command::Command(XPLMCommandRef ref, - std::function cb, - bool run_before_sim) - : callback(cb), ref_(ref), before_(run_before_sim ? 1 : 0) { - XPLMRegisterCommandHandler(ref_, scb, before_, this); +namespace PPL { + +Command::Command(XPLMDataRef cmd_ref) + : cmd_ref(cmd_ref) +{ } -PPL::Command::Command(std::string cmd_to_replace, - std::function callback, - bool run_before) - : Command(XPLMFindCommand(cmd_to_replace.c_str()), callback, run_before) {} +std::optional Command::find(std::string& name) +{ + XPLMCommandRef cmd_ref = XPLMFindCommand(name.c_str()); + if (cmd_ref == nullptr) { + return std::nullopt; + } else { + return Command(cmd_ref); + } +} -PPL::Command::Command(std::string new_cmd, - std::string description, - std::function callback, - bool run_before) - : Command(XPLMCreateCommand(new_cmd.c_str(), description.c_str()), - callback, - run_before) {} +Command Command::create(std::string& name, std::string& description) +{ + if (Command::find(name)) { + throw CommandAlreadyExists(name); + } + XPLMCommandRef cmd_ref = XPLMCreateCommand(name.c_str(), description.c_str()); + return Command(cmd_ref); +} -PPL::Command::~Command() { - XPLMUnregisterCommandHandler(ref_, scb, before_, this); +CommandAlreadyExists::CommandAlreadyExists(const std::string& command) + : std::runtime_error("The command `"s + command + "` already exists. It cannot be created."s) +{ } -PPL::Command::Phase PPL::Command::phase() const { return phase_; } +template + requires impl_CommandHandler +RegisteredCommandHandler Command::handle(bool before_xp, T handler) +{ + RegisteredCommandHandler(cmd_ref, before_xp, handler); +} -void PPL::Command::begin() { XPLMCommandBegin(ref_); } +void Command::trigger_once() +{ + XPLMCommandOnce(cmd_ref); +} -void PPL::Command::end() { XPLMCommandEnd(ref_); } +CommandHold Command::hold_down() +{ + return CommandHold(*this); +} -void PPL::Command::once() { XPLMCommandOnce(ref_); } +CommandHold::CommandHold(Command& cmd) + : cmd(cmd) +{ + XPLMCommandBegin(cmd.cmd_ref); +} -int PPL::Command::scb(XPLMCommandRef ref, XPLMCommandPhase phase, void *vp) { - // scb = shared/static call-back - // converts C conventions with (post?)modern C++ - auto cmd = reinterpret_cast(vp); - if (phase == xplm_CommandBegin) cmd->phase_ = Phase::Begin; - if (phase == xplm_CommandContinue) cmd->phase_ = Phase::Continue; - if (phase == xplm_CommandEnd) cmd->phase_ = Phase::End; - auto outcome = cmd->callback(ref, cmd->phase_); - return outcome == Outcome::Halt ? 0 : 1; +CommandHold::~CommandHold() +{ + XPLMCommandEnd(cmd.cmd_ref); +} + +template + requires impl_CommandHandler +RegisteredCommandHandler::RegisteredCommandHandler(XPLMCommandRef cmd_ref, bool before_xp, T handler_) + : cmd_ref(cmd_ref) + , before_xp(before_xp) + , handler(handler_) +{ + XPLMRegisterCommandHandler( + cmd_ref, + RegisteredCommandHandler::handle, + before_xp, + &handler); +} + +template + requires impl_CommandHandler +int RegisteredCommandHandler::handle(XPLMCommandRef _ref, XPLMCommandPhase phase, void* refcon) +{ + T* handler = static_cast(refcon); + CommandOutcome outcome; + switch (phase) { + case xplm_CommandBegin: + outcome = handler->command_begin(); + break; + case xplm_CommandContinue: + outcome = handler->command_continue(); + break; + case xplm_CommandEnd: + outcome = handler->command_end(); + break; + default: + outcome = CommandOutcome::Continue; + Log() << Log::Error << "XPLM has called a command handler with an invalid phase!" << Log::endl; + break; + } + switch (outcome) { + case CommandOutcome::Irrelevant: + case CommandOutcome::Continue: + return 1; + break; + case CommandOutcome::Halt: + return 0; + break; + } +} + +template + requires impl_CommandHandler +RegisteredCommandHandler::~RegisteredCommandHandler() +{ + XPLMUnregisterCommandHandler( + cmd_ref, + RegisteredCommandHandler::handle, + before_xp, + &handler); +} } diff --git a/src/command.h b/src/command.h index c0b0b23..54e425f 100644 --- a/src/command.h +++ b/src/command.h @@ -1,132 +1,146 @@ -#pragma once - -/* Copyright (c) 2018, Jack Deeth github@jackdeeth.org.uk +// Copyright (c) 2023, Jack Deeth , Julia DeMille // All rights reserved // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, -this -// list of conditions and the following disclaimer. +// this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE -FOR -// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // The views and conclusions contained in the software and documentation are -those -// of the authors and should not be interpreted as representing official -policies, -// either expressed or implied, of the FreeBSD Project. -*/ - -/* Example usage: - -// disable an existing command -PPL::Command left_brake_cmd_{"sim/flight_controls/left_brake"}; - -// hijack an existing command -const auto End{PPL::Command::Phase::End}; -const auto Halt{PPL::Command::Outcome::Halt}; -PPL::Command brake_toggle_cmd_{ - "sim/flight_controls/brakes_toggle_regular", - [&](XPLMCommandRef, PPL::Command::Phase phase) { - if (phase == End) { - if (handbrake_lever_ratio_ < 0.5f) - handbrake_lever_ratio_ = 0.5f; - else - handbrake_lever_ratio_ = 0.f; - } - return Halt; - }}; - -// create a new command -class CompileMessage { -public: - CompileMessage(std::string command_name = -"YourName/YourProject/show_compile_msg", - std::string desc = "Announces compilation date/time of plugin" - : cmd_{command_name, desc, [](XPLMCommandRef, PPL::Command::Phase phase) { - if (phase == PPL::Command::Phase::Begin) - XPLMSpeakString(compile_message().c_str()); - return PPL::Command::Outcome::Halt; - }} {} - - static std::string compile_message() { - std::stringstream msg; - msg << "Compiled " << __DATE__ << " at " << __TIME__; - return msg.str(); - } -private: - PPL::Command cmd_; -}; +// those of the authors and should not be interpreted as representing official +// policies, either expressed or implied, of the FreeBSD Project. -*/ +#ifndef PPL_COMMAND_H_ +#define PPL_COMMAND_H_ #include -#include +#include +#include +#include +#include #include // RAII wrapper for X-Plane Commands API namespace PPL { +class CommandHandler; + +template +concept impl_CommandHandler = std::derived_from && (!std::same_as); + +template + requires impl_CommandHandler +class RegisteredCommandHandler; + +class CommandHold; + class Command { + friend CommandHold; + public: - enum class Outcome { Continue, Halt }; - enum class Phase { Begin, Continue, End }; + static std::optional find(std::string& name); + static Command create(std::string& name, std::string& description); + template + requires impl_CommandHandler + RegisteredCommandHandler handle(bool before_xp, T handler); + void trigger_once(); + CommandHold hold_down(); - // Finds a command by reference and attaches a new callback to it - Command(XPLMCommandRef ref, - std::function callback = - [](...) { return Outcome::Halt; }, - bool run_before = true); +private: + Command(XPLMCommandRef cmd_ref); + XPLMCommandRef cmd_ref; +}; - // Finds a command by name and attaches a new callback to it - // (useful for hijacking and/or blocking existing commands) - Command(std::string cmd_to_replace, - std::function callback = - [](...) { return Outcome::Halt; }, - bool run_before = true); +class CommandAlreadyExists : public std::runtime_error { + friend Command; - // Creates a new command and attaches a callback to it - Command(std::string new_cmd, - std::string description, - std::function callback = - [](...) { return Outcome::Halt; }, - bool run_before = true); +private: + CommandAlreadyExists(const std::string& command); +}; - ~Command(); +class CommandHold { + friend Command; - Phase phase() const; +public: + ~CommandHold(); + +private: + CommandHold(Command& cmd); + Command& cmd; +}; + +enum class CommandOutcome { + /** + * @brief Allow X-Plane to handle the command. + */ + Continue, + /** + * @brief Prevent X-Plane from handling the command. + */ + Halt, + /** + * @brief Return this if handling the command after X-Plane. + */ + Irrelevant +}; - void begin(); - void end(); - void once(); +enum class CommandPhase { + /** + * @brief The command has begun. + */ + Begin, + /** + * @brief Periodic events with this phase are sent when the command is held. + */ + Continue, + /** + * @brief The command has been released. + */ + End +}; + +class CommandHandler { +public: + CommandHandler(); + virtual ~CommandHandler() = 0; + virtual CommandOutcome command_begin() = 0; + virtual CommandOutcome command_continue() = 0; + virtual CommandOutcome command_end() = 0; +}; - std::function callback; +template + requires impl_CommandHandler +class RegisteredCommandHandler { + friend Command; - static int scb(XPLMCommandRef ref, XPLMCommandPhase phase, void *vp); +public: + ~RegisteredCommandHandler(); private: - XPLMCommandRef ref_; - int before_; - Phase phase_{Phase::End}; + RegisteredCommandHandler(XPLMCommandRef cmd_ref, bool before_xp, T handler); + XPLMCommandRef cmd_ref; + bool before_xp; + T handler; + static int handle(XPLMCommandRef ref, XPLMCommandPhase phase, void* refcon); }; -} // namespace +} // namespace + +#endif // PPL_COMMAND_H_