SDL_main_impl.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_windows_h_
  19. #define SDL_main_windows_h_
  20. #if !defined(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. #if defined(__WIN32__) || defined(__GDK__)
  32. /* these defines/typedefs are needed for the WinMain() definition */
  33. #ifndef WINAPI
  34. #define WINAPI __stdcall
  35. #endif
  36. #ifdef main
  37. # undef main
  38. #endif /* main */
  39. #include <SDL3/begin_code.h>
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. typedef struct HINSTANCE__ * HINSTANCE;
  44. typedef char* LPSTR;
  45. #ifndef __GDK__ /* this is only needed for Win32 */
  46. #if defined(_MSC_VER)
  47. /* The VC++ compiler needs main/wmain defined */
  48. # define console_ansi_main main
  49. # if defined(UNICODE) && UNICODE
  50. # define console_wmain wmain
  51. # endif
  52. #endif
  53. #if defined( UNICODE ) && UNICODE
  54. /* This is where execution begins [console apps, unicode] */
  55. int
  56. console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
  57. {
  58. return SDL_Win32RunApp(SDL_main, NULL);
  59. }
  60. #else /* ANSI */
  61. /* This is where execution begins [console apps, ansi] */
  62. int
  63. console_ansi_main(int argc, char *argv[])
  64. {
  65. return SDL_Win32RunApp(SDL_main, NULL);
  66. }
  67. #endif /* UNICODE/ANSI */
  68. #endif /* not __GDK__ */
  69. /* This is where execution begins [windowed apps and GDK] */
  70. int WINAPI
  71. WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
  72. {
  73. #ifdef __GDK__
  74. return SDL_GDKRunApp(SDL_main, NULL);
  75. #else
  76. return SDL_Win32RunApp(SDL_main, NULL);
  77. #endif
  78. }
  79. #ifdef __cplusplus
  80. } /* extern "C" */
  81. #endif
  82. #include <SDL3/close_code.h>
  83. /* rename users main() function to SDL_main() so it can be called from the wrapper above */
  84. #define main SDL_main
  85. /* end of __WIN32__ and __GDK__ impls */
  86. #elif defined(__WINRT__)
  87. /* WinRT main based on SDL_winrt_main_NonXAML.cpp, placed in the public domain by David Ludwig 3/13/14 */
  88. #include <wrl.h>
  89. /* At least one file in any SDL/WinRT app appears to require compilation
  90. with C++/CX, otherwise a Windows Metadata file won't get created, and
  91. an APPX0702 build error can appear shortly after linking.
  92. The following set of preprocessor code forces this file to be compiled
  93. as C++/CX, which appears to cause Visual C++ 2012's build tools to
  94. create this .winmd file, and will help allow builds of SDL/WinRT apps
  95. to proceed without error.
  96. If other files in an app's project enable C++/CX compilation, then it might
  97. be possible for the .cpp file including SDL_main.h to be compiled without /ZW,
  98. for Visual C++'s build tools to create a winmd file, and for the app to
  99. build without APPX0702 errors. In this case, if
  100. SDL_WINRT_METADATA_FILE_AVAILABLE is defined as a C/C++ macro, then
  101. the #error (to force C++/CX compilation) will be disabled.
  102. Please note that /ZW can be specified on a file-by-file basis. To do this,
  103. right click on the file in Visual C++, click Properties, then change the
  104. setting through the dialog that comes up.
  105. */
  106. #ifndef SDL_WINRT_METADATA_FILE_AVAILABLE
  107. #if !defined(__cplusplus) || !defined(__cplusplus_winrt)
  108. #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.
  109. #endif
  110. #endif
  111. /* Prevent MSVC++ from warning about threading models when defining our
  112. custom WinMain. The threading model will instead be set via a direct
  113. call to Windows::Foundation::Initialize (rather than via an attributed
  114. function).
  115. To note, this warning (C4447) does not seem to come up unless this file
  116. is compiled with C++/CX enabled (via the /ZW compiler flag).
  117. */
  118. #ifdef _MSC_VER
  119. #pragma warning(disable : 4447)
  120. #endif
  121. /* Make sure the function to initialize the Windows Runtime gets linked in. */
  122. #ifdef _MSC_VER
  123. #pragma comment(lib, "runtimeobject.lib")
  124. #endif
  125. int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  126. {
  127. return SDL_WinRTRunApp(SDL_main, NULL);
  128. }
  129. /* end of WinRT impl */
  130. #elif defined(__IOS__) || defined(__TVOS__)
  131. #ifdef main
  132. # undef main
  133. #endif /* main */
  134. #include <SDL3/begin_code.h>
  135. #ifdef __cplusplus
  136. extern "C" {
  137. #endif
  138. int main(int argc, char *argv[])
  139. {
  140. return SDL_UIKitRunApp(argc, argv, SDL_main);
  141. }
  142. #ifdef __cplusplus
  143. } /* extern "C" */
  144. #endif
  145. #include <SDL3/close_code.h>
  146. /* rename users main() function to SDL_main() so it can be called from the wrapper above */
  147. #define main SDL_main
  148. /* end of __IOS__ and __TVOS__ impls */
  149. /* TODO: remaining platforms */
  150. #endif /* __WIN32__ etc */
  151. #endif /* SDL_MAIN_HANDLED */
  152. #endif /* SDL_main_windows_h_ */
  153. /* vi: set ts=4 sw=4 expandtab: */