entry.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #ifndef ENTRY_H_HEADER_GUARD
  6. #define ENTRY_H_HEADER_GUARD
  7. #include "dbg.h"
  8. #include <bx/bx.h>
  9. #include <bx/string.h>
  10. namespace bx { struct FileReaderI; struct FileWriterI; struct AllocatorI; }
  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. {
  53. enum Enum
  54. {
  55. None = 0,
  56. LeftAlt = 0x01,
  57. RightAlt = 0x02,
  58. LeftCtrl = 0x04,
  59. RightCtrl = 0x08,
  60. LeftShift = 0x10,
  61. RightShift = 0x20,
  62. LeftMeta = 0x40,
  63. RightMeta = 0x80,
  64. };
  65. };
  66. struct Key
  67. {
  68. enum Enum
  69. {
  70. None = 0,
  71. Esc,
  72. Return,
  73. Tab,
  74. Space,
  75. Backspace,
  76. Up,
  77. Down,
  78. Left,
  79. Right,
  80. Insert,
  81. Delete,
  82. Home,
  83. End,
  84. PageUp,
  85. PageDown,
  86. Print,
  87. Plus,
  88. Minus,
  89. LeftBracket,
  90. RightBracket,
  91. Semicolon,
  92. Quote,
  93. Comma,
  94. Period,
  95. Slash,
  96. Backslash,
  97. Tilde,
  98. F1,
  99. F2,
  100. F3,
  101. F4,
  102. F5,
  103. F6,
  104. F7,
  105. F8,
  106. F9,
  107. F10,
  108. F11,
  109. F12,
  110. NumPad0,
  111. NumPad1,
  112. NumPad2,
  113. NumPad3,
  114. NumPad4,
  115. NumPad5,
  116. NumPad6,
  117. NumPad7,
  118. NumPad8,
  119. NumPad9,
  120. Key0,
  121. Key1,
  122. Key2,
  123. Key3,
  124. Key4,
  125. Key5,
  126. Key6,
  127. Key7,
  128. Key8,
  129. Key9,
  130. KeyA,
  131. KeyB,
  132. KeyC,
  133. KeyD,
  134. KeyE,
  135. KeyF,
  136. KeyG,
  137. KeyH,
  138. KeyI,
  139. KeyJ,
  140. KeyK,
  141. KeyL,
  142. KeyM,
  143. KeyN,
  144. KeyO,
  145. KeyP,
  146. KeyQ,
  147. KeyR,
  148. KeyS,
  149. KeyT,
  150. KeyU,
  151. KeyV,
  152. KeyW,
  153. KeyX,
  154. KeyY,
  155. KeyZ,
  156. GamepadA,
  157. GamepadB,
  158. GamepadX,
  159. GamepadY,
  160. GamepadThumbL,
  161. GamepadThumbR,
  162. GamepadShoulderL,
  163. GamepadShoulderR,
  164. GamepadUp,
  165. GamepadDown,
  166. GamepadLeft,
  167. GamepadRight,
  168. GamepadBack,
  169. GamepadStart,
  170. GamepadGuide,
  171. Count
  172. };
  173. };
  174. struct Suspend
  175. {
  176. enum Enum
  177. {
  178. WillSuspend,
  179. DidSuspend,
  180. WillResume,
  181. DidResume,
  182. Count
  183. };
  184. };
  185. const char* getName(Key::Enum _key);
  186. struct MouseState
  187. {
  188. MouseState()
  189. : m_mx(0)
  190. , m_my(0)
  191. , m_mz(0)
  192. {
  193. for (uint32_t ii = 0; ii < entry::MouseButton::Count; ++ii)
  194. {
  195. m_buttons[ii] = entry::MouseButton::None;
  196. }
  197. }
  198. int32_t m_mx;
  199. int32_t m_my;
  200. int32_t m_mz;
  201. uint8_t m_buttons[entry::MouseButton::Count];
  202. };
  203. struct GamepadState
  204. {
  205. GamepadState()
  206. {
  207. bx::memSet(m_axis, 0, sizeof(m_axis) );
  208. }
  209. int32_t m_axis[entry::GamepadAxis::Count];
  210. };
  211. bool processEvents(uint32_t& _width, uint32_t& _height, uint32_t& _debug, uint32_t& _reset, MouseState* _mouse = NULL);
  212. bx::FileReaderI* getFileReader();
  213. bx::FileWriterI* getFileWriter();
  214. bx::AllocatorI* getAllocator();
  215. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags = ENTRY_WINDOW_FLAG_NONE, const char* _title = "");
  216. void destroyWindow(WindowHandle _handle);
  217. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y);
  218. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height);
  219. void setWindowTitle(WindowHandle _handle, const char* _title);
  220. void toggleWindowFrame(WindowHandle _handle);
  221. void toggleFullscreen(WindowHandle _handle);
  222. void setMouseLock(WindowHandle _handle, bool _lock);
  223. void setCurrentDir(const char* _dir);
  224. struct WindowState
  225. {
  226. WindowState()
  227. : m_width(0)
  228. , m_height(0)
  229. , m_nwh(NULL)
  230. {
  231. m_handle.idx = UINT16_MAX;
  232. }
  233. WindowHandle m_handle;
  234. uint32_t m_width;
  235. uint32_t m_height;
  236. MouseState m_mouse;
  237. void* m_nwh;
  238. };
  239. bool processWindowEvents(WindowState& _state, uint32_t& _debug, uint32_t& _reset);
  240. struct BX_NO_VTABLE AppI
  241. {
  242. virtual ~AppI() = 0;
  243. virtual void init(int _argc, char** _argv) = 0;
  244. virtual int shutdown() = 0;
  245. virtual bool update() = 0;
  246. };
  247. inline AppI::~AppI()
  248. {
  249. }
  250. class App : public AppI
  251. {
  252. public:
  253. App(const char* _name);
  254. virtual ~App();
  255. const char* getName() const
  256. {
  257. return m_name;
  258. }
  259. AppI* getNext()
  260. {
  261. return m_next;
  262. }
  263. private:
  264. const char* m_name;
  265. App* m_next;
  266. };
  267. ///
  268. App* getFirstApp();
  269. ///
  270. int runApp(AppI* _app, int _argc, char** _argv);
  271. } // namespace entry
  272. #endif // ENTRY_H_HEADER_GUARD