diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index efc2434..73b1e0c 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -1,11 +1,14 @@ #include "csharp/type.hpp" -#include "game/gdevicemanager.hpp" #include "game/time.hpp" -#include "platform-dx/game-dx.hpp" +#include "game/component.hpp" +#include "game/servicecontainer.hpp" #include "platform-dx/implementations.hpp" +#include "game/gdevicemanager.hpp" +#include "content/manager.hpp" namespace xna { Game::Game() { + impl = unew(); services = New(); _contentManager = New("", services); @@ -19,10 +22,50 @@ namespace xna { _gameComponents = New(); } + Game::~Game() { + impl = nullptr; + } + void Game::Exit() { _gameWindow->impl->Close(); } + int Game::StartGameLoop() { + MSG msg{}; + + impl->_stepTimer = DX::StepTimer(); + + do { + if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + else { + Step(); + } + + } while (msg.message != WM_QUIT); + + return static_cast(msg.wParam); + } + + void Game::Step() + { + impl->_stepTimer.Tick([&]() + { + const auto elapsed = impl->_stepTimer.GetElapsedSeconds(); + const auto total = impl->_stepTimer.GetTotalSeconds(); + const auto elapsedTimeSpan = TimeSpan::FromSeconds(elapsed); + const auto totalTimeSpan = TimeSpan::FromSeconds(total); + _currentGameTime.ElapsedGameTime = elapsedTimeSpan; + _currentGameTime.TotalGameTime = totalTimeSpan; + Update(_currentGameTime); + }); + + Draw(_currentGameTime); + } + int Game::Run() { Initialize(); @@ -31,7 +74,7 @@ namespace xna { return EXIT_FAILURE; } - return startLoop(); + return StartGameLoop(); } void Game::Initialize() { @@ -105,38 +148,10 @@ namespace xna { } } - int Game::startLoop() { - MSG msg{}; - _stepTimer = DX::StepTimer(); - - do { - if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - else { - step(); - } - - } while (msg.message != WM_QUIT); - - return static_cast(msg.wParam); - } - - void Game::step() - { - _stepTimer.Tick([&]() - { - const auto elapsed = _stepTimer.GetElapsedSeconds(); - const auto total = _stepTimer.GetTotalSeconds(); - const auto elapsedTimeSpan = TimeSpan::FromSeconds(elapsed); - const auto totalTimeSpan = TimeSpan::FromSeconds(total); - _currentGameTime.ElapsedGameTime = elapsedTimeSpan; - _currentGameTime.TotalGameTime = totalTimeSpan; - Update(_currentGameTime); - }); - - Draw(_currentGameTime); - } + sptr Game::Window() { return _gameWindow; } + sptr Game::GetGraphicsDevice() { return graphicsDevice; } + sptr Game::Components() { return _gameComponents; } + sptr Game::Services() { return services; } + sptr Game::Content() { return _contentManager; } + void Game::EnableGameComponents(bool value) { _enabledGameComponents = value; } } diff --git a/framework/platform/gdevicemanager-dx.cpp b/framework/platform/gdevicemanager-dx.cpp index 2fe6b0f..621586c 100644 --- a/framework/platform/gdevicemanager-dx.cpp +++ b/framework/platform/gdevicemanager-dx.cpp @@ -1,7 +1,6 @@ #include "game/gdevicemanager.hpp" #include "graphics/presentparams.hpp" #include "graphics/swapchain.hpp" -#include "platform-dx/game-dx.hpp" #include "platform-dx/implementations.hpp" namespace xna { diff --git a/inc/forward.hpp b/inc/forward.hpp index 335f2bc..a42ed22 100644 --- a/inc/forward.hpp +++ b/inc/forward.hpp @@ -53,6 +53,7 @@ namespace xna { class IGameTime; class IGameComponent; class GameServiceContainer; + class GameComponentCollection; //Graphics class BlendState; diff --git a/inc/game/game.hpp b/inc/game/game.hpp index 3656808..091de3f 100644 --- a/inc/game/game.hpp +++ b/inc/game/game.hpp @@ -2,26 +2,49 @@ #define XNA_GAME_GAME_HPP #include "../default.hpp" -#include "time.hpp" -#include "component.hpp" -#include "servicecontainer.hpp" +#include "game/time.hpp" namespace xna { - class IGame { + class Game : public std::enable_shared_from_this { public: - virtual void Exit() = 0; - virtual int Run() = 0; - virtual sptr Window() = 0; - virtual sptr GetGraphicsDevice() = 0; - virtual sptr Components() = 0; - virtual sptr Services() = 0; - virtual sptr Content() = 0; + Game(); + ~Game(); + void Exit(); + int Run(); + sptr Window(); + sptr GetGraphicsDevice(); + sptr Components(); + sptr Services(); + sptr Content(); + void EnableGameComponents(bool value); protected: - virtual void Draw(GameTime const& gameTime) = 0; - virtual void Initialize() = 0; - virtual void LoadContent() = 0; - virtual void Update(GameTime const& gameTime) = 0; + virtual void Draw(GameTime const& gameTime); + virtual void Initialize(); + virtual void LoadContent(){} + virtual void Update(GameTime const& gameTime); + int StartGameLoop(); + void Step(); + + public: + sptr graphicsDevice = nullptr; + + protected: + sptr services = nullptr; + + private: + sptr _gameComponents = nullptr; + sptr _gameWindow{ nullptr }; + sptr _audioEngine = nullptr; + sptr _contentManager; + std::vector> _drawableGameComponents; + size_t _drawableGameComponentsCount{ 0 }; + bool _enabledGameComponents{ false }; + GameTime _currentGameTime{}; + + public: + struct PlatformImplementation; + uptr impl = nullptr; }; } diff --git a/inc/platform-dx/game-dx.hpp b/inc/platform-dx/game-dx.hpp deleted file mode 100644 index 45d9724..0000000 --- a/inc/platform-dx/game-dx.hpp +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef XNA_PLATFORM_GAME_DX_HPP -#define XNA_PLATFORM_GAME_DX_HPP - -#include "../content/manager.hpp" -#include "../default.hpp" -#include "../game/game.hpp" -#include "dxheaders.hpp" -#include "platform-dx/dx/StepTimer.hpp" - -namespace xna { - class Game : public IGame, public std::enable_shared_from_this { - public: - Game(); - - void Exit() override; - int Run() override; - - inline sptr Window() override { return _gameWindow; } - inline sptr GetGraphicsDevice() override { return graphicsDevice; } - inline sptr Components() override { return _gameComponents; } - inline sptr Services() override { return services; } - inline sptr Content() override { return _contentManager; } - constexpr void EnableGameComponents(bool value) { _enabledGameComponents = value; } - - protected: - virtual void Draw(GameTime const& gameTime) override; - virtual void Initialize() override; - virtual void LoadContent() override{} - virtual void Update(GameTime const& gameTime) override; - - public: - sptr graphicsDevice = nullptr; - - protected: - sptr services = nullptr; - - private: - sptr _gameComponents = nullptr; - sptr _gameWindow{ nullptr }; - sptr _audioEngine = nullptr; - sptr _contentManager; - std::vector> _drawableGameComponents; - size_t _drawableGameComponentsCount{ 0 }; - bool _enabledGameComponents{ false }; - GameTime _currentGameTime{}; - DX::StepTimer _stepTimer{}; - - int startLoop(); - void step(); - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/implementations.hpp b/inc/platform-dx/implementations.hpp index a0545fc..17b8c72 100644 --- a/inc/platform-dx/implementations.hpp +++ b/inc/platform-dx/implementations.hpp @@ -2,6 +2,7 @@ #define XNA_PLATFORM_DX_IMPLEMENTATIONS_HPP #include "dxheaders.hpp" +#include "dx/StepTimer.hpp" #include "graphics/device.hpp" #include "graphics/adapter.hpp" #include "graphics/blendstate.hpp" @@ -23,6 +24,7 @@ #include "audio/audioengine.hpp" #include "graphics/viewport.hpp" #include "common/color.hpp" +#include "game/game.hpp" namespace xna { struct SpriteFont::PlatformImplementation { @@ -176,7 +178,7 @@ namespace xna { } ID3D11Buffer* dxBuffer = nullptr; - }; + }; struct Keyboard::PlatformImplementation { inline static uptr _dxKeyboard = nullptr; @@ -299,7 +301,7 @@ namespace xna { ID3D11Buffer* dxBuffer = nullptr; UINT size{ 0 }; - }; + }; struct VertexInputLayout::PlatformImplementation { ~PlatformImplementation() { @@ -413,7 +415,7 @@ namespace xna { int _windowPosY{ 0 }; float _windowCenterX{ 0 }; float _windowCenterY{ 0 }; - + inline void setPosition() { _windowPosX = GetSystemMetrics(SM_CXSCREEN) / 2 - _windowWidth / 2; _windowPosY = GetSystemMetrics(SM_CYSCREEN) / 2 - _windowHeight / 2; @@ -423,7 +425,7 @@ namespace xna { _windowCenterX = _windowWidth / 2.0f; _windowCenterY = _windowHeight / 2.0f; } - }; + }; struct AudioEngine::PlatformImplementation { PlatformImplementation() { @@ -431,10 +433,10 @@ namespace xna { #ifdef _DEBUG DirectX::AudioEngine_Debug #endif - ); + ); } - ~PlatformImplementation(){ + ~PlatformImplementation() { if (_dxAudioEngine) { _dxAudioEngine->Suspend(); } @@ -458,7 +460,14 @@ namespace xna { private: friend class GraphicsDevice; float _backgroundColor[4] = { 0, 0, 0, 0 }; - bool _usevsync{ true }; + bool _usevsync{ true }; + }; + + struct Game::PlatformImplementation { + private: + friend class Game; + + DX::StepTimer _stepTimer{}; }; template diff --git a/inc/platform-dx/xna-dx.hpp b/inc/platform-dx/xna-dx.hpp index 8b893e9..ee3e379 100644 --- a/inc/platform-dx/xna-dx.hpp +++ b/inc/platform-dx/xna-dx.hpp @@ -1,7 +1,6 @@ #include "content-readers/texture2Dreader-dx.hpp" #include "dx/StepTimer.hpp" #include "dxheaders.hpp" -#include "game-dx.hpp" #include "init-dx.hpp" #include "soundeffect-dx.hpp" #include "implementations.hpp" \ No newline at end of file