entry_glfw.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "entry_p.h"
  6. #if ENTRY_CONFIG_USE_GLFW
  7. #if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  8. # define GLFW_EXPOSE_NATIVE_X11
  9. # define GLFW_EXPOSE_NATIVE_GLX
  10. #elif BX_PLATFORM_OSX
  11. # define GLFW_EXPOSE_NATIVE_COCOA
  12. # define GLFW_EXPOSE_NATIVE_NSGL
  13. #elif BX_PLATFORM_WINDOWS
  14. # define GLFW_EXPOSE_NATIVE_WIN32
  15. # define GLFW_EXPOSE_NATIVE_WGL
  16. #endif //
  17. #define GLFW_DLL
  18. #include <GLFW/glfw3.h>
  19. #include <GLFW/glfw3native.h>
  20. #include <bgfx/bgfxplatform.h>
  21. #include "dbg.h"
  22. // This is just trivial implementation of GLFW3 integration.
  23. // It's here just for testing purpose.
  24. namespace entry
  25. {
  26. inline void glfwSetWindow(GLFWwindow* _window)
  27. {
  28. bgfx::PlatformData pd;
  29. # if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  30. pd.ndt = glfwGetX11Display();
  31. pd.nwh = (void*)(uintptr_t)glfwGetGLXWindow(_window);
  32. pd.context = glfwGetGLXContext(_window);
  33. # elif BX_PLATFORM_OSX
  34. pd.ndt = NULL;
  35. pd.nwh = glfwGetCocoaWindow(_window);
  36. pd.context = glfwGetNSGLContext(_window);
  37. # elif BX_PLATFORM_WINDOWS
  38. pd.ndt = NULL;
  39. pd.nwh = glfwGetWin32Window(_window);
  40. pd.context = NULL;
  41. # endif // BX_PLATFORM_WINDOWS
  42. pd.backBuffer = NULL;
  43. pd.backBufferDS = NULL;
  44. bgfx::setPlatformData(pd);
  45. }
  46. static void errorCb(int _error, const char* _description)
  47. {
  48. DBG("GLFW error %d: %s", _error, _description);
  49. }
  50. struct Context
  51. {
  52. Context()
  53. {
  54. }
  55. int run(int _argc, char** _argv)
  56. {
  57. glfwSetErrorCallback(errorCb);
  58. glfwInit();
  59. m_window = glfwCreateWindow(1280, 720, "bgfx", NULL, NULL);
  60. glfwMakeContextCurrent(m_window);
  61. glfwSetKeyCallback(m_window, keyCb);
  62. glfwSetWindow(m_window);
  63. int result = main(_argc, _argv);
  64. glfwDestroyWindow(m_window);
  65. glfwTerminate();
  66. return result;
  67. }
  68. static void keyCb(GLFWwindow* _window, int _key, int _scancode, int _action, int _mods);
  69. EventQueue m_eventQueue;
  70. GLFWwindow* m_window;
  71. };
  72. Context s_ctx;
  73. void Context::keyCb(GLFWwindow* _window, int _key, int _scancode, int _action, int _mods)
  74. {
  75. BX_UNUSED(_window, _scancode, _mods);
  76. if (_key == GLFW_KEY_Q
  77. && _action == GLFW_PRESS
  78. && _mods == GLFW_MOD_CONTROL)
  79. {
  80. s_ctx.m_eventQueue.postExitEvent();
  81. }
  82. }
  83. const Event* poll()
  84. {
  85. glfwPollEvents();
  86. if (glfwWindowShouldClose(s_ctx.m_window) )
  87. {
  88. s_ctx.m_eventQueue.postExitEvent();
  89. }
  90. return s_ctx.m_eventQueue.poll();
  91. }
  92. const Event* poll(WindowHandle _handle)
  93. {
  94. return s_ctx.m_eventQueue.poll(_handle);
  95. }
  96. void release(const Event* _event)
  97. {
  98. s_ctx.m_eventQueue.release(_event);
  99. }
  100. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  101. {
  102. BX_UNUSED(_x, _y, _width, _height, _flags, _title);
  103. WindowHandle handle = { UINT16_MAX };
  104. return handle;
  105. }
  106. void destroyWindow(WindowHandle _handle)
  107. {
  108. BX_UNUSED(_handle);
  109. }
  110. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  111. {
  112. BX_UNUSED(_handle, _x, _y);
  113. }
  114. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  115. {
  116. BX_UNUSED(_handle, _width, _height);
  117. }
  118. void setWindowTitle(WindowHandle _handle, const char* _title)
  119. {
  120. BX_UNUSED(_handle, _title);
  121. }
  122. void toggleWindowFrame(WindowHandle _handle)
  123. {
  124. BX_UNUSED(_handle);
  125. }
  126. void toggleFullscreen(WindowHandle _handle)
  127. {
  128. BX_UNUSED(_handle);
  129. }
  130. void setMouseLock(WindowHandle _handle, bool _lock)
  131. {
  132. BX_UNUSED(_handle, _lock);
  133. }
  134. }
  135. int main(int _argc, char** _argv)
  136. {
  137. using namespace entry;
  138. return s_ctx.run(_argc, _argv);
  139. }
  140. #endif // ENTRY_CONFIG_USE_GLFW