Main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.physicsCooking = false;
  69. startUpDesc.primaryWindowDesc.videoMode = VideoMode(resolutionWidth, resolutionHeight);
  70. startUpDesc.primaryWindowDesc.title = toString(gameSettings->titleBarText);
  71. startUpDesc.primaryWindowDesc.fullscreen = false;
  72. startUpDesc.primaryWindowDesc.hidden = gameSettings->fullscreen;
  73. startUpDesc.primaryWindowDesc.depthBuffer = false;
  74. Application::startUp(startUpDesc);
  75. // Note: What if script tries to load resources during startup? The manifest nor the mapping wont be set up yet.
  76. Path resourcesPath = Paths::getGameResourcesPath();
  77. Path resourceMappingPath = resourcesPath + GAME_RESOURCE_MAPPING_NAME;
  78. FileDecoder mappingFd(resourceMappingPath);
  79. SPtr<ResourceMapping> resMapping = std::static_pointer_cast<ResourceMapping>(mappingFd.decode());
  80. GameResourceManager::instance().setMapping(resMapping);
  81. if (gameSettings->fullscreen)
  82. {
  83. if (gameSettings->useDesktopResolution)
  84. {
  85. const VideoModeInfo& videoModeInfo = RenderAPI::getVideoModeInfo();
  86. const VideoOutputInfo& primaryMonitorInfo = videoModeInfo.getOutputInfo(0);
  87. const VideoMode& selectedVideoMode = primaryMonitorInfo.getDesktopVideoMode();
  88. SPtr<RenderWindow> window = gApplication().getPrimaryWindow();
  89. window->setFullscreen(selectedVideoMode);
  90. resolutionWidth = selectedVideoMode.getWidth();
  91. resolutionHeight = selectedVideoMode.getHeight();
  92. }
  93. else
  94. {
  95. resolutionWidth = gameSettings->resolutionWidth;
  96. resolutionHeight = gameSettings->resolutionHeight;
  97. VideoMode videoMode(resolutionWidth, resolutionHeight);
  98. SPtr<RenderWindow> window = gApplication().getPrimaryWindow();
  99. window->show();
  100. window->setFullscreen(videoMode);
  101. }
  102. }
  103. gameSettings->useDesktopResolution = false; // Not relevant after first startup
  104. // TODO - Save full video mode
  105. gameSettings->resolutionWidth = resolutionWidth;
  106. gameSettings->resolutionHeight = resolutionHeight;
  107. FileEncoder fe(gameSettingsPath);
  108. fe.encode(gameSettings.get());
  109. Path resourceManifestPath = resourcesPath + GAME_RESOURCE_MANIFEST_NAME;
  110. SPtr<ResourceManifest> manifest;
  111. if (FileSystem::exists(resourceManifestPath))
  112. {
  113. Path resourceRoot = resourcesPath;
  114. resourceRoot.makeParent(); // Remove /Resources entry, as we expect all resources to be relative to that path
  115. manifest = ResourceManifest::load(resourceManifestPath, resourceRoot);
  116. gResources().registerResourceManifest(manifest);
  117. }
  118. {
  119. HPrefab mainScene = static_resource_cast<Prefab>(gResources().loadFromUUID(gameSettings->mainSceneUUID,
  120. false, ResourceLoadFlag::LoadDependencies));
  121. if (mainScene.isLoaded(false))
  122. {
  123. HSceneObject root = mainScene->instantiate();
  124. HSceneObject oldRoot = gSceneManager().getRootNode();
  125. gSceneManager().setRootNode(root);
  126. oldRoot->destroy();
  127. }
  128. }
  129. Application::instance().runMainLoop();
  130. Application::shutDown();
  131. }