Main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #else
  41. int main()
  42. {
  43. runApplication();
  44. return 0;
  45. }
  46. #endif // End BS_PLATFORM
  47. using namespace bs;
  48. void runApplication()
  49. {
  50. Path gameSettingsPath = Paths::getGameSettingsPath();
  51. FileDecoder fd(gameSettingsPath);
  52. SPtr<GameSettings> gameSettings = std::static_pointer_cast<GameSettings>(fd.decode());
  53. if (gameSettings == nullptr)
  54. gameSettings = bs_shared_ptr_new<GameSettings>();
  55. unsigned int resolutionWidth = 200;
  56. unsigned int resolutionHeight = 200;
  57. if (!gameSettings->fullscreen)
  58. {
  59. resolutionWidth = gameSettings->resolutionWidth;
  60. resolutionHeight = gameSettings->resolutionHeight;
  61. }
  62. START_UP_DESC startUpDesc;
  63. startUpDesc.renderAPI = BS_RENDER_API_MODULE;
  64. startUpDesc.renderer = BS_RENDERER_MODULE;
  65. startUpDesc.audio = BS_AUDIO_MODULE;
  66. startUpDesc.physics = BS_PHYSICS_MODULE;
  67. startUpDesc.scripting = true;
  68. startUpDesc.primaryWindowDesc.videoMode = VideoMode(resolutionWidth, resolutionHeight);
  69. startUpDesc.primaryWindowDesc.title = toString(gameSettings->titleBarText);
  70. startUpDesc.primaryWindowDesc.fullscreen = false;
  71. startUpDesc.primaryWindowDesc.hidden = gameSettings->fullscreen;
  72. startUpDesc.primaryWindowDesc.depthBuffer = false;
  73. Application::startUp(startUpDesc);
  74. // Note: What if script tries to load resources during startup? The manifest nor the mapping wont be set up yet.
  75. Path resourcesPath = Paths::getGameResourcesPath();
  76. Path resourceMappingPath = resourcesPath + GAME_RESOURCE_MAPPING_NAME;
  77. FileDecoder mappingFd(resourceMappingPath);
  78. SPtr<ResourceMapping> resMapping = std::static_pointer_cast<ResourceMapping>(mappingFd.decode());
  79. GameResourceManager::instance().setMapping(resMapping);
  80. if (gameSettings->fullscreen)
  81. {
  82. if (gameSettings->useDesktopResolution)
  83. {
  84. const VideoModeInfo& videoModeInfo = RenderAPI::getVideoModeInfo();
  85. const VideoOutputInfo& primaryMonitorInfo = videoModeInfo.getOutputInfo(0);
  86. const VideoMode& selectedVideoMode = primaryMonitorInfo.getDesktopVideoMode();
  87. SPtr<RenderWindow> window = gApplication().getPrimaryWindow();
  88. window->setFullscreen(selectedVideoMode);
  89. resolutionWidth = selectedVideoMode.getWidth();
  90. resolutionHeight = selectedVideoMode.getHeight();
  91. }
  92. else
  93. {
  94. resolutionWidth = gameSettings->resolutionWidth;
  95. resolutionHeight = gameSettings->resolutionHeight;
  96. VideoMode videoMode(resolutionWidth, resolutionHeight);
  97. SPtr<RenderWindow> window = gApplication().getPrimaryWindow();
  98. window->show();
  99. window->setFullscreen(videoMode);
  100. }
  101. }
  102. gameSettings->useDesktopResolution = false; // Not relevant after first startup
  103. // TODO - Save full video mode
  104. gameSettings->resolutionWidth = resolutionWidth;
  105. gameSettings->resolutionHeight = resolutionHeight;
  106. FileEncoder fe(gameSettingsPath);
  107. fe.encode(gameSettings.get());
  108. Path resourceManifestPath = resourcesPath + GAME_RESOURCE_MANIFEST_NAME;
  109. SPtr<ResourceManifest> manifest;
  110. if (FileSystem::exists(resourceManifestPath))
  111. {
  112. Path resourceRoot = resourcesPath;
  113. resourceRoot.makeParent(); // Remove /Resources entry, as we expect all resources to be relative to that path
  114. manifest = ResourceManifest::load(resourceManifestPath, resourceRoot);
  115. gResources().registerResourceManifest(manifest);
  116. }
  117. {
  118. HPrefab mainScene = static_resource_cast<Prefab>(gResources().loadFromUUID(gameSettings->mainSceneUUID,
  119. false, ResourceLoadFlag::LoadDependencies));
  120. if (mainScene.isLoaded(false))
  121. {
  122. HSceneObject root = mainScene->instantiate();
  123. HSceneObject oldRoot = gSceneManager().getRootNode();
  124. gSceneManager()._setRootNode(root);
  125. oldRoot->destroy();
  126. }
  127. }
  128. Application::instance().runMainLoop();
  129. Application::shutDown();
  130. }