Main.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "ProcessUtils.h"
  24. #ifdef WIN32
  25. #include "MiniDump.h"
  26. #include <windows.h>
  27. #ifdef _MSC_VER
  28. #include <crtdbg.h>
  29. #endif
  30. #endif
  31. // Define a platform-specific main function, which in turn executes the user-defined function
  32. // MSVC debug mode: use memory leak reporting
  33. #if defined(_MSC_VER) && defined(_DEBUG)
  34. #define DEFINE_MAIN(function) \
  35. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) \
  36. { \
  37. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); \
  38. Urho3D::ParseArguments(GetCommandLineW()); \
  39. return function; \
  40. }
  41. // MSVC release mode: write minidump on crash
  42. #elif defined(_MSC_VER) && defined(ENABLE_MINIDUMPS)
  43. #define DEFINE_MAIN(function) \
  44. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) \
  45. { \
  46. Urho3D::ParseArguments(GetCommandLineW()); \
  47. int exitCode; \
  48. __try \
  49. { \
  50. exitCode = function; \
  51. } \
  52. __except(Urho3D::WriteMiniDump("Urho3D", GetExceptionInformation())) \
  53. { \
  54. } \
  55. return exitCode; \
  56. }
  57. // Other Win32 or minidumps disabled: just execute the function
  58. #elif defined(WIN32)
  59. #define DEFINE_MAIN(function) \
  60. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) \
  61. { \
  62. Urho3D::ParseArguments(GetCommandLineW()); \
  63. return function; \
  64. }
  65. // Android or iOS: use SDL_main
  66. #elif defined(ANDROID) || defined(IOS)
  67. #define DEFINE_MAIN(function) \
  68. extern "C" int SDL_main(int argc, char** argv); \
  69. int SDL_main(int argc, char** argv) \
  70. { \
  71. Urho3D::ParseArguments(argc, argv); \
  72. return function; \
  73. }
  74. // Linux or OS X: use main
  75. #else
  76. #define DEFINE_MAIN(function) \
  77. int main(int argc, char** argv) \
  78. { \
  79. Urho3D::ParseArguments(argc, argv); \
  80. return function; \
  81. }
  82. #endif