From 094e70ee00e3e6460594d16a99f494ba7d68c4ca Mon Sep 17 00:00:00 2001 From: jonathan hoffstadt Date: Wed, 5 Aug 2026 07:18:35 -0500 Subject: [PATCH] wip --- extensions/pl_dearimgui_ext_m.cpp | 31 +- sandbox/app.py | 369 +++++++----- scripts/gen_build.py | 94 +-- src/pilotlight.c | 1 - src/pl_main.cpp | 970 +----------------------------- 5 files changed, 239 insertions(+), 1226 deletions(-) diff --git a/extensions/pl_dearimgui_ext_m.cpp b/extensions/pl_dearimgui_ext_m.cpp index c22ca73..01bdfd9 100644 --- a/extensions/pl_dearimgui_ext_m.cpp +++ b/extensions/pl_dearimgui_ext_m.cpp @@ -110,7 +110,7 @@ dear_imgui_initialize(PyObject* self, PyObject* args, PyObject* kwargs) } Py_DECREF(ptPythonAttachmentDepth); - pl_dear_imgui_initialize(ptDevice, ptSwapchain, &tInfo); + pl_dear_imgui_initialize(nullptr); // ImPlot::SetCurrentContext((ImPlotContext*)ptDataRegistry->get_data("implot")); ImGuiIO& tImGuiIO = ImGui::GetIO(); @@ -141,30 +141,6 @@ dear_imgui_new_frame(PyObject* self, PyObject* args, PyObject* kwargs) Py_RETURN_NONE; } -PyObject* -dear_imgui_get_texture_id_from_bindgroup(PyObject* self, PyObject* args, PyObject* kwargs) -{ - - PyObject* ptPythonDevice = nullptr; - plBindGroupHandle tBGHandle = {0}; - - static const char* apcKeywords[] = { - "device", - "bindgroup", - NULL, - }; - - if (!pl_parse_args("OI", (const char**)apcKeywords, args, kwargs, __FUNCTION__, - &ptPythonDevice, &tBGHandle.uData)) - return NULL; - - plDevice* ptDevice = (plDevice*)PyCapsule_GetPointer(ptPythonDevice, "plDevice"); - - void* ptTexture = pl_dear_imgui_get_texture_id_from_bindgroup(ptDevice, tBGHandle); - - return PyCapsule_New(ptTexture, "void", NULL); -} - PyObject* dear_imgui_render(PyObject* self, PyObject* args, PyObject* kwargs) { @@ -314,9 +290,9 @@ Image(PyObject* self, PyObject* args, PyObject* kwargs) void* ptTexture = (plDevice*)PyCapsule_GetPointer(ptPyTexture, "void"); - ImTextureRef tTexture = ImTextureRef(ptTexture); + // ImTextureRef tTexture = ImTextureRef(ptTexture); - ImGui::Image(tTexture, tSize, tUV0, tUV1); + // ImGui::Image(tTexture, tSize, tUV0, tUV1); Py_RETURN_NONE; } @@ -4124,7 +4100,6 @@ static PyMethodDef gatplDearImGuiICommands[] = {"new_frame", (PyCFunction)dear_imgui_new_frame, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL}, {"render", (PyCFunction)dear_imgui_render, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL}, {"cleanup", (PyCFunction)dear_imgui_cleanup, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL}, - {"get_texture_id_from_bindgroup", (PyCFunction)dear_imgui_get_texture_id_from_bindgroup, METH_VARARGS | METH_KEYWORDS | METH_STATIC, NULL}, {NULL, NULL, 0, NULL} }; diff --git a/sandbox/app.py b/sandbox/app.py index c824fee..272b433 100644 --- a/sandbox/app.py +++ b/sandbox/app.py @@ -1,220 +1,255 @@ import os -import math from pathlib import Path # core import pilotlight.pilotlight as pl from pilotlight.pilotlight import * -from pilotlight.enums import * from pilotlight.imgui import * +from pilotlight.enums import * from pilotlight.types import * -from pilotlight.pl_script_camera import pl_script_run - class App: + """ + Simple Pilot Light Python example showing how to: + - create a window + - initialize the starter/shader systems + - draw basic 2D primitives every frame + - organize drawing code in a readable way + """ def __init__(self): self.ptWindow = None - self.bResize = False - self.counter = None - self.test_bool = True - self.show_imgui_demo = False - self.show_implot_demo = False - self.tMainCamera = None - self.some_string_array = "pizza" - self.float_array = [1.0, 2.0, 3.0, 4.0] + self.ptFont = None - def pl_app_load(self): + # ------------------------------------------------------------------------- + # Application lifetime + # ------------------------------------------------------------------------- + def pl_app_load(self): + """ + Called once when the app starts. + Set up virtual file system mounts, create a window, and initialize + the Pilot Light systems we want to use. + """ + + # Mount directories used by the shader system. + # /shaders points at the Python package shader folder. + # /shader-temp is where compiled/intermediate shader output can go. plVfsI.mount_directory("/cache", str(Path.cwd()) + "/../cache") plVfsI.mount_directory("/shaders", os.path.dirname(os.path.abspath(pl.__file__)) + "/shaders") plVfsI.mount_directory("/shader-temp", str(Path.cwd()) + "/../shader-temp") - plVfsI.mount_directory("/assets", str(Path.cwd()) + "/../../pilotlight/assets") - plVfsI.mount_directory("/environments", str(Path.cwd()) + "/../../pilotlight/assets/development/environments") - plVfsI.mount_directory("/gltf-samples", str(Path.cwd()) + "/../../pilotlight/assets/gltf-samples/Models") - - plJobI.initialize() + # Create and show the OS window. window_desc = plWindowDesc() - window_desc.pcTitle = "Python Example" + window_desc.pcTitle = "Pilot Light Python - Basic Example 0" + window_desc.iXPos = 100 + window_desc.iYPos = 100 + window_desc.uWidth = 1280 + window_desc.uHeight = 720 _, self.ptWindow = plWindowI.create(window_desc) plWindowI.show(self.ptWindow) + # Initialize the starter extension. + # We explicitly disable the shader ext from the starter flags because + # we are going to initialize the shader system ourselves below. starterInit = plStarterInit(plStarterFlag.PL_STARTER_FLAGS_ALL_EXTENSIONS | plStarterFlag.PL_STARTER_FLAGS_MSAA, self.ptWindow) starterInit.eFlags &= ~plStarterFlag.PL_STARTER_FLAGS_SHADER_EXT plStarterI.initialize(starterInit) + # Initialize shader system. + # This is required even for simple drawing examples because the + # rendering path may still rely on shader compilation/setup. shader_options = plShaderOptions() shader_options.pcCacheOutputDirectory = "/shader-temp/" shader_options.apcDirectories = ["/shaders/"] shader_options.apcIncludeDirectories = ["/shaders/"] - shader_options.eFlags = plShaderFlags.PL_SHADER_FLAGS_AUTO_OUTPUT | plShaderFlags.PL_SHADER_FLAGS_INCLUDE_DEBUG | plShaderFlags.PL_SHADER_FLAGS_ALWAYS_COMPILE + shader_options.eFlags = ( + plShaderFlags.PL_SHADER_FLAGS_AUTO_OUTPUT + | plShaderFlags.PL_SHADER_FLAGS_INCLUDE_DEBUG + | plShaderFlags.PL_SHADER_FLAGS_ALWAYS_COMPILE + ) + plShaderI.initialize(shader_options) + # Complete starter initialization after custom systems are ready. plStarterI.finalize() - tRenderAttachmentInfo = plRenderAttachmentInfo() - plStarterI.get_render_attachment_info(tRenderAttachmentInfo) - plDearImGuiI.initialize(plStarterI.get_device(), plStarterI.get_swapchain(), tRenderAttachmentInfo) - - self.counter = plStatsI.get_counter("python counter") - - plShaderVariantI.initialize(plStarterI.get_device()) - - plRendererI.initialize(plStarterI.get_device(), plStarterI.get_swapchain()) - - - plEcsI.initialize() - plRendererEcsI.register_system() - plScriptI.register_ecs_system() - plCameraEcsI.register_ecs_system() - plAnimationI.register_ecs_system() - plMeshI.register_ecs_system() - plPhysicsI.register_ecs_system() - plMaterialI.register_ecs_system() - plEcsI.finalize() - self.ptComponentLibrary = plEcsI.get_default_library() - - result, self.tMainCamera, self.ptScene, self.ptView = plRendererI.load_test_world("/assets/core/scenes/scene-humanoid.json", self.ptComponentLibrary) - - - self.drawlist = plDrawI.request_2d_drawlist() - self.ptFGLayer = plDrawI.request_2d_layer(self.drawlist) - - self.texture_resource = plResourceI.load("/assets/core/textures/sprite_map.png", 0) - self.texture_handle = plResourceI.get_texture(self.texture_resource) - self.texture_bg = plDrawI.create_bind_group_for_texture(self.texture_handle) - self.texture_bg2 = plDearImGuiI.get_texture_id_from_bindgroup(plStarterI.get_device(), self.texture_bg) - - ImGui.StyleColorsDark() + ptFontAtlas = plDrawI.get_current_font_atlas() + self.ptFont = plDrawI.get_first_font(ptFontAtlas) def pl_app_shutdown(self): + """ + Called once when the app exits. + Flush GPU work, clean up engine systems, then destroy the window. + """ + plGraphicsI.flush_device(plStarterI.get_device()) - plEcsI.cleanup() - plRendererI.cleanup() - plDearImGuiI.cleanup() plStarterI.cleanup() plWindowI.destroy(self.ptWindow) def pl_app_resize(self): - - io = plIOI.get_io() - camera = plEcsI.get_component(self.ptComponentLibrary, plCameraEcsI.get_ecs_type_key(), self.tMainCamera) - plCameraI.set_viewport(camera, io.tMainViewportSize.x, io.tMainViewportSize.y) - plCameraI.update(camera) + """ + Called when the window changes size. + Let the starter extension rebuild any size-dependent resources. + """ plStarterI.resize() - self.bResize = True - def pl_app_update(self): + # ------------------------------------------------------------------------- + # Per-frame update + # ------------------------------------------------------------------------- - io = plIOI.get_io() + def pl_app_update(self): + """ + Called once per frame. + This is where all drawing for the frame happens. + """ + # Begin the frame. If it returns False, skip rendering this frame. if not plStarterI.begin_frame(): return - - plResourceI.new_frame() - plRendererI.begin_frame() - - if self.bResize: - plRendererI.resize_view(self.ptView, io.tMainViewportSize) - self.bResize = False - - plDearImGuiI.new_frame(plStarterI.get_device()) - - if self.show_imgui_demo: - self.show_imgui_demo = ImGui.ShowDemoWindow(self.show_imgui_demo) - - if self.show_implot_demo: - self.show_implot_demo = ImPlot.ShowDemoWindow(self.show_implot_demo) - - # script here - ptCamera = plEcsI.get_component(self.ptComponentLibrary, plCameraEcsI.get_ecs_type_key(), self.tMainCamera) - pl_script_run(ptCamera) - plAnimationI.run_animation_update_system(self.ptComponentLibrary, io.fDeltaTime) - plPhysicsI.update(io.fDeltaTime, self.ptComponentLibrary) - plEcsI.run_transform_update_system(self.ptComponentLibrary) - plEcsI.run_hierarchy_update_system(self.ptComponentLibrary) - plRendererEcsI.run_light_update_system(self.ptComponentLibrary) - plCameraEcsI.run_ecs(self.ptComponentLibrary) - plAnimationI.run_inverse_kinematics_update_system(self.ptComponentLibrary) - plRendererEcsI.run_skin_update_system(self.ptComponentLibrary) - plRendererEcsI.run_object_update_system(self.ptComponentLibrary) - plRendererEcsI.run_environment_probe_update_system(self.ptComponentLibrary) - - # drawing API + + # Foreground fgLayer is a convenient draw list for 2D overlay-style + # rendering. fgLayer = plStarterI.get_foreground_layer() - plDrawI.add_triangle_filled(fgLayer, [50.0, 100.0], [200.0, 0.0], [100.0, 200.0], plDrawSolidOptions(PL_COLOR_32_GREEN)) - plDrawI.add_triangle(fgLayer, plVec2(50.0, 300.0), plVec2(200.0, 200.0), plVec2(100.0, 400.0), plDrawLineOptions(PL_COLOR_32_WHITE)) - - # io API - if plIOI.is_key_pressed(plKey.PL_KEY_P): - print("P key pressed!") - - # ui API - if plUiI.begin_window("Debug Window"): - - _, self.some_string_array = plUiI.input_text("Input", self.some_string_array) - if plUiI.button("Press me"): - print("Button Pressed") - current_value = pl_get_pointer_value(self.counter) - current_value += 1 - pl_set_pointer_value(self.counter, current_value) - - if plUiI.button("Add Message"): - plScreenLogI.add_message(1.0, "Logging from python!") - - _, self.show_imgui_demo = plUiI.checkbox("Show ImGui Demo", self.show_imgui_demo) - _, self.show_implot_demo = plUiI.checkbox("Show ImPlot Demo", self.show_implot_demo) - - plUiI.end_window() - - # dear imgui API - if ImGui.BeginMainMenuBar(): - if ImGui.BeginMenu("File"): - ImGui.EndMenu() - if ImGui.BeginMenu("Edit", False): - ImGui.EndMenu() - if ImGui.BeginMenu("Tools"): - _, self.show_imgui_demo = ImGui.MenuItem("Show ImGui Demo", "", self.show_imgui_demo) - _, self.show_implot_demo = ImGui.MenuItem("Show ImPlot Demo", "", self.show_implot_demo) - ImGui.EndMenu() - if ImGui.BeginMenu("Help"): - ImGui.MenuItemSimple("Check For Update") - ImGui.MenuItemSimple("About", "-a") - ImGui.EndMenu() - ImGui.EndMainMenuBar() - if ImGui.Begin("ImGui Window"): - if ImGui.Button("Press Me"): - print("Pressed Imgui Button") - ImGui.DragFloat2("DragFloat2", self.float_array) - ImGui.End() - - camera = plEcsI.get_component(self.ptComponentLibrary, plCameraEcsI.get_ecs_type_key(), self.tMainCamera) - plRendererI.prepare_scene(self.ptScene, [camera]) - plRendererI.prepare_view(self.ptView, camera) - plRendererI.render_view(self.ptView, camera) - - tUV = plVec2() - - uTexture, tUV.x, tUV.y = plRendererI.get_view_color_bind_group(self.ptView) - plDrawI.add_image(self.ptFGLayer, uTexture, plVec2(), io.tMainViewportSize, plVec2(), tUV, PL_COLOR_32_WHITE) - plDrawI.submit_2d_layer(self.ptFGLayer) - - if self.test_bool: - _, self.test_bool = ImGui.Begin("Testing", open=self.test_bool) - ImGui.Image(self.texture_bg2, [500, 500]) - ImGui.End() - - cmdBuffer = plStarterI.begin_main_pass() - - tRenderAttachmentInfo = plRenderAttachmentInfo() - plStarterI.get_render_attachment_info(tRenderAttachmentInfo) - plDrawI.submit_2d_drawlist(self.drawlist, cmdBuffer, io.tMainViewportSize.x, io.tMainViewportSize.y, plGraphicsI.get_swapchain_info(plStarterI.get_swapchain()).eSampleCount, tRenderAttachmentInfo) - plDearImGuiI.render(cmdBuffer) - - plStarterI.end_main_pass() + # Outer frame + plDrawI.add_rect( + fgLayer, + [40.0, 40.0], + [1240.0, 680.0], + plDrawLineOptions(PL_COLOR_32_YELLOW, 2.0) + ) + + # Diagonal guide line + plDrawI.add_line( + fgLayer, + [40.0, 40.0], + [1240.0, 680.0], + plDrawLineOptions(PL_COLOR_32_GREEN, 2.0) + ) + + # Rounded panel region + plDrawI.add_rect_rounded( + fgLayer, + [70.0, 70.0], + [500.0, 300.0], + 16.0, # rounding radius + 0, # segment count (0 = automatic/default) + plDrawRectFlag.PL_DRAW_RECT_FLAG_NONE, + plDrawLineOptions(PL_COLOR_32_CYAN, 2.0) + ) + + # Triangle outline + plDrawI.add_triangle( + fgLayer, + [120.0, 240.0], + [240.0, 120.0], + [320.0, 260.0], + plDrawLineOptions(PL_COLOR_32_WHITE) + ) + + # Arbitrary quad outline + plDrawI.add_quad( + fgLayer, + [580.0, 100.0], + [760.0, 120.0], + [720.0, 260.0], + [540.0, 220.0], + plDrawLineOptions(PL_COLOR_32_ORANGE) + ) + + # Circle outline + plDrawI.add_circle( + fgLayer, + [930.0, 180.0], + 70.0, + 0, # segment count (0 = automatic/default) + plDrawLineOptions(PL_COLOR_32_BLUE) + ) + + # Lines + plDrawI.add_lines( + fgLayer, + [ + [1040.0, 110.0], + [1180.0, 250.0], + [1140.0, 90.0] + ], + plDrawLineOptions(PL_COLOR_32_GREEN) + ) + + # text + plDrawI.add_text(fgLayer, [300, 300], "Hello Python", plDrawTextOptions(self.ptFont, 25.0, PL_COLOR_32_CYAN)) + + # Polygon outline + plDrawI.add_polygon( + fgLayer, + [ + [1040.0, 110.0], + [1140.0, 90.0], + [1200.0, 150.0], + [1180.0, 250.0], + [1070.0, 260.0], + [1010.0, 180.0], + ], + plDrawLineOptions(PL_COLOR_32_RED) + ) + + plDrawI.add_bezier_quad( + fgLayer, + [120.0, 420.0], # start + [260.0, 320.0], # control + [420.0, 450.0], # end + 0, # segment count (0 = automatic/default) + plDrawLineOptions(PL_COLOR_32_BLUE) + ) + + # Cubic bezier with thicker line + plDrawI.add_bezier_cubic( + fgLayer, + [120.0, 560.0], # start + [220.0, 460.0], # control 1 + [360.0, 660.0], # control 2 + [460.0, 540.0], # end + 0, # segment count + plDrawLineOptions(PL_COLOR_32_RED, 3.0) + ) + + # Filled triangle + plDrawI.add_triangle_filled( + fgLayer, + [640.0, 420.0], + [820.0, 360.0], + [760.0, 560.0], + plDrawSolidOptions(PL_COLOR_32_WHITE) + ) + + # Filled-looking composition using multiple primitives + plDrawI.add_line( + fgLayer, + [900.0, 360.0], + [1180.0, 360.0], + plDrawLineOptions(PL_COLOR_32_WHITE, 4.0) + ) + + plDrawI.add_rect( + fgLayer, + [920.0, 390.0], + [1160.0, 560.0], + plDrawLineOptions(PL_COLOR_32_WHITE, 3.0) + ) + + plDrawI.add_circle( + fgLayer, + [1040.0, 475.0], + 50.0, + 0, + plDrawLineOptions(PL_COLOR_32_GREEN) + ) + + # Submit/present the frame. plStarterI.end_frame() -# run app + +# Run the application. pl_run(App()) \ No newline at end of file diff --git a/scripts/gen_build.py b/scripts/gen_build.py index 54cfb0d..5d52d72 100644 --- a/scripts/gen_build.py +++ b/scripts/gen_build.py @@ -43,7 +43,6 @@ "../../pilotlight/src", "../../pilotlight/shaders", "../../pilotlight/thirdparty/stb", - "../../pilotlight/thirdparty/glfw/include/", "../../pilotlight/thirdparty/imgui/", "../../pilotlight/thirdparty/cgltf", "../dependencies/cpython/", @@ -89,71 +88,6 @@ # configs pl.add_profile(configuration_filter=["debug"], definitions=["_DEBUG", "PL_CONFIG_DEBUG"]) - #----------------------------------------------------------------------------- - # [SECTION] glfw - #----------------------------------------------------------------------------- - - with pl.target("glfw", pl.TargetType.STATIC_LIBRARY, False, False): - - pl.add_source_files("../../pilotlight/thirdparty/glfw/src/glfw_unity.c") - pl.add_source_files("../../pilotlight/thirdparty/glfw/src/null_window.c") - - with pl.configuration("debug"): - - pl.set_output_binary("glfwd") - - # win32 - with pl.platform("Windows"): - with pl.compiler("msvc"): - pl.add_include_directories("%VULKAN_SDK%\\Include") - pl.add_definitions("UNICODE", "_UNICODE", "_CRT_SECURE_NO_WARNINGS", "_GLFW_VULKAN_STATIC", "_GLFW_WIN32", "_DEBUG") - pl.add_compiler_flags("-std:c11", "-Od", "-MDd", "-Zi", "-permissive", "-wd4244") - - # linux - with pl.platform("Linux"): - with pl.compiler("gcc"): - pl.add_definitions("_GLFW_VULKAN_STATIC", "_GLFW_X11") - pl.add_include_directories('$VULKAN_SDK/include', '/usr/include/vulkan', '/usr/include/vulkan') - pl.add_dynamic_link_libraries("xcb", "X11", "X11-xcb", "xkbcommon", "pthread", "xcb-cursor", "vulkan") - pl.add_link_directories('$VULKAN_SDK/lib') - pl.add_compiler_flags("-std=gnu99") - pl.add_source_files("../../pilotlight/thirdparty/glfw/src/posix_poll.c") - - # apple - with pl.platform("Darwin"): - with pl.compiler("clang"): - pl.add_definitions("_GLFW_VULKAN_STATIC", "_GLFW_COCOA") - pl.add_compiler_flags("-Wno-deprecated-declarations", "-std=c99", "-fmodules", "-ObjC") - pl.add_link_frameworks("Cocoa", "IOKit", "CoreFoundation") - - with pl.configuration("deploy"): - - pl.set_output_binary("glfw") - - # win32 - with pl.platform("Windows"): - with pl.compiler("msvc"): - pl.add_include_directories("%VULKAN_SDK%\\Include") - pl.add_definitions("UNICODE", "_UNICODE", "_CRT_SECURE_NO_WARNINGS", "_GLFW_VULKAN_STATIC", "_GLFW_WIN32") - pl.add_compiler_flags("-std:c11", "-O2", "-MD", "-Zi", "-permissive", "-wd4244") - - # linux - with pl.platform("Linux"): - with pl.compiler("gcc"): - pl.add_definitions("_GLFW_VULKAN_STATIC", "_GLFW_X11") - pl.add_include_directories('$VULKAN_SDK/include', '/usr/include/vulkan', '/usr/include/vulkan') - pl.add_dynamic_link_libraries("xcb", "X11", "X11-xcb", "xkbcommon", "pthread", "xcb-cursor", "vulkan") - pl.add_link_directories('$VULKAN_SDK/lib') - pl.add_compiler_flags("-std=gnu99") - pl.add_source_files("../../pilotlight/thirdparty/glfw/src/posix_poll.c") - - # apple - with pl.platform("Darwin"): - with pl.compiler("clang"): - pl.add_definitions("_GLFW_VULKAN_STATIC", "_GLFW_COCOA") - pl.add_compiler_flags("-std=c99", "-fmodules", "-ObjC", "-Wno-deprecated-declarations") - pl.add_link_frameworks("Cocoa", "IOKit", "CoreFoundation") - #----------------------------------------------------------------------------- # [SECTION] imgui & implot #----------------------------------------------------------------------------- @@ -167,7 +101,6 @@ with pl.configuration("debug"): pl.set_output_binary("dearimguid") - pl.add_static_link_libraries("glfwd") # win32 with pl.platform("Windows"): @@ -189,7 +122,6 @@ with pl.configuration("deploy"): pl.set_output_binary("dearimgui") - pl.add_static_link_libraries("glfw") # win32 with pl.platform("Windows"): @@ -231,8 +163,11 @@ # linux with pl.platform("Linux"): with pl.compiler("gcc"): - pl.add_source_files("../../pilotlight/extensions/pl_platform_linux_ext.c") - pl.add_dynamic_link_libraries("pthread") + pl.add_source_files("../../pilotlight/extensions/pl_platform_x11_ext.c") + pl.add_dynamic_link_libraries("xcb", "X11", "X11-xcb", "xkbcommon", "xcb-cursor", "xcb-xfixes", + "xcb-keysyms", "pthread") + pl.add_compiler_flags("-std=gnu11", "-fPIC", "--debug", "-g") + pl.add_linker_flags("-ldl", "-lm") # mac os with pl.platform("Darwin"): @@ -252,8 +187,11 @@ # linux with pl.platform("Linux"): with pl.compiler("gcc"): - pl.add_source_files("../../pilotlight/extensions/pl_platform_linux_ext.c") - pl.add_dynamic_link_libraries("pthread") + pl.add_source_files("../../pilotlight/extensions/pl_platform_x11_ext.c") + pl.add_dynamic_link_libraries("xcb", "X11", "X11-xcb", "xkbcommon", "xcb-cursor", "xcb-xfixes", + "xcb-keysyms", "pthread") + pl.add_compiler_flags("-std=gnu11", "-fPIC") + pl.add_linker_flags("-ldl", "-lm") # mac os with pl.platform("Darwin"): @@ -272,7 +210,7 @@ with pl.configuration("debug"): - pl.add_static_link_libraries("dearimguid", "glfwd", "pl_platform_ext") + pl.add_static_link_libraries("dearimguid", "pl_platform_ext") pl.set_output_binary("pilotlight_main") # win32 @@ -302,7 +240,7 @@ with pl.configuration("deploy"): - pl.add_static_link_libraries("dearimgui", "glfw", "pl_platform_ext") + pl.add_static_link_libraries("dearimgui", "pl_platform_ext") pl.set_output_binary("pilotlight_main") # win32 @@ -343,7 +281,7 @@ with pl.configuration("debug"): pl.set_output_directory("../pilotlight") - pl.add_static_link_libraries("dearimguid", "glfwd", "pl_platform_ext") + pl.add_static_link_libraries("dearimguid") # win32 with pl.platform("Windows"): @@ -383,7 +321,7 @@ pl.set_output_binary("imgui") pl.set_output_directory("../pilotlight") - pl.add_static_link_libraries("glfw", "pl_platform_ext", "dearimgui") + pl.add_static_link_libraries("dearimgui") # win32 with pl.platform("Windows"): @@ -427,7 +365,7 @@ with pl.configuration("debug"): - pl.add_static_link_libraries("dearimguid", "glfwd") + pl.add_static_link_libraries("dearimguid") # win32 with pl.platform("Windows"): @@ -475,7 +413,7 @@ with pl.configuration("deploy"): pl.set_output_binary("pilotlight") - pl.add_static_link_libraries("dearimgui", "glfw") + pl.add_static_link_libraries("dearimgui") # win32 with pl.platform("Windows"): diff --git a/src/pilotlight.c b/src/pilotlight.c index 9279de0..560ab27 100644 --- a/src/pilotlight.c +++ b/src/pilotlight.c @@ -100,7 +100,6 @@ pl_python_run(PyObject* self, PyObject* arg) pl_load_ext((plApiRegistryI*)ptApiRegistry, false); pl_load_platform_ext((plApiRegistryI*)ptApiRegistry, false); plIO* ptIO = gptIOI->get_io(); - ptWindows2 = pl_get_api_latest(ptApiRegistry, plWindowI); PyObject* ptResult = PyObject_CallMethod(arg, "pl_app_load", NULL); diff --git a/src/pl_main.cpp b/src/pl_main.cpp index 4428a02..089e3e4 100644 --- a/src/pl_main.cpp +++ b/src/pl_main.cpp @@ -34,7 +34,6 @@ Index of this file: #define WIN32_LEAN_AND_MEAN #include - #define GLFW_EXPOSE_NATIVE_WIN32 #elif defined(__APPLE__) @@ -48,7 +47,6 @@ Index of this file: #include // close #include // O_RDONLY, O_WRONLY ,O_CREAT #include - #define GLFW_EXPOSE_NATIVE_COCOA #else // linux @@ -60,18 +58,9 @@ Index of this file: #include // usleep() #include #include - #define GLFW_EXPOSE_NATIVE_X11 #endif -// imgui -#include "imgui.h" -#include "imgui_internal.h" // ImLerp -#include "imgui_impl_glfw.h" - -// glfw -#include -#include "GLFW/glfw3native.h" #include "pl_internal.h" #define PL_SINGLE_UNIT_BUILD @@ -114,735 +103,6 @@ Index of this file: } #endif -// glfw -GLFWwindow* gptGlfwWindow; -GLFWwindow* ptMouseWindow; -GLFWcursor* atMouseCursors[PL_MOUSE_CURSOR_COUNT]; -bool bMouseIgnoreButtonUpWaitForFocusLoss; -bool bMouseIgnoreButtonUp; -plVec2 tLastValidMousePos; -GLFWwindow* atKeyOwnerWindows[GLFW_KEY_LAST]; - -//----------------------------------------------------------------------------- -// [SECTION] internal enums -//----------------------------------------------------------------------------- - -enum _plWindowInternalFlags -{ - PL_WINDOW_INTERNAL_FLAG_NONE = 0, - PL_WINDOW_INTERNAL_FLAG_MAXIMIZE_REQUESTED = 1 << 0, - PL_WINDOW_INTERNAL_FLAG_MINIMIZE_REQUESTED = 1 << 1, - PL_WINDOW_INTERNAL_FLAG_RESIZE_REQUESTED = 1 << 2, - PL_WINDOW_INTERNAL_FLAG_FULL_SCREEN_REQUESTED = 1 << 3, -}; - -//----------------------------------------------------------------------------- -// [SECTION] forward declarations -//----------------------------------------------------------------------------- - -void pl_glfw_error_callback (int error, const char* description); -void pl_glfw_mouse_button_callback (GLFWwindow*, int button, int action, int mods); -void pl_glfw_mouse_pos_callback (GLFWwindow*, double x, double y); -void pl_glfw_cursor_enter_callback (GLFWwindow*, int entered); -void pl_glfw_window_focus_callback (GLFWwindow*, int focused); -void pl_glfw_scroll_callback (GLFWwindow*, double xoffset, double yoffset); -void pl_glfw_char_callback (GLFWwindow*, unsigned int c); -void pl_glfw_key_callback (GLFWwindow*, int keycode, int scancode, int action, int mods); -void pl_glfw_size_callback (GLFWwindow*, int width, int height); -void pl_glfw_window_iconified_callback(GLFWwindow*, int iconified); -void pl_glfw_window_close_callback (GLFWwindow*); -plKey pl_glfw_key_translate (int keycode, int scancode); - -//----------------------------------------------------------------------------- -// [SECTION] window api -//----------------------------------------------------------------------------- - -bool -pl_set_window_attribute(plWindow* ptWindow, plWindowAttribute tAttribute, const plWindowAttributeValue* ptValue) -{ - GLFWwindow* ptGlfwWindow = (GLFWwindow*)ptWindow->_pBackendData2; - switch (tAttribute) - { - case PL_WINDOW_ATTRIBUTE_SIZE: - { - ptWindow->_tInternalFlags |= PL_WINDOW_INTERNAL_FLAG_RESIZE_REQUESTED; - // ptWindow->_tInternalFlags |= PL_WINDOW_INTERNAL_FLAG_ATTRIBUTE_CHANGE_REQUESTED; // incase full screen - ptWindow->_uRequestedWidth = ptValue->tuVec2.x; - ptWindow->_uRequestedHeight = ptValue->tuVec2.y; - glfwGetWindowPos(ptGlfwWindow, &ptWindow->_iXPos, &ptWindow->_iYPos); - break; - } - - case PL_WINDOW_ATTRIBUTE_POSITION: - { - glfwSetWindowPos(ptGlfwWindow, ptValue->tiVec2.x, ptValue->tiVec2.y); - break; - } - - case PL_WINDOW_ATTRIBUTE_MAXIMIZED: - ptWindow->_tInternalFlags |= PL_WINDOW_INTERNAL_FLAG_MAXIMIZE_REQUESTED; - break; - - case PL_WINDOW_ATTRIBUTE_MINIMIZED: - if(ptValue->bValue) - ptWindow->_tInternalFlags |= PL_WINDOW_INTERNAL_FLAG_MINIMIZE_REQUESTED; - else - { - ptWindow->_tInternalFlags &= ~PL_WINDOW_INTERNAL_FLAG_MINIMIZE_REQUESTED; - glfwRestoreWindow(ptGlfwWindow); - } - break; - - case PL_WINDOW_ATTRIBUTE_VISIBLE: - if(ptValue->bValue) - glfwShowWindow(ptGlfwWindow); - else - glfwHideWindow(ptGlfwWindow); - break; - - case PL_WINDOW_ATTRIBUTE_FOCUSED: - glfwFocusWindow(ptGlfwWindow); - break; - - case PL_WINDOW_ATTRIBUTE_SIZE_LIMITS: - glfwSetWindowSizeLimits(ptGlfwWindow, (int)ptValue->tuVec4.x, (int)ptValue->tuVec4.y, (int)ptValue->tuVec4.z, (int)ptValue->tuVec4.w); - break; - - default: - break; - } - return true; -} - -bool -pl_get_window_attribute(plWindow* ptWindow, plWindowAttribute tAttribute, plWindowAttributeValue* ptValue) -{ - GLFWwindow* ptGlfwWindow = (GLFWwindow*)ptWindow->_pBackendData2; - switch (tAttribute) - { - case PL_WINDOW_ATTRIBUTE_SIZE: - { - int iWidth = 0; - int iHeight = 0; - glfwGetWindowSize(ptGlfwWindow, &iWidth, &iHeight); - ptValue->tuVec2.x = (uint32_t)iWidth; - ptValue->tuVec2.y = (uint32_t)iHeight; - break; - } - - case PL_WINDOW_ATTRIBUTE_POSITION: - { - glfwGetWindowPos(ptGlfwWindow, &ptValue->tiVec2.x, &ptValue->tiVec2.y); - break; - } - - case PL_WINDOW_ATTRIBUTE_RESIZABLE: - { - if (glfwGetWindowAttrib(ptGlfwWindow, GLFW_RESIZABLE)) - ptValue->bValue = true; - else - ptValue->bValue = false; - break; - } - - case PL_WINDOW_ATTRIBUTE_FOCUSED: - { - if (glfwGetWindowAttrib(ptGlfwWindow, GLFW_FOCUSED)) - ptValue->bValue = true; - else - ptValue->bValue = false; - break; - } - - case PL_WINDOW_ATTRIBUTE_DECORATED: - { - if (glfwGetWindowAttrib(ptGlfwWindow, GLFW_DECORATED)) - ptValue->bValue = true; - else - ptValue->bValue = false; - break; - } - - case PL_WINDOW_ATTRIBUTE_TOP_MOST: - { - if (glfwGetWindowAttrib(ptGlfwWindow, GLFW_FLOATING)) - ptValue->bValue = true; - else - ptValue->bValue = false; - break; - } - - case PL_WINDOW_ATTRIBUTE_HOVERED: - { - if (glfwGetWindowAttrib(ptGlfwWindow, GLFW_HOVERED)) - ptValue->bValue = true; - else - ptValue->bValue = false; - break; - } - - case PL_WINDOW_ATTRIBUTE_MAXIMIZED: - { - if (glfwGetWindowAttrib(ptGlfwWindow, GLFW_MAXIMIZED)) - ptValue->bValue = true; - else - ptValue->bValue = false; - break; - } - - case PL_WINDOW_ATTRIBUTE_MINIMIZED: - { - if (glfwGetWindowAttrib(ptGlfwWindow, GLFW_ICONIFIED)) - ptValue->bValue = true; - else - ptValue->bValue = false; - break; - } - - case PL_WINDOW_ATTRIBUTE_VISIBLE: - { - if (glfwGetWindowAttrib(ptGlfwWindow, GLFW_VISIBLE)) - ptValue->bValue = true; - else - ptValue->bValue = false; - break; - } - - case PL_WINDOW_ATTRIBUTE_SIZE_LIMITS: - return false; - - default: - break; - } - return true; -} - -bool -pl_set_cursor_mode(plWindow* ptWindow, plCursorMode tMode) -{ - GLFWwindow* ptGlfwWindow = (GLFWwindow*)ptWindow->_pBackendData2; - switch(tMode) - { - case PL_CURSOR_MODE_NORMAL: - glfwSetInputMode(ptGlfwWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); - break; - case PL_CURSOR_MODE_HIDDEN: - glfwSetInputMode(ptGlfwWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - break; - - case PL_CURSOR_MODE_CAPTURED: - glfwSetInputMode(ptGlfwWindow, GLFW_CURSOR, GLFW_CURSOR_CAPTURED); - break; - - case PL_CURSOR_MODE_DISABLED: - glfwSetInputMode(ptGlfwWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - break; - - default: - return false; - break; - } - return true; -} - -plCursorMode -pl_get_cursor_mode(plWindow* ptWindow) -{ - GLFWwindow* ptGlfwWindow = (GLFWwindow*)ptWindow->_pBackendData2; - int iMode = glfwGetInputMode(ptGlfwWindow, GLFW_CURSOR); - switch(iMode) - { - case GLFW_CURSOR_NORMAL: return PL_CURSOR_MODE_NORMAL; - case GLFW_CURSOR_HIDDEN: return PL_CURSOR_MODE_HIDDEN; - case GLFW_CURSOR_CAPTURED: return PL_CURSOR_MODE_CAPTURED; - case GLFW_CURSOR_DISABLED: return PL_CURSOR_MODE_DISABLED; - default: - return PL_CURSOR_MODE_NORMAL; - } -} - -void -pl_show_window(plWindow* ptWindow) -{ - GLFWwindow* ptGlfwWindow = (GLFWwindow*)ptWindow->_pBackendData2; - glfwShowWindow(ptGlfwWindow); -} - -void -pl_full_screen_window(plWindow* ptWindow, int iMonitor) -{ - GLFWwindow* ptGlfwWindow = (GLFWwindow*)ptWindow->_pBackendData2; - ptWindow->_tInternalFlags |= PL_WINDOW_INTERNAL_FLAG_FULL_SCREEN_REQUESTED; - ptWindow->_iRequestedMonitor = iMonitor; -} - -bool -pl_set_raw_mouse_input(plWindow* ptWindow, bool bValue) -{ - GLFWwindow* ptGlfwWindow = (GLFWwindow*)ptWindow->_pBackendData2; - if (glfwRawMouseMotionSupported()) - { - glfwSetInputMode(ptGlfwWindow, GLFW_RAW_MOUSE_MOTION, bValue ? GLFW_TRUE : GLFW_FALSE); - return true; - } - return false; -} - -bool -pl_set_fullscreen(plWindow* ptWindow, const plFullScreenDesc* tDesc) -{ - GLFWwindow* ptGlfwWindow = (GLFWwindow*)ptWindow->_pBackendData2; - switch (tDesc->tMode) - { - case PL_FULLSCREEN_MODE_EXCLUSIVE: - ptWindow->_tInternalFlags |= PL_WINDOW_INTERNAL_FLAG_FULL_SCREEN_REQUESTED; - ptWindow->_iRequestedMonitor = tDesc->iMonitor; - return true; - - case PL_FULLSCREEN_MODE_BORDERLESS: - return false; - - case PL_FULLSCREEN_MODE_NONE: - ptWindow->_tInternalFlags |= PL_WINDOW_INTERNAL_FLAG_RESIZE_REQUESTED; - ptWindow->_uRequestedWidth = 500; - ptWindow->_uRequestedHeight = 500; - return true; - - default: - return true; - } -} - -const plWindowCapabilities* -pl_get_window_capabilities(void) -{ - static plWindowCapabilities tCapabilities = {}; - - tCapabilities.uCursorModeCount = (uint32_t)PL_CURSOR_MODE_COUNT; - tCapabilities.uAttributeCount = (uint32_t)PL_WINDOW_ATTRIBUTE_COUNT; - tCapabilities.uFullScreenModeCount = 2; - - static const plWindowAttribute atSupportedAttributes[] = { - PL_WINDOW_ATTRIBUTE_SIZE, - PL_WINDOW_ATTRIBUTE_POSITION, - PL_WINDOW_ATTRIBUTE_RESIZABLE, - PL_WINDOW_ATTRIBUTE_DECORATED, - PL_WINDOW_ATTRIBUTE_TOP_MOST, - PL_WINDOW_ATTRIBUTE_MINIMIZED, - PL_WINDOW_ATTRIBUTE_MAXIMIZED, - PL_WINDOW_ATTRIBUTE_VISIBLE, - PL_WINDOW_ATTRIBUTE_FOCUSED, - PL_WINDOW_ATTRIBUTE_HOVERED - }; - - static const plCursorMode atSupportedCursorModes[] = { - PL_CURSOR_MODE_NORMAL, - PL_CURSOR_MODE_HIDDEN, - PL_CURSOR_MODE_CAPTURED, - PL_CURSOR_MODE_DISABLED - }; - - static const plFullScreenMode atSupportedScreenModes[] = { - PL_FULLSCREEN_MODE_NONE, - PL_FULLSCREEN_MODE_EXCLUSIVE - }; - - tCapabilities.atCursorModes = atSupportedCursorModes; - tCapabilities.atFullScreenModes = atSupportedScreenModes; - tCapabilities.atWindowAttributes = atSupportedAttributes; - tCapabilities.tFlags = PL_WINDOW_CAPABILITY_FLAGS_RAW_MOUSE_INPUT; - - return &tCapabilities; -} - -plWindowResult -pl_create_window(plWindowDesc tDesc, plWindow** pptWindowOut) -{ - plWindow* ptWindow = (plWindow*)malloc(sizeof(plWindow)); - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - - glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); - glfwWindowHint(GLFW_DECORATED, GLFW_TRUE); - glfwWindowHint(GLFW_FLOATING, GLFW_FALSE); - glfwWindowHint(GLFW_POSITION_X, tDesc.iXPos); - glfwWindowHint(GLFW_POSITION_Y, tDesc.iYPos); - - gptGlfwWindow = glfwCreateWindow((int)tDesc.uWidth, (int)tDesc.uHeight, tDesc.pcTitle, NULL, NULL); - - ptWindow->_pBackendData2 = gptGlfwWindow; - - if(gptMainWindow == NULL) - gptMainWindow = ptWindow; - - glfwSetMouseButtonCallback(gptGlfwWindow, pl_glfw_mouse_button_callback); - glfwSetCursorPosCallback(gptGlfwWindow, pl_glfw_mouse_pos_callback); - glfwSetCursorEnterCallback(gptGlfwWindow, pl_glfw_cursor_enter_callback); - glfwSetScrollCallback(gptGlfwWindow, pl_glfw_scroll_callback); - glfwSetCharCallback(gptGlfwWindow, pl_glfw_char_callback); - glfwSetKeyCallback(gptGlfwWindow, pl_glfw_key_callback); - glfwSetWindowFocusCallback(gptGlfwWindow, pl_glfw_window_focus_callback); - glfwSetWindowSizeCallback(gptGlfwWindow, pl_glfw_size_callback); - glfwSetWindowIconifyCallback(gptGlfwWindow, pl_glfw_window_iconified_callback); - glfwSetWindowCloseCallback(gptGlfwWindow, pl_glfw_window_close_callback); - - #ifdef PL_METAL_BACKEND - if(pl_sb_size(gsbtWindows) == 0) - ImGui_ImplGlfw_InitForOther(gptGlfwWindow, true); - #else - if(pl_sb_size(gsbtWindows) == 0) - ImGui_ImplGlfw_InitForVulkan(gptGlfwWindow, true); - #endif - - #ifdef _WIN32 - HWND tHandle = glfwGetWin32Window(gptGlfwWindow); - ptWindow->_pBackendData = tHandle; - #elif defined(__APPLE__) - device = MTLCreateSystemDefaultDevice(); - gptIOCtx->pBackendPlatformData = device; - - nswin = glfwGetCocoaWindow(gptGlfwWindow); - layer = [CAMetalLayer layer]; - layer.device = device; - layer.pixelFormat = MTLPixelFormatBGRA8Unorm; - nswin.contentView.layer = layer; - nswin.contentView.wantsLayer = YES; - - typedef struct _plWindowData - { - void* ptWindow; - void* ptViewController; - CAMetalLayer* ptLayer; - } plWindowData; - - plWindowData* ptData = (plWindowData*)malloc(sizeof(plWindowData)); - ptData->ptLayer = layer; - ptWindow->_pBackendData = ptData; - - #else // linux - typedef struct plPlatformData - { - uint32_t header; - Display* dpy; - Window window; - } plPlatformData; - static plPlatformData tPlatformData = {UINT32_MAX}; - tPlatformData.dpy = glfwGetX11Display(); - tPlatformData.window = glfwGetX11Window(gptGlfwWindow); - ptWindow->_pBackendData = &tPlatformData; - #endif - *pptWindowOut = ptWindow; - pl_sb_push(gsbtWindows, ptWindow); - return PL_WINDOW_RESULT_SUCCESS; -} - -void -pl_destroy_window(plWindow* ptWindow) -{ - free(ptWindow); -} - -//----------------------------------------------------------------------------- -// [SECTION] glfw callbacks -//----------------------------------------------------------------------------- - -void -pl_glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods) -{ - // if(ImGui::GetIO().WantCaptureMouse) - // return; - - // update key modifiers - gptIOI->add_key_event(PL_KEY_MOD_CTRL, (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)); - gptIOI->add_key_event(PL_KEY_MOD_SHIFT, (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS)); - gptIOI->add_key_event(PL_KEY_MOD_ALT, (glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS)); - gptIOI->add_key_event(PL_KEY_MOD_SUPER, (glfwGetKey(window, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS)); - - plIO* ptIO = gptIOI->get_io(); - if (button >= 0 && button < PL_MOUSE_BUTTON_COUNT) - gptIOI->add_mouse_button_event(button, action == GLFW_PRESS); -} - -void -pl_glfw_mouse_pos_callback(GLFWwindow* window, double x, double y) -{ - gptIOI->add_mouse_pos_event((float)x, (float)y); - tLastValidMousePos.x = (float)x; - tLastValidMousePos.y = (float)y; -} - -void -pl_glfw_cursor_enter_callback(GLFWwindow* window, int entered) -{ - // if(ImGui::GetIO().WantCaptureMouse) - // return; - - plIO* ptIO = gptIOI->get_io(); - if (entered) - { - ptMouseWindow = window; - gptIOI->add_mouse_pos_event(tLastValidMousePos.x, tLastValidMousePos.y); - } - else if (ptMouseWindow == window) - { - tLastValidMousePos = ptIO->_tMousePos; - ptMouseWindow = NULL; - gptIOI->add_mouse_pos_event(-FLT_MAX, -FLT_MAX); - } -} - -void -pl_glfw_window_focus_callback(GLFWwindow* window, int focused) -{ - // Workaround for Linux: when losing focus with bMouseIgnoreButtonUpWaitForFocusLoss set, we will temporarily ignore subsequent Mouse Up events - bMouseIgnoreButtonUp = (bMouseIgnoreButtonUpWaitForFocusLoss && focused == 0); - bMouseIgnoreButtonUpWaitForFocusLoss = false; - - // gptIOI->add_focus_event(focused != 0); -} - -void -pl_glfw_scroll_callback(GLFWwindow* window, double xoffset, double yoffset) -{ - // if(ImGui::GetIO().WantCaptureMouse) - // return; - gptIOI->add_mouse_wheel_event((float)xoffset, (float)yoffset); -} - -void -pl_glfw_char_callback(GLFWwindow* window, unsigned int c) -{ - // if(ImGui::GetIO().WantTextInput) - // return; - gptIOI->add_text_event(c); -} - -int -pl_translate_untranslated_key(int key, int scancode) -{ - if (key >= GLFW_KEY_KP_0 && key <= GLFW_KEY_KP_EQUAL) - return key; - - GLFWerrorfun prev_error_callback = glfwSetErrorCallback(NULL); - const char* key_name = glfwGetKeyName(key, scancode); - glfwSetErrorCallback(prev_error_callback); - (void)glfwGetError(NULL); - - if (key_name && key_name[0] != 0 && key_name[1] == 0) - { - const char char_names[] = "`-=[]\\,;\'./"; - const int char_keys[] = { GLFW_KEY_GRAVE_ACCENT, GLFW_KEY_MINUS, GLFW_KEY_EQUAL, GLFW_KEY_LEFT_BRACKET, GLFW_KEY_RIGHT_BRACKET, GLFW_KEY_BACKSLASH, GLFW_KEY_COMMA, GLFW_KEY_SEMICOLON, GLFW_KEY_APOSTROPHE, GLFW_KEY_PERIOD, GLFW_KEY_SLASH, 0 }; - PL_ASSERT(PL_ARRAYSIZE(char_names) == PL_ARRAYSIZE(char_keys)); - if (key_name[0] >= '0' && key_name[0] <= '9') { key = GLFW_KEY_0 + (key_name[0] - '0'); } - else if (key_name[0] >= 'A' && key_name[0] <= 'Z') { key = GLFW_KEY_A + (key_name[0] - 'A'); } - else if (key_name[0] >= 'a' && key_name[0] <= 'z') { key = GLFW_KEY_A + (key_name[0] - 'a'); } - else - { - const char* p = strchr(char_names, key_name[0]); - if(p) - { - key = char_keys[p - char_names]; - } - } - - } - // if (action == GLFW_PRESS) printf("key %d scancode %d name '%s'\n", key, scancode, key_name); - return key; -} - -void -pl_glfw_size_callback(GLFWwindow* window, int width, int height) -{ - gptIOCtx->bViewportSizeChanged = true; - if(width == 0 || height == 0) - { - gptIOCtx->bViewportMinimized = true; - } - else - { - int fwidth, fheight; - glfwGetFramebufferSize(window, &fwidth, &fheight); - gptIOCtx->tMainFramebufferScale.x = (float)fwidth / (float)width; - gptIOCtx->tMainFramebufferScale.y = (float)fheight / (float)height; - - gptIOCtx->bViewportMinimized = false; - gptIOCtx->tMainViewportSize.x = (float)width; - gptIOCtx->tMainViewportSize.y = (float)height; - // gsbtWindows[0]->tDesc.uWidth = (uint32_t)width; - // gsbtWindows[0]->tDesc.uHeight = (uint32_t)height; - - // pl__app_resize(gptMainWindow, gpUserData); - } -} - -void -pl_glfw_window_iconified_callback(GLFWwindow* window, int iconified) -{ - gptIOCtx->bViewportMinimized = iconified; -} - -void -pl_glfw_window_close_callback(GLFWwindow* window) -{ - gptIOCtx->bRunning = false; -} - -void -pl_glfw_key_callback(GLFWwindow* window, int keycode, int scancode, int action, int mods) -{ - if (action != GLFW_PRESS && action != GLFW_RELEASE) - return; - - // if(ImGui::GetIO().WantCaptureKeyboard) - // return; - - // update key modifiers - gptIOI->add_key_event(PL_KEY_MOD_CTRL, (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)); - gptIOI->add_key_event(PL_KEY_MOD_SHIFT, (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS)); - gptIOI->add_key_event(PL_KEY_MOD_ALT, (glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS)); - gptIOI->add_key_event(PL_KEY_MOD_SUPER, (glfwGetKey(window, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS)); - - if (keycode >= 0 && keycode < PL_ARRAYSIZE(atKeyOwnerWindows)) - atKeyOwnerWindows[keycode] = (action == GLFW_PRESS) ? window : NULL; - - keycode = pl_translate_untranslated_key(keycode, scancode); - - plKey imgui_key = pl_glfw_key_translate(keycode, scancode); - gptIOI->add_key_event(imgui_key, (action == GLFW_PRESS)); -} - -plKey -pl_glfw_key_translate(int keycode, int scancode) -{ - switch (keycode) - { - case GLFW_KEY_TAB: return PL_KEY_TAB; - case GLFW_KEY_LEFT: return PL_KEY_LEFT_ARROW; - case GLFW_KEY_RIGHT: return PL_KEY_RIGHT_ARROW; - case GLFW_KEY_UP: return PL_KEY_UP_ARROW; - case GLFW_KEY_DOWN: return PL_KEY_DOWN_ARROW; - case GLFW_KEY_PAGE_UP: return PL_KEY_PAGE_UP; - case GLFW_KEY_PAGE_DOWN: return PL_KEY_PAGE_DOWN; - case GLFW_KEY_HOME: return PL_KEY_HOME; - case GLFW_KEY_END: return PL_KEY_END; - case GLFW_KEY_INSERT: return PL_KEY_INSERT; - case GLFW_KEY_DELETE: return PL_KEY_DELETE; - case GLFW_KEY_BACKSPACE: return PL_KEY_BACKSPACE; - case GLFW_KEY_SPACE: return PL_KEY_SPACE; - case GLFW_KEY_ENTER: return PL_KEY_ENTER; - case GLFW_KEY_ESCAPE: return PL_KEY_ESCAPE; - case GLFW_KEY_APOSTROPHE: return PL_KEY_APOSTROPHE; - case GLFW_KEY_COMMA: return PL_KEY_COMMA; - case GLFW_KEY_MINUS: return PL_KEY_MINUS; - case GLFW_KEY_PERIOD: return PL_KEY_PERIOD; - case GLFW_KEY_SLASH: return PL_KEY_SLASH; - case GLFW_KEY_SEMICOLON: return PL_KEY_SEMICOLON; - case GLFW_KEY_EQUAL: return PL_KEY_EQUAL; - case GLFW_KEY_LEFT_BRACKET: return PL_KEY_LEFT_BRACKET; - case GLFW_KEY_BACKSLASH: return PL_KEY_BACKSLASH; - // case GLFW_KEY_WORLD_1: return PL_KEY_Oem102; - // case GLFW_KEY_WORLD_2: return PL_KEY_Oem102; - case GLFW_KEY_RIGHT_BRACKET: return PL_KEY_RIGHT_BRACKET; - case GLFW_KEY_GRAVE_ACCENT: return PL_KEY_GRAVE_ACCENT; - case GLFW_KEY_CAPS_LOCK: return PL_KEY_CAPS_LOCK; - case GLFW_KEY_SCROLL_LOCK: return PL_KEY_SCROLL_LOCK; - case GLFW_KEY_NUM_LOCK: return PL_KEY_NUM_LOCK; - case GLFW_KEY_PRINT_SCREEN: return PL_KEY_PRINT_SCREEN; - case GLFW_KEY_PAUSE: return PL_KEY_PAUSE; - case GLFW_KEY_KP_0: return PL_KEY_KEYPAD_0; - case GLFW_KEY_KP_1: return PL_KEY_KEYPAD_1; - case GLFW_KEY_KP_2: return PL_KEY_KEYPAD_2; - case GLFW_KEY_KP_3: return PL_KEY_KEYPAD_3; - case GLFW_KEY_KP_4: return PL_KEY_KEYPAD_4; - case GLFW_KEY_KP_5: return PL_KEY_KEYPAD_5; - case GLFW_KEY_KP_6: return PL_KEY_KEYPAD_6; - case GLFW_KEY_KP_7: return PL_KEY_KEYPAD_7; - case GLFW_KEY_KP_8: return PL_KEY_KEYPAD_8; - case GLFW_KEY_KP_9: return PL_KEY_KEYPAD_9; - case GLFW_KEY_KP_DECIMAL: return PL_KEY_KEYPAD_DECIMAL; - case GLFW_KEY_KP_DIVIDE: return PL_KEY_KEYPAD_DIVIDE; - case GLFW_KEY_KP_MULTIPLY: return PL_KEY_KEYPAD_MULTIPLY; - case GLFW_KEY_KP_SUBTRACT: return PL_KEY_KEYPAD_SUBTRACT; - case GLFW_KEY_KP_ADD: return PL_KEY_KEYPAD_ADD; - case GLFW_KEY_KP_ENTER: return PL_KEY_KEYPAD_ENTER; - case GLFW_KEY_KP_EQUAL: return PL_KEY_KEYPAD_EQUAL; - case GLFW_KEY_LEFT_SHIFT: return PL_KEY_LEFT_SHIFT; - case GLFW_KEY_LEFT_CONTROL: return PL_KEY_LEFT_CTRL; - case GLFW_KEY_LEFT_ALT: return PL_KEY_LEFT_ALT; - case GLFW_KEY_LEFT_SUPER: return PL_KEY_LEFT_SUPER; - case GLFW_KEY_RIGHT_SHIFT: return PL_KEY_RIGHT_SHIFT; - case GLFW_KEY_RIGHT_CONTROL: return PL_KEY_RIGHT_CTRL; - case GLFW_KEY_RIGHT_ALT: return PL_KEY_RIGHT_ALT; - case GLFW_KEY_RIGHT_SUPER: return PL_KEY_RIGHT_SUPER; - case GLFW_KEY_MENU: return PL_KEY_MENU; - case GLFW_KEY_0: return PL_KEY_0; - case GLFW_KEY_1: return PL_KEY_1; - case GLFW_KEY_2: return PL_KEY_2; - case GLFW_KEY_3: return PL_KEY_3; - case GLFW_KEY_4: return PL_KEY_4; - case GLFW_KEY_5: return PL_KEY_5; - case GLFW_KEY_6: return PL_KEY_6; - case GLFW_KEY_7: return PL_KEY_7; - case GLFW_KEY_8: return PL_KEY_8; - case GLFW_KEY_9: return PL_KEY_9; - case GLFW_KEY_A: return PL_KEY_A; - case GLFW_KEY_B: return PL_KEY_B; - case GLFW_KEY_C: return PL_KEY_C; - case GLFW_KEY_D: return PL_KEY_D; - case GLFW_KEY_E: return PL_KEY_E; - case GLFW_KEY_F: return PL_KEY_F; - case GLFW_KEY_G: return PL_KEY_G; - case GLFW_KEY_H: return PL_KEY_H; - case GLFW_KEY_I: return PL_KEY_I; - case GLFW_KEY_J: return PL_KEY_J; - case GLFW_KEY_K: return PL_KEY_K; - case GLFW_KEY_L: return PL_KEY_L; - case GLFW_KEY_M: return PL_KEY_M; - case GLFW_KEY_N: return PL_KEY_N; - case GLFW_KEY_O: return PL_KEY_O; - case GLFW_KEY_P: return PL_KEY_P; - case GLFW_KEY_Q: return PL_KEY_Q; - case GLFW_KEY_R: return PL_KEY_R; - case GLFW_KEY_S: return PL_KEY_S; - case GLFW_KEY_T: return PL_KEY_T; - case GLFW_KEY_U: return PL_KEY_U; - case GLFW_KEY_V: return PL_KEY_V; - case GLFW_KEY_W: return PL_KEY_W; - case GLFW_KEY_X: return PL_KEY_X; - case GLFW_KEY_Y: return PL_KEY_Y; - case GLFW_KEY_Z: return PL_KEY_Z; - case GLFW_KEY_F1: return PL_KEY_F1; - case GLFW_KEY_F2: return PL_KEY_F2; - case GLFW_KEY_F3: return PL_KEY_F3; - case GLFW_KEY_F4: return PL_KEY_F4; - case GLFW_KEY_F5: return PL_KEY_F5; - case GLFW_KEY_F6: return PL_KEY_F6; - case GLFW_KEY_F7: return PL_KEY_F7; - case GLFW_KEY_F8: return PL_KEY_F8; - case GLFW_KEY_F9: return PL_KEY_F9; - case GLFW_KEY_F10: return PL_KEY_F10; - case GLFW_KEY_F11: return PL_KEY_F11; - case GLFW_KEY_F12: return PL_KEY_F12; - case GLFW_KEY_F13: return PL_KEY_F13; - case GLFW_KEY_F14: return PL_KEY_F14; - case GLFW_KEY_F15: return PL_KEY_F15; - case GLFW_KEY_F16: return PL_KEY_F16; - case GLFW_KEY_F17: return PL_KEY_F17; - case GLFW_KEY_F18: return PL_KEY_F18; - case GLFW_KEY_F19: return PL_KEY_F19; - case GLFW_KEY_F20: return PL_KEY_F20; - case GLFW_KEY_F21: return PL_KEY_F21; - case GLFW_KEY_F22: return PL_KEY_F22; - case GLFW_KEY_F23: return PL_KEY_F23; - case GLFW_KEY_F24: return PL_KEY_F24; - default: return PL_KEY_NONE; - } -} - -void -pl_glfw_error_callback(int error, const char* description) -{ - fprintf(stderr, "GLFW Error %d: %s\n", error, description); -} //----------------------------------------------------------------------------- // [SECTION] thread api @@ -956,9 +216,6 @@ pl_has_library_changed(plSharedLibrary* ptLibrary) } -extern "C" const plWindowI* ptWindows2; -// const plDataRegistryI* gptDataRegistry = nullptr; - extern "C" const plApiRegistryI* pl__python_load(void) { @@ -974,7 +231,6 @@ pl__python_setup(void) // pl_load_ext((plApiRegistryI*)ptApiRegistry, false); // pl_load_platform_ext((plApiRegistryI*)ptApiRegistry, false); - ptWindows2 = pl_get_api_latest(ptApiRegistry, plWindowI); gptDataRegistry = pl_get_api_latest(ptApiRegistry, plDataRegistryI); @@ -992,127 +248,11 @@ pl__python_setup(void) // command line args gptIOCtx->iArgc = 0; gptIOCtx->apArgv = NULL; - - glfwSetErrorCallback(pl_glfw_error_callback); - - // setup glfw - if (!glfwInit()) - return; - - atMouseCursors[PL_MOUSE_CURSOR_ARROW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); - atMouseCursors[PL_MOUSE_CURSOR_TEXT_INPUT] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR); - atMouseCursors[PL_MOUSE_CURSOR_RESIZE_NS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR); - atMouseCursors[PL_MOUSE_CURSOR_RESIZE_EW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR); - atMouseCursors[PL_MOUSE_CURSOR_HAND] = glfwCreateStandardCursor(GLFW_HAND_CURSOR); - atMouseCursors[PL_MOUSE_CURSOR_RESIZE_ALL] = glfwCreateStandardCursor(GLFW_RESIZE_ALL_CURSOR); - atMouseCursors[PL_MOUSE_CURSOR_RESIZE_NESW] = glfwCreateStandardCursor(GLFW_RESIZE_NESW_CURSOR); - atMouseCursors[PL_MOUSE_CURSOR_RESIZE_NWSE] = glfwCreateStandardCursor(GLFW_RESIZE_NWSE_CURSOR); - atMouseCursors[PL_MOUSE_CURSOR_NOT_ALLOWED] = glfwCreateStandardCursor(GLFW_NOT_ALLOWED_CURSOR); - - // set clipboard functions (may need to move this to OS api) - gptIOCtx->set_clipboard_text_fn = [](void* pUnused, const char* text) { glfwSetClipboardString(nullptr, text); }; - gptIOCtx->get_clipboard_text_fn = [](void* pUnused) { return glfwGetClipboardString(nullptr); }; - - // Setup Dear ImGui context - IMGUI_CHECKVERSION(); - ImGuiContext* ptImGuiCtx = ImGui::CreateContext(); - - ImGuiMemAllocFunc p_alloc_func = nullptr; - ImGuiMemFreeFunc p_free_func = nullptr; - void* p_user_data = nullptr; - ImGui::GetAllocatorFunctions(&p_alloc_func, &p_free_func, &p_user_data); - - gptDataRegistry->set_data("imgui", ptImGuiCtx); - gptDataRegistry->set_data("imgui allocate", (void*)p_alloc_func); - gptDataRegistry->set_data("imgui free", (void*)p_free_func); - ImGuiIO& io = ImGui::GetIO(); (void)io; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking - io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows - //io.ConfigViewportsNoAutoMerge = true; - //io.ConfigViewportsNoTaskBarIcon = true; - - // Setup Dear ImGui style - ImGui::StyleColorsDark(); - - // setup pilot light style - ImVec4* colors = ImGui::GetStyle().Colors; - colors[ImGuiCol_Button] = ImVec4(0.51f, 0.02f, 0.10f, 1.00f); - colors[ImGuiCol_ButtonHovered] = ImVec4(0.61f, 0.02f, 0.10f, 1.00f); - colors[ImGuiCol_ButtonActive] = ImVec4(0.87f, 0.02f, 0.10f, 1.00f); - colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); - colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); - colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); - colors[ImGuiCol_ChildBg] = ImVec4(0.25f, 0.10f, 0.10f, 0.78f); - colors[ImGuiCol_PopupBg] = ImVec4(0.15f, 0.10f, 0.10f, 0.78f); - colors[ImGuiCol_Border] = ImVec4(0.33f, 0.02f, 0.10f, 1.00f); - colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); - colors[ImGuiCol_FrameBg] = ImVec4(0.23f, 0.02f, 0.10f, 1.00f); - colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f); - colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f); - colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); - colors[ImGuiCol_TitleBgActive] = ImVec4(0.33f, 0.02f, 0.10f, 1.00f); - colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); - colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f); - colors[ImGuiCol_ScrollbarBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.85f); - colors[ImGuiCol_ScrollbarGrab] = colors[ImGuiCol_Button]; - colors[ImGuiCol_ScrollbarGrabHovered] = colors[ImGuiCol_ButtonHovered]; - colors[ImGuiCol_ScrollbarGrabActive] = colors[ImGuiCol_ButtonActive]; - colors[ImGuiCol_CheckMark] = colors[ImGuiCol_ButtonActive]; - colors[ImGuiCol_SliderGrab] = colors[ImGuiCol_Button]; - colors[ImGuiCol_SliderGrabActive] = colors[ImGuiCol_ButtonActive]; - colors[ImGuiCol_Header] = colors[ImGuiCol_Button]; - colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f); - colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); - colors[ImGuiCol_Separator] = colors[ImGuiCol_Border]; - colors[ImGuiCol_SeparatorHovered] = ImVec4(0.10f, 0.40f, 0.75f, 0.78f); - colors[ImGuiCol_SeparatorActive] = ImVec4(0.10f, 0.40f, 0.75f, 1.00f); - colors[ImGuiCol_ResizeGrip] = ImVec4(0.98f, 0.59f, 0.26f, 0.20f); - colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.98f, 0.59f, 0.26f, 0.67f); - colors[ImGuiCol_ResizeGripActive] = ImVec4(0.98f, 0.59f, 0.26f, 0.95f); - colors[ImGuiCol_InputTextCursor] = colors[ImGuiCol_Text]; - colors[ImGuiCol_TabHovered] = colors[ImGuiCol_HeaderHovered]; - colors[ImGuiCol_Tab] = ImLerp(colors[ImGuiCol_Header], colors[ImGuiCol_TitleBgActive], 0.80f); - colors[ImGuiCol_TabSelected] = ImLerp(colors[ImGuiCol_HeaderActive], colors[ImGuiCol_TitleBgActive], 0.60f); - colors[ImGuiCol_TabSelectedOverline] = colors[ImGuiCol_HeaderActive]; - colors[ImGuiCol_TabDimmed] = ImLerp(colors[ImGuiCol_Tab], colors[ImGuiCol_TitleBg], 0.80f); - colors[ImGuiCol_TabDimmedSelected] = ImLerp(colors[ImGuiCol_TabSelected], colors[ImGuiCol_TitleBg], 0.40f); - colors[ImGuiCol_TabDimmedSelectedOverline] = ImVec4(0.50f, 0.50f, 0.50f, 0.00f); - colors[ImGuiCol_DockingPreview] = ImVec4(0.51f, 0.02f, 0.10f, 0.7f); - colors[ImGuiCol_DockingEmptyBg] = ImVec4(0.20f, 0.20f, 0.20f, 1.00f); - colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); - colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); - colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); - colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); - colors[ImGuiCol_TableHeaderBg] = ImVec4(0.19f, 0.19f, 0.20f, 1.00f); - colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.35f, 1.00f); // Prefer using Alpha=1.0 here - colors[ImGuiCol_TableBorderLight] = ImVec4(0.23f, 0.23f, 0.25f, 1.00f); // Prefer using Alpha=1.0 here - colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); - colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f); - colors[ImGuiCol_TextLink] = colors[ImGuiCol_HeaderActive]; - colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f); - colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); - colors[ImGuiCol_NavCursor] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f); - colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); - colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); - colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); - - // when viewports are enabled we tweak WindowRounding/WindowBg so - // platform windows can look identical to regular ones. - ImGuiStyle& style = ImGui::GetStyle(); - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - { - style.WindowRounding = 0.0f; - style.Colors[ImGuiCol_WindowBg].w = 1.0f; - } } extern "C" void pl__python_shutdown(void) { - ImGui_ImplGlfw_Shutdown(); - ImGui::DestroyContext(); } extern "C" int @@ -1122,7 +262,24 @@ pl__python_pre_update(void) @autoreleasepool #endif { - glfwPollEvents(); + // while queue has messages, remove and dispatch them (but do not block on empty queue) + MSG tMsg = {0}; + while (PeekMessage(&tMsg, NULL, 0, 0, PM_REMOVE)) + { + // check for quit because peekmessage does not signal this via return val + if (tMsg.message == WM_QUIT) + { + gptIOCtx->bRunning = false; + break; + } + // TranslateMessage will post auxilliary WM_CHAR messages from key msgs + TranslateMessage(&tMsg); + DispatchMessage(&tMsg); + } + + if(gptIOCtx->platform_new_frame) + gptIOCtx->platform_new_frame(gptIOCtx->pBackendPlatformData); + pl__garbage_collect_data_reg(); // update time step @@ -1142,95 +299,6 @@ pl__python_pre_update(void) gptIOCtx->fDeltaTime = (float)(dCurrentTime - gdTime); gdTime = dCurrentTime; #endif - - // start imgui glfw frame - ImGui_ImplGlfw_NewFrame(); - - // update mouse cursor - plMouseCursor tCursor0 = gptIOCtx->tNextCursor; - if(tCursor0 != PL_MOUSE_CURSOR_ARROW) - { - glfwSetCursor(gptGlfwWindow, atMouseCursors[tCursor0] ? atMouseCursors[tCursor0] : atMouseCursors[PL_MOUSE_CURSOR_ARROW]); - glfwSetInputMode(gptGlfwWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); - } - gptIOCtx->tNextCursor = PL_MOUSE_CURSOR_ARROW; - gptIOCtx->bCursorChanged = false; - - // handle retina displays - #ifdef __APPLE__ - - float fCurrentScale = nswin.screen.backingScaleFactor; - layer.contentsScale = fCurrentScale; - - int width, height; - glfwGetFramebufferSize(gptGlfwWindow, &width, &height); - layer.drawableSize = CGSizeMake(width, height); - gptIOCtx->pBackendPlatformData = layer; - - // Setup display size (every frame to accommodate for window resizing) - int w, h; - glfwGetWindowSize(gptGlfwWindow, &w, &h); - - if (w > 0 && h > 0) - { - bool bResize = false; - - if(gptIOCtx->bViewportSizeChanged) - bResize = true; - - if(w != gptIOCtx->tMainViewportSize.x || h != gptIOCtx->tMainViewportSize.y) - bResize = true; - else if(fCurrentScale != gptIOCtx->tMainFramebufferScale.x || fCurrentScale != gptIOCtx->tMainFramebufferScale.y ) - bResize = true; - - if(bResize) - { - gptIOCtx->tMainViewportSize.x = w; - gptIOCtx->tMainViewportSize.y = h; - gptIOCtx->tMainFramebufferScale.x = fCurrentScale; - gptIOCtx->tMainFramebufferScale.y = fCurrentScale; - return 1; - } - } - #else - - int width, height; - glfwGetFramebufferSize(gptGlfwWindow, &width, &height); - - // Setup display size (every frame to accommodate for window resizing) - int w, h; - glfwGetWindowSize(gptGlfwWindow, &w, &h); - - if (w > 0 && h > 0) - { - bool bResize = false; - - if(gptIOCtx->bViewportSizeChanged) - bResize = true; - - if((float)w != gptIOCtx->tMainViewportSize.x || (float)h != gptIOCtx->tMainViewportSize.y) - bResize = true; - - if(bResize) - { - gptIOCtx->tMainViewportSize.x = (float)w; - gptIOCtx->tMainViewportSize.y = (float)h; - gptIOCtx->tMainFramebufferScale.x = 1.0f; - gptIOCtx->tMainFramebufferScale.y = 1.0f; - return 1; - - } - } - #endif - - if (glfwGetWindowAttrib(gptGlfwWindow, GLFW_ICONIFIED) != 0) - { - #ifdef _WIN32 - Sleep(10); - #else - usleep(10 * 1000); - #endif - } } return 0; } @@ -1238,5 +306,3 @@ pl__python_pre_update(void) #define PL_STRING_IMPLEMENTATION #include "pl_string.h" #undef PL_STRING_IMPLEMENTATION - -#include "imgui_impl_glfw.cpp"