entry_p.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright 2011-2021 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #ifndef ENTRY_PRIVATE_H_HEADER_GUARD
  6. #define ENTRY_PRIVATE_H_HEADER_GUARD
  7. #define TINYSTL_ALLOCATOR entry::TinyStlAllocator
  8. #include <bx/spscqueue.h>
  9. #include <bx/filepath.h>
  10. #include "entry.h"
  11. #ifndef ENTRY_CONFIG_USE_NOOP
  12. # define ENTRY_CONFIG_USE_NOOP 0
  13. #endif // ENTRY_CONFIG_USE_NOOP
  14. #ifndef ENTRY_CONFIG_USE_SDL
  15. # define ENTRY_CONFIG_USE_SDL 0
  16. #endif // ENTRY_CONFIG_USE_SDL
  17. #ifndef ENTRY_CONFIG_USE_GLFW
  18. # define ENTRY_CONFIG_USE_GLFW 0
  19. #endif // ENTRY_CONFIG_USE_GLFW
  20. #ifndef ENTRY_CONFIG_USE_WAYLAND
  21. # define ENTRY_CONFIG_USE_WAYLAND 0
  22. #endif // ENTRY_CONFIG_USE_WAYLAND
  23. #if !defined(ENTRY_CONFIG_USE_NATIVE) \
  24. && !ENTRY_CONFIG_USE_NOOP \
  25. && !ENTRY_CONFIG_USE_SDL \
  26. && !ENTRY_CONFIG_USE_GLFW
  27. # define ENTRY_CONFIG_USE_NATIVE 1
  28. #else
  29. # define ENTRY_CONFIG_USE_NATIVE 0
  30. #endif // ...
  31. #ifndef ENTRY_CONFIG_MAX_WINDOWS
  32. # define ENTRY_CONFIG_MAX_WINDOWS 8
  33. #endif // ENTRY_CONFIG_MAX_WINDOWS
  34. #ifndef ENTRY_CONFIG_MAX_GAMEPADS
  35. # define ENTRY_CONFIG_MAX_GAMEPADS 4
  36. #endif // ENTRY_CONFIG_MAX_GAMEPADS
  37. #if !defined(ENTRY_DEFAULT_WIDTH) && !defined(ENTRY_DEFAULT_HEIGHT)
  38. # define ENTRY_DEFAULT_WIDTH 1280
  39. # define ENTRY_DEFAULT_HEIGHT 720
  40. #elif !defined(ENTRY_DEFAULT_WIDTH) || !defined(ENTRY_DEFAULT_HEIGHT)
  41. # error "Both ENTRY_DEFAULT_WIDTH and ENTRY_DEFAULT_HEIGHT must be defined."
  42. #endif // ENTRY_DEFAULT_WIDTH
  43. #ifndef ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
  44. # define ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR 1
  45. #endif // ENTRY_CONFIG_IMPLEMENT_DEFAULT_ALLOCATOR
  46. #ifndef ENTRY_CONFIG_PROFILER
  47. # define ENTRY_CONFIG_PROFILER 0
  48. #endif // ENTRY_CONFIG_PROFILER
  49. #define ENTRY_IMPLEMENT_EVENT(_class, _type) \
  50. _class(WindowHandle _handle) : Event(_type, _handle) {}
  51. namespace entry
  52. {
  53. struct TinyStlAllocator
  54. {
  55. static void* static_allocate(size_t _bytes);
  56. static void static_deallocate(void* _ptr, size_t /*_bytes*/);
  57. };
  58. int main(int _argc, const char* const* _argv);
  59. char keyToAscii(Key::Enum _key, uint8_t _modifiers);
  60. struct Event
  61. {
  62. enum Enum
  63. {
  64. Axis,
  65. Char,
  66. Exit,
  67. Gamepad,
  68. Key,
  69. Mouse,
  70. Size,
  71. Window,
  72. Suspend,
  73. DropFile,
  74. };
  75. Event(Enum _type)
  76. : m_type(_type)
  77. {
  78. m_handle.idx = UINT16_MAX;
  79. }
  80. Event(Enum _type, WindowHandle _handle)
  81. : m_type(_type)
  82. , m_handle(_handle)
  83. {
  84. }
  85. Event::Enum m_type;
  86. WindowHandle m_handle;
  87. };
  88. struct AxisEvent : public Event
  89. {
  90. ENTRY_IMPLEMENT_EVENT(AxisEvent, Event::Axis);
  91. GamepadAxis::Enum m_axis;
  92. int32_t m_value;
  93. GamepadHandle m_gamepad;
  94. };
  95. struct CharEvent : public Event
  96. {
  97. ENTRY_IMPLEMENT_EVENT(CharEvent, Event::Char);
  98. uint8_t m_len;
  99. uint8_t m_char[4];
  100. };
  101. struct GamepadEvent : public Event
  102. {
  103. ENTRY_IMPLEMENT_EVENT(GamepadEvent, Event::Gamepad);
  104. GamepadHandle m_gamepad;
  105. bool m_connected;
  106. };
  107. struct KeyEvent : public Event
  108. {
  109. ENTRY_IMPLEMENT_EVENT(KeyEvent, Event::Key);
  110. Key::Enum m_key;
  111. uint8_t m_modifiers;
  112. bool m_down;
  113. };
  114. struct MouseEvent : public Event
  115. {
  116. ENTRY_IMPLEMENT_EVENT(MouseEvent, Event::Mouse);
  117. int32_t m_mx;
  118. int32_t m_my;
  119. int32_t m_mz;
  120. MouseButton::Enum m_button;
  121. bool m_down;
  122. bool m_move;
  123. };
  124. struct SizeEvent : public Event
  125. {
  126. ENTRY_IMPLEMENT_EVENT(SizeEvent, Event::Size);
  127. uint32_t m_width;
  128. uint32_t m_height;
  129. };
  130. struct WindowEvent : public Event
  131. {
  132. ENTRY_IMPLEMENT_EVENT(WindowEvent, Event::Window);
  133. void* m_nwh;
  134. };
  135. struct SuspendEvent : public Event
  136. {
  137. ENTRY_IMPLEMENT_EVENT(SuspendEvent, Event::Suspend);
  138. Suspend::Enum m_state;
  139. };
  140. struct DropFileEvent : public Event
  141. {
  142. ENTRY_IMPLEMENT_EVENT(DropFileEvent, Event::DropFile);
  143. bx::FilePath m_filePath;
  144. };
  145. const Event* poll();
  146. const Event* poll(WindowHandle _handle);
  147. void release(const Event* _event);
  148. class EventQueue
  149. {
  150. public:
  151. EventQueue()
  152. : m_queue(getAllocator() )
  153. {
  154. }
  155. ~EventQueue()
  156. {
  157. for (const Event* ev = poll(); NULL != ev; ev = poll() )
  158. {
  159. release(ev);
  160. }
  161. }
  162. void postAxisEvent(WindowHandle _handle, GamepadHandle _gamepad, GamepadAxis::Enum _axis, int32_t _value)
  163. {
  164. AxisEvent* ev = BX_NEW(getAllocator(), AxisEvent)(_handle);
  165. ev->m_gamepad = _gamepad;
  166. ev->m_axis = _axis;
  167. ev->m_value = _value;
  168. m_queue.push(ev);
  169. }
  170. void postCharEvent(WindowHandle _handle, uint8_t _len, const uint8_t _char[4])
  171. {
  172. CharEvent* ev = BX_NEW(getAllocator(), CharEvent)(_handle);
  173. ev->m_len = _len;
  174. bx::memCopy(ev->m_char, _char, 4);
  175. m_queue.push(ev);
  176. }
  177. void postExitEvent()
  178. {
  179. Event* ev = BX_NEW(getAllocator(), Event)(Event::Exit);
  180. m_queue.push(ev);
  181. }
  182. void postGamepadEvent(WindowHandle _handle, GamepadHandle _gamepad, bool _connected)
  183. {
  184. GamepadEvent* ev = BX_NEW(getAllocator(), GamepadEvent)(_handle);
  185. ev->m_gamepad = _gamepad;
  186. ev->m_connected = _connected;
  187. m_queue.push(ev);
  188. }
  189. void postKeyEvent(WindowHandle _handle, Key::Enum _key, uint8_t _modifiers, bool _down)
  190. {
  191. KeyEvent* ev = BX_NEW(getAllocator(), KeyEvent)(_handle);
  192. ev->m_key = _key;
  193. ev->m_modifiers = _modifiers;
  194. ev->m_down = _down;
  195. m_queue.push(ev);
  196. }
  197. void postMouseEvent(WindowHandle _handle, int32_t _mx, int32_t _my, int32_t _mz)
  198. {
  199. MouseEvent* ev = BX_NEW(getAllocator(), MouseEvent)(_handle);
  200. ev->m_mx = _mx;
  201. ev->m_my = _my;
  202. ev->m_mz = _mz;
  203. ev->m_button = MouseButton::None;
  204. ev->m_down = false;
  205. ev->m_move = true;
  206. m_queue.push(ev);
  207. }
  208. void postMouseEvent(WindowHandle _handle, int32_t _mx, int32_t _my, int32_t _mz, MouseButton::Enum _button, bool _down)
  209. {
  210. MouseEvent* ev = BX_NEW(getAllocator(), MouseEvent)(_handle);
  211. ev->m_mx = _mx;
  212. ev->m_my = _my;
  213. ev->m_mz = _mz;
  214. ev->m_button = _button;
  215. ev->m_down = _down;
  216. ev->m_move = false;
  217. m_queue.push(ev);
  218. }
  219. void postSizeEvent(WindowHandle _handle, uint32_t _width, uint32_t _height)
  220. {
  221. SizeEvent* ev = BX_NEW(getAllocator(), SizeEvent)(_handle);
  222. ev->m_width = _width;
  223. ev->m_height = _height;
  224. m_queue.push(ev);
  225. }
  226. void postWindowEvent(WindowHandle _handle, void* _nwh = NULL)
  227. {
  228. WindowEvent* ev = BX_NEW(getAllocator(), WindowEvent)(_handle);
  229. ev->m_nwh = _nwh;
  230. m_queue.push(ev);
  231. }
  232. void postSuspendEvent(WindowHandle _handle, Suspend::Enum _state)
  233. {
  234. SuspendEvent* ev = BX_NEW(getAllocator(), SuspendEvent)(_handle);
  235. ev->m_state = _state;
  236. m_queue.push(ev);
  237. }
  238. void postDropFileEvent(WindowHandle _handle, const bx::FilePath& _filePath)
  239. {
  240. DropFileEvent* ev = BX_NEW(getAllocator(), DropFileEvent)(_handle);
  241. ev->m_filePath = _filePath;
  242. m_queue.push(ev);
  243. }
  244. const Event* poll()
  245. {
  246. return m_queue.pop();
  247. }
  248. const Event* poll(WindowHandle _handle)
  249. {
  250. if (isValid(_handle) )
  251. {
  252. Event* ev = m_queue.peek();
  253. if (NULL == ev
  254. || ev->m_handle.idx != _handle.idx)
  255. {
  256. return NULL;
  257. }
  258. }
  259. return poll();
  260. }
  261. void release(const Event* _event) const
  262. {
  263. BX_DELETE(getAllocator(), const_cast<Event*>(_event) );
  264. }
  265. private:
  266. bx::SpScUnboundedQueueT<Event> m_queue;
  267. };
  268. } // namespace entry
  269. #endif // ENTRY_PRIVATE_H_HEADER_GUARD