entry.h 4.8 KB

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