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

Implementa Texture2D::FromStream

This commit is contained in:
Danilo 2024-04-07 14:06:12 -03:00
parent 321d8f2a3a
commit a57491f161
16 changed files with 213 additions and 115 deletions

View File

@ -26,7 +26,8 @@
"strategy": "external" "strategy": "external"
}, },
"cacheVariables": { "cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug" "CMAKE_BUILD_TYPE": "Debug",
"CMAKE_TOOLCHAIN_FILE": "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
} }
}, },
{ {
@ -34,7 +35,8 @@
"displayName": "x64 Release", "displayName": "x64 Release",
"inherits": "x64-debug", "inherits": "x64-debug",
"cacheVariables": { "cacheVariables": {
"CMAKE_BUILD_TYPE": "Release" "CMAKE_BUILD_TYPE": "Release",
"CMAKE_TOOLCHAIN_FILE": "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
} }
}, },
{ {
@ -46,7 +48,8 @@
"strategy": "external" "strategy": "external"
}, },
"cacheVariables": { "cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug" "CMAKE_BUILD_TYPE": "Debug",
"CMAKE_TOOLCHAIN_FILE": "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
} }
}, },
{ {
@ -54,7 +57,8 @@
"displayName": "x86 Release", "displayName": "x86 Release",
"inherits": "x86-debug", "inherits": "x86-debug",
"cacheVariables": { "cacheVariables": {
"CMAKE_BUILD_TYPE": "Release" "CMAKE_BUILD_TYPE": "Release",
"CMAKE_TOOLCHAIN_FILE": "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
} }
}, },
{ {

View File

@ -10,4 +10,23 @@ if (CMAKE_VERSION VERSION_GREATER 3.12)
endif() endif()
# TODO: Add tests and install targets if needed. # TODO: Add tests and install targets if needed.
target_link_libraries(${PROJECT_NAME} D3d11.lib dxgi.lib)
# -- Biblioteca DirectxTK --
# Url: https://github.com/microsoft/DirectXTK/wiki/DirectXTK
#
# -- Instalação via vcpkg --
# Para efetuar o download do vcpkg verifique o caminho abaixo
# Url: https://learn.microsoft.com/pt-br/vcpkg/get_started/get-started?pivots=shell-cmd
#
# Siga os procedimentos da página oficial do DirectxTK para instalação via vcpkg
#
# [!] Atualize o arquivo CMakePresets.json, nos 'presets' necessários,
# para que find_package execute corretamente
#
# "cacheVariables": {
# "CMAKE_TOOLCHAIN_FILE": "{VCPKG_DIR}\\scripts\\buildsystems\\vcpkg.cmake"
# }
find_package(directxtk CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} D3d11.lib dxgi.lib Microsoft::DirectXTK)

View File

@ -99,6 +99,8 @@ namespace xna {
using PTexture = std::shared_ptr<Texture>; using PTexture = std::shared_ptr<Texture>;
class Texture2D; class Texture2D;
using PTexture2D = std::shared_ptr<Texture2D>; using PTexture2D = std::shared_ptr<Texture2D>;
class VertexInputLayout;
using PVertexInputLayout = std::shared_ptr<VertexInputLayout>;
struct Viewport; struct Viewport;
using PViewport = std::shared_ptr<Viewport>; using PViewport = std::shared_ptr<Viewport>;
} }

View File

@ -9,7 +9,7 @@ namespace xna {
public: public:
virtual ~IRenderTarget2D(){} virtual ~IRenderTarget2D(){}
virtual bool Apply() = 0; virtual bool Bind() = 0;
}; };
} }

View File

@ -9,9 +9,11 @@ namespace xna {
class Texture { class Texture {
}; };
class ITexture2D { class ITexture2D : public Texture {
public: public:
virtual ~ITexture2D(){} virtual ~ITexture2D(){}
virtual Int Width() const = 0;
virtual Int Height() const = 0;
}; };
} }

View File

@ -69,7 +69,7 @@ namespace xna {
_renderTarget2D = New<RenderTarget2D>(this); _renderTarget2D = New<RenderTarget2D>(this);
} }
if (!_renderTarget2D->Apply()) if (!_renderTarget2D->Bind())
return false; return false;
D3D11_VIEWPORT view{}; D3D11_VIEWPORT view{};

View File

@ -16,7 +16,7 @@ namespace xna {
} }
int Game::Run() { int Game::Run() {
if (GraphicsDevice == nullptr) { if (_graphicsDevice == nullptr) {
MessageBox(nullptr, "O dispositivo gráfico não foi inicializar corretamente", "Xna Game Engine", MB_OK); MessageBox(nullptr, "O dispositivo gráfico não foi inicializar corretamente", "Xna Game Engine", MB_OK);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -50,7 +50,7 @@ namespace xna {
this->Draw(gameTime); this->Draw(gameTime);
GraphicsDevice->Present(); _graphicsDevice->Present();
endElapsedTime = _clock.ElapsedTime(); endElapsedTime = _clock.ElapsedTime();
} }

View File

@ -22,7 +22,7 @@ namespace xna {
} }
virtual PGraphicsDevice GetGraphicsDevice() override { virtual PGraphicsDevice GetGraphicsDevice() override {
return GraphicsDevice; return _graphicsDevice;
} }
protected: protected:
@ -31,7 +31,7 @@ namespace xna {
virtual void Update(GameTime const& gameTime) override{} virtual void Update(GameTime const& gameTime) override{}
public: public:
PGraphicsDevice GraphicsDevice{ nullptr }; PGraphicsDevice _graphicsDevice{ nullptr };
protected: protected:
PGameWindow _gameWindow{ nullptr }; PGameWindow _gameWindow{ nullptr };

View File

@ -44,7 +44,7 @@ namespace xna {
return; return;
} }
_game->GraphicsDevice = _device; _game->_graphicsDevice = _device;
} }
void GraphicsDeviceManager::ChangeDevice() { void GraphicsDeviceManager::ChangeDevice() {

View File

@ -8,7 +8,7 @@ namespace xna {
_device = device; _device = device;
} }
bool RenderTarget2D::Apply() { bool RenderTarget2D::Bind() {
if (_texture2D) { if (_texture2D) {
_texture2D->Release(); _texture2D->Release();
_texture2D = nullptr; _texture2D = nullptr;

View File

@ -18,10 +18,11 @@ namespace xna {
} }
} }
virtual bool Apply() override; virtual bool Bind() override;
public: public:
ID3D11RenderTargetView* _renderTargetView = nullptr; ID3D11RenderTargetView* _renderTargetView = nullptr;
D3D11_RENDER_TARGET_VIEW_DESC _renderTargetDesc{};
GraphicsDevice* _device{ nullptr }; GraphicsDevice* _device{ nullptr };
}; };
} }

View File

@ -1,6 +1,54 @@
#include "texture-dx.hpp" #include "texture-dx.hpp"
#include "WICTextureLoader.h"
#include "device-dx.hpp"
#include "../helpers.hpp"
namespace xna { namespace xna {
Texture2D::Texture2D() { Texture2D::Texture2D() {
} }
PTexture2D Texture2D::FromStream(GraphicsDevice& device, String const& fileName, xna_error_ptr_arg)
{
ID3D11Resource* resource = nullptr;
//D3D11ShaderResourceView* view = nullptr;
auto wstr = StringToWString(fileName);
HRESULT result = DirectX::CreateWICTextureFromFile(
device._device, device._context, wstr.c_str(),
&resource, nullptr, 0U);
if (FAILED(result))
{
xna_error_apply(err, XnaErrorCode::STREAM_ERROR);
if (resource) {
resource->Release();
resource = nullptr;
}
return nullptr;
}
ID3D11Texture2D* txt2d = nullptr;
result = resource->QueryInterface(IID_ID3D11Texture2D, (void**)&txt2d);
if (FAILED(result)) {
xna_error_apply(err, XnaErrorCode::BAD_CAST);
if (resource) {
resource->Release();
resource = nullptr;
}
return nullptr;
}
auto texture2d = New<Texture2D>();
texture2d->_texture2D = txt2d;
resource->Release();
resource = nullptr;
return texture2d;
}
} }

View File

@ -4,6 +4,7 @@
#include "../graphics/texture.hpp" #include "../graphics/texture.hpp"
#include "dxgi.h" #include "dxgi.h"
#include "d3d11.h" #include "d3d11.h"
#include "../xnaerror.hpp"
namespace xna { namespace xna {
class Texture2D : public ITexture2D { class Texture2D : public ITexture2D {
@ -17,8 +18,22 @@ namespace xna {
} }
} }
virtual constexpr Int Width() const override {
return _width;
}
virtual constexpr Int Height() const override {
return _height;
}
static PTexture2D FromStream(GraphicsDevice& device, String const& fileName, xna_error_nullarg);
public: public:
ID3D11Texture2D* _texture2D{nullptr}; ID3D11Texture2D* _texture2D{nullptr};
private:
Uint _width{ 0 };
Uint _height{ 0 };
}; };
} }

View File

@ -16,15 +16,19 @@ class Game1 : public Game {
public: public:
Game1() { Game1() {
graphics = New<GraphicsDeviceManager>(this); graphics = New<GraphicsDeviceManager>(this);
XnaErrorCode err;
Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err);
} }
virtual void Update(GameTime const& gameTime) { virtual void Update(GameTime const& gameTime) override {
Game::Update(gameTime); Game::Update(gameTime);
} }
virtual void Draw(GameTime const& gameTime) { virtual void Draw(GameTime const& gameTime) override {
GraphicsDevice->Clear(); _graphicsDevice->Clear();
Game::Draw(gameTime); Game::Draw(gameTime);
} }

View File

@ -12,5 +12,6 @@
#include "platform/game-dx.hpp" #include "platform/game-dx.hpp"
#include "csharp/stream.hpp" #include "csharp/stream.hpp"
#include "platform/gdevicemanager-dx.hpp" #include "platform/gdevicemanager-dx.hpp"
#include "platform/texture-dx.hpp"
// TODO: Reference additional headers your program requires here. // TODO: Reference additional headers your program requires here.

View File

@ -9,6 +9,8 @@ namespace xna {
INVALID_OPERATION, INVALID_OPERATION,
OVERFLOW_OPERATION, OVERFLOW_OPERATION,
NULL_CAST, NULL_CAST,
BAD_CAST,
STREAM_ERROR
}; };
inline void xna_error_apply(XnaErrorCode* source, XnaErrorCode const& value) { inline void xna_error_apply(XnaErrorCode* source, XnaErrorCode const& value) {