diff --git a/framework/input/gamepad.hpp b/framework/input/gamepad.hpp index e5785b8..c59373f 100644 --- a/framework/input/gamepad.hpp +++ b/framework/input/gamepad.hpp @@ -63,10 +63,10 @@ namespace xna { public: virtual ~IGamePad(){} - static GamePadState GetState(PlayerIndex index); - static GamePadState GetState(PlayerIndex index, GamePadDeadZone deadZone); - static GamePadCapabilities GetCapabilities(PlayerIndex index); - static bool SetVibration(PlayerIndex index, float leftMotor, float rightMotor, float leftTrigger = 0, float rightTrigger = 0); + virtual GamePadState GetState(PlayerIndex index) = 0; + virtual GamePadState GetState(PlayerIndex index, GamePadDeadZone deadZone) = 0; + virtual GamePadCapabilities GetCapabilities(PlayerIndex index) = 0; + virtual bool SetVibration(PlayerIndex index, float leftMotor, float rightMotor, float leftTrigger = 0, float rightTrigger = 0) = 0; }; } diff --git a/framework/platform/dxheaders.hpp b/framework/platform/dxheaders.hpp index e7b4394..a2fc464 100644 --- a/framework/platform/dxheaders.hpp +++ b/framework/platform/dxheaders.hpp @@ -5,4 +5,7 @@ #include #include #define NOMINMAX -#include \ No newline at end of file +#include +#include +#include +#include \ No newline at end of file diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index 2532a8a..787deb9 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -8,30 +8,17 @@ #include "keyboard-dx.hpp" #include "mouse-dx.hpp" #include "window-dx.hpp" -#include "Windows.h" +#include namespace xna { Game::Game() { - Keyboard::_dxKeyboard = uNew(); - Mouse::_dxMouse = uNew(); - GamePad::_dxGamePad = uNew(); - _gameWindow = New(); _gameWindow->Color(255, 155, 55); _gameWindow->Title("XN65"); _gameWindow->Size( GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight, false); - - //CoInitializeEx é requisito para inicialização correta de AudioEngine - const auto hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); - - if (FAILED(hr)) { - MessageBox(nullptr, "Ocorreu um erro ao executar a função CoInitializeEx", "XN65", MB_OK); - } - - _audioEngine = New(); - } + } void Game::Exit() { @@ -54,6 +41,25 @@ namespace xna { } void Game::Initialize() { + Keyboard::Initialize(); + Mouse::Initialize(); + + //initialize é requisito para GamePad + Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED); + + if (FAILED(initialize)) + MessageBox(nullptr, "Ocorreu um erro ao executar Microsoft::WRL::Wrappers::RoInitializeWrapper. O GamePad não foi inicializado corretamente.", "XN65", MB_OK); + + GamePad.Initialize(); + + //CoInitializeEx é requisito para AudioEngine + const auto hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); + + if (FAILED(hr)) + MessageBox(nullptr, "Ocorreu um erro ao executar CoInitializeEx. O AudioEngine não foi inicializado corretamente.", "XN65", MB_OK); + + _audioEngine = New(); + LoadContent(); } @@ -78,19 +84,19 @@ namespace xna { } while (msg.message != WM_QUIT); return static_cast(msg.wParam); - } + } void Game::step() { - _stepTimer.Tick([&]() + _stepTimer.Tick([&]() { const auto elapsed = _stepTimer.GetElapsedSeconds(); - const auto total =_stepTimer.GetTotalSeconds(); + 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); + Update(_currentGameTime); }); Draw(_currentGameTime); diff --git a/framework/platform/game-dx.hpp b/framework/platform/game-dx.hpp index 1fe6a32..a22ef34 100644 --- a/framework/platform/game-dx.hpp +++ b/framework/platform/game-dx.hpp @@ -3,7 +3,6 @@ #include "../default.hpp" #include "../game/game.hpp" -#include "clock-dx.hpp" #include "dxheaders.hpp" #include "dx/StepTimer.hpp" diff --git a/framework/platform/gamepad-dx.cpp b/framework/platform/gamepad-dx.cpp index 7747809..ea55d66 100644 --- a/framework/platform/gamepad-dx.cpp +++ b/framework/platform/gamepad-dx.cpp @@ -1,6 +1,39 @@ #include "gamepad-dx.hpp" -#include namespace xna { + GamePadState _GamePad::GetState(PlayerIndex index) { + if (!_dxGamePad) + return GamePadState(); + const auto state = _dxGamePad->GetState( + static_cast(index) + ); + return GamePadState(state); + } + + GamePadState _GamePad::GetState(PlayerIndex index, GamePadDeadZone deadZone) { + if (!_dxGamePad) + return GamePadState(); + + const auto state = _dxGamePad->GetState( + static_cast(index), + static_cast(deadZone) + ); + return GamePadState(state); + } + + GamePadCapabilities _GamePad::GetCapabilities(PlayerIndex index) { + if (!_dxGamePad) + return GamePadCapabilities(); + + const auto capabilities = _dxGamePad->GetCapabilities(static_cast(index)); + return GamePadCapabilities(capabilities); + } + + bool _GamePad::SetVibration(PlayerIndex index, float leftMotor, float rightMotor, float leftTrigger, float rightTrigger) { + if (!_dxGamePad) + return false; + + return _dxGamePad->SetVibration(static_cast(index), leftMotor, rightMotor, leftTrigger, rightTrigger); + } } \ No newline at end of file diff --git a/framework/platform/gamepad-dx.hpp b/framework/platform/gamepad-dx.hpp index 5775ad6..57d0d01 100644 --- a/framework/platform/gamepad-dx.hpp +++ b/framework/platform/gamepad-dx.hpp @@ -357,55 +357,34 @@ namespace xna { DirectX::GamePad::State _dxState{}; }; - class GamePad : public IGamePad { + class _GamePad : public IGamePad { public: - static void Initialize() { + constexpr _GamePad() = default; + + virtual ~_GamePad() override { + if (_dxGamePad) { + _dxGamePad->Suspend(); + _dxGamePad = nullptr; + } + } + + void Initialize() { _dxGamePad = uNew(); } - inline static uptr _dxGamePad = nullptr; + virtual GamePadState GetState(PlayerIndex index) override; + virtual GamePadState GetState(PlayerIndex index, GamePadDeadZone deadZone) override; + virtual GamePadCapabilities GetCapabilities(PlayerIndex index) override; + virtual bool SetVibration(PlayerIndex index, float leftMotor, float rightMotor, float leftTrigger, float rightTrigger) override; - private: - constexpr GamePad() = default; - constexpr GamePad(GamePad&&) = default; - constexpr GamePad(const GamePad&) = default; + public: + uptr _dxGamePad = nullptr; + + private: + constexpr _GamePad(_GamePad&&) = default; }; - inline GamePadState IGamePad::GetState(PlayerIndex index) { - if (!GamePad::_dxGamePad) - return GamePadState(); - - const auto state = GamePad::_dxGamePad->GetState( - static_cast(index) - ); - return GamePadState(state); - } - - inline GamePadState IGamePad::GetState(PlayerIndex index, GamePadDeadZone deadZone) { - if (!GamePad::_dxGamePad) - return GamePadState(); - - const auto state = GamePad::_dxGamePad->GetState( - static_cast(index), - static_cast(deadZone) - ); - return GamePadState(state); - } - - inline GamePadCapabilities IGamePad::GetCapabilities(PlayerIndex index) { - if (!GamePad::_dxGamePad) - return GamePadCapabilities(); - - const auto capabilities = GamePad::_dxGamePad->GetCapabilities(static_cast(index)); - return GamePadCapabilities(capabilities); - } - - inline bool IGamePad::SetVibration(PlayerIndex index, float leftMotor, float rightMotor, float leftTrigger, float rightTrigger) { - if (!GamePad::_dxGamePad) - return false; - - return GamePad::_dxGamePad->SetVibration(static_cast(index), leftMotor, rightMotor, leftTrigger, rightTrigger); - } + inline static _GamePad GamePad = _GamePad(); } #endif \ No newline at end of file diff --git a/framework/platform/keyboard-dx.hpp b/framework/platform/keyboard-dx.hpp index af5ad26..3f48e42 100644 --- a/framework/platform/keyboard-dx.hpp +++ b/framework/platform/keyboard-dx.hpp @@ -24,7 +24,11 @@ namespace xna { DirectX::Keyboard::State _state{}; }; - class Keyboard : public IKeyboard { + struct Keyboard : public IKeyboard { + inline static void Initialize() { + _dxKeyboard = uNew(); + } + private: constexpr Keyboard() = default; constexpr Keyboard(Keyboard&&) = default; diff --git a/framework/platform/mouse-dx.hpp b/framework/platform/mouse-dx.hpp index 9ffdd28..941d029 100644 --- a/framework/platform/mouse-dx.hpp +++ b/framework/platform/mouse-dx.hpp @@ -21,6 +21,10 @@ namespace xna { }; struct Mouse : public IMouse { + inline static void Initialize() { + _dxMouse = uNew(); + } + public: inline static uptr _dxMouse = nullptr; diff --git a/framework/platform/window-dx.cpp b/framework/platform/window-dx.cpp index 5352cda..194e8ad 100644 --- a/framework/platform/window-dx.cpp +++ b/framework/platform/window-dx.cpp @@ -1,6 +1,7 @@ #include "window-dx.hpp" #include "keyboard-dx.hpp" #include "mouse-dx.hpp" +#include "gamepad-dx.hpp" namespace xna { GameWindow::GameWindow() { @@ -168,8 +169,13 @@ namespace xna { case WM_MOUSEHOVER: Mouse::_dxMouse->ProcessMessage(msg, wParam, lParam); break; + case WM_KILLFOCUS: + if(GamePad._dxGamePad) GamePad._dxGamePad->Suspend(); + break; + case WM_SETFOCUS: + if (GamePad._dxGamePad) GamePad._dxGamePad->Resume(); + break; } - return DefWindowProc(hWnd, msg, wParam, lParam); } } \ No newline at end of file diff --git a/framework/xna.cpp b/framework/xna.cpp index 21274ea..ed6d1ec 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -6,11 +6,6 @@ using namespace std; using namespace xna; -//int main() -//{ -// cout << "Hello CMake." << endl; -// return 0; -//} namespace xna { class Game1 : public Game { public: @@ -34,12 +29,12 @@ namespace xna { texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); auto audio = AudioEngine(); - + Game::LoadContent(); } void Update(GameTime const& gameTime) override { - if (Keyboard::GetState().IsKeyDown(Keys::Escape) || GamePad::GetState(PlayerIndex::One).IsButtonDown(Buttons::Back)) + if (Keyboard::GetState().IsKeyDown(Keys::Escape) || GamePad.GetState(PlayerIndex::One).IsButtonDown(Buttons::Back)) Exit(); Game::Update(gameTime);