SDL_main.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. /**
  22. * \file SDL_main.h
  23. *
  24. * \brief Redefine main() on some platforms so that it is called by SDL.
  25. */
  26. #ifndef SDL_MAIN_HANDLED
  27. #ifdef __WIN32__
  28. /* On Windows SDL provides WinMain(), which parses the command line and passes
  29. the arguments to your main function.
  30. If you provide your own WinMain(), you may define SDL_MAIN_HANDLED
  31. */
  32. #define SDL_MAIN_AVAILABLE
  33. #elif defined(__WINRT__)
  34. /* On WinRT, SDL provides a main function that initializes CoreApplication,
  35. creating an instance of IFrameworkView in the process.
  36. Ideally, #include'ing SDL_main.h is enough to get a main() function working.
  37. However, that requires the source file your main() is in to be compiled
  38. as C++ *and* with the /ZW compiler flag. If that's not feasible, add an
  39. otherwise empty .cpp file that only contains `#include <SDL3/SDL_main.h>`
  40. and build that with /ZW (still include SDL_main.h in your other file with main()!).
  41. In XAML apps, instead the function SDL_RunApp() must be called with a pointer
  42. to the Direct3D-hosted XAML control passed in as the "reserved" argument.
  43. */
  44. #define SDL_MAIN_NEEDED
  45. #elif defined(__GDK__)
  46. /* On GDK, SDL provides a main function that initializes the game runtime.
  47. If you prefer to write your own WinMain-function instead of having SDL
  48. provide one that calls your main() function,
  49. #define SDL_MAIN_HANDLED before #include'ing SDL_main.h
  50. and call the SDL_RunApp function from your entry point.
  51. */
  52. #define SDL_MAIN_NEEDED
  53. #elif defined(__IOS__)
  54. /* On iOS SDL provides a main function that creates an application delegate
  55. and starts the iOS application run loop.
  56. To use it, just #include SDL_main.h in the source file that contains your
  57. main() function.
  58. See src/video/uikit/SDL_uikitappdelegate.m for more details.
  59. */
  60. #define SDL_MAIN_NEEDED
  61. #elif defined(__ANDROID__)
  62. /* On Android SDL provides a Java class in SDLActivity.java that is the
  63. main activity entry point.
  64. See docs/README-android.md for more details on extending that class.
  65. */
  66. #define SDL_MAIN_NEEDED
  67. /* We need to export SDL_main so it can be launched from Java */
  68. #define SDLMAIN_DECLSPEC DECLSPEC
  69. #elif defined(__PSP__)
  70. /* On PSP SDL provides a main function that sets the module info,
  71. activates the GPU and starts the thread required to be able to exit
  72. the software.
  73. If you provide this yourself, you may define SDL_MAIN_HANDLED
  74. */
  75. #define SDL_MAIN_AVAILABLE
  76. #elif defined(__PS2__)
  77. #define SDL_MAIN_AVAILABLE
  78. #define SDL_PS2_SKIP_IOP_RESET() \
  79. void reset_IOP(); \
  80. void reset_IOP() {}
  81. #elif defined(__3DS__)
  82. /*
  83. On N3DS, SDL provides a main function that sets up the screens
  84. and storage.
  85. If you provide this yourself, you may define SDL_MAIN_HANDLED
  86. */
  87. #define SDL_MAIN_AVAILABLE
  88. #elif defined(__NGAGE__)
  89. /*
  90. TODO: not sure if it should be SDL_MAIN_NEEDED, in SDL2 ngage had a
  91. main implementation, but wasn't mentioned in SDL_main.h
  92. */
  93. #define SDL_MAIN_AVAILABLE
  94. #endif
  95. #endif /* SDL_MAIN_HANDLED */
  96. #ifndef SDLMAIN_DECLSPEC
  97. #define SDLMAIN_DECLSPEC
  98. #endif
  99. /**
  100. * \file SDL_main.h
  101. *
  102. * The application's main() function must be called with C linkage,
  103. * and should be declared like this:
  104. * \code
  105. * #ifdef __cplusplus
  106. * extern "C"
  107. * #endif
  108. * int main(int argc, char *argv[])
  109. * {
  110. * }
  111. * \endcode
  112. */
  113. #if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE)
  114. #define main SDL_main
  115. #endif
  116. #include <SDL3/SDL_begin_code.h>
  117. #ifdef __cplusplus
  118. extern "C" {
  119. #endif
  120. /**
  121. * The prototype for the application's main() function
  122. */
  123. typedef int (*SDL_main_func)(int argc, char *argv[]);
  124. extern SDLMAIN_DECLSPEC int SDL_main(int argc, char *argv[]);
  125. /**
  126. * Circumvent failure of SDL_Init() when not using SDL_main() as an entry
  127. * point.
  128. *
  129. * This function is defined in SDL_main.h, along with the preprocessor rule to
  130. * redefine main() as SDL_main(). Thus to ensure that your main() function
  131. * will not be changed it is necessary to define SDL_MAIN_HANDLED before
  132. * including SDL.h.
  133. *
  134. * \since This function is available since SDL 3.0.0.
  135. *
  136. * \sa SDL_Init
  137. */
  138. extern DECLSPEC void SDLCALL SDL_SetMainReady(void);
  139. /**
  140. * Initializes and launches an SDL application, by doing platform-specific
  141. * initialization before calling your mainFunction and cleanups after it
  142. * returns, if that is needed for a specific platform, otherwise it just calls
  143. * mainFunction.
  144. *
  145. * You can use this if you want to use your own main() implementation without
  146. * using SDL_main (like when using SDL_MAIN_HANDLED). When using this, you do
  147. * *not* need SDL_SetMainReady().
  148. *
  149. * \param argc The argc parameter from the application's main() function, or 0
  150. * if the platform's main-equivalent has no argc
  151. * \param argv The argv parameter from the application's main() function, or
  152. * NULL if the platform's main-equivalent has no argv
  153. * \param mainFunction Your SDL app's C-style main(), an SDL_main_func. NOT
  154. * the function you're calling this from! Its name doesn't
  155. * matter, but its signature must be like int my_main(int
  156. * argc, char* argv[])
  157. * \param reserved should be NULL (reserved for future use, will probably be
  158. * platform-specific then)
  159. * \returns the return value from mainFunction: 0 on success, -1 on failure;
  160. * SDL_GetError() might have more information on the failure
  161. *
  162. * \since This function is available since SDL 3.0.0.
  163. */
  164. extern DECLSPEC int SDLCALL SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved);
  165. #if defined(__WIN32__) || defined(__GDK__)
  166. /**
  167. * Register a win32 window class for SDL's use.
  168. *
  169. * This can be called to set the application window class at startup. It is
  170. * safe to call this multiple times, as long as every call is eventually
  171. * paired with a call to SDL_UnregisterApp, but a second registration attempt
  172. * while a previous registration is still active will be ignored, other than
  173. * to increment a counter.
  174. *
  175. * Most applications do not need to, and should not, call this directly; SDL
  176. * will call it when initializing the video subsystem.
  177. *
  178. * \param name the window class name, in UTF-8 encoding. If NULL, SDL
  179. * currently uses "SDL_app" but this isn't guaranteed.
  180. * \param style the value to use in WNDCLASSEX::style. If `name` is NULL, SDL
  181. * currently uses `(CS_BYTEALIGNCLIENT | CS_OWNDC)` regardless of
  182. * what is specified here.
  183. * \param hInst the HINSTANCE to use in WNDCLASSEX::hInstance. If zero, SDL
  184. * will use `GetModuleHandle(NULL)` instead.
  185. * \returns 0 on success or a negative error code on failure; call
  186. * SDL_GetError() for more information.
  187. *
  188. * \since This function is available since SDL 3.0.0.
  189. */
  190. extern DECLSPEC int SDLCALL SDL_RegisterApp(const char *name, Uint32 style, void *hInst);
  191. /**
  192. * Deregister the win32 window class from an SDL_RegisterApp call.
  193. *
  194. * This can be called to undo the effects of SDL_RegisterApp.
  195. *
  196. * Most applications do not need to, and should not, call this directly; SDL
  197. * will call it when deinitializing the video subsystem.
  198. *
  199. * It is safe to call this multiple times, as long as every call is eventually
  200. * paired with a prior call to SDL_RegisterApp. The window class will only be
  201. * deregistered when the registration counter in SDL_RegisterApp decrements to
  202. * zero through calls to this function.
  203. *
  204. * \since This function is available since SDL 3.0.0.
  205. */
  206. extern DECLSPEC void SDLCALL SDL_UnregisterApp(void);
  207. #endif /* defined(__WIN32__) || defined(__GDK__) */
  208. #ifdef __WINRT__
  209. /* for compatibility with SDL2's function of this name */
  210. #define SDL_WinRTRunApp(MAIN_FUNC, RESERVED) SDL_RunApp(0, NULL, MAIN_FUNC, RESERVED)
  211. #endif /* __WINRT__ */
  212. #ifdef __IOS__
  213. /* for compatibility with SDL2's function of this name */
  214. #define SDL_UIKitRunApp(ARGC, ARGV, MAIN_FUNC) SDL_RunApp(ARGC, ARGV, MAIN_FUNC, NULL)
  215. #endif /* __IOS__ */
  216. #ifdef __GDK__
  217. /* for compatibility with SDL2's function of this name */
  218. #define SDL_GDKRunApp(MAIN_FUNC, RESERVED) SDL_RunApp(0, NULL, MAIN_FUNC, RESERVED)
  219. /**
  220. * Callback from the application to let the suspend continue.
  221. *
  222. * \since This function is available since SDL 3.0.0.
  223. */
  224. extern DECLSPEC void SDLCALL SDL_GDKSuspendComplete(void);
  225. #endif /* __GDK__ */
  226. #ifdef __cplusplus
  227. }
  228. #endif
  229. #include <SDL3/SDL_close_code.h>
  230. #if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
  231. /* include header-only SDL_main implementations */
  232. #if defined(__WIN32__) || defined(__GDK__) || defined(__IOS__) || defined(__TVOS__) \
  233. || defined(__3DS__) || defined(__NGAGE__) || defined(__PS2__) || defined(__PSP__)
  234. /* platforms whichs main (-equivalent) can be implemented in plain C */
  235. #include <SDL3/SDL_main_impl.h>
  236. #elif defined(__WINRT__) /* C++ platforms */
  237. #ifdef __cplusplus
  238. #include <SDL3/SDL_main_impl.h>
  239. #else
  240. /* Note: to get rid of the following warning, you can #define SDL_MAIN_NOIMPL before including SDL_main.h
  241. * 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()!
  242. */
  243. #ifdef _MSC_VER
  244. #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>")
  245. #elif defined(__GNUC__) /* gcc, clang, mingw and compatible are matched by this and have #warning */
  246. #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>"
  247. #endif /* __GNUC__ */
  248. #endif /* __cplusplus */
  249. #endif /* C++ platforms like __WINRT__ etc */
  250. #endif /* SDL_MAIN_HANDLED */
  251. #endif /* SDL_main_h_ */