1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementações em ContentManager

This commit is contained in:
Danilo 2024-06-02 20:36:41 -03:00
parent 9327fc0ba5
commit 686693023f
7 changed files with 53 additions and 27 deletions

View File

@ -1 +1,13 @@
#include "content/manager.hpp"
#include "content/manager.hpp"
namespace xna {
sptr<Stream> ContentManager::OpenStream(String const& assetName) const {
const String filePath = _rootDirectory + "\\" + assetName + contentExtension;
const auto stream = New<FileStream>(filePath, FileMode::Open);
if (stream->IsClosed())
return nullptr;
return reinterpret_pointer_cast<Stream>(stream);
}
}

View File

@ -11,7 +11,8 @@ namespace xna {
impl = unew<PlatformImplementation>();
services = New<GameServiceContainer>();
auto iservice = reinterpret_pointer_cast<IServiceProvider>(services);
_contentManager = New<ContentManager>("", services);
_contentManager = New<ContentManager>(services, "");
_contentManager->_gameServices = iservice;
_gameWindow = New<GameWindow>();
_gameWindow->impl->Color(146, 150, 154);

View File

@ -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 <algorithm>
#include <filesystem>
#include <map>
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<IServiceProvider> const& services) :
_rootDirectory("") {
_services = services;
};
ContentManager(String const& rootDirectory, sptr<IServiceProvider> const& services) :
ContentManager(sptr<IServiceProvider> const& services, String const& rootDirectory) :
_rootDirectory(rootDirectory){
_services = services;
};
static sptr<IServiceProvider> Services() {
//Gets the service provider associated with the ContentManager.
sptr<IServiceProvider> 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 <typename T>
auto Load(String const& assetName) {
if (assetName.empty()) {
return XnaHelper::ReturnDefaultOrNull<T>();
}
auto obj2 = ReadAsset<T>(assetName);
const auto obj2 = ReadAsset<T>(assetName);
return obj2;
}
//Gets the service provider associated with the main Game.
static sptr<IServiceProvider> GameServiceProvider() {
return _gameServices;
}
protected:
template <typename T>
auto ReadAsset(String const& assetName) {
auto input = OpenStream(assetName);
if (input->IsClosed())
if (!input)
return XnaHelper::ReturnDefaultOrNull<T>();
auto contentReader = ContentReader::Create(this, input, assetName);
@ -56,19 +66,18 @@ namespace xna {
return asset;
}
sptr<Stream> OpenStream(String const& assetName) {
String filePath = _rootDirectory + "\\" + assetName + contentExtension;
const auto stream = New<FileStream>(filePath, FileMode::Open);
//const auto stream = New<FileStream>(filePath);
return reinterpret_pointer_cast<Stream>(stream);
}
sptr<Stream> OpenStream(String const& assetName) const;
private:
friend class ContentReader;
friend class Game;
String _rootDirectory;
std::vector<Byte> byteBuffer;
sptr<IServiceProvider> _services = nullptr;
inline static sptr<IServiceProvider> _gameServices = nullptr;
inline const static String contentExtension = ".xnb";
inline static sptr<IServiceProvider> _services = nullptr;
};
}

View File

@ -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 <any>

View File

@ -22,7 +22,7 @@ namespace xna {
const auto height = input.ReadInt32();
const auto mipMaps = input.ReadInt32();
auto a_device = ContentManager::Services()->GetService(*typeof<GraphicsDevice>());
auto a_device = ContentManager::GameServiceProvider()->GetService(*typeof<GraphicsDevice>());
sptr<GraphicsDevice> device = nullptr;
if (a_device.has_value())

View File

@ -43,6 +43,10 @@ namespace xna {
constexpr double DoubleMaxValue = (std::numeric_limits<double>::max)();
constexpr double DoubleMinValue = (std::numeric_limits<double>::min)();
//
// About strings: https://stackoverflow.com/questions/402283/stdwstring-vs-stdstring
//
using String = std::string;
using WString = std::wstring;

View File

@ -10,7 +10,7 @@
namespace PlatformerStarterKit {
Level::Level(xna::sptr<xna::IServiceProvider> const& serviceProvider, xna::String const& path) : path(path)
{
content = xna::snew<xna::ContentManager>("Content", serviceProvider);
content = xna::snew<xna::ContentManager>(serviceProvider, "Content");
timeRemaining = xna::TimeSpan::FromMinutes(2.0);
layers = std::vector<xna::PTexture2D>(3);