entry.h 5.3 KB

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