Main.cpp 4.9 KB

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