entry_glfw.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "entry_p.h"
  6. #if ENTRY_CONFIG_USE_GLFW
  7. #define GLFW_DLL
  8. #include <GLFW/glfw3.h>
  9. #include <bgfx/bgfxplatform.h>
  10. #include "dbg.h"
  11. // This is just trivial implementation of GLFW3 integration.
  12. // It's here just for testing purpose.
  13. namespace entry
  14. {
  15. static void errorCb(int _error, const char* _description)
  16. {
  17. DBG("GLFW error %d: %s", _error, _description);
  18. }
  19. struct Context
  20. {
  21. Context()
  22. {
  23. }
  24. int run(int _argc, char** _argv)
  25. {
  26. glfwSetErrorCallback(errorCb);
  27. glfwInit();
  28. m_window = glfwCreateWindow(1280, 720, "bgfx", NULL, NULL);
  29. glfwMakeContextCurrent(m_window);
  30. glfwSetKeyCallback(m_window, keyCb);
  31. bgfx::glfwSetWindow(m_window);
  32. int result = main(_argc, _argv);
  33. glfwDestroyWindow(m_window);
  34. glfwTerminate();
  35. return result;
  36. }
  37. static void keyCb(GLFWwindow* _window, int _key, int _scancode, int _action, int _mods);
  38. EventQueue m_eventQueue;
  39. GLFWwindow* m_window;
  40. };
  41. Context s_ctx;
  42. void Context::keyCb(GLFWwindow* _window, int _key, int _scancode, int _action, int _mods)
  43. {
  44. BX_UNUSED(_window, _scancode, _mods);
  45. if (_key == GLFW_KEY_Q
  46. && _action == GLFW_PRESS
  47. && _mods == GLFW_MOD_CONTROL)
  48. {
  49. s_ctx.m_eventQueue.postExitEvent();
  50. }
  51. }
  52. const Event* poll()
  53. {
  54. glfwPollEvents();
  55. if (glfwWindowShouldClose(s_ctx.m_window) )
  56. {
  57. s_ctx.m_eventQueue.postExitEvent();
  58. }
  59. return s_ctx.m_eventQueue.poll();
  60. }
  61. const Event* poll(WindowHandle _handle)
  62. {
  63. return s_ctx.m_eventQueue.poll(_handle);
  64. }
  65. void release(const Event* _event)
  66. {
  67. s_ctx.m_eventQueue.release(_event);
  68. }
  69. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  70. {
  71. BX_UNUSED(_x, _y, _width, _height, _flags, _title);
  72. WindowHandle handle = { UINT16_MAX };
  73. return handle;
  74. }
  75. void destroyWindow(WindowHandle _handle)
  76. {
  77. BX_UNUSED(_handle);
  78. }
  79. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  80. {
  81. BX_UNUSED(_handle, _x, _y);
  82. }
  83. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  84. {
  85. BX_UNUSED(_handle, _width, _height);
  86. }
  87. void setWindowTitle(WindowHandle _handle, const char* _title)
  88. {
  89. BX_UNUSED(_handle, _title);
  90. }
  91. void toggleWindowFrame(WindowHandle _handle)
  92. {
  93. BX_UNUSED(_handle);
  94. }
  95. void toggleFullscreen(WindowHandle _handle)
  96. {
  97. BX_UNUSED(_handle);
  98. }
  99. void setMouseLock(WindowHandle _handle, bool _lock)
  100. {
  101. BX_UNUSED(_handle, _lock);
  102. }
  103. }
  104. int main(int _argc, char** _argv)
  105. {
  106. using namespace entry;
  107. return s_ctx.run(_argc, _argv);
  108. }
  109. #endif // ENTRY_CONFIG_USE_GLFW