Main.cpp 3.7 KB

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