SDL_main.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef SDL_main_h_
  19. #define SDL_main_h_
  20. #include <SDL3/SDL_stdinc.h>
  21. #include <SDL3/SDL_events.h>
  22. /*
  23. * For details on how SDL_main works, and how to use it, please refer to:
  24. *
  25. * https://wiki.libsdl.org/SDL3/README/main-functions
  26. *
  27. * (or docs/README-main-functions.md in the SDL source tree)
  28. */
  29. /**
  30. * \file SDL_main.h
  31. *
  32. * Redefine main() on some platforms so that it is called by SDL.
  33. */
  34. #ifndef SDL_MAIN_HANDLED
  35. #ifdef __WIN32__
  36. /* On Windows SDL provides WinMain(), which parses the command line and passes
  37. the arguments to your main function.
  38. If you provide your own WinMain(), you may define SDL_MAIN_HANDLED
  39. */
  40. #define SDL_MAIN_AVAILABLE
  41. #elif defined(__WINRT__)
  42. /* On WinRT, SDL provides a main function that initializes CoreApplication,
  43. creating an instance of IFrameworkView in the process.
  44. Ideally, #include'ing SDL_main.h is enough to get a main() function working.
  45. However, that requires the source file your main() is in to be compiled
  46. as C++ *and* with the /ZW compiler flag. If that's not feasible, add an
  47. otherwise empty .cpp file that only contains `#include <SDL3/SDL_main.h>`
  48. and build that with /ZW (still include SDL_main.h in your other file with main()!).
  49. In XAML apps, instead the function SDL_RunApp() must be called with a pointer
  50. to the Direct3D-hosted XAML control passed in as the "reserved" argument.
  51. */
  52. #define SDL_MAIN_NEEDED
  53. #elif defined(__GDK__)
  54. /* On GDK, SDL provides a main function that initializes the game runtime.
  55. If you prefer to write your own WinMain-function instead of having SDL
  56. provide one that calls your main() function,
  57. #define SDL_MAIN_HANDLED before #include'ing SDL_main.h
  58. and call the SDL_RunApp function from your entry point.
  59. */
  60. #define SDL_MAIN_NEEDED
  61. #elif defined(__IOS__)
  62. /* On iOS SDL provides a main function that creates an application delegate
  63. and starts the iOS application run loop.
  64. To use it, just #include SDL_main.h in the source file that contains your
  65. main() function.
  66. See src/video/uikit/SDL_uikitappdelegate.m for more details.
  67. */
  68. #define SDL_MAIN_NEEDED
  69. #elif defined(__ANDROID__)
  70. /* On Android SDL provides a Java class in SDLActivity.java that is the
  71. main activity entry point.
  72. See docs/README-android.md for more details on extending that class.
  73. */
  74. #define SDL_MAIN_NEEDED
  75. /* We need to export SDL_main so it can be launched from Java */
  76. #define SDLMAIN_DECLSPEC DECLSPEC
  77. #elif defined(__PSP__)
  78. /* On PSP SDL provides a main function that sets the module info,
  79. activates the GPU and starts the thread required to be able to exit
  80. the software.
  81. If you provide this yourself, you may define SDL_MAIN_HANDLED
  82. */
  83. #define SDL_MAIN_AVAILABLE
  84. #elif defined(__PS2__)
  85. #define SDL_MAIN_AVAILABLE
  86. #define SDL_PS2_SKIP_IOP_RESET() \
  87. void reset_IOP(); \
  88. void reset_IOP() {}
  89. #elif defined(__3DS__)
  90. /*
  91. On N3DS, SDL provides a main function that sets up the screens
  92. and storage.
  93. If you provide this yourself, you may define SDL_MAIN_HANDLED
  94. */
  95. #define SDL_MAIN_AVAILABLE
  96. #elif defined(__NGAGE__)
  97. /*
  98. TODO: not sure if it should be SDL_MAIN_NEEDED, in SDL2 ngage had a
  99. main implementation, but wasn't mentioned in SDL_main.h
  100. */
  101. #define SDL_MAIN_AVAILABLE
  102. #endif
  103. #endif /* SDL_MAIN_HANDLED */
  104. #ifndef SDLMAIN_DECLSPEC
  105. #define SDLMAIN_DECLSPEC
  106. #endif
  107. /**
  108. * \file SDL_main.h
  109. *
  110. * The application's main() function must be called with C linkage,
  111. * and should be declared like this:
  112. * \code
  113. * #ifdef __cplusplus
  114. * extern "C"
  115. * #endif
  116. * int main(int argc, char *argv[])
  117. * {
  118. * }
  119. * \endcode
  120. */
  121. #if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE) || defined(SDL_MAIN_USE_CALLBACKS)
  122. #define main SDL_main
  123. #endif
  124. #include <SDL3/SDL_begin_code.h>
  125. #ifdef __cplusplus
  126. extern "C" {
  127. #endif
  128. typedef int (SDLCALL *SDL_AppInit_func)(int argc, char *argv[]);
  129. typedef int (SDLCALL *SDL_AppIterate_func)(void);
  130. typedef int (SDLCALL *SDL_AppEvent_func)(const SDL_Event *event);
  131. typedef void (SDLCALL *SDL_AppQuit_func)(void);
  132. /**
  133. * You can (optionally!) define SDL_MAIN_USE_CALLBACKS before including
  134. * SDL_main.h, and then your application will _not_ have a standard
  135. * "main" entry point. Instead, it will operate as a collection of
  136. * functions that are called as necessary by the system. On some
  137. * platforms, this is just a layer where SDL drives your program
  138. * instead of your program driving SDL, on other platforms this might
  139. * hook into the OS to manage the lifecycle. Programs on most platforms
  140. * can use whichever approach they prefer, but the decision boils down
  141. * to:
  142. *
  143. * - Using a standard "main" function: this works like it always has for
  144. * the past 50+ years in C programming, and your app is in control.
  145. * - Using the callback functions: this might clean up some code,
  146. * avoid some #ifdef blocks in your program for some platforms, be more
  147. * resource-friendly to the system, and possibly be the primary way to
  148. * access some future platforms (but none require this at the moment).
  149. *
  150. * This is up to the app; both approaches are considered valid and supported
  151. * ways to write SDL apps.
  152. *
  153. * If using the callbacks, don't define a "main" function. Instead, implement
  154. * the functions listed below in your program.
  155. */
  156. #ifdef SDL_MAIN_USE_CALLBACKS
  157. /**
  158. * App-implemented initial entry point for SDL_MAIN_USE_CALLBACKS apps.
  159. *
  160. * Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If
  161. * using a standard "main" function, you should not supply this.
  162. *
  163. * This function is called by SDL once, at startup. The function should
  164. * initialize whatever is necessary, possibly create windows and open
  165. * audio devices, etc. The `argc` and `argv` parameters work like they would
  166. * with a standard "main" function.
  167. *
  168. * This function should not go into an infinite mainloop; it should do any
  169. * one-time setup it requires and then return.
  170. *
  171. * If this function returns 0, the app will proceed to normal operation,
  172. * and will begin receiving repeated calls to SDL_AppIterate and SDL_AppEvent
  173. * for the life of the program. If this function returns < 0, SDL will
  174. * call SDL_AppQuit and terminate the process with an exit code that reports
  175. * an error to the platform. If it returns > 0, the SDL calls SDL_AppQuit
  176. * and terminates with an exit code that reports success to the platform.
  177. *
  178. * \param argc The standard ANSI C main's argc; number of elements in `argv`
  179. * \param argv The standard ANSI C main's argv; array of command line arguments.
  180. * \returns -1 to terminate with an error, 1 to terminate with success, 0 to continue.
  181. *
  182. * \threadsafety This function is not thread safe.
  183. *
  184. * \since This function is available since SDL 3.0.0.
  185. *
  186. * \sa SDL_AppIterate
  187. * \sa SDL_AppEvent
  188. * \sa SDL_AppQuit
  189. */
  190. extern SDLMAIN_DECLSPEC int SDLCALL SDL_AppInit(int argc, char *argv[]);
  191. /**
  192. * App-implemented iteration entry point for SDL_MAIN_USE_CALLBACKS apps.
  193. *
  194. * Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If
  195. * using a standard "main" function, you should not supply this.
  196. *
  197. * This function is called repeatedly by SDL after SDL_AppInit returns 0.
  198. * The function should operate as a single iteration the program's primary
  199. * loop; it should update whatever state it needs and draw a new frame of
  200. * video, usually.
  201. *
  202. * On some platforms, this function will be called at the refresh rate of
  203. * the display (which might change during the life of your app!). There are
  204. * no promises made about what frequency this function might run at. You
  205. * should use SDL's timer functions if you need to see how much time has
  206. * passed since the last iteration.
  207. *
  208. * There is no need to process the SDL event queue during this function;
  209. * SDL will send events as they arrive in SDL_AppEvent, and in most cases
  210. * the event queue will be empty when this function runs anyhow.
  211. *
  212. * This function should not go into an infinite mainloop; it should do one
  213. * iteration of whatever the program does and return.
  214. *
  215. * If this function returns 0, the app will continue normal operation,
  216. * receiving repeated calls to SDL_AppIterate and SDL_AppEvent for the life
  217. * of the program. If this function returns < 0, SDL will call SDL_AppQuit
  218. * and terminate the process with an exit code that reports an error to the
  219. * platform. If it returns > 0, the SDL calls SDL_AppQuit and terminates with
  220. * an exit code that reports success to the platform.
  221. *
  222. * \returns -1 to terminate with an error, 1 to terminate with success, 0 to continue.
  223. *
  224. * \threadsafety This function is not thread safe.
  225. *
  226. * \since This function is available since SDL 3.0.0.
  227. *
  228. * \sa SDL_AppInit
  229. * \sa SDL_AppEvent
  230. * \sa SDL_AppQuit
  231. */
  232. extern SDLMAIN_DECLSPEC int SDLCALL SDL_AppIterate(void);
  233. /**
  234. * App-implemented event entry point for SDL_MAIN_USE_CALLBACKS apps.
  235. *
  236. * Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If
  237. * using a standard "main" function, you should not supply this.
  238. *
  239. * This function is called as needed by SDL after SDL_AppInit returns 0;
  240. * It is called once for each new event.
  241. *
  242. * There is (currently) no guarantee about what thread this will be called
  243. * from; whatever thread pushes an event onto SDL's queue will trigger this
  244. * function. SDL is responsible for pumping the event queue between
  245. * each call to SDL_AppIterate, so in normal operation one should only
  246. * get events in a serial fashion, but be careful if you have a thread that
  247. * explicitly calls SDL_PushEvent.
  248. *
  249. * Events sent to this function are not owned by the app; if you need to
  250. * save the data, you should copy it.
  251. *
  252. * You do not need to free event data (such as the `file` string in
  253. * SDL_EVENT_DROP_FILE), as SDL will free it once this function returns.
  254. * Note that this is different than one might expect when using a standard
  255. * "main" function!
  256. *
  257. * This function should not go into an infinite mainloop; it should handle
  258. * the provided event appropriately and return.
  259. *
  260. * If this function returns 0, the app will continue normal operation,
  261. * receiving repeated calls to SDL_AppIterate and SDL_AppEvent for the life
  262. * of the program. If this function returns < 0, SDL will call SDL_AppQuit
  263. * and terminate the process with an exit code that reports an error to the
  264. * platform. If it returns > 0, the SDL calls SDL_AppQuit and terminates with
  265. * an exit code that reports success to the platform.
  266. *
  267. * \returns -1 to terminate with an error, 1 to terminate with success, 0 to continue.
  268. *
  269. * \threadsafety This function is not thread safe.
  270. *
  271. * \since This function is available since SDL 3.0.0.
  272. *
  273. * \sa SDL_AppInit
  274. * \sa SDL_AppIterate
  275. * \sa SDL_AppQuit
  276. */
  277. extern SDLMAIN_DECLSPEC int SDLCALL SDL_AppEvent(const SDL_Event *event);
  278. /**
  279. * App-implemented deinit entry point for SDL_MAIN_USE_CALLBACKS apps.
  280. *
  281. * Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If
  282. * using a standard "main" function, you should not supply this.
  283. *
  284. * This function is called once by SDL before terminating the program.
  285. *
  286. * This function will be called no matter what, even if SDL_AppInit
  287. * requests termination.
  288. *
  289. * This function should not go into an infinite mainloop; it should
  290. * deinitialize any resources necessary, perform whatever shutdown
  291. * activities, and return.
  292. *
  293. * You do not need to call SDL_Quit() in this function, as SDL will call
  294. * it after this function returns and before the process terminates, but
  295. * it is safe to do so.
  296. *
  297. * \threadsafety This function is not thread safe.
  298. *
  299. * \since This function is available since SDL 3.0.0.
  300. *
  301. * \sa SDL_AppInit
  302. * \sa SDL_AppIterate
  303. * \sa SDL_AppEvent
  304. */
  305. extern SDLMAIN_DECLSPEC void SDLCALL SDL_AppQuit(void);
  306. #endif /* SDL_MAIN_USE_CALLBACKS */
  307. /**
  308. * The prototype for the application's main() function
  309. */
  310. typedef int (SDLCALL *SDL_main_func)(int argc, char *argv[]);
  311. extern SDLMAIN_DECLSPEC int SDLCALL SDL_main(int argc, char *argv[]);
  312. /**
  313. * Circumvent failure of SDL_Init() when not using SDL_main() as an entry
  314. * point.
  315. *
  316. * This function is defined in SDL_main.h, along with the preprocessor rule to
  317. * redefine main() as SDL_main(). Thus to ensure that your main() function
  318. * will not be changed it is necessary to define SDL_MAIN_HANDLED before
  319. * including SDL.h.
  320. *
  321. * \since This function is available since SDL 3.0.0.
  322. *
  323. * \sa SDL_Init
  324. */
  325. extern DECLSPEC void SDLCALL SDL_SetMainReady(void);
  326. /**
  327. * Initializes and launches an SDL application, by doing platform-specific
  328. * initialization before calling your mainFunction and cleanups after it
  329. * returns, if that is needed for a specific platform, otherwise it just calls
  330. * mainFunction.
  331. *
  332. * You can use this if you want to use your own main() implementation without
  333. * using SDL_main (like when using SDL_MAIN_HANDLED). When using this, you do
  334. * *not* need SDL_SetMainReady().
  335. *
  336. * \param argc The argc parameter from the application's main() function, or 0
  337. * if the platform's main-equivalent has no argc
  338. * \param argv The argv parameter from the application's main() function, or
  339. * NULL if the platform's main-equivalent has no argv
  340. * \param mainFunction Your SDL app's C-style main(), an SDL_main_func. NOT
  341. * the function you're calling this from! Its name doesn't
  342. * matter, but its signature must be like int my_main(int
  343. * argc, char* argv[])
  344. * \param reserved should be NULL (reserved for future use, will probably be
  345. * platform-specific then)
  346. * \returns the return value from mainFunction: 0 on success, -1 on failure;
  347. * SDL_GetError() might have more information on the failure
  348. *
  349. * \since This function is available since SDL 3.0.0.
  350. */
  351. extern DECLSPEC int SDLCALL SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved);
  352. /**
  353. * An entry point for SDL's use in SDL_MAIN_USE_CALLBACKS.
  354. *
  355. * Generally, you should not call this function directly. This only exists to
  356. * hand off work into SDL as soon as possible, where it has a lot more control
  357. * and functionality available, and make the inline code in SDL_main.h as
  358. * small as possible.
  359. *
  360. * Not all platforms use this, it's actual use is hidden in a magic
  361. * header-only library, and you should not call this directly unless you
  362. * _really_ know what you're doing.
  363. *
  364. * \param argc standard Unix main argc
  365. * \param argv standard Unix main argv
  366. * \param appinit The application's SDL_AppInit function
  367. * \param appiter The application's SDL_AppIterate function
  368. * \param appevent The application's SDL_AppEvent function
  369. * \param appquit The application's SDL_AppQuit function
  370. * \returns standard Unix main return value
  371. *
  372. * \threadsafety It is not safe to call this anywhere except as the only
  373. * function call in SDL_main.
  374. *
  375. * \since This function is available since SDL 3.0.0.
  376. */
  377. extern DECLSPEC int SDLCALL SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit);
  378. #if defined(__WIN32__) || defined(__GDK__)
  379. /**
  380. * Register a win32 window class for SDL's use.
  381. *
  382. * This can be called to set the application window class at startup. It is
  383. * safe to call this multiple times, as long as every call is eventually
  384. * paired with a call to SDL_UnregisterApp, but a second registration attempt
  385. * while a previous registration is still active will be ignored, other than
  386. * to increment a counter.
  387. *
  388. * Most applications do not need to, and should not, call this directly; SDL
  389. * will call it when initializing the video subsystem.
  390. *
  391. * \param name the window class name, in UTF-8 encoding. If NULL, SDL
  392. * currently uses "SDL_app" but this isn't guaranteed.
  393. * \param style the value to use in WNDCLASSEX::style. If `name` is NULL, SDL
  394. * currently uses `(CS_BYTEALIGNCLIENT | CS_OWNDC)` regardless of
  395. * what is specified here.
  396. * \param hInst the HINSTANCE to use in WNDCLASSEX::hInstance. If zero, SDL
  397. * will use `GetModuleHandle(NULL)` instead.
  398. * \returns 0 on success or a negative error code on failure; call
  399. * SDL_GetError() for more information.
  400. *
  401. * \since This function is available since SDL 3.0.0.
  402. */
  403. extern DECLSPEC int SDLCALL SDL_RegisterApp(const char *name, Uint32 style, void *hInst);
  404. /**
  405. * Deregister the win32 window class from an SDL_RegisterApp call.
  406. *
  407. * This can be called to undo the effects of SDL_RegisterApp.
  408. *
  409. * Most applications do not need to, and should not, call this directly; SDL
  410. * will call it when deinitializing the video subsystem.
  411. *
  412. * It is safe to call this multiple times, as long as every call is eventually
  413. * paired with a prior call to SDL_RegisterApp. The window class will only be
  414. * deregistered when the registration counter in SDL_RegisterApp decrements to
  415. * zero through calls to this function.
  416. *
  417. * \since This function is available since SDL 3.0.0.
  418. */
  419. extern DECLSPEC void SDLCALL SDL_UnregisterApp(void);
  420. #endif /* defined(__WIN32__) || defined(__GDK__) */
  421. #ifdef __WINRT__
  422. /* for compatibility with SDL2's function of this name */
  423. #define SDL_WinRTRunApp(MAIN_FUNC, RESERVED) SDL_RunApp(0, NULL, MAIN_FUNC, RESERVED)
  424. #endif /* __WINRT__ */
  425. #ifdef __IOS__
  426. /* for compatibility with SDL2's function of this name */
  427. #define SDL_UIKitRunApp(ARGC, ARGV, MAIN_FUNC) SDL_RunApp(ARGC, ARGV, MAIN_FUNC, NULL)
  428. #endif /* __IOS__ */
  429. #ifdef __GDK__
  430. /* for compatibility with SDL2's function of this name */
  431. #define SDL_GDKRunApp(MAIN_FUNC, RESERVED) SDL_RunApp(0, NULL, MAIN_FUNC, RESERVED)
  432. /**
  433. * Callback from the application to let the suspend continue.
  434. *
  435. * \since This function is available since SDL 3.0.0.
  436. */
  437. extern DECLSPEC void SDLCALL SDL_GDKSuspendComplete(void);
  438. #endif /* __GDK__ */
  439. #ifdef __cplusplus
  440. }
  441. #endif
  442. #include <SDL3/SDL_close_code.h>
  443. #if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
  444. /* include header-only SDL_main implementations */
  445. #if defined(SDL_MAIN_USE_CALLBACKS) \
  446. || defined(__WIN32__) || defined(__GDK__) || defined(__IOS__) || defined(__TVOS__) \
  447. || defined(__3DS__) || defined(__NGAGE__) || defined(__PS2__) || defined(__PSP__)
  448. /* platforms which main (-equivalent) can be implemented in plain C */
  449. #include <SDL3/SDL_main_impl.h>
  450. #elif defined(__WINRT__) /* C++ platforms */
  451. #ifdef __cplusplus
  452. #include <SDL3/SDL_main_impl.h>
  453. #else
  454. /* Note: to get rid of the following warning, you can #define SDL_MAIN_NOIMPL before including SDL_main.h
  455. * in your C sourcefile that contains the standard main. Do *not* use SDL_MAIN_HANDLED for that, then SDL_main won't find your main()!
  456. */
  457. #ifdef _MSC_VER
  458. #pragma message("Note: Your platform needs the SDL_main implementation in a C++ source file. You can keep your main() in plain C (then continue including SDL_main.h there!) and create a fresh .cpp file that only contains #include <SDL3/SDL_main.h>")
  459. #elif defined(__GNUC__) /* gcc, clang, mingw and compatible are matched by this and have #warning */
  460. #warning "Note: Your platform needs the SDL_main implementation in a C++ source file. You can keep your main() in plain C and create a fresh .cpp file that only contains #include <SDL3/SDL_main.h>"
  461. #endif /* __GNUC__ */
  462. #endif /* __cplusplus */
  463. #endif /* C++ platforms like __WINRT__ etc */
  464. #endif /* SDL_MAIN_HANDLED */
  465. #endif /* SDL_main_h_ */