entry_p.h 6.9 KB

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