entry.h 5.9 KB

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