Main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. FileDecoder fd(GAME_SETTINGS_PATH);
  40. SPtr<GameSettings> gameSettings = std::static_pointer_cast<GameSettings>(fd.decode());
  41. if (gameSettings == nullptr)
  42. gameSettings = bs_shared_ptr_new<GameSettings>();
  43. unsigned int resolutionWidth = 200;
  44. unsigned int resolutionHeight = 200;
  45. if (!gameSettings->fullscreen)
  46. {
  47. resolutionWidth = gameSettings->resolutionWidth;
  48. resolutionHeight = gameSettings->resolutionHeight;
  49. }
  50. RENDER_WINDOW_DESC renderWindowDesc;
  51. renderWindowDesc.videoMode = VideoMode(resolutionWidth, resolutionHeight);
  52. renderWindowDesc.title = toString(gameSettings->titleBarText);
  53. renderWindowDesc.fullscreen = false;
  54. renderWindowDesc.hidden = gameSettings->fullscreen;
  55. Application::startUp(renderWindowDesc, RenderAPIPlugin::DX11);
  56. if (gameSettings->fullscreen)
  57. {
  58. if (gameSettings->useDesktopResolution)
  59. {
  60. const VideoModeInfo& videoModeInfo = RenderAPI::getVideoModeInfo();
  61. const VideoOutputInfo& primaryMonitorInfo = videoModeInfo.getOutputInfo(0);
  62. const VideoMode& selectedVideoMode = primaryMonitorInfo.getDesktopVideoMode();
  63. RenderWindowPtr window = gApplication().getPrimaryWindow();
  64. window->setFullscreen(gCoreAccessor(), selectedVideoMode);
  65. resolutionWidth = selectedVideoMode.getWidth();
  66. resolutionHeight = selectedVideoMode.getHeight();
  67. }
  68. else
  69. {
  70. resolutionWidth = gameSettings->resolutionWidth;
  71. resolutionHeight = gameSettings->resolutionHeight;
  72. VideoMode videoMode(resolutionWidth, resolutionHeight);
  73. RenderWindowPtr window = gApplication().getPrimaryWindow();
  74. window->show(gCoreAccessor());
  75. window->setFullscreen(gCoreAccessor(), videoMode);
  76. }
  77. }
  78. gameSettings->useDesktopResolution = false; // Not relevant after first startup
  79. // TODO - Save full video mode
  80. gameSettings->resolutionWidth = resolutionWidth;
  81. gameSettings->resolutionHeight = resolutionHeight;
  82. FileEncoder fe(GAME_SETTINGS_PATH);
  83. fe.encode(gameSettings.get());
  84. Path resourceManifestPath = GAME_RESOURCES_PATH + GAME_RESOURCE_MANIFEST_NAME;
  85. ResourceManifestPtr manifest;
  86. if (FileSystem::exists(resourceManifestPath))
  87. {
  88. Path resourcesPath = FileSystem::getWorkingDirectoryPath();
  89. resourcesPath.append(APP_ROOT);
  90. manifest = ResourceManifest::load(resourceManifestPath, resourcesPath);
  91. gResources().registerResourceManifest(manifest);
  92. }
  93. HPrefab mainScene = static_resource_cast<Prefab>(gResources().loadFromUUID(gameSettings->mainSceneUUID));
  94. if (mainScene != nullptr)
  95. {
  96. HSceneObject root = mainScene->instantiate();
  97. HSceneObject oldRoot = gSceneManager().getRootNode();
  98. gSceneManager()._setRootNode(root);
  99. oldRoot->destroy();
  100. }
  101. Application::instance().runMainLoop();
  102. Application::shutDown();
  103. }