Main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include "BsRenderAPI.h"
  13. void runApplication();
  14. #if BS_PLATFORM == BS_PLATFORM_WIN32
  15. #include <windows.h>
  16. using namespace BansheeEngine;
  17. int CALLBACK WinMain(
  18. _In_ HINSTANCE hInstance,
  19. _In_ HINSTANCE hPrevInstance,
  20. _In_ LPSTR lpCmdLine,
  21. _In_ int nCmdShow
  22. )
  23. {
  24. CrashHandler::startUp();
  25. __try
  26. {
  27. runApplication();
  28. }
  29. __except (gCrashHandler().reportCrash(GetExceptionInformation()))
  30. {
  31. PlatformUtility::terminate(true);
  32. }
  33. CrashHandler::shutDown();
  34. return 0;
  35. }
  36. #endif // End BS_PLATFORM
  37. using namespace BansheeEngine;
  38. void runApplication()
  39. {
  40. Path gameSettingsPath = Paths::getGameSettingsPath();
  41. FileDecoder fd(gameSettingsPath);
  42. SPtr<GameSettings> gameSettings = std::static_pointer_cast<GameSettings>(fd.decode());
  43. if (gameSettings == nullptr)
  44. gameSettings = bs_shared_ptr_new<GameSettings>();
  45. unsigned int resolutionWidth = 200;
  46. unsigned int resolutionHeight = 200;
  47. if (!gameSettings->fullscreen)
  48. {
  49. resolutionWidth = gameSettings->resolutionWidth;
  50. resolutionHeight = gameSettings->resolutionHeight;
  51. }
  52. RENDER_WINDOW_DESC renderWindowDesc;
  53. renderWindowDesc.videoMode = VideoMode(resolutionWidth, resolutionHeight);
  54. renderWindowDesc.title = toString(gameSettings->titleBarText);
  55. renderWindowDesc.fullscreen = false;
  56. renderWindowDesc.hidden = gameSettings->fullscreen;
  57. Application::startUp(renderWindowDesc, RenderAPIPlugin::DX11);
  58. if (gameSettings->fullscreen)
  59. {
  60. if (gameSettings->useDesktopResolution)
  61. {
  62. const VideoModeInfo& videoModeInfo = RenderAPI::getVideoModeInfo();
  63. const VideoOutputInfo& primaryMonitorInfo = videoModeInfo.getOutputInfo(0);
  64. const VideoMode& selectedVideoMode = primaryMonitorInfo.getDesktopVideoMode();
  65. RenderWindowPtr window = gApplication().getPrimaryWindow();
  66. window->setFullscreen(gCoreAccessor(), selectedVideoMode);
  67. resolutionWidth = selectedVideoMode.getWidth();
  68. resolutionHeight = selectedVideoMode.getHeight();
  69. }
  70. else
  71. {
  72. resolutionWidth = gameSettings->resolutionWidth;
  73. resolutionHeight = gameSettings->resolutionHeight;
  74. VideoMode videoMode(resolutionWidth, resolutionHeight);
  75. RenderWindowPtr window = gApplication().getPrimaryWindow();
  76. window->show(gCoreAccessor());
  77. window->setFullscreen(gCoreAccessor(), videoMode);
  78. }
  79. }
  80. gameSettings->useDesktopResolution = false; // Not relevant after first startup
  81. // TODO - Save full video mode
  82. gameSettings->resolutionWidth = resolutionWidth;
  83. gameSettings->resolutionHeight = resolutionHeight;
  84. FileEncoder fe(gameSettingsPath);
  85. fe.encode(gameSettings.get());
  86. Path resourcesPath = Paths::getGameResourcesPath();
  87. Path resourceManifestPath = resourcesPath + GAME_RESOURCE_MANIFEST_NAME;
  88. ResourceManifestPtr manifest;
  89. if (FileSystem::exists(resourceManifestPath))
  90. {
  91. Path resourceRoot = FileSystem::getWorkingDirectoryPath();
  92. resourceRoot.append(resourcesPath);
  93. resourceRoot.makeParent(); // Remove /Resources entry, as we expect all resources to be relative to that path
  94. manifest = ResourceManifest::load(resourceManifestPath, resourceRoot);
  95. gResources().registerResourceManifest(manifest);
  96. }
  97. {
  98. HPrefab mainScene = static_resource_cast<Prefab>(gResources().loadFromUUID(gameSettings->mainSceneUUID, false, true, false));
  99. if (mainScene != nullptr)
  100. {
  101. HSceneObject root = mainScene->instantiate();
  102. HSceneObject oldRoot = gSceneManager().getRootNode();
  103. gSceneManager()._setRootNode(root);
  104. oldRoot->destroy();
  105. }
  106. }
  107. Application::instance().runMainLoop();
  108. Application::shutDown();
  109. }