entry.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef ENTRY_H_HEADER_GUARD
  6. #define ENTRY_H_HEADER_GUARD
  7. #include "dbg.h"
  8. #include <string.h> // memset
  9. #include <bx/bx.h>
  10. namespace bx { struct FileReaderI; struct FileWriterI; struct ReallocatorI; }
  11. extern "C" int _main_(int _argc, char** _argv);
  12. #define ENTRY_WINDOW_FLAG_NONE UINT32_C(0x00000000)
  13. #define ENTRY_WINDOW_FLAG_ASPECT_RATIO UINT32_C(0x00000001)
  14. #define ENTRY_WINDOW_FLAG_FRAME UINT32_C(0x00000002)
  15. #define ENTRY_IMPLEMENT_MAIN(_app) \
  16. int _main_(int _argc, char** _argv) \
  17. { \
  18. _app app; \
  19. return entry::runApp(&app, _argc, _argv); \
  20. }
  21. namespace entry
  22. {
  23. struct WindowHandle { uint16_t idx; };
  24. inline bool isValid(WindowHandle _handle) { return UINT16_MAX != _handle.idx; }
  25. struct GamepadHandle { uint16_t idx; };
  26. inline bool isValid(GamepadHandle _handle) { return UINT16_MAX != _handle.idx; }
  27. struct MouseButton
  28. {
  29. enum Enum
  30. {
  31. None,
  32. Left,
  33. Middle,
  34. Right,
  35. Count
  36. };
  37. };
  38. struct GamepadAxis
  39. {
  40. enum Enum
  41. {
  42. LeftX,
  43. LeftY,
  44. LeftZ,
  45. RightX,
  46. RightY,
  47. RightZ,
  48. Count
  49. };
  50. };
  51. struct Modifier
  52. { enum Enum
  53. {
  54. None = 0,
  55. LeftAlt = 0x01,
  56. RightAlt = 0x02,
  57. LeftCtrl = 0x04,
  58. RightCtrl = 0x08,
  59. LeftShift = 0x10,
  60. RightShift = 0x20,
  61. LeftMeta = 0x40,
  62. RightMeta = 0x80,
  63. };
  64. };
  65. struct Key
  66. {
  67. enum Enum
  68. {
  69. None = 0,
  70. Esc,
  71. Return,
  72. Tab,
  73. Space,
  74. Backspace,
  75. Up,
  76. Down,
  77. Left,
  78. Right,
  79. Insert,
  80. Delete,
  81. Home,
  82. End,
  83. PageUp,
  84. PageDown,
  85. Print,
  86. Plus,
  87. Minus,
  88. LeftBracket,
  89. RightBracket,
  90. Semicolon,
  91. Quote,
  92. Comma,
  93. Period,
  94. Slash,
  95. Backslash,
  96. Tilde,
  97. F1,
  98. F2,
  99. F3,
  100. F4,
  101. F5,
  102. F6,
  103. F7,
  104. F8,
  105. F9,
  106. F10,
  107. F11,
  108. F12,
  109. NumPad0,
  110. NumPad1,
  111. NumPad2,
  112. NumPad3,
  113. NumPad4,
  114. NumPad5,
  115. NumPad6,
  116. NumPad7,
  117. NumPad8,
  118. NumPad9,
  119. Key0,
  120. Key1,
  121. Key2,
  122. Key3,
  123. Key4,
  124. Key5,
  125. Key6,
  126. Key7,
  127. Key8,
  128. Key9,
  129. KeyA,
  130. KeyB,
  131. KeyC,
  132. KeyD,
  133. KeyE,
  134. KeyF,
  135. KeyG,
  136. KeyH,
  137. KeyI,
  138. KeyJ,
  139. KeyK,
  140. KeyL,
  141. KeyM,
  142. KeyN,
  143. KeyO,
  144. KeyP,
  145. KeyQ,
  146. KeyR,
  147. KeyS,
  148. KeyT,
  149. KeyU,
  150. KeyV,
  151. KeyW,
  152. KeyX,
  153. KeyY,
  154. KeyZ,
  155. GamepadA,
  156. GamepadB,
  157. GamepadX,
  158. GamepadY,
  159. GamepadThumbL,
  160. GamepadThumbR,
  161. GamepadShoulderL,
  162. GamepadShoulderR,
  163. GamepadUp,
  164. GamepadDown,
  165. GamepadLeft,
  166. GamepadRight,
  167. GamepadBack,
  168. GamepadStart,
  169. GamepadGuide,
  170. Count
  171. };
  172. };
  173. const char* getName(Key::Enum _key);
  174. struct MouseState
  175. {
  176. MouseState()
  177. : m_mx(0)
  178. , m_my(0)
  179. , m_mz(0)
  180. {
  181. for (uint32_t ii = 0; ii < entry::MouseButton::Count; ++ii)
  182. {
  183. m_buttons[ii] = entry::MouseButton::None;
  184. }
  185. }
  186. int32_t m_mx;
  187. int32_t m_my;
  188. int32_t m_mz;
  189. uint8_t m_buttons[entry::MouseButton::Count];
  190. };
  191. struct GamepadState
  192. {
  193. GamepadState()
  194. {
  195. memset(m_axis, 0, sizeof(m_axis) );
  196. }
  197. int32_t m_axis[entry::GamepadAxis::Count];
  198. };
  199. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse = NULL);
  200. bx::FileReaderI* getFileReader();
  201. bx::FileWriterI* getFileWriter();
  202. bx::ReallocatorI* getAllocator();
  203. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags = ENTRY_WINDOW_FLAG_NONE, const char* _title = "");
  204. void destroyWindow(WindowHandle _handle);
  205. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y);
  206. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height);
  207. void setWindowTitle(WindowHandle _handle, const char* _title);
  208. void toggleWindowFrame(WindowHandle _handle);
  209. void toggleFullscreen(WindowHandle _handle);
  210. void setMouseLock(WindowHandle _handle, bool _lock);
  211. struct WindowState
  212. {
  213. WindowState()
  214. : m_nwh(NULL)
  215. {
  216. m_handle.idx = UINT16_MAX;
  217. }
  218. WindowHandle m_handle;
  219. uint32_t m_width;
  220. uint32_t m_height;
  221. MouseState m_mouse;
  222. void* m_nwh;
  223. };
  224. bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset);
  225. struct BX_NO_VTABLE AppI
  226. {
  227. virtual ~AppI() = 0;
  228. virtual void init(int _argc, char** _argv) = 0;
  229. virtual int shutdown() = 0;
  230. virtual bool update() = 0;
  231. };
  232. inline AppI::~AppI()
  233. {
  234. }
  235. int runApp(AppI* _app, int _argc, char** _argv);
  236. } // namespace entry
  237. #endif // ENTRY_H_HEADER_GUARD