diff --git a/framework/content/manager.cpp b/framework/content/manager.cpp index 350c8e3..c20cb6d 100644 --- a/framework/content/manager.cpp +++ b/framework/content/manager.cpp @@ -3,7 +3,7 @@ namespace xna { sptr ContentManager::OpenStream(String const& assetName) const { const String filePath = _rootDirectory + "\\" + assetName + contentExtension; - const auto stream = New(filePath, FileMode::Open); + const auto stream = snew(filePath, FileMode::Open); if (stream->IsClosed()) return nullptr; diff --git a/framework/content/typereadermanager.cpp b/framework/content/typereadermanager.cpp index cb5277b..5ab9d76 100644 --- a/framework/content/typereadermanager.cpp +++ b/framework/content/typereadermanager.cpp @@ -157,7 +157,7 @@ namespace xna { void ContentTypeReaderManager::initMaps() { if (targetTypeToReader.empty() && readerTypeToReader.empty()) { - auto typeReader = New(); + auto typeReader = snew(); auto contentTypeReader = reinterpret_pointer_cast(typeReader); targetTypeToReader.insert({ typeof(), contentTypeReader}); diff --git a/framework/platform-dx/adapter.cpp b/framework/platform-dx/adapter.cpp index e2c0973..c3a3030 100644 --- a/framework/platform-dx/adapter.cpp +++ b/framework/platform-dx/adapter.cpp @@ -8,7 +8,7 @@ namespace xna { static uptr createDisplayModeCollection(std::vector const& source); GraphicsAdapter::GraphicsAdapter() { - impl = uNew(); + impl = unew(); } GraphicsAdapter::~GraphicsAdapter() { @@ -24,7 +24,7 @@ namespace xna { IDXGIAdapter1* pAdapter = nullptr; if (pFactory->EnumAdapters1(0, &pAdapter) != DXGI_ERROR_NOT_FOUND) { - auto adp = uNew(); + auto adp = unew(); adp->impl->_index = 0; adp->impl->dxadapter = pAdapter; @@ -48,7 +48,7 @@ namespace xna { UINT count = 0; for (; pFactory->EnumAdapters1(count, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++count) { - auto adp = New(); + auto adp = snew(); adp->impl->_index = count; adp->impl->dxadapter = pAdapter; @@ -70,7 +70,7 @@ namespace xna { UINT count = 0; for (; pFactory->EnumAdapters1(count, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++count) { - auto adp = uNew(); + auto adp = unew(); adp->impl->_index = count; adp->impl->dxadapter = pAdapter; @@ -218,7 +218,7 @@ namespace xna { pOutput->GetDisplayModeList(format, 0, &numModes, nullptr); if (numModes == 0) - return uNew(); + return unew(); std::vector buffer(numModes); pOutput->GetDisplayModeList(format, 0, &numModes, buffer.data()); @@ -229,7 +229,7 @@ namespace xna { return createDisplayModeCollection(buffer); } - return uNew(); + return unew(); } sptr GraphicsAdapter::CurrentDisplayMode() { @@ -289,7 +289,7 @@ namespace xna { } static uptr createDisplayModeCollection(std::vector const& source) { - auto collection = uNew(); + auto collection = unew(); DisplayMode currentDisplayMode; std::vector> displayList; sptr pDisplay = nullptr; @@ -306,7 +306,7 @@ namespace xna { pDisplay->impl->Descriptions.push_back(description); } else { - pDisplay = New(); + pDisplay = snew(); pDisplay->Width = modedesc.Width; pDisplay->Height = modedesc.Height; pDisplay->Format = DxHelpers::ConvertDXGIFORMATToSurface(modedesc.Format); diff --git a/framework/platform-dx/blendstate.cpp b/framework/platform-dx/blendstate.cpp index c840811..2dd4544 100644 --- a/framework/platform-dx/blendstate.cpp +++ b/framework/platform-dx/blendstate.cpp @@ -4,11 +4,11 @@ namespace xna { BlendState::BlendState() : GraphicsResource(nullptr) { - impl = uNew(); + impl = unew(); } BlendState::BlendState(sptr const& device) : GraphicsResource(device) { - impl = uNew(); + impl = unew(); } BlendState::~BlendState() { @@ -76,7 +76,7 @@ namespace xna { } uptr BlendState::Opaque() { - auto blendState = uNew(); + auto blendState = unew(); blendState->impl->dxDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE; blendState->impl->dxDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO; blendState->impl->dxDescription.RenderTarget[0].DestBlend = D3D11_BLEND_DEST_ALPHA; diff --git a/framework/platform-dx/buffer.cpp b/framework/platform-dx/buffer.cpp index 1fb1b0c..938aced 100644 --- a/framework/platform-dx/buffer.cpp +++ b/framework/platform-dx/buffer.cpp @@ -4,11 +4,11 @@ namespace xna { ConstantBuffer::ConstantBuffer() : GraphicsResource(nullptr){ - impl = uNew(); + impl = unew(); } ConstantBuffer::ConstantBuffer(sptr const& device) : GraphicsResource(device){ - impl = uNew(); + impl = unew(); } ConstantBuffer::~ConstantBuffer() { @@ -39,11 +39,11 @@ namespace xna { } DataBuffer::DataBuffer() : GraphicsResource(nullptr) { - impl = uNew(); + impl = unew(); } DataBuffer::DataBuffer(sptr const& device) : GraphicsResource(device) { - impl = uNew(); + impl = unew(); } DataBuffer::~DataBuffer() { @@ -64,11 +64,11 @@ namespace xna { } IndexBuffer::IndexBuffer() : GraphicsResource(nullptr) { - impl = uNew(); + impl = unew(); } IndexBuffer::IndexBuffer(sptr const& device) : GraphicsResource(device) { - impl = uNew(); + impl = unew(); } IndexBuffer::~IndexBuffer() { @@ -86,11 +86,11 @@ namespace xna { } VertexBuffer::VertexBuffer() : GraphicsResource(nullptr) { - impl = uNew(); + impl = unew(); } VertexBuffer::VertexBuffer(sptr const& device) : GraphicsResource(device) { - impl = uNew(); + impl = unew(); } VertexBuffer::~VertexBuffer() { @@ -115,11 +115,11 @@ namespace xna { } VertexInputLayout::VertexInputLayout() : GraphicsResource(nullptr) { - impl = uNew(); + impl = unew(); } VertexInputLayout::VertexInputLayout(sptr const& device) : GraphicsResource(device) { - impl = uNew(); + impl = unew(); } VertexInputLayout::~VertexInputLayout() { diff --git a/framework/platform-dx/depthstencilstate.cpp b/framework/platform-dx/depthstencilstate.cpp index 7afc539..d334fc2 100644 --- a/framework/platform-dx/depthstencilstate.cpp +++ b/framework/platform-dx/depthstencilstate.cpp @@ -26,12 +26,12 @@ namespace xna { } DepthStencilState::DepthStencilState() : GraphicsResource(nullptr) { - impl = uNew(); + impl = unew(); impl->dxDescription = defaultDesc(); } DepthStencilState::DepthStencilState(sptr const& device) : GraphicsResource(device) { - impl = uNew(); + impl = unew(); impl->dxDescription = defaultDesc(); } @@ -77,7 +77,7 @@ namespace xna { } uptr DepthStencilState::None() { - auto stencil = uNew(); + auto stencil = unew(); stencil->impl->dxDescription.DepthEnable = false; stencil->impl->dxDescription.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; @@ -85,7 +85,7 @@ namespace xna { } uptr DepthStencilState::Default() { - auto stencil = uNew(); + auto stencil = unew(); stencil->impl->dxDescription.DepthEnable = true; stencil->impl->dxDescription.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; @@ -93,7 +93,7 @@ namespace xna { } uptr DepthStencilState::DepthRead() { - auto stencil = uNew(); + auto stencil = unew(); stencil->impl->dxDescription.DepthEnable = true; stencil->impl->dxDescription.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 9c1cba5..2af9b6b 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -114,7 +114,7 @@ namespace xna { impl->_backgroundColor[2] = GetBValue(color) / 255.0f; impl->_backgroundColor[3] = 1.0f; - impl->_swapChain = New(_this); + impl->_swapChain = snew(_this); impl->_swapChain->Initialize(); hr = impl->_factory->MakeWindowAssociation(impl->_gameWindow->impl->WindowHandle(), DXGI_MWA_NO_ALT_ENTER); @@ -122,7 +122,7 @@ namespace xna { if (FAILED(hr)) Exception::Throw(ExMessage::MakeWindowAssociation); - impl->_renderTarget2D = New(_this); + impl->_renderTarget2D = snew(_this); if (!impl->_renderTarget2D->Initialize()) return false; diff --git a/framework/platform-dx/displaymode.cpp b/framework/platform-dx/displaymode.cpp index 3ae4ace..f959848 100644 --- a/framework/platform-dx/displaymode.cpp +++ b/framework/platform-dx/displaymode.cpp @@ -3,7 +3,7 @@ namespace xna { DisplayMode::DisplayMode() { - impl = uNew(); + impl = unew(); } DisplayMode::~DisplayMode() { diff --git a/framework/platform-dx/game.cpp b/framework/platform-dx/game.cpp index 5a00943..0c598dc 100644 --- a/framework/platform-dx/game.cpp +++ b/framework/platform-dx/game.cpp @@ -9,19 +9,19 @@ namespace xna { Game::Game() { impl = unew(); - services = New(); + services = snew(); auto iservice = reinterpret_pointer_cast(services); - _contentManager = New(services, ""); + _contentManager = snew(services, ""); _contentManager->_gameServices = iservice; - _gameWindow = New(); + _gameWindow = snew(); _gameWindow->impl->Color(146, 150, 154); _gameWindow->Title("XN65"); _gameWindow->impl->Size( GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight, false); - _gameComponents = New(); + _gameComponents = snew(); } Game::~Game() { diff --git a/framework/platform-dx/gamepad.cpp b/framework/platform-dx/gamepad.cpp index 037df03..c62a8c1 100644 --- a/framework/platform-dx/gamepad.cpp +++ b/framework/platform-dx/gamepad.cpp @@ -3,8 +3,8 @@ namespace xna { void GamePad::Initialize() { - impl = uNew(); - impl->_dxGamePad = uNew(); + impl = unew(); + impl->_dxGamePad = unew(); } GamePadState GamePad::GetState(PlayerIndex index) { diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index 28e2837..551e724 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -84,7 +84,7 @@ namespace xna { bool initDevice(GraphicsDeviceInformation& info, Game& game, sptr& device) { - device = New(info); + device = snew(info); if (!device->Initialize()) { MessageBox(info.Window->impl->WindowHandle(), "Falha na inicialização do dispositivo gráfico", "XN65", MB_OK); diff --git a/framework/platform-dx/keyboard.cpp b/framework/platform-dx/keyboard.cpp index ec0da4e..b8968cb 100644 --- a/framework/platform-dx/keyboard.cpp +++ b/framework/platform-dx/keyboard.cpp @@ -14,8 +14,8 @@ namespace xna { } void Keyboard::Initialize() { - impl = uNew(); - impl->_dxKeyboard = uNew(); + impl = unew(); + impl->_dxKeyboard = unew(); } bool Keyboard::IsConnected() { diff --git a/framework/platform-dx/mouse.cpp b/framework/platform-dx/mouse.cpp index 577edef..53dc95a 100644 --- a/framework/platform-dx/mouse.cpp +++ b/framework/platform-dx/mouse.cpp @@ -49,7 +49,7 @@ namespace xna { } void Mouse::Initialize() { - impl = uNew(); - impl->_dxMouse = uNew(); + impl = unew(); + impl->_dxMouse = unew(); } } \ No newline at end of file diff --git a/framework/platform-dx/rasterizerstate.cpp b/framework/platform-dx/rasterizerstate.cpp index 89d2c54..3e400c0 100644 --- a/framework/platform-dx/rasterizerstate.cpp +++ b/framework/platform-dx/rasterizerstate.cpp @@ -54,7 +54,7 @@ namespace xna { uptr RasterizerState::CullNone() { - auto raster = uNew(); + auto raster = unew(); raster->impl->dxDescription.FillMode = D3D11_FILL_SOLID; raster->impl->dxDescription.CullMode = D3D11_CULL_MODE::D3D11_CULL_NONE; raster->impl->dxDescription.DepthClipEnable = true; @@ -63,7 +63,7 @@ namespace xna { uptr RasterizerState::CullClockwise() { - auto raster = uNew(); + auto raster = unew(); raster->impl->dxDescription.FillMode = D3D11_FILL_SOLID; raster->impl->dxDescription.CullMode = D3D11_CULL_MODE::D3D11_CULL_FRONT; raster->impl->dxDescription.DepthClipEnable = true; @@ -72,7 +72,7 @@ namespace xna { uptr RasterizerState::CullCounterClockwise() { - auto raster = uNew(); + auto raster = unew(); raster->impl->dxDescription.FillMode = D3D11_FILL_SOLID; raster->impl->dxDescription.CullMode = D3D11_CULL_MODE::D3D11_CULL_BACK; raster->impl->dxDescription.DepthClipEnable = true; diff --git a/framework/platform-dx/sprite.cpp b/framework/platform-dx/sprite.cpp index e316927..15776be 100644 --- a/framework/platform-dx/sprite.cpp +++ b/framework/platform-dx/sprite.cpp @@ -60,7 +60,7 @@ namespace xna { dxGlyps[i] = g; } - impl = uNew(); + impl = unew(); impl->_dxSpriteFont = unew( //ID3D11ShaderResourceView* texture texture->impl->dxShaderResource, @@ -127,8 +127,8 @@ namespace xna { if (!device->impl->_context) return; - implementation = uNew(); - implementation->_dxspriteBatch = New( + implementation = unew(); + implementation->_dxspriteBatch = snew( //ID3D11DeviceContext* deviceContext device->impl->_context ); diff --git a/framework/platform-dx/texture.cpp b/framework/platform-dx/texture.cpp index 703f200..4925138 100644 --- a/framework/platform-dx/texture.cpp +++ b/framework/platform-dx/texture.cpp @@ -8,7 +8,7 @@ namespace xna { sptr Texture2D::FromStream(GraphicsDevice& device, String const& fileName) { auto _this = device.shared_from_this(); - auto texture2d = New(_this); + auto texture2d = snew(_this); ID3D11Resource* resource = nullptr; auto wstr = XnaHelper::ToWString(fileName); @@ -285,7 +285,7 @@ namespace xna { sptr Texture2D::FromMemory(GraphicsDevice& device, std::vector const& data) { auto _this = device.shared_from_this(); - auto texture2d = New(_this); + auto texture2d = snew(_this); ID3D11Resource* resource = nullptr; auto hr = DirectX::CreateWICTextureFromMemory( diff --git a/inc/xna/content/readers/graphics.hpp b/inc/xna/content/readers/graphics.hpp index 6c4194c..2254ca0 100644 --- a/inc/xna/content/readers/graphics.hpp +++ b/inc/xna/content/readers/graphics.hpp @@ -28,7 +28,7 @@ namespace xna { if (a_device.has_value()) device = std::any_cast>(a_device); - auto texture2D = New(device, width, height, mipMaps, format); + auto texture2D = snew(device, width, height, mipMaps, format); for (size_t level = 0; level < mipMaps; ++level) { auto elementCount = input.ReadInt32(); diff --git a/inc/xna/csharp/type.hpp b/inc/xna/csharp/type.hpp index 089e7bd..934ee84 100644 --- a/inc/xna/csharp/type.hpp +++ b/inc/xna/csharp/type.hpp @@ -51,7 +51,7 @@ namespace xna { template inline sptr typeof() { if (std::is_arithmetic::value) { - auto primitiveType = New(); + auto primitiveType = snew(); primitiveType->fullName = typeid(T).name(); primitiveType->isPrimitive = true; primitiveType->isValueType = true; @@ -59,7 +59,7 @@ namespace xna { } if (std::is_enum::value) { - auto enumType = New(); + auto enumType = snew(); enumType->fullName = typeid(T).name(); enumType->isValueType = true; enumType->isEnum = true; @@ -67,14 +67,14 @@ namespace xna { } if (std::is_pointer::value) { - auto pointerType = New(); + auto pointerType = snew(); pointerType->fullName = typeid(T).name(); pointerType->isPointer = true; return pointerType; } if (std::is_class::value) { - auto classType = New(); + auto classType = snew(); classType->fullName = typeid(T).name(); classType->isClass = true; diff --git a/inc/xna/default.hpp b/inc/xna/default.hpp index 1e7dae5..634624e 100644 --- a/inc/xna/default.hpp +++ b/inc/xna/default.hpp @@ -1,4 +1,5 @@ #include "types.hpp" #include "forward.hpp" #include "enums.hpp" -#include "helpers.hpp" \ No newline at end of file +#include "helpers.hpp" +#include "exception.hpp" \ No newline at end of file diff --git a/inc/xna/exception.hpp b/inc/xna/exception.hpp index 01ee089..d981002 100644 --- a/inc/xna/exception.hpp +++ b/inc/xna/exception.hpp @@ -6,6 +6,8 @@ #include namespace xna { + + //A list of standard exceptions struct ExMessage { inline static const std::string InvalidOperation = "An invalid operation occurred."; inline static const std::string InitializeComponent = "Unable to initialize component"; @@ -13,9 +15,13 @@ namespace xna { inline static const std::string ApplyComponent = "Failed to apply component"; inline static const std::string UnintializedComponent = "Component is not initialized"; inline static const std::string MakeWindowAssociation = "Failed to create association with window"; + inline static const std::string BuildObject = "Unable to build object"; }; + //Structure for throwing exceptions with a message and information from the source file struct Exception { + + //Raises an exception with a message. Source file information is automatically captured. static void Throw(std::string const& message, const std::source_location location = std::source_location::current()) { std::string error; diff --git a/inc/xna/helpers.hpp b/inc/xna/helpers.hpp index 20b509b..b14afec 100644 --- a/inc/xna/helpers.hpp +++ b/inc/xna/helpers.hpp @@ -3,13 +3,20 @@ #include #include -#include +#include "exception.hpp" namespace xna { + //Class for helper functions struct XnaHelper { + + // + // Smart Pointer Comparator + // + template struct is_shared_ptr : std::false_type {}; template struct is_shared_ptr> : std::true_type {}; + //Convert a string to wstring static inline std::wstring ToWString(const std::string& str) { std::wstring wstr; @@ -19,6 +26,7 @@ namespace xna { return wstr; } + //Convert a wstring to string static inline std::string ToString(const std::wstring& wstr) { std::string str; @@ -28,20 +36,23 @@ namespace xna { return str; } + //Returns a hash reporting input values template static constexpr void HashCombine(std::size_t& seed, const T& v) { std::hash hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } + //Returns null if the type is a smart pointer or default value if the type has a default constructor. + //Throws an exception if the object cannot be created template - static inline auto ReturnDefaultOrNull() { + static inline auto ReturnDefaultOrNull(const std::source_location location = std::source_location::current()) { if constexpr (is_shared_ptr::value) return (T)nullptr; else if (std::is_default_constructible::value) return T(); else - throw std::runtime_error("Unable to build object"); + Exception::Throw(ExMessage::BuildObject, location); } }; } diff --git a/inc/xna/platform-dx/init.hpp b/inc/xna/platform-dx/init.hpp index 7a27a07..b047fe9 100644 --- a/inc/xna/platform-dx/init.hpp +++ b/inc/xna/platform-dx/init.hpp @@ -36,7 +36,7 @@ namespace xna { template static void insertActivadorReader() { ContentTypeReaderActivador::SetActivador(typeof(), []() -> sptr { - auto obj = New (); + auto obj = snew(); return reinterpret_pointer_cast(obj); }); } diff --git a/inc/xna/platforminit.hpp b/inc/xna/platforminit.hpp index 9c869b7..10071c6 100644 --- a/inc/xna/platforminit.hpp +++ b/inc/xna/platforminit.hpp @@ -2,7 +2,10 @@ #define XNA_PLATFORMINIT_HPP namespace xna { + //Exposes functions that must be implemented by the platform struct Platform { + //Initialization function, which must be implemented by the platform, + //and be called before the game is executed static void Init(); }; } diff --git a/inc/xna/types.hpp b/inc/xna/types.hpp index f159402..12c673a 100644 --- a/inc/xna/types.hpp +++ b/inc/xna/types.hpp @@ -11,6 +11,11 @@ #include namespace xna { + + // + // C# standard types + // + using Sbyte = int8_t; using Byte = uint8_t; using Short = int16_t; @@ -21,6 +26,10 @@ namespace xna { using Ulong = uint64_t; using Char = char16_t; + // + // C# Min and Max Value + // + constexpr Sbyte SbyteMaxValue = (std::numeric_limits::max)(); constexpr Sbyte SbyteMinValue = (std::numeric_limits::min)(); constexpr Byte ByteMaxValue = (std::numeric_limits::max)(); @@ -48,29 +57,27 @@ namespace xna { // About strings: https://stackoverflow.com/questions/402283/stdwstring-vs-stdstring // + //Same as std::string using String = std::string; + + //Same as std::wstring using WString = std::wstring; + //Same as std::shared_ptr template using sptr = std::shared_ptr; + + //Same as std::unique_ptr template - using uptr = std::unique_ptr; - - template - inline std::shared_ptr<_Ty> New(_Types&&... _Args) { - return std::make_shared<_Ty>(std::forward<_Types>(_Args)...); - } - - template - inline std::unique_ptr<_Ty> uNew(_Types&&... _Args) { - return std::make_unique<_Ty>(std::forward<_Types>(_Args)...); - } - + using uptr = std::unique_ptr; + + //Same as std::make_shared template inline std::shared_ptr<_Ty> snew(_Types&&... _Args) { return std::make_shared<_Ty>(std::forward<_Types>(_Args)...); } + //Same as std::make_unique template inline std::unique_ptr<_Ty> unew(_Types&&... _Args) { return std::make_unique<_Ty>(std::forward<_Types>(_Args)...); diff --git a/samples/01_blank/xna.cpp b/samples/01_blank/xna.cpp index 38352e0..c14c086 100644 --- a/samples/01_blank/xna.cpp +++ b/samples/01_blank/xna.cpp @@ -15,7 +15,7 @@ namespace xna { void Initialize() override { auto game = reinterpret_cast(this); - graphics = New(game->shared_from_this()); + graphics = snew(game->shared_from_this()); graphics->Initialize(); std::any device = graphicsDevice; @@ -25,7 +25,7 @@ namespace xna { } void LoadContent() override { - spriteBatch = New(graphicsDevice); + spriteBatch = snew(graphicsDevice); auto texture = Content()->Load("Idle"); Game::LoadContent(); } diff --git a/samples/02_PlatfformerStarterKit/game.cpp b/samples/02_PlatfformerStarterKit/game.cpp index 63b77be..d7ee920 100644 --- a/samples/02_PlatfformerStarterKit/game.cpp +++ b/samples/02_PlatfformerStarterKit/game.cpp @@ -21,7 +21,7 @@ namespace PlatformerStarterKit { void Initialize() override { auto game = reinterpret_cast(this); - graphics = New(game->shared_from_this()); + graphics = snew(game->shared_from_this()); graphics->Initialize(); std::any device = graphicsDevice; @@ -31,7 +31,7 @@ namespace PlatformerStarterKit { } void LoadContent() override { - spriteBatch = New(graphicsDevice); + spriteBatch = snew(graphicsDevice); // Load fonts hudFont = Content()->Load("Fonts/Hud");