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

@ -1,101 +1,105 @@
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "linux-debug",
"displayName": "Linux Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
},
{
"name": "macos-debug",
"displayName": "macOS Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Darwin"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_TOOLCHAIN_FILE": "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_TOOLCHAIN_FILE": "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_TOOLCHAIN_FILE": "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_TOOLCHAIN_FILE": "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
}
},
{
"name": "linux-debug",
"displayName": "Linux Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
]
}
},
{
"name": "macos-debug",
"displayName": "macOS Debug",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Darwin"
},
"vendor": {
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
}
]
}

View File

@ -10,4 +10,23 @@ if (CMAKE_VERSION VERSION_GREATER 3.12)
endif()
# 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>;
class Texture2D;
using PTexture2D = std::shared_ptr<Texture2D>;
class VertexInputLayout;
using PVertexInputLayout = std::shared_ptr<VertexInputLayout>;
struct Viewport;
using PViewport = std::shared_ptr<Viewport>;
}

View File

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

View File

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

View File

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

View File

@ -16,7 +16,7 @@ namespace xna {
}
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);
return EXIT_FAILURE;
}
@ -50,7 +50,7 @@ namespace xna {
this->Draw(gameTime);
GraphicsDevice->Present();
_graphicsDevice->Present();
endElapsedTime = _clock.ElapsedTime();
}

View File

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

View File

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

View File

@ -8,7 +8,7 @@ namespace xna {
_device = device;
}
bool RenderTarget2D::Apply() {
bool RenderTarget2D::Bind() {
if (_texture2D) {
_texture2D->Release();
_texture2D = nullptr;
@ -18,7 +18,7 @@ namespace xna {
return false;
auto& device = _device->_device;
if FAILED(device->CreateRenderTargetView(_texture2D, NULL, &_renderTargetView))
return false;

View File

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

View File

@ -1,6 +1,54 @@
#include "texture-dx.hpp"
#include "WICTextureLoader.h"
#include "device-dx.hpp"
#include "../helpers.hpp"
namespace xna {
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 "dxgi.h"
#include "d3d11.h"
#include "../xnaerror.hpp"
namespace xna {
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:
ID3D11Texture2D* _texture2D{nullptr};
private:
Uint _width{ 0 };
Uint _height{ 0 };
};
}

View File

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

View File

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

View File

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