SDL_main_impl.h 7.1 KB

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