Main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsApplication.h"
  4. #include "BsCrashHandler.h"
  5. #include "BsCoreThread.h"
  6. #include "BsFileSerializer.h"
  7. #include "BsGameSettings.h"
  8. #include "BsFileSystem.h"
  9. #include "BsResources.h"
  10. #include "BsResourceManifest.h"
  11. #include "BsPrefab.h"
  12. #include "BsSceneObject.h"
  13. #include "BsSceneManager.h"
  14. #include "BsRenderAPI.h"
  15. #include "BsGameResourceManager.h"
  16. void runApplication();
  17. #if BS_PLATFORM == BS_PLATFORM_WIN32
  18. #include <windows.h>
  19. using namespace BansheeEngine;
  20. int CALLBACK WinMain(
  21. _In_ HINSTANCE hInstance,
  22. _In_ HINSTANCE hPrevInstance,
  23. _In_ LPSTR lpCmdLine,
  24. _In_ int nCmdShow
  25. )
  26. {
  27. CrashHandler::startUp();
  28. __try
  29. {
  30. runApplication();
  31. }
  32. __except (gCrashHandler().reportCrash(GetExceptionInformation()))
  33. {
  34. PlatformUtility::terminate(true);
  35. }
  36. CrashHandler::shutDown();
  37. return 0;
  38. }
  39. #endif // End BS_PLATFORM
  40. using namespace BansheeEngine;
  41. void runApplication()
  42. {
  43. Path gameSettingsPath = Paths::getGameSettingsPath();
  44. FileDecoder fd(gameSettingsPath);
  45. SPtr<GameSettings> gameSettings = std::static_pointer_cast<GameSettings>(fd.decode());
  46. if (gameSettings == nullptr)
  47. gameSettings = bs_shared_ptr_new<GameSettings>();
  48. unsigned int resolutionWidth = 200;
  49. unsigned int resolutionHeight = 200;
  50. if (!gameSettings->fullscreen)
  51. {
  52. resolutionWidth = gameSettings->resolutionWidth;
  53. resolutionHeight = gameSettings->resolutionHeight;
  54. }
  55. RENDER_WINDOW_DESC renderWindowDesc;
  56. renderWindowDesc.videoMode = VideoMode(resolutionWidth, resolutionHeight);
  57. renderWindowDesc.title = toString(gameSettings->titleBarText);
  58. renderWindowDesc.fullscreen = false;
  59. renderWindowDesc.hidden = gameSettings->fullscreen;
  60. renderWindowDesc.depthBuffer = false;
  61. Application::startUp(renderWindowDesc, RenderAPIPlugin::DX11);
  62. // Note: What if script tries to load resources during startup? The manifest nor the mapping wont be set up yet.
  63. Path resourcesPath = Paths::getGameResourcesPath();
  64. Path resourceMappingPath = resourcesPath + GAME_RESOURCE_MAPPING_NAME;
  65. FileDecoder mappingFd(resourceMappingPath);
  66. SPtr<ResourceMapping> resMapping = std::static_pointer_cast<ResourceMapping>(mappingFd.decode());
  67. GameResourceManager::instance().setMapping(resMapping);
  68. if (gameSettings->fullscreen)
  69. {
  70. if (gameSettings->useDesktopResolution)
  71. {
  72. const VideoModeInfo& videoModeInfo = RenderAPI::getVideoModeInfo();
  73. const VideoOutputInfo& primaryMonitorInfo = videoModeInfo.getOutputInfo(0);
  74. const VideoMode& selectedVideoMode = primaryMonitorInfo.getDesktopVideoMode();
  75. SPtr<RenderWindow> window = gApplication().getPrimaryWindow();
  76. window->setFullscreen(gCoreAccessor(), selectedVideoMode);
  77. resolutionWidth = selectedVideoMode.getWidth();
  78. resolutionHeight = selectedVideoMode.getHeight();
  79. }
  80. else
  81. {
  82. resolutionWidth = gameSettings->resolutionWidth;
  83. resolutionHeight = gameSettings->resolutionHeight;
  84. VideoMode videoMode(resolutionWidth, resolutionHeight);
  85. SPtr<RenderWindow> window = gApplication().getPrimaryWindow();
  86. window->show(gCoreAccessor());
  87. window->setFullscreen(gCoreAccessor(), videoMode);
  88. }
  89. }
  90. gameSettings->useDesktopResolution = false; // Not relevant after first startup
  91. // TODO - Save full video mode
  92. gameSettings->resolutionWidth = resolutionWidth;
  93. gameSettings->resolutionHeight = resolutionHeight;
  94. FileEncoder fe(gameSettingsPath);
  95. fe.encode(gameSettings.get());
  96. Path resourceManifestPath = resourcesPath + GAME_RESOURCE_MANIFEST_NAME;
  97. SPtr<ResourceManifest> manifest;
  98. if (FileSystem::exists(resourceManifestPath))
  99. {
  100. Path resourceRoot = FileSystem::getWorkingDirectoryPath();
  101. resourceRoot.append(resourcesPath);
  102. resourceRoot.makeParent(); // Remove /Resources entry, as we expect all resources to be relative to that path
  103. manifest = ResourceManifest::load(resourceManifestPath, resourceRoot);
  104. gResources().registerResourceManifest(manifest);
  105. }
  106. {
  107. HPrefab mainScene = static_resource_cast<Prefab>(gResources().loadFromUUID(gameSettings->mainSceneUUID,
  108. false, ResourceLoadFlag::LoadDependencies));
  109. if (mainScene.isLoaded(false))
  110. {
  111. HSceneObject root = mainScene->instantiate();
  112. HSceneObject oldRoot = gSceneManager().getRootNode();
  113. gSceneManager()._setRootNode(root);
  114. oldRoot->destroy();
  115. }
  116. }
  117. Application::instance().runMainLoop();
  118. Application::shutDown();
  119. }