Main.cpp 4.6 KB

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