entry.h 5.5 KB

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