diff --git a/framework/content/manager.cpp b/framework/content/manager.cpp index 28a523e..ce6b668 100644 --- a/framework/content/manager.cpp +++ b/framework/content/manager.cpp @@ -1 +1,13 @@ -#include "content/manager.hpp" \ No newline at end of file +#include "content/manager.hpp" + +namespace xna { + sptr ContentManager::OpenStream(String const& assetName) const { + const String filePath = _rootDirectory + "\\" + assetName + contentExtension; + const auto stream = New(filePath, FileMode::Open); + + if (stream->IsClosed()) + return nullptr; + + return reinterpret_pointer_cast(stream); + } +} \ No newline at end of file diff --git a/framework/platform-dx/game.cpp b/framework/platform-dx/game.cpp index a539ba9..21e9a23 100644 --- a/framework/platform-dx/game.cpp +++ b/framework/platform-dx/game.cpp @@ -11,7 +11,8 @@ namespace xna { impl = unew(); services = New(); auto iservice = reinterpret_pointer_cast(services); - _contentManager = New("", services); + _contentManager = New(services, ""); + _contentManager->_gameServices = iservice; _gameWindow = New(); _gameWindow->impl->Color(146, 150, 154); diff --git a/inc/content/manager.hpp b/inc/content/manager.hpp index e455ce0..37337c7 100644 --- a/inc/content/manager.hpp +++ b/inc/content/manager.hpp @@ -1,53 +1,63 @@ #ifndef XNA_CONTENT_MANAGER_HPP #define XNA_CONTENT_MANAGER_HPP -#include "../csharp/stream.hpp" -#include "../default.hpp" -#include "../game/servicecontainer.hpp" +#include "csharp/stream.hpp" +#include "default.hpp" +#include "csharp/service.hpp" #include "reader.hpp" -#include -#include -#include namespace xna { + //The run-time component which loads managed objects from the binary files produced by the design time content pipeline. class ContentManager { public: - friend class ContentReader; + ContentManager(sptr const& services) : + _rootDirectory("") { + _services = services; + }; - ContentManager(String const& rootDirectory, sptr const& services) : + ContentManager(sptr const& services, String const& rootDirectory) : _rootDirectory(rootDirectory){ _services = services; }; - static sptr Services() { + //Gets the service provider associated with the ContentManager. + sptr ServiceProvider() const { return _services; } + //Gets or sets the root directory associated with this ContentManager. constexpr String RootDirectory() const { return _rootDirectory; } + //Gets or sets the root directory associated with this ContentManager. void RootDirectory(String const& value) { _rootDirectory = value; } + //Loads an asset that has been processed by the Content Pipeline. template auto Load(String const& assetName) { if (assetName.empty()) { return XnaHelper::ReturnDefaultOrNull(); } - auto obj2 = ReadAsset(assetName); + const auto obj2 = ReadAsset(assetName); return obj2; } + //Gets the service provider associated with the main Game. + static sptr GameServiceProvider() { + return _gameServices; + } + protected: template auto ReadAsset(String const& assetName) { auto input = OpenStream(assetName); - if (input->IsClosed()) + if (!input) return XnaHelper::ReturnDefaultOrNull(); auto contentReader = ContentReader::Create(this, input, assetName); @@ -56,19 +66,18 @@ namespace xna { return asset; } - sptr OpenStream(String const& assetName) { - String filePath = _rootDirectory + "\\" + assetName + contentExtension; - const auto stream = New(filePath, FileMode::Open); - //const auto stream = New(filePath); - return reinterpret_pointer_cast(stream); - } + sptr OpenStream(String const& assetName) const; private: + friend class ContentReader; + friend class Game; + String _rootDirectory; std::vector byteBuffer; + sptr _services = nullptr; + inline static sptr _gameServices = nullptr; inline const static String contentExtension = ".xnb"; - inline static sptr _services = nullptr; }; } diff --git a/inc/content/reader.hpp b/inc/content/reader.hpp index 221dbad..94b0e17 100644 --- a/inc/content/reader.hpp +++ b/inc/content/reader.hpp @@ -1,11 +1,11 @@ #ifndef XNA_CONTENT_READER_HPP #define XNA_CONTENT_READER_HPP -#include "../common/color.hpp" -#include "../common/numerics.hpp" -#include "../csharp/binary.hpp" -#include "../csharp/type.hpp" -#include "../default.hpp" +#include "common/color.hpp" +#include "common/numerics.hpp" +#include "csharp/binary.hpp" +#include "csharp/type.hpp" +#include "default.hpp" #include "typereadermanager.hpp" #include diff --git a/inc/content/readers/graphics.hpp b/inc/content/readers/graphics.hpp index dbaca67..14297c2 100644 --- a/inc/content/readers/graphics.hpp +++ b/inc/content/readers/graphics.hpp @@ -22,7 +22,7 @@ namespace xna { const auto height = input.ReadInt32(); const auto mipMaps = input.ReadInt32(); - auto a_device = ContentManager::Services()->GetService(*typeof()); + auto a_device = ContentManager::GameServiceProvider()->GetService(*typeof()); sptr device = nullptr; if (a_device.has_value()) diff --git a/inc/types.hpp b/inc/types.hpp index 70e067b..b58a932 100644 --- a/inc/types.hpp +++ b/inc/types.hpp @@ -43,6 +43,10 @@ namespace xna { constexpr double DoubleMaxValue = (std::numeric_limits::max)(); constexpr double DoubleMinValue = (std::numeric_limits::min)(); + // + // About strings: https://stackoverflow.com/questions/402283/stdwstring-vs-stdstring + // + using String = std::string; using WString = std::wstring; diff --git a/samples/02_PlatfformerStarterKit/level.cpp b/samples/02_PlatfformerStarterKit/level.cpp index 03b9f76..6d70f62 100644 --- a/samples/02_PlatfformerStarterKit/level.cpp +++ b/samples/02_PlatfformerStarterKit/level.cpp @@ -10,7 +10,7 @@ namespace PlatformerStarterKit { Level::Level(xna::sptr const& serviceProvider, xna::String const& path) : path(path) { - content = xna::snew("Content", serviceProvider); + content = xna::snew(serviceProvider, "Content"); timeRemaining = xna::TimeSpan::FromMinutes(2.0); layers = std::vector(3);