Main.cpp 4.7 KB

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