Bladeren bron

Added a macro for the different main() implementations.

Lasse Öörni 13 jaren geleden
bovenliggende
commit
e994485472
3 gewijzigde bestanden met toevoegingen van 91 en 46 verwijderingen
  1. 1 1
      Docs/GettingStarted.dox
  2. 88 0
      Engine/Core/Main.h
  3. 2 45
      Urho3D/Urho3D.cpp

+ 1 - 1
Docs/GettingStarted.dox

@@ -378,7 +378,7 @@ The example application is now complete. To try it out, save it as HelloWorld.as
 
 This example shows how to create an Urho3D C++ application from the ground up. The actual functionality will be the same as in \ref ScriptQuickstart "Quickstart in script"; it is strongly recommended that you familiarize yourself with it first.
 
-For simplicity, the application is assumed to be compiled on Windows and therefore defines the WinMain() function; look at the file Urho3D.cpp in the Urho3D subdirectory on how to handle cross-platform startup. On Linux and OS X, a main() function would be used instead, and SDL_main() on Android and iOS.
+For simplicity, the application is assumed to be compiled on Windows and therefore defines the WinMain() function; look at the file Urho3D.cpp in the Urho3D subdirectory on how to handle cross-platform startup using a macro defined in Main.h in the Core library.
 
 To start with, create a subdirectory "HelloWorld" into the Urho3D root directory, and add the following line to the root directory's CMakeLists.txt %file:
 

+ 88 - 0
Engine/Core/Main.h

@@ -0,0 +1,88 @@
+//
+// Urho3D Engine
+// Copyright (c) 2008-2012 Lasse Öörni
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include "ProcessUtils.h"
+
+#ifdef WIN32
+#include "MiniDump.h"
+#include <windows.h>
+#endif
+
+// Define a platform-specific main function, which in turn executes the user-defined function
+
+// MSVC debug mode: use memory leak reporting
+#if defined(_MSC_VER) && defined(_DEBUG)
+#define DEFINE_MAIN(function) \
+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) \
+{ \
+    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); \
+    ParseArguments(GetCommandLineW()); \
+    function; \
+    return 0; \
+}
+// MSVC release mode: write minidump on crash
+#elif defined(_MSC_VER) && defined(ENABLE_MINIDUMPS)
+#define DEFINE_MAIN(function) \
+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) \
+{ \
+    ParseArguments(GetCommandLineW()); \
+    __try \
+    { \
+        function; \
+    } \
+    __except(WriteMiniDump("Urho3D", GetExceptionInformation())) \
+    { \
+    } \
+    return 0; \
+}
+// Other Win32: just execute the function
+#elif defined(WIN32)
+#define DEFINE_MAIN(function) \
+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) \
+{ \
+    ParseArguments(GetCommandLineW()); \
+    function; \
+    return 0; \
+}
+// Android or iOS: use SDL_main
+#elif defined(ANDROID) || defined(IOS)
+#define DEFINE_MAIN(function) \
+extern "C" int SDL_main(int argc, char** argv); \
+int SDL_main(int argc, char** argv) \
+{ \
+    ParseArguments(argc, argv); \
+    function; \
+    return 0; \
+}
+// Linux or OS X
+#else
+#define DEFINE_MAIN(function) \
+int main(int argc, char** argv) \
+{ \
+    ParseArguments(argc, argv); \
+    function; \
+    return 0; \
+}
+#endif

+ 2 - 45
Urho3D/Urho3D.cpp

@@ -25,59 +25,15 @@
 #include "Engine.h"
 #include "FileSystem.h"
 #include "Log.h"
-#include "MiniDump.h"
+#include "Main.h"
 #include "ProcessUtils.h"
 #include "ResourceCache.h"
 #include "ScriptFile.h"
 
 #include <exception>
 
-#ifdef WIN32
-#include <windows.h>
-#endif
-
 #include "DebugNew.h"
 
-void Run();
-
-#ifdef WIN32
-int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
-{
-    #if defined(_MSC_VER) && defined(_DEBUG)
-    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
-    #endif
-    
-    ParseArguments(GetCommandLineW());
-    
-    #if defined(_MSC_VER) && defined(ENABLE_MINIDUMPS) && !defined(_DEBUG)
-    __try
-    {
-        Run();
-    }
-    __except(WriteMiniDump("Urho3D", GetExceptionInformation()))
-    {
-    }
-    #else
-    Run();
-    #endif
-
-    return 0;
-}
-#else
-#if defined(ANDROID) || defined(IOS)
-extern "C" int SDL_main(int argc, char** argv);
-
-int SDL_main(int argc, char** argv)
-#else
-int main(int argc, char** argv)
-#endif
-{
-    ParseArguments(argc, argv);
-    Run();
-    return 0;
-}
-#endif
-
 void Run()
 {
     try
@@ -144,3 +100,4 @@ void Run()
     }
 }
 
+DEFINE_MAIN(Run());