diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index be1e3aa..d657a28 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -13,7 +13,7 @@ add_executable (xna WIN32 "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" -"platform/clock-dx.cpp" + "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" @@ -21,7 +21,7 @@ add_executable (xna WIN32 "platform/rasterizerstate-dx.cpp" "platform/vertexbuffer-dx.cpp" "platform/indexbuffer-dx.cpp" -"platform/constbuffer-dx.cpp" + "platform/databuffer-dx.cpp" "platform/samplerstate-dx.cpp" "platform/sprite-dx.cpp" @@ -47,7 +47,7 @@ add_executable (xna WIN32 "common/color.cpp" "common/collision.cpp" "common/gjk.cpp" -"common/numerics.cpp" "common/packedvalue.cpp") +"common/numerics.cpp" "common/packedvalue.cpp" "platform/buffer.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET xna PROPERTY CXX_STANDARD 20) diff --git a/framework/platform/buffer.cpp b/framework/platform/buffer.cpp new file mode 100644 index 0000000..53e927c --- /dev/null +++ b/framework/platform/buffer.cpp @@ -0,0 +1,39 @@ +#include "graphics/buffer.hpp" +#include "common/numerics.hpp" +#include "platform-dx/dxheaders.hpp" +#include "platform-dx/device-dx.hpp" +#include "platform-dx/implementations.hpp" + +namespace xna { + ConstantBuffer::ConstantBuffer() : GraphicsResource(nullptr){} + ConstantBuffer::ConstantBuffer(sptr const& device) : GraphicsResource(device){} + + ConstantBuffer::~ConstantBuffer() { + impl = nullptr; + } + + bool ConstantBuffer::Initialize(xna_error_ptr_arg) + { + if (!m_device || !m_device->_device) { + xna_error_apply(err, XnaErrorCode::INVALID_OPERATION); + return false; + } + + if (impl->_buffer) { + impl->_buffer->Release(); + impl->_buffer = nullptr; + } + + const auto hr = m_device->_device->CreateBuffer( + &impl->_description, + &impl->_subResource, + &impl->_buffer); + + if (FAILED(hr)) { + xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/framework/platform/clock-dx.cpp b/framework/platform/clock-dx.cpp deleted file mode 100644 index 40dab2d..0000000 --- a/framework/platform/clock-dx.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include "platform-dx/clock-dx.hpp" -#include - -namespace xna { - void GameClock::Reset() { - _start = ClockNow(); - _end = std::chrono::steady_clock::time_point(); - _stopped = false; - _suspended = false; - } - - void GameClock::Start() { - Reset(); - _total = 0; - _start = ClockNow(); - } - - void GameClock::Resume() { - if (_stopped) - return; - - if (_suspended) { - auto elapsed = _end - _start; - auto now = ClockNow(); - - _start = now - elapsed; - _suspended = false; - } - else { - _start = ClockNow(); - } - } - - void GameClock::Suspend() { - if (_suspended || _stopped) - return; - - _end = ClockNow(); - _suspended = true; - } - - void GameClock::Stop() { - Reset(); - _total = 0; - _stopped = true; - } - - TimeSpan GameClock::ElapsedTime() { - if (_stopped) return TimeSpan(); - - if (!_suspended) { - _end = ClockNow(); - } - - auto nano = GetNanoseconds(_start, _end); - return TimeSpan::FromSeconds(nano / 1'000'000'000.0); - } - - TimeSpan GameClock::TotalTime() { - if (_stopped) return TimeSpan(); - - if (!_suspended) { - _end = ClockNow(); - } - - auto nano = GetNanoseconds(_start, _end); - _total += nano; - - return TimeSpan::FromSeconds(_total / 1'000'000'000.0); - } -} \ No newline at end of file diff --git a/framework/platform/constbuffer-dx.cpp b/framework/platform/constbuffer-dx.cpp deleted file mode 100644 index 0329e3f..0000000 --- a/framework/platform/constbuffer-dx.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "platform-dx/constbuffer-dx.hpp" -#include "platform-dx/device-dx.hpp" - -namespace xna { - bool ConstantBuffer::Initialize(GraphicsDevice& device, xna_error_ptr_arg) - { - if (!device._device) { - xna_error_apply(err, XnaErrorCode::ARGUMENT_IS_NULL); - return false; - } - - if (_buffer) { - _buffer->Release(); - _buffer = nullptr; - } - - const auto hr = device._device->CreateBuffer( - &_description, - &_subResource, - &_buffer); - - if (FAILED(hr)) { - xna_error_apply(err, XnaErrorCode::FAILED_OPERATION); - return false; - } - - return true; - } -} \ No newline at end of file diff --git a/inc/game/clock.hpp b/inc/game/clock.hpp deleted file mode 100644 index 7804874..0000000 --- a/inc/game/clock.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef XNA_GAME_CLOCK_HPP -#define XNA_GAME_CLOCK_HPP - -#include "../types.hpp" -#include "../forward.hpp" -#include "../enums.hpp" -#include "../csharp/timespan.hpp" - -namespace xna { - class IGameClock { - public: - virtual ~IGameClock() {} - - virtual void Start() = 0; - virtual void Resume() = 0; - virtual void Reset() = 0; - virtual void Suspend() = 0; - virtual void Stop() = 0; - virtual TimeSpan ElapsedTime() = 0; - virtual TimeSpan TotalTime() = 0; - virtual bool IsActive() = 0; - }; -} - -#endif \ No newline at end of file diff --git a/inc/graphics/buffer.hpp b/inc/graphics/buffer.hpp new file mode 100644 index 0000000..e0f422e --- /dev/null +++ b/inc/graphics/buffer.hpp @@ -0,0 +1,21 @@ +#ifndef XNA_GRAPHICS_BUFFER_HPP +#define XNA_GRAPHICS_BUFFER_HPP + +#include "../default.hpp" +#include "gresource.hpp" + +namespace xna { + class ConstantBuffer : public GraphicsResource { + public: + ConstantBuffer(); + ConstantBuffer(sptr const&); + ~ConstantBuffer(); + bool Initialize(xna_error_nullarg); + + public: + struct PlatformImplementation; + uptr impl = nullptr; + }; +} + +#endif \ No newline at end of file diff --git a/inc/graphics/constbuffer.hpp b/inc/graphics/constbuffer.hpp deleted file mode 100644 index 2dd94ac..0000000 --- a/inc/graphics/constbuffer.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef XNA_GRAPHICS_CONSTBUFFER_HPP -#define XNA_GRAPHICS_CONSTBUFFER_HPP - -#include "../default.hpp" - -namespace xna { - class IConstantBuffer { - public: - virtual ~IConstantBuffer(){} - virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) = 0; - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/clock-dx.hpp b/inc/platform-dx/clock-dx.hpp deleted file mode 100644 index 9f00b4f..0000000 --- a/inc/platform-dx/clock-dx.hpp +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef XNA_PLATFORM_CLOCK_DX_HPP -#define XNA_PLATFORM_CLOCK_DX_HPP - -#include "../game/clock.hpp" -#include - -namespace xna { - using SteadyClock = std::chrono::steady_clock; - - class GameClock : public IGameClock { - public: - virtual ~GameClock() override {} - virtual void Reset() override; - virtual void Start() override; - virtual void Resume() override; - virtual void Suspend() override; - virtual void Stop() override; - virtual TimeSpan ElapsedTime() override; - virtual TimeSpan TotalTime() override; - - virtual constexpr bool IsActive() override { - return !_suspended && !_stopped; - } - - inline SteadyClock::time_point ClockNow() { - return SteadyClock::now(); - } - - constexpr long long GetNanoseconds(SteadyClock::time_point start, SteadyClock::time_point end) { - return std::chrono::duration_cast(end - start).count(); - } - - constexpr long long GetMicroseconds(SteadyClock::time_point start, SteadyClock::time_point end) { - return std::chrono::duration_cast(end - start).count(); - } - - constexpr long long GetMilliSeconds(SteadyClock::time_point start, SteadyClock::time_point end) { - return std::chrono::duration_cast(end - start).count(); - } - - private: - SteadyClock _clock; - SteadyClock::time_point _start; - SteadyClock::time_point _end; - long long _total{ 0 }; - bool _suspended{ false }; - bool _stopped { true }; - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/constbuffer-dx.hpp b/inc/platform-dx/constbuffer-dx.hpp deleted file mode 100644 index b7afc32..0000000 --- a/inc/platform-dx/constbuffer-dx.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef XNA_PLATFORM_CONSTBUFFER_DX_HPP -#define XNA_PLATFORM_CONSTBUFFER_DX_HPP - -#include "../graphics/constbuffer.hpp" -#include "../common/numerics.hpp" -#include "dxheaders.hpp" -#include - -namespace xna { - class ConstantBuffer : public IConstantBuffer { - public: - virtual ~ConstantBuffer() override { - if (_buffer) { - _buffer->Release(); - _buffer = nullptr; - } - } - - virtual bool Initialize(GraphicsDevice& device, xna_error_nullarg) override; - - public: - D3D11_BUFFER_DESC _description{}; - D3D11_SUBRESOURCE_DATA _subResource{}; - ID3D11Buffer* _buffer = nullptr; - DirectX::XMMATRIX _worldViewProjection; - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/implementations.hpp b/inc/platform-dx/implementations.hpp index 0b324b1..125bff1 100644 --- a/inc/platform-dx/implementations.hpp +++ b/inc/platform-dx/implementations.hpp @@ -1,6 +1,7 @@ #include "graphics/sprite.hpp" #include "graphics/device.hpp" #include "graphics/adapter.hpp" +#include "graphics/buffer.hpp" #include "platform-dx/presentparameters-dx.hpp" #include "dxheaders.hpp" #include "platform-dx/swapchain-dx.hpp" @@ -218,4 +219,18 @@ namespace xna { } } }; + + struct ConstantBuffer::PlatformImplementation { + ~PlatformImplementation() { + if (_buffer) { + _buffer->Release(); + _buffer = nullptr; + } + } + + D3D11_BUFFER_DESC _description{}; + D3D11_SUBRESOURCE_DATA _subResource{}; + ID3D11Buffer* _buffer = nullptr; + DirectX::XMMATRIX _worldViewProjection; + }; } \ No newline at end of file diff --git a/inc/platform-dx/xna-dx.hpp b/inc/platform-dx/xna-dx.hpp index b251868..6219942 100644 --- a/inc/platform-dx/xna-dx.hpp +++ b/inc/platform-dx/xna-dx.hpp @@ -1,6 +1,4 @@ #include "audioengine-dx.hpp" -#include "clock-dx.hpp" -#include "constbuffer-dx.hpp" #include "databuffer-dx.hpp" #include "depthstencilstate-dx.hpp" #include "device-dx.hpp" diff --git a/inc/xna.hpp b/inc/xna.hpp index 83b3ab0..64f082f 100644 --- a/inc/xna.hpp +++ b/inc/xna.hpp @@ -24,7 +24,6 @@ #include "csharp/stream.hpp" #include "csharp/timespan.hpp" #include "csharp/type.hpp" -#include "game/clock.hpp" #include "game/component.hpp" #include "game/game.hpp" #include "game/gdeviceinfo.hpp" @@ -34,7 +33,6 @@ #include "game/window.hpp" #include "graphics/adapter.hpp" #include "graphics/blendstate.hpp" -#include "graphics/constbuffer.hpp" #include "graphics/databuffer.hpp" #include "graphics/depthstencilstate.hpp" #include "graphics/device.hpp"