diff --git a/CMakePresets.json b/CMakePresets.json index f4bc98b..5d2f8c4 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -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}" + } + } + } + ] } diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 4a1c5d6..fe9c8b4 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -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) diff --git a/framework/forward.hpp b/framework/forward.hpp index 597eb50..fc4e37c 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -99,6 +99,8 @@ namespace xna { using PTexture = std::shared_ptr; class Texture2D; using PTexture2D = std::shared_ptr; + class VertexInputLayout; + using PVertexInputLayout = std::shared_ptr; struct Viewport; using PViewport = std::shared_ptr; } diff --git a/framework/graphics/rendertarget.hpp b/framework/graphics/rendertarget.hpp index 18075de..1a8e6f1 100644 --- a/framework/graphics/rendertarget.hpp +++ b/framework/graphics/rendertarget.hpp @@ -9,7 +9,7 @@ namespace xna { public: virtual ~IRenderTarget2D(){} - virtual bool Apply() = 0; + virtual bool Bind() = 0; }; } diff --git a/framework/graphics/texture.hpp b/framework/graphics/texture.hpp index 5207d94..bbf9bba 100644 --- a/framework/graphics/texture.hpp +++ b/framework/graphics/texture.hpp @@ -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; }; } diff --git a/framework/platform/device-dx.cpp b/framework/platform/device-dx.cpp index ff3e82c..b016cf1 100644 --- a/framework/platform/device-dx.cpp +++ b/framework/platform/device-dx.cpp @@ -69,7 +69,7 @@ namespace xna { _renderTarget2D = New(this); } - if (!_renderTarget2D->Apply()) + if (!_renderTarget2D->Bind()) return false; D3D11_VIEWPORT view{}; diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index 50cfc8a..fe72d4b 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -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(); } diff --git a/framework/platform/game-dx.hpp b/framework/platform/game-dx.hpp index d4c3461..0e5c475 100644 --- a/framework/platform/game-dx.hpp +++ b/framework/platform/game-dx.hpp @@ -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 }; diff --git a/framework/platform/gdevicemanager-dx.cpp b/framework/platform/gdevicemanager-dx.cpp index a486fe6..9fa3019 100644 --- a/framework/platform/gdevicemanager-dx.cpp +++ b/framework/platform/gdevicemanager-dx.cpp @@ -44,7 +44,7 @@ namespace xna { return; } - _game->GraphicsDevice = _device; + _game->_graphicsDevice = _device; } void GraphicsDeviceManager::ChangeDevice() { diff --git a/framework/platform/rendertarget-dx.cpp b/framework/platform/rendertarget-dx.cpp index b75ba07..126b7f8 100644 --- a/framework/platform/rendertarget-dx.cpp +++ b/framework/platform/rendertarget-dx.cpp @@ -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; diff --git a/framework/platform/rendertarget-dx.hpp b/framework/platform/rendertarget-dx.hpp index 8dbea9f..8bec094 100644 --- a/framework/platform/rendertarget-dx.hpp +++ b/framework/platform/rendertarget-dx.hpp @@ -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 }; }; } diff --git a/framework/platform/texture-dx.cpp b/framework/platform/texture-dx.cpp index 93ca7c6..996fef0 100644 --- a/framework/platform/texture-dx.cpp +++ b/framework/platform/texture-dx.cpp @@ -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 = txt2d; + + resource->Release(); + resource = nullptr; + + return texture2d; + } } \ No newline at end of file diff --git a/framework/platform/texture-dx.hpp b/framework/platform/texture-dx.hpp index 7bb8db5..8f6873b 100644 --- a/framework/platform/texture-dx.hpp +++ b/framework/platform/texture-dx.hpp @@ -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 }; }; } diff --git a/framework/xna.cpp b/framework/xna.cpp index fd435ec..42678a8 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -16,15 +16,19 @@ class Game1 : public Game { public: Game1() { graphics = New(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); } diff --git a/framework/xna.h b/framework/xna.h index 577deec..bca6a78 100644 --- a/framework/xna.h +++ b/framework/xna.h @@ -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. diff --git a/framework/xnaerror.hpp b/framework/xnaerror.hpp index 65a6da3..437df37 100644 --- a/framework/xnaerror.hpp +++ b/framework/xnaerror.hpp @@ -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) {