mirror of
https://github.com/borgesdan/xn65
synced 2024-12-29 21:54:47 +01:00
Implementações em Content
This commit is contained in:
parent
319b545a03
commit
d3ceb91ca1
@ -15,15 +15,16 @@ namespace xna {
|
||||
friend class ContentReader;
|
||||
|
||||
ContentManager(String const& rootDirectory, sptr<GameServiceContainer> const& services) :
|
||||
_rootDirectory(rootDirectory),
|
||||
_services(services),
|
||||
_path(rootDirectory){};
|
||||
_rootDirectory(rootDirectory),
|
||||
_path(rootDirectory){
|
||||
_services = services;
|
||||
};
|
||||
|
||||
virtual ~ContentManager(){
|
||||
Unload();
|
||||
}
|
||||
|
||||
sptr<GameServiceContainer> Services() {
|
||||
static sptr<GameServiceContainer> Services() {
|
||||
return _services;
|
||||
}
|
||||
|
||||
@ -44,14 +45,14 @@ namespace xna {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T Load(String const& assetName) {
|
||||
sptr<T> Load(String const& assetName) {
|
||||
if (assetName.empty()) return nullptr;
|
||||
|
||||
if (_loadedAssets.contains(assetName)) {
|
||||
auto& ptr = _loadedAssets[assetName];
|
||||
auto obj1 = reinterpret_pointer_cast<T>(ptr);
|
||||
|
||||
return *obj1;
|
||||
return obj1;
|
||||
}
|
||||
|
||||
auto obj2 = ReadAsset<T>(assetName);
|
||||
@ -61,7 +62,7 @@ namespace xna {
|
||||
|
||||
protected:
|
||||
template <typename T>
|
||||
T ReadAsset(String const& assetName) {
|
||||
sptr<T> ReadAsset(String const& assetName) {
|
||||
auto input = OpenStream(assetName);
|
||||
auto contentReader = ContentReader::Create(this, input, assetName);
|
||||
|
||||
@ -80,7 +81,8 @@ namespace xna {
|
||||
std::map<String, sptr<void>> _loadedAssets;
|
||||
inline const static String contentExtension = ".xnb";
|
||||
std::vector<Byte> byteBuffer;
|
||||
sptr<GameServiceContainer> _services = nullptr;
|
||||
|
||||
inline static sptr<GameServiceContainer> _services = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -17,13 +17,13 @@ namespace xna {
|
||||
static sptr<ContentReader> Create(ContentManager* contentManager, sptr<Stream>& input, String const& assetName);
|
||||
|
||||
template <typename T>
|
||||
T ReadAsset();
|
||||
sptr<T> ReadAsset();
|
||||
|
||||
template <typename T>
|
||||
T ReadObject();
|
||||
sptr<T> ReadObject();
|
||||
|
||||
template <typename T>
|
||||
T ReadObject(T existingInstance);
|
||||
sptr<T> ReadObject(T existingInstance);
|
||||
|
||||
Vector2 ReadVector2();
|
||||
Vector3 ReadVector3();
|
||||
@ -45,10 +45,10 @@ namespace xna {
|
||||
Int ReadHeader();
|
||||
|
||||
template <typename T>
|
||||
T ReadObjectInternal(std::any& existingInstance, xna_error_nullarg);
|
||||
sptr<T> ReadObjectInternal(std::any& existingInstance, xna_error_nullarg);
|
||||
|
||||
template <typename T>
|
||||
T InvokeReader(ContentTypeReader& reader, std::any& existingInstance, xna_error_nullarg);
|
||||
sptr<T> InvokeReader(ContentTypeReader& reader, std::any& existingInstance, xna_error_nullarg);
|
||||
|
||||
private:
|
||||
ContentManager* _contentManager = nullptr;
|
||||
@ -63,19 +63,19 @@ namespace xna {
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline T ContentReader::ReadObjectInternal(std::any& existingInstance, xna_error_ptr_arg)
|
||||
inline sptr<T> ContentReader::ReadObjectInternal(std::any& existingInstance, xna_error_ptr_arg)
|
||||
{
|
||||
const auto num = Read7BitEncodedInt();
|
||||
|
||||
if (num == 0) {
|
||||
return T();
|
||||
return New<T>();
|
||||
}
|
||||
|
||||
const auto index = num - 1;
|
||||
|
||||
if (index >= typeReaders.size()) {
|
||||
xna_error_apply(err, XnaErrorCode::ARGUMENT_OUT_OF_RANGE);
|
||||
return T();
|
||||
return New<T>();
|
||||
}
|
||||
|
||||
auto reader = typeReaders[index];
|
||||
@ -83,38 +83,38 @@ namespace xna {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T ContentReader::InvokeReader(ContentTypeReader& reader, std::any& existingInstance, xna_error_ptr_arg)
|
||||
inline sptr<T> ContentReader::InvokeReader(ContentTypeReader& reader, std::any& existingInstance, xna_error_ptr_arg)
|
||||
{
|
||||
auto contentTypeReader = reinterpret_cast<ContentTypeReaderT<T>*>(&reader);
|
||||
T objB = T();
|
||||
sptr<T> objB = nullptr;
|
||||
|
||||
if (contentTypeReader) {
|
||||
T existingInstance1 = existingInstance.has_value() ? std::any_cast<T>(existingInstance) : T();
|
||||
objB = contentTypeReader->Read(*this, existingInstance1);
|
||||
auto existingInstance1 = existingInstance.has_value() ? std::any_cast<sptr<T>>(existingInstance) : nullptr;
|
||||
objB = contentTypeReader->Read(*this, *existingInstance1);
|
||||
return objB;
|
||||
}
|
||||
|
||||
return T();
|
||||
return New<T>();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T ContentReader::ReadAsset()
|
||||
inline sptr<T> ContentReader::ReadAsset()
|
||||
{
|
||||
const auto sharedResourceCount = ReadHeader();
|
||||
T obj = ReadObject<T>();
|
||||
auto obj = ReadObject<T>();
|
||||
//this.ReadSharedResources(sharedResourceCount);
|
||||
return obj;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T ContentReader::ReadObject()
|
||||
inline sptr<T> ContentReader::ReadObject()
|
||||
{
|
||||
auto a = std::any();
|
||||
return ReadObjectInternal<T>(a);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T ContentReader::ReadObject(T existingInstance)
|
||||
inline sptr<T> ContentReader::ReadObject(T existingInstance)
|
||||
{
|
||||
return ReadObjectInternal<T>(std::any(existingInstance));
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ namespace xna {
|
||||
return std::any();
|
||||
}
|
||||
|
||||
virtual T Read(ContentReader& input, T& existingInstance) = 0;
|
||||
virtual sptr<T> Read(ContentReader& input, T& existingInstance) = 0;
|
||||
};
|
||||
|
||||
//-------------------------------------------------------//
|
||||
@ -175,8 +175,8 @@ namespace xna {
|
||||
});
|
||||
}
|
||||
|
||||
virtual Object Read(ContentReader& input, Object& existingInstance) override {
|
||||
return Object();
|
||||
virtual sptr<Object> Read(ContentReader& input, Object& existingInstance) override {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -3,24 +3,30 @@
|
||||
|
||||
#include "../../content/reader.hpp"
|
||||
#include "../texture-dx.hpp"
|
||||
#include "../../content/manager.hpp"
|
||||
#include "../../csharp/type.hpp"
|
||||
|
||||
namespace xna {
|
||||
class Texture2DReader : public ContentTypeReaderT<Texture2D> {
|
||||
public:
|
||||
Texture2DReader() : ContentTypeReaderT(typeof<Texture2D>()){}
|
||||
|
||||
Texture2D Read(ContentReader& input, Texture2D& existingInstance) override{
|
||||
sptr<Texture2D> Read(ContentReader& input, Texture2D& existingInstance) override{
|
||||
const auto format = static_cast<SurfaceFormat>(input.ReadInt32());
|
||||
const auto width = input.ReadInt32();
|
||||
const auto height = input.ReadInt32();
|
||||
const auto mipMaps = input.ReadInt32();
|
||||
auto texture2D = Texture2D(nullptr, width, height, mipMaps, format);
|
||||
|
||||
auto a_device = ContentManager::Services()->GetService(*typeof<GraphicsDevice>());
|
||||
auto device = std::any_cast<sptr<GraphicsDevice>>(a_device);
|
||||
|
||||
auto texture2D = New<Texture2D>(device.get(), width, height, mipMaps, format);
|
||||
|
||||
for (size_t level = 0; level < mipMaps; ++level) {
|
||||
auto elementCount = input.ReadInt32();
|
||||
std::vector<Byte> data = input.ReadByteBuffer(elementCount);
|
||||
|
||||
texture2D.SetData(level, nullptr, data, 0, elementCount);
|
||||
texture2D->SetData(level, nullptr, data, 0, elementCount);
|
||||
}
|
||||
|
||||
return texture2D;
|
||||
|
@ -19,23 +19,18 @@ namespace xna {
|
||||
|
||||
void Initialize() override {
|
||||
graphics->Initialize();
|
||||
|
||||
std::any device = _graphicsDevice;
|
||||
_services->AddService(*typeof<GraphicsDevice>(), device);
|
||||
|
||||
Game::Initialize();
|
||||
}
|
||||
|
||||
void LoadContent() override {
|
||||
spriteBatch = New<SpriteBatch>(*_graphicsDevice);
|
||||
|
||||
XnaErrorCode err{0};
|
||||
//texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err);
|
||||
//texture = New<Texture2D>(_graphicsDevice.get(), 256, 256);
|
||||
//std::vector<Color> data(256 * 256, 4278190080U);
|
||||
//std::vector<UINT> data(256 * 256, 0xffffffff);
|
||||
//std::vector<Uint> data(256 * 256, 4278190080U);
|
||||
//texture->SetData(data, 0, data.size());
|
||||
tx = contentManager->Load<Texture2D>("Idle");
|
||||
tx.Bind(_graphicsDevice.get());
|
||||
tx.Initialize(&err);
|
||||
|
||||
XnaErrorCode err{0};
|
||||
texture = contentManager->Load<Texture2D>("Idle");
|
||||
Game::LoadContent();
|
||||
}
|
||||
|
||||
@ -50,7 +45,7 @@ namespace xna {
|
||||
_graphicsDevice->Clear(Colors::CornflowerBlue);
|
||||
|
||||
spriteBatch->Begin();
|
||||
spriteBatch->Draw(tx, position, Colors::White);
|
||||
spriteBatch->Draw(*texture, position, Colors::White);
|
||||
spriteBatch->End();
|
||||
|
||||
Game::Draw(gameTime);
|
||||
@ -66,7 +61,7 @@ namespace xna {
|
||||
MouseState oldState{};
|
||||
float vel = 1;
|
||||
int var = 0;
|
||||
Texture2D tx;
|
||||
//Texture2D tx;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -26,5 +26,6 @@
|
||||
#include "content/manager.hpp"
|
||||
#include "content/decstream.hpp"
|
||||
#include "platform/init-dx.hpp"
|
||||
#include "csharp/type.hpp"
|
||||
|
||||
// TODO: Reference additional headers your program requires here.
|
||||
|
Loading…
x
Reference in New Issue
Block a user