ApplicationWindowLinux.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Window/ApplicationWindowLinux.h>
  6. #include <Utils/Log.h>
  7. void ApplicationWindowLinux::Initialize()
  8. {
  9. // Open connection to X server
  10. mDisplay = XOpenDisplay(nullptr);
  11. if (!mDisplay)
  12. FatalError("Failed to open X display");
  13. // Create a simple window
  14. int screen = DefaultScreen(mDisplay);
  15. mWindow = XCreateSimpleWindow(mDisplay, RootWindow(mDisplay, screen), 0, 0, mWindowWidth, mWindowHeight, 1, BlackPixel(mDisplay, screen), WhitePixel(mDisplay, screen));
  16. // Select input events
  17. XSelectInput(mDisplay, mWindow, ExposureMask | StructureNotifyMask | KeyPressMask);
  18. // Set window title
  19. XStoreName(mDisplay, mWindow, "TestFramework");
  20. // Register WM_DELETE_WINDOW to handle the close button
  21. mWmDeleteWindow = XInternAtom(mDisplay, "WM_DELETE_WINDOW", false);
  22. XSetWMProtocols(mDisplay, mWindow, &mWmDeleteWindow, 1);
  23. // Map the window (make it visible)
  24. XMapWindow(mDisplay, mWindow);
  25. // Flush the display to ensure commands are executed
  26. XFlush(mDisplay);
  27. }
  28. void ApplicationWindowLinux::MainLoop(RenderCallback inRenderCallback)
  29. {
  30. for (;;)
  31. {
  32. while (XPending(mDisplay) > 0)
  33. {
  34. XEvent event;
  35. XNextEvent(mDisplay, &event);
  36. if (event.type == ClientMessage && static_cast<Atom>(event.xclient.data.l[0]) == mWmDeleteWindow)
  37. {
  38. // Handle quit events
  39. return;
  40. }
  41. else if (event.type == ConfigureNotify)
  42. {
  43. // Handle window resize events
  44. XConfigureEvent xce = event.xconfigure;
  45. if (xce.width != mWindowWidth || xce.height != mWindowHeight)
  46. OnWindowResized(xce.width, xce.height);
  47. }
  48. else
  49. mEventListener(event);
  50. }
  51. // Call the render callback
  52. if (!inRenderCallback())
  53. return;
  54. }
  55. }