mirror of
https://github.com/borgesdan/xn65
synced 2024-12-29 21:54:47 +01:00
Comentários e implementações em Texture2D::FromStream
This commit is contained in:
parent
3c4a25e27e
commit
f9672c2262
@ -11,8 +11,8 @@ namespace xna {
|
||||
}
|
||||
|
||||
int LzxDecoder::Decompress(Stream* inData, int inLen, Stream* outData, int outLen) {
|
||||
auto input = reinterpret_cast<mspack_file*>(inData->Data());
|
||||
auto output = reinterpret_cast<mspack_file*>(outData->Data());
|
||||
mspack_file* input = nullptr;
|
||||
mspack_file* output = nullptr;
|
||||
|
||||
auto lzxstream = lzxd_init(
|
||||
//struct mspack_system* system,
|
||||
|
@ -1,41 +1,7 @@
|
||||
#include "xna/xna-dx.hpp"
|
||||
|
||||
namespace xna {
|
||||
Texture2D::~Texture2D() {
|
||||
impl = nullptr;
|
||||
}
|
||||
|
||||
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName)
|
||||
{
|
||||
auto _this = device.shared_from_this();
|
||||
auto texture2d = snew<Texture2D>(_this);
|
||||
comptr<ID3D11Resource> 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<UINT>(height);
|
||||
impl->dxDescription.MipLevels = static_cast<UINT>(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<ID3D11Resource> 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<Uint> 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<Byte> const& data, size_t startIndex, size_t elementCount)
|
||||
{
|
||||
@ -244,7 +178,48 @@ namespace xna {
|
||||
internalSetData(*impl, *m_device, finalData.data());
|
||||
}
|
||||
|
||||
sptr<Texture2D> Texture2D::FromMemory(GraphicsDevice& device, std::vector<Byte> const& data)
|
||||
P_Texture2D Texture2D::FromStream(GraphicsDevice& device, P_Stream const& stream)
|
||||
{
|
||||
std::vector<Byte> data;
|
||||
const auto lenght = stream->Length();
|
||||
stream->Read(data, 0, lenght - 1);
|
||||
|
||||
return FromStream(device, data);
|
||||
}
|
||||
|
||||
sptr<Texture2D> Texture2D::FromStream(GraphicsDevice& device, String const& fileName)
|
||||
{
|
||||
auto _this = device.shared_from_this();
|
||||
auto texture2d = snew<Texture2D>(_this);
|
||||
comptr<ID3D11Resource> 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> Texture2D::FromStream(GraphicsDevice& device, std::vector<Byte> const& data)
|
||||
{
|
||||
auto _this = device.shared_from_this();
|
||||
auto texture2d = snew<Texture2D>(_this);
|
||||
@ -277,24 +252,50 @@ namespace xna {
|
||||
}
|
||||
|
||||
Int Texture2D::Width() const {
|
||||
if (!impl) return 0;
|
||||
|
||||
return static_cast<Int>(impl->dxDescription.Width);
|
||||
}
|
||||
|
||||
Int Texture2D::Height() const {
|
||||
if (!impl) return 0;
|
||||
|
||||
return static_cast<Int>(impl->dxDescription.Height);
|
||||
}
|
||||
|
||||
Rectangle Texture2D::Bounds() const {
|
||||
if (!impl) return {};
|
||||
|
||||
return Rectangle(
|
||||
0, 0,
|
||||
static_cast<Int>(impl->dxDescription.Width),
|
||||
static_cast<Int>(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<ID3D11Resource> 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;
|
||||
}
|
||||
}
|
@ -38,9 +38,7 @@ namespace xna {
|
||||
virtual void Write(std::vector<Byte> 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<Byte> const& buffer, Int offset, Int count) override;
|
||||
virtual void WriteByte(Byte value) override;
|
||||
|
||||
virtual void* Data() override {
|
||||
return _buffer.data();
|
||||
}
|
||||
|
||||
public:
|
||||
std::vector<Byte> _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<Byte> 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;
|
||||
|
@ -202,6 +202,9 @@ namespace xna {
|
||||
using P_RasterizerState = sptr<RasterizerState>;
|
||||
using P_PresentationParameters = sptr<PresentationParameters>;
|
||||
using P_SamplerStateCollection = sptr<SamplerStateCollection>;
|
||||
using P_Stream = sptr<Stream>;
|
||||
using P_MemoryStream = sptr<MemoryStream>;
|
||||
using P_FileStream = sptr<FileStream>;
|
||||
using P_Texture = sptr<Texture>;
|
||||
using P_Texture2D = sptr<Texture2D>;
|
||||
}
|
||||
|
@ -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<Color> const& data, size_t startIndex = 0, size_t elementCount = 0);
|
||||
//Sets data to the texture.
|
||||
void SetData(std::vector<Uint> const& data, size_t startIndex = 0, size_t elementCount = 0);
|
||||
//Sets data to the texture.
|
||||
void SetData(std::vector<Byte> const& data, size_t startIndex = 0, size_t elementCount = 0);
|
||||
//Sets data to the texture.
|
||||
void SetData(Int level, Rectangle* rect, std::vector<Byte> 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<Byte> 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<Byte> const& data);
|
||||
|
||||
void Initialize();
|
||||
|
||||
private:
|
||||
SurfaceFormat surfaceFormat{ SurfaceFormat::Color };
|
||||
Int levelCount{ 0 };
|
||||
|
||||
public:
|
||||
struct PlatformImplementation;
|
||||
|
Loading…
x
Reference in New Issue
Block a user