SDL_main_impl.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. /* 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. unless the real entry point needs to be somewhere else entirely, like Android where it's in Java code */
  52. #if (!defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD)) && !defined(SDL_MAIN_EXPORTED)
  53. #if defined(SDL_PLATFORM_PRIVATE_MAIN)
  54. /* Private platforms may have their own ideas about entry points. */
  55. #include "SDL_main_impl_private.h"
  56. #elif defined(SDL_PLATFORM_WINDOWS)
  57. /* these defines/typedefs are needed for the WinMain() definition */
  58. #ifndef WINAPI
  59. #define WINAPI __stdcall
  60. #endif
  61. typedef struct HINSTANCE__ * HINSTANCE;
  62. typedef char *LPSTR;
  63. typedef wchar_t *PWSTR;
  64. /* The VC++ compiler needs main/wmain defined, but not for GDK */
  65. #if defined(_MSC_VER) && !defined(SDL_PLATFORM_GDK)
  66. /* This is where execution begins [console apps] */
  67. #if defined(UNICODE) && UNICODE
  68. int wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
  69. {
  70. (void)argc;
  71. (void)wargv;
  72. (void)wenvp;
  73. return SDL_RunApp(0, NULL, SDL_main, NULL);
  74. }
  75. #else /* ANSI */
  76. int main(int argc, char *argv[])
  77. {
  78. (void)argc;
  79. (void)argv;
  80. return SDL_RunApp(0, NULL, SDL_main, NULL);
  81. }
  82. #endif /* UNICODE */
  83. #endif /* _MSC_VER && ! SDL_PLATFORM_GDK */
  84. /* This is where execution begins [windowed apps and GDK] */
  85. #ifdef __cplusplus
  86. extern "C" {
  87. #endif
  88. #if defined(UNICODE) && UNICODE
  89. int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, PWSTR szCmdLine, int sw)
  90. #else /* ANSI */
  91. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
  92. #endif
  93. {
  94. (void)hInst;
  95. (void)hPrev;
  96. (void)szCmdLine;
  97. (void)sw;
  98. return SDL_RunApp(0, NULL, SDL_main, NULL);
  99. }
  100. #ifdef __cplusplus
  101. } /* extern "C" */
  102. #endif
  103. /* end of SDL_PLATFORM_WINDOWS impls */
  104. #else /* platforms that use a standard main() and just call SDL_RunApp(), like iOS and 3DS */
  105. int main(int argc, char *argv[])
  106. {
  107. return SDL_RunApp(argc, argv, SDL_main, NULL);
  108. }
  109. /* end of impls for standard-conforming platforms */
  110. #endif /* SDL_PLATFORM_WIN32 etc */
  111. #endif /* !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD) */
  112. /* rename users main() function to SDL_main() so it can be called from the wrappers above */
  113. #define main SDL_main
  114. #endif /* SDL_MAIN_HANDLED */
  115. #endif /* SDL_main_impl_h_ */