diff --git a/framework/content/lzx/decoder.cpp b/framework/content/lzx/decoder.cpp index c545867..81ad46a 100644 --- a/framework/content/lzx/decoder.cpp +++ b/framework/content/lzx/decoder.cpp @@ -11,8 +11,8 @@ namespace xna { } int LzxDecoder::Decompress(Stream* inData, int inLen, Stream* outData, int outLen) { - auto input = reinterpret_cast(inData->Data()); - auto output = reinterpret_cast(outData->Data()); + mspack_file* input = nullptr; + mspack_file* output = nullptr; auto lzxstream = lzxd_init( //struct mspack_system* system, diff --git a/framework/platform-dx/texture.cpp b/framework/platform-dx/texture.cpp index 2d74881..fe4191b 100644 --- a/framework/platform-dx/texture.cpp +++ b/framework/platform-dx/texture.cpp @@ -1,41 +1,7 @@ #include "xna/xna-dx.hpp" namespace xna { - Texture2D::~Texture2D() { - impl = nullptr; - } - - sptr Texture2D::FromStream(GraphicsDevice& device, String const& fileName) - { - auto _this = device.shared_from_this(); - auto texture2d = snew(_this); - comptr resource = nullptr; - auto wstr = XnaHelper::ToWString(fileName); - - HRESULT result = DirectX::CreateWICTextureFromFile( - device.impl->_device.Get(), - device.impl->_context.Get(), - wstr.c_str(), - resource.GetAddressOf(), - texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf(), - 0U); - - if (FAILED(result)) { - return nullptr; - } - - result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf()); - - if (FAILED(result)) { - return nullptr; - } - - D3D11_TEXTURE2D_DESC desc; - texture2d->impl->dxTexture2D->GetDesc(&desc); - texture2d->impl->dxDescription = desc; - - return texture2d; - } + static HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data); void Texture2D::Initialize() { @@ -105,39 +71,7 @@ namespace xna { impl->dxDescription.Height = static_cast(height); impl->dxDescription.MipLevels = static_cast(mipMap); impl->dxDescription.Format = DxHelpers::SurfaceFormatToDx(format); - } - - HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data) - { - if (!impl.dxTexture2D) { - auto hr = device.impl->_device->CreateTexture2D(&impl.dxDescription, nullptr, impl.dxTexture2D.ReleaseAndGetAddressOf()); - - if (FAILED(hr)) { - Exception::Throw(Exception::FAILED_TO_CREATE); - } - } - - comptr resource = nullptr; - auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); - - if (FAILED(hr)) { - Exception::Throw(Exception::INVALID_OPERATION); - } - - constexpr int R8G8B8A8U_BYTE_SIZE = 4; - device.impl->_context->UpdateSubresource(resource.Get(), 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); - - impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels; - hr = device.impl->_device->CreateShaderResourceView(resource.Get(), &impl.dxShaderDescription, impl.dxShaderResource.ReleaseAndGetAddressOf()); - - if (FAILED(hr)) { - Exception::Throw(Exception::FAILED_TO_CREATE); - } - - impl.dxTexture2D->GetDesc(&impl.dxDescription); - - return NO_ERROR; - } + } void Texture2D::SetData(std::vector const& data, size_t startIndex, size_t elementCount) { @@ -167,7 +101,7 @@ namespace xna { } internalSetData(*impl, *m_device, finalData.data()); - } + } void Texture2D::SetData(Int level, Rectangle* rect, std::vector const& data, size_t startIndex, size_t elementCount) { @@ -244,7 +178,48 @@ namespace xna { internalSetData(*impl, *m_device, finalData.data()); } - sptr Texture2D::FromMemory(GraphicsDevice& device, std::vector const& data) + P_Texture2D Texture2D::FromStream(GraphicsDevice& device, P_Stream const& stream) + { + std::vector data; + const auto lenght = stream->Length(); + stream->Read(data, 0, lenght - 1); + + return FromStream(device, data); + } + + sptr Texture2D::FromStream(GraphicsDevice& device, String const& fileName) + { + auto _this = device.shared_from_this(); + auto texture2d = snew(_this); + comptr resource = nullptr; + auto wstr = XnaHelper::ToWString(fileName); + + HRESULT result = DirectX::CreateWICTextureFromFile( + device.impl->_device.Get(), + device.impl->_context.Get(), + wstr.c_str(), + resource.GetAddressOf(), + texture2d->impl->dxShaderResource.ReleaseAndGetAddressOf(), + 0U); + + if (FAILED(result)) { + return nullptr; + } + + result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)texture2d->impl->dxTexture2D.ReleaseAndGetAddressOf()); + + if (FAILED(result)) { + return nullptr; + } + + D3D11_TEXTURE2D_DESC desc; + texture2d->impl->dxTexture2D->GetDesc(&desc); + texture2d->impl->dxDescription = desc; + + return texture2d; + } + + sptr Texture2D::FromStream(GraphicsDevice& device, std::vector const& data) { auto _this = device.shared_from_this(); auto texture2d = snew(_this); @@ -277,24 +252,50 @@ namespace xna { } Int Texture2D::Width() const { - if (!impl) return 0; - return static_cast(impl->dxDescription.Width); } Int Texture2D::Height() const { - if (!impl) return 0; - return static_cast(impl->dxDescription.Height); } Rectangle Texture2D::Bounds() const { - if (!impl) return {}; - return Rectangle( 0, 0, static_cast(impl->dxDescription.Width), static_cast(impl->dxDescription.Height) ); } + + HRESULT internalSetData(Texture2D::PlatformImplementation& impl, GraphicsDevice& device, UINT const* data) + { + if (!impl.dxTexture2D) { + auto hr = device.impl->_device->CreateTexture2D(&impl.dxDescription, nullptr, impl.dxTexture2D.ReleaseAndGetAddressOf()); + + if (FAILED(hr)) { + Exception::Throw(Exception::FAILED_TO_CREATE); + } + } + + comptr resource = nullptr; + auto hr = impl.dxTexture2D->QueryInterface(IID_ID3D11Resource, (void**)resource.GetAddressOf()); + + if (FAILED(hr)) { + Exception::Throw(Exception::INVALID_OPERATION); + } + + constexpr int R8G8B8A8U_BYTE_SIZE = 4; + device.impl->_context->UpdateSubresource(resource.Get(), 0, nullptr, data, impl.dxDescription.Width * R8G8B8A8U_BYTE_SIZE, 0); + + impl.dxShaderDescription.Texture2D.MipLevels = impl.dxDescription.MipLevels; + hr = device.impl->_device->CreateShaderResourceView(resource.Get(), &impl.dxShaderDescription, impl.dxShaderResource.ReleaseAndGetAddressOf()); + + if (FAILED(hr)) { + Exception::Throw(Exception::FAILED_TO_CREATE); + } + + impl.dxTexture2D->GetDesc(&impl.dxDescription); + + return NO_ERROR; + } } \ No newline at end of file diff --git a/inc/xna/csharp/stream.hpp b/inc/xna/csharp/stream.hpp index 103f881..58c0304 100644 --- a/inc/xna/csharp/stream.hpp +++ b/inc/xna/csharp/stream.hpp @@ -38,9 +38,7 @@ namespace xna { virtual void Write(std::vector const& buffer, Int offset, Int count) = 0; //Writes a byte to the current position in the stream and advances the position within the stream by one byte. - virtual void WriteByte(Byte value) = 0; - - virtual void* Data() = 0; + virtual void WriteByte(Byte value) = 0; }; //A simplified port of the System.IO.MemoryStream. @@ -84,10 +82,6 @@ namespace xna { virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) override; virtual void Write(std::vector const& buffer, Int offset, Int count) override; virtual void WriteByte(Byte value) override; - - virtual void* Data() override { - return _buffer.data(); - } public: std::vector _buffer; @@ -125,11 +119,7 @@ namespace xna { virtual Int ReadByte() override; virtual void Write(Byte const* buffer, Int bufferLength, Int offset, Int count) override; virtual void Write(std::vector const& buffer, Int offset, Int count) override; - virtual void WriteByte(Byte value) override; - - virtual void* Data() override { - return _fstream.rdbuf(); - } + virtual void WriteByte(Byte value) override; public: std::fstream _fstream; diff --git a/inc/xna/default.hpp b/inc/xna/default.hpp index 9d2c563..b6a4602 100644 --- a/inc/xna/default.hpp +++ b/inc/xna/default.hpp @@ -202,6 +202,9 @@ namespace xna { using P_RasterizerState = sptr; using P_PresentationParameters = sptr; using P_SamplerStateCollection = sptr; + using P_Stream = sptr; + using P_MemoryStream = sptr; + using P_FileStream = sptr; using P_Texture = sptr; using P_Texture2D = sptr; } diff --git a/inc/xna/graphics/texture.hpp b/inc/xna/graphics/texture.hpp index 6d6a00c..58796ae 100644 --- a/inc/xna/graphics/texture.hpp +++ b/inc/xna/graphics/texture.hpp @@ -13,33 +13,53 @@ namespace xna { virtual ~Texture() {} //Gets the format of the texture data. - constexpr SurfaceFormat Format() const { return surfaceFormat; } + virtual SurfaceFormat Format() const = 0; //Gets the number of texture levels in a multilevel texture. - constexpr Int LevelCount() const { return levelCount; } - - protected: - SurfaceFormat surfaceFormat{SurfaceFormat::Color}; - Int levelCount{ 0 }; + virtual Int LevelCount() const = 0; }; + //Represents a 2D grid of texels. class Texture2D : public Texture { public: Texture2D(); Texture2D(P_GraphicsDevice const& device); Texture2D(P_GraphicsDevice const& device, size_t width, size_t height); - Texture2D(P_GraphicsDevice const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); - ~Texture2D() override; + Texture2D(P_GraphicsDevice const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); + + ~Texture2D() override {} + + //Gets the width of this texture resource, in pixels. Int Width() const; + //Gets the height of this texture resource, in pixels. Int Height() const; + //Gets the size of this resource. Rectangle Bounds() const; + //Gets the format of the texture data. + constexpr SurfaceFormat Format() const override { return surfaceFormat; } + //Gets the number of texture levels in a multilevel texture. + constexpr Int LevelCount() const { return levelCount; } + + //Sets data to the texture. void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); + //Sets data to the texture. void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); + //Sets data to the texture. void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); + //Sets data to the texture. void SetData(Int level, Rectangle* rect, std::vector const& data, size_t startIndex, size_t elementCount); - static P_Texture2D FromStream(GraphicsDevice& device, String const& fileName); - static P_Texture2D FromMemory(GraphicsDevice& device, std::vector const& data); - void Initialize(); + //Loads texture data from a stream. + static P_Texture2D FromStream(GraphicsDevice& device, P_Stream const& stream); + //Loads texture data from a file. + static P_Texture2D FromStream(GraphicsDevice& device, String const& fileName); + //Loads texture data from a data. + static P_Texture2D FromStream(GraphicsDevice& device, std::vector const& data); + + void Initialize(); + + private: + SurfaceFormat surfaceFormat{ SurfaceFormat::Color }; + Int levelCount{ 0 }; public: struct PlatformImplementation;