entry.h 5.8 KB

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