SDL_main_impl.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. /* WIKI CATEGORY: Main */
  19. #ifndef SDL_main_impl_h_
  20. #define SDL_main_impl_h_
  21. #ifndef SDL_main_h_
  22. #error "This header should not be included directly, but only via SDL_main.h!"
  23. #endif
  24. /* if someone wants to include SDL_main.h but doesn't want the main handing magic,
  25. (maybe to call SDL_RegisterApp()) they can #define SDL_MAIN_HANDLED first
  26. SDL_MAIN_NOIMPL is for SDL-internal usage (only affects implementation,
  27. not definition of SDL_MAIN_AVAILABLE etc in SDL_main.h) and if the user wants
  28. to have the SDL_main implementation (from this header) in another source file
  29. than their main() function, for example if SDL_main requires C++
  30. and main() is implemented in plain C */
  31. #if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
  32. /* the implementations below must be able to use the implement real main(), nothing renamed
  33. (the user's main() will be renamed to SDL_main so it can be called from here) */
  34. #ifdef main
  35. #undef main
  36. #endif
  37. #ifdef SDL_MAIN_USE_CALLBACKS
  38. #if 0
  39. /* currently there are no platforms that _need_ a magic entry point here
  40. for callbacks, but if one shows up, implement it here. */
  41. #else /* use a standard SDL_main, which the app SHOULD NOT ALSO SUPPLY. */
  42. /* this define makes the normal SDL_main entry point stuff work...we just provide SDL_main() instead of the app. */
  43. #define SDL_MAIN_CALLBACK_STANDARD 1
  44. int SDL_main(int argc, char **argv)
  45. {
  46. return SDL_EnterAppMainCallbacks(argc, argv, SDL_AppInit, SDL_AppIterate, SDL_AppEvent, SDL_AppQuit);
  47. }
  48. #endif /* platform-specific tests */
  49. #endif /* SDL_MAIN_USE_CALLBACKS */
  50. /* set up the usual SDL_main stuff if we're not using callbacks or if we are but need the normal entry point. */
  51. #if !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD)
  52. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  53. /* these defines/typedefs are needed for the WinMain() definition */
  54. #ifndef WINAPI
  55. #define WINAPI __stdcall
  56. #endif
  57. typedef struct HINSTANCE__ * HINSTANCE;
  58. typedef char* LPSTR;
  59. typedef wchar_t* PWSTR;
  60. /* The VC++ compiler needs main/wmain defined, but not for GDK */
  61. #if defined(_MSC_VER) && !defined(SDL_PLATFORM_GDK)
  62. /* This is where execution begins [console apps] */
  63. #if defined( UNICODE ) && UNICODE
  64. int wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
  65. {
  66. (void)argc;
  67. (void)wargv;
  68. (void)wenvp;
  69. return SDL_RunApp(0, NULL, SDL_main, NULL);
  70. }
  71. #else /* ANSI */
  72. int main(int argc, char *argv[])
  73. {
  74. (void)argc;
  75. (void)argv;
  76. return SDL_RunApp(0, NULL, SDL_main, NULL);
  77. }
  78. #endif /* UNICODE */
  79. #endif /* _MSC_VER && ! SDL_PLATFORM_GDK */
  80. /* This is where execution begins [windowed apps and GDK] */
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. #if defined( UNICODE ) && UNICODE
  85. int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, PWSTR szCmdLine, int sw)
  86. #else /* ANSI */
  87. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
  88. #endif
  89. {
  90. (void)hInst;
  91. (void)hPrev;
  92. (void)szCmdLine;
  93. (void)sw;
  94. return SDL_RunApp(0, NULL, SDL_main, NULL);
  95. }
  96. #ifdef __cplusplus
  97. } /* extern "C" */
  98. #endif
  99. /* end of SDL_PLATFORM_WIN32 and SDL_PLATFORM_GDK impls */
  100. #elif defined(SDL_PLATFORM_WINRT)
  101. /* WinRT main based on SDL_winrt_main_NonXAML.cpp, placed in the public domain by David Ludwig 3/13/14 */
  102. #include <wrl.h>
  103. /* At least one file in any SDL/WinRT app appears to require compilation
  104. with C++/CX, otherwise a Windows Metadata file won't get created, and
  105. an APPX0702 build error can appear shortly after linking.
  106. The following set of preprocessor code forces this file to be compiled
  107. as C++/CX, which appears to cause Visual C++ 2012's build tools to
  108. create this .winmd file, and will help allow builds of SDL/WinRT apps
  109. to proceed without error.
  110. If other files in an app's project enable C++/CX compilation, then it might
  111. be possible for the .cpp file including SDL_main.h to be compiled without /ZW,
  112. for Visual C++'s build tools to create a winmd file, and for the app to
  113. build without APPX0702 errors. In this case, if
  114. SDL_WINRT_METADATA_FILE_AVAILABLE is defined as a C/C++ macro, then
  115. the #error (to force C++/CX compilation) will be disabled.
  116. Please note that /ZW can be specified on a file-by-file basis. To do this,
  117. right click on the file in Visual C++, click Properties, then change the
  118. setting through the dialog that comes up.
  119. */
  120. #ifndef SDL_WINRT_METADATA_FILE_AVAILABLE
  121. #if !defined(__cplusplus) || (!defined(__cplusplus_winrt) && _MSVC_LANG < 202002L)
  122. #error The C++ file that includes SDL_main.h must be compiled as C++ code with /ZW, otherwise build errors due to missing .winmd files can occur.
  123. #endif
  124. #endif
  125. /* Prevent MSVC++ from warning about threading models when defining our
  126. custom WinMain. The threading model will instead be set via a direct
  127. call to Windows::Foundation::Initialize (rather than via an attributed
  128. function).
  129. To note, this warning (C4447) does not seem to come up unless this file
  130. is compiled with C++/CX enabled (via the /ZW compiler flag).
  131. */
  132. #ifdef _MSC_VER
  133. #pragma warning(disable : 4447)
  134. /* Make sure the function to initialize the Windows Runtime gets linked in. */
  135. #pragma comment(lib, "runtimeobject.lib")
  136. #endif
  137. #ifdef __cplusplus
  138. extern "C" {
  139. #endif
  140. int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  141. {
  142. return SDL_RunApp(0, NULL, SDL_main, NULL);
  143. }
  144. #if _MSVC_LANG >= 202002L
  145. int main(int argc, char** argv) {
  146. return SDL_RunApp(argc, argv, SDL_main, NULL);
  147. }
  148. #endif
  149. #ifdef __cplusplus
  150. } /* extern "C" */
  151. #endif
  152. /* end of WinRT impl */
  153. #elif defined(SDL_PLATFORM_NGAGE)
  154. /* same typedef as in ngage SDKs e32def.h */
  155. typedef signed int TInt;
  156. /* TODO: if it turns out that this only works when built as C++,
  157. move SDL_PLATFORM_NGAGE into the C++ section in SDL_main.h */
  158. TInt E32Main()
  159. {
  160. return SDL_RunApp(0, NULL, SDL_main, NULL);
  161. }
  162. /* end of SDL_PLATFORM_NGAGE impl */
  163. #else /* platforms that use a standard main() and just call SDL_RunApp(), like iOS and 3DS */
  164. int main(int argc, char *argv[])
  165. {
  166. return SDL_RunApp(argc, argv, SDL_main, NULL);
  167. }
  168. /* end of impls for standard-conforming platforms */
  169. #endif /* SDL_PLATFORM_WIN32 etc */
  170. #endif /* !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD) */
  171. /* rename users main() function to SDL_main() so it can be called from the wrappers above */
  172. #define main SDL_main
  173. #endif /* SDL_MAIN_HANDLED */
  174. #endif /* SDL_main_impl_h_ */