Main.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Core/ProcessUtils.h"
  5. #if defined(_WIN32) && !defined(URHO3D_WIN32_CONSOLE)
  6. #include "../Core/MiniDump.h"
  7. #include "../Engine/WinWrapped.h"
  8. #ifdef _MSC_VER
  9. #include <crtdbg.h>
  10. #endif
  11. #endif
  12. // Define a platform-specific main function, which in turn executes the user-defined function
  13. // MSVC debug mode: use memory leak reporting
  14. #if defined(_MSC_VER) && defined(_DEBUG) && !defined(URHO3D_WIN32_CONSOLE)
  15. #define URHO3D_DEFINE_MAIN(function) \
  16. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) \
  17. { \
  18. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); \
  19. Urho3D::ParseArguments(GetCommandLineW()); \
  20. return function; \
  21. }
  22. // MSVC release mode: write minidump on crash
  23. #elif defined(_MSC_VER) && defined(URHO3D_MINIDUMPS) && !defined(URHO3D_WIN32_CONSOLE)
  24. #define URHO3D_DEFINE_MAIN(function) \
  25. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) \
  26. { \
  27. Urho3D::ParseArguments(GetCommandLineW()); \
  28. int exitCode; \
  29. __try \
  30. { \
  31. exitCode = function; \
  32. } \
  33. __except(Urho3D::WriteMiniDump("Urho3D", GetExceptionInformation())) \
  34. { \
  35. } \
  36. return exitCode; \
  37. }
  38. // Other Win32 or minidumps disabled: just execute the function
  39. #elif defined(_WIN32) && !defined(URHO3D_WIN32_CONSOLE)
  40. #define URHO3D_DEFINE_MAIN(function) \
  41. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) \
  42. { \
  43. Urho3D::ParseArguments(GetCommandLineW()); \
  44. return function; \
  45. }
  46. // Android or iOS or tvOS: use SDL_main
  47. #elif defined(__ANDROID__) || defined(IOS) || defined(TVOS)
  48. #define URHO3D_DEFINE_MAIN(function) \
  49. extern "C" __attribute__((visibility("default"))) int SDL_main(int argc, char** argv); \
  50. int SDL_main(int argc, char** argv) \
  51. { \
  52. Urho3D::ParseArguments(argc, argv); \
  53. return function; \
  54. }
  55. // Linux or OS X: use main
  56. #else
  57. #define URHO3D_DEFINE_MAIN(function) \
  58. int main(int argc, char** argv) \
  59. { \
  60. Urho3D::ParseArguments(argc, argv); \
  61. return function; \
  62. }
  63. #endif