SDL_main.h 10 KB

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