Main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "BsApplication.h"
  2. #include "BsCrashHandler.h"
  3. #include "BsCoreThread.h"
  4. #include "BsFileSerializer.h"
  5. #include "BsGameSettings.h"
  6. #include "BsFileSystem.h"
  7. #include "BsResources.h"
  8. #include "BsResourceManifest.h"
  9. #include "BsPrefab.h"
  10. #include "BsSceneObject.h"
  11. #include "BsSceneManager.h"
  12. using namespace BansheeEngine;
  13. void runApplication()
  14. {
  15. Path gameSettingsPath = RUNTIME_DATA_PATH + GAME_SETTINGS_NAME;
  16. FileDecoder fd(gameSettingsPath);
  17. SPtr<GameSettings> gameSettings = std::static_pointer_cast<GameSettings>(fd.decode());
  18. if (gameSettings == nullptr)
  19. gameSettings = bs_shared_ptr_new<GameSettings>();
  20. unsigned int resolutionWidth = 200;
  21. unsigned int resolutionHeight = 200;
  22. if (!gameSettings->fullscreen)
  23. {
  24. resolutionWidth = gameSettings->resolutionWidth;
  25. resolutionHeight = gameSettings->resolutionHeight;
  26. }
  27. RENDER_WINDOW_DESC renderWindowDesc;
  28. renderWindowDesc.videoMode = VideoMode(resolutionWidth, resolutionHeight);
  29. renderWindowDesc.title = toString(gameSettings->titleBarText);
  30. renderWindowDesc.fullscreen = false;
  31. renderWindowDesc.hideUntilSwap = true;
  32. Application::startUp(renderWindowDesc, RenderAPIPlugin::DX11);
  33. if (gameSettings->fullscreen)
  34. {
  35. if (gameSettings->useDesktopResolution)
  36. {
  37. const VideoModeInfo& videoModeInfo = RenderAPI::getVideoModeInfo();
  38. const VideoOutputInfo& primaryMonitorInfo = videoModeInfo.getOutputInfo(0);
  39. const VideoMode& selectedVideoMode = primaryMonitorInfo.getDesktopVideoMode();
  40. RenderWindowPtr window = gApplication().getPrimaryWindow();
  41. window->setFullscreen(gCoreAccessor(), selectedVideoMode);
  42. resolutionWidth = selectedVideoMode.getWidth();
  43. resolutionHeight = selectedVideoMode.getHeight();
  44. }
  45. else
  46. {
  47. VideoMode videoMode(resolutionWidth, resolutionHeight);
  48. RenderWindowPtr window = gApplication().getPrimaryWindow();
  49. window->setFullscreen(gCoreAccessor(), videoMode);
  50. }
  51. }
  52. gameSettings->useDesktopResolution = false; // Not relevant after first startup
  53. // TODO - Save full video mode
  54. gameSettings->resolutionWidth = resolutionWidth;
  55. gameSettings->resolutionHeight = resolutionHeight;
  56. FileEncoder fe(gameSettingsPath);
  57. fe.encode(gameSettings.get());
  58. Path resourceManifestPath = GAME_RESOURCES_PATH + GAME_RESOURCE_MANIFEST_NAME;
  59. ResourceManifestPtr manifest;
  60. if (FileSystem::exists(resourceManifestPath))
  61. {
  62. Path resourcesPath = FileSystem::getWorkingDirectoryPath();
  63. resourcesPath.append(GAME_RESOURCES_PATH);
  64. manifest = ResourceManifest::load(resourceManifestPath, resourcesPath);
  65. gResources().registerResourceManifest(manifest);
  66. }
  67. HPrefab mainScene = static_resource_cast<Prefab>(gResources().loadFromUUID(gameSettings->mainSceneUUID));
  68. if (mainScene != nullptr)
  69. {
  70. HSceneObject root = mainScene->instantiate();
  71. HSceneObject oldRoot = gSceneManager().getRootNode();
  72. gSceneManager()._setRootNode(root);
  73. oldRoot->destroy();
  74. }
  75. Application::instance().runMainLoop();
  76. Application::shutDown();
  77. }
  78. #if BS_PLATFORM == BS_PLATFORM_WIN32
  79. #include <windows.h>
  80. int CALLBACK WinMain(
  81. _In_ HINSTANCE hInstance,
  82. _In_ HINSTANCE hPrevInstance,
  83. _In_ LPSTR lpCmdLine,
  84. _In_ int nCmdShow
  85. )
  86. {
  87. CrashHandler::startUp();
  88. __try
  89. {
  90. runApplication();
  91. }
  92. __except (gCrashHandler().reportCrash(GetExceptionInformation()))
  93. {
  94. PlatformUtility::terminate(true);
  95. }
  96. CrashHandler::shutDown();
  97. return 0;
  98. }
  99. #endif // End BS_PLATFORM