1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementações em GamePad

This commit is contained in:
Danilo 2024-04-24 10:11:53 -03:00
parent aa63d7cef9
commit aef3e85b5d
10 changed files with 106 additions and 77 deletions

View File

@ -63,10 +63,10 @@ namespace xna {
public: public:
virtual ~IGamePad(){} virtual ~IGamePad(){}
static GamePadState GetState(PlayerIndex index); virtual GamePadState GetState(PlayerIndex index) = 0;
static GamePadState GetState(PlayerIndex index, GamePadDeadZone deadZone); virtual GamePadState GetState(PlayerIndex index, GamePadDeadZone deadZone) = 0;
static GamePadCapabilities GetCapabilities(PlayerIndex index); virtual GamePadCapabilities GetCapabilities(PlayerIndex index) = 0;
static bool SetVibration(PlayerIndex index, float leftMotor, float rightMotor, float leftTrigger = 0, float rightTrigger = 0); virtual bool SetVibration(PlayerIndex index, float leftMotor, float rightMotor, float leftTrigger = 0, float rightTrigger = 0) = 0;
}; };
} }

View File

@ -5,4 +5,7 @@
#include <d3dcompiler.h> #include <d3dcompiler.h>
#include <DirectXMath.h> #include <DirectXMath.h>
#define NOMINMAX #define NOMINMAX
#include <Windows.h> #include <Windows.h>
#include <Windows.Foundation.h>
#include <wrl\wrappers\corewrappers.h>
#include <wrl\client.h>

View File

@ -8,30 +8,17 @@
#include "keyboard-dx.hpp" #include "keyboard-dx.hpp"
#include "mouse-dx.hpp" #include "mouse-dx.hpp"
#include "window-dx.hpp" #include "window-dx.hpp"
#include "Windows.h" #include <Windows.h>
namespace xna { namespace xna {
Game::Game() { Game::Game() {
Keyboard::_dxKeyboard = uNew<DirectX::Keyboard>();
Mouse::_dxMouse = uNew<DirectX::Mouse>();
GamePad::_dxGamePad = uNew<DirectX::GamePad>();
_gameWindow = New<GameWindow>(); _gameWindow = New<GameWindow>();
_gameWindow->Color(255, 155, 55); _gameWindow->Color(255, 155, 55);
_gameWindow->Title("XN65"); _gameWindow->Title("XN65");
_gameWindow->Size( _gameWindow->Size(
GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferWidth,
GraphicsDeviceManager::DefaultBackBufferHeight, false); 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<AudioEngine>();
}
void Game::Exit() void Game::Exit()
{ {
@ -54,6 +41,25 @@ namespace xna {
} }
void Game::Initialize() { 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<AudioEngine>();
LoadContent(); LoadContent();
} }
@ -78,19 +84,19 @@ namespace xna {
} while (msg.message != WM_QUIT); } while (msg.message != WM_QUIT);
return static_cast<int>(msg.wParam); return static_cast<int>(msg.wParam);
} }
void Game::step() void Game::step()
{ {
_stepTimer.Tick([&]() _stepTimer.Tick([&]()
{ {
const auto elapsed = _stepTimer.GetElapsedSeconds(); const auto elapsed = _stepTimer.GetElapsedSeconds();
const auto total =_stepTimer.GetTotalSeconds(); const auto total = _stepTimer.GetTotalSeconds();
const auto elapsedTimeSpan = TimeSpan::FromSeconds(elapsed); const auto elapsedTimeSpan = TimeSpan::FromSeconds(elapsed);
const auto totalTimeSpan = TimeSpan::FromSeconds(total); const auto totalTimeSpan = TimeSpan::FromSeconds(total);
_currentGameTime.ElapsedGameTime = elapsedTimeSpan; _currentGameTime.ElapsedGameTime = elapsedTimeSpan;
_currentGameTime.TotalGameTime = totalTimeSpan; _currentGameTime.TotalGameTime = totalTimeSpan;
Update(_currentGameTime); Update(_currentGameTime);
}); });
Draw(_currentGameTime); Draw(_currentGameTime);

View File

@ -3,7 +3,6 @@
#include "../default.hpp" #include "../default.hpp"
#include "../game/game.hpp" #include "../game/game.hpp"
#include "clock-dx.hpp"
#include "dxheaders.hpp" #include "dxheaders.hpp"
#include "dx/StepTimer.hpp" #include "dx/StepTimer.hpp"

View File

@ -1,6 +1,39 @@
#include "gamepad-dx.hpp" #include "gamepad-dx.hpp"
#include <GamePad.h>
namespace xna { namespace xna {
GamePadState _GamePad::GetState(PlayerIndex index) {
if (!_dxGamePad)
return GamePadState();
const auto state = _dxGamePad->GetState(
static_cast<int>(index)
);
return GamePadState(state);
}
GamePadState _GamePad::GetState(PlayerIndex index, GamePadDeadZone deadZone) {
if (!_dxGamePad)
return GamePadState();
const auto state = _dxGamePad->GetState(
static_cast<int>(index),
static_cast<DirectX::GamePad::DeadZone>(deadZone)
);
return GamePadState(state);
}
GamePadCapabilities _GamePad::GetCapabilities(PlayerIndex index) {
if (!_dxGamePad)
return GamePadCapabilities();
const auto capabilities = _dxGamePad->GetCapabilities(static_cast<int>(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<int>(index), leftMotor, rightMotor, leftTrigger, rightTrigger);
}
} }

View File

@ -357,55 +357,34 @@ namespace xna {
DirectX::GamePad::State _dxState{}; DirectX::GamePad::State _dxState{};
}; };
class GamePad : public IGamePad { class _GamePad : public IGamePad {
public: public:
static void Initialize() { constexpr _GamePad() = default;
virtual ~_GamePad() override {
if (_dxGamePad) {
_dxGamePad->Suspend();
_dxGamePad = nullptr;
}
}
void Initialize() {
_dxGamePad = uNew<DirectX::GamePad>(); _dxGamePad = uNew<DirectX::GamePad>();
} }
inline static uptr<DirectX::GamePad> _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: public:
constexpr GamePad() = default; uptr<DirectX::GamePad> _dxGamePad = nullptr;
constexpr GamePad(GamePad&&) = default;
constexpr GamePad(const GamePad&) = default; private:
constexpr _GamePad(_GamePad&&) = default;
}; };
inline GamePadState IGamePad::GetState(PlayerIndex index) { inline static _GamePad GamePad = _GamePad();
if (!GamePad::_dxGamePad)
return GamePadState();
const auto state = GamePad::_dxGamePad->GetState(
static_cast<int>(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<int>(index),
static_cast<DirectX::GamePad::DeadZone>(deadZone)
);
return GamePadState(state);
}
inline GamePadCapabilities IGamePad::GetCapabilities(PlayerIndex index) {
if (!GamePad::_dxGamePad)
return GamePadCapabilities();
const auto capabilities = GamePad::_dxGamePad->GetCapabilities(static_cast<int>(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<int>(index), leftMotor, rightMotor, leftTrigger, rightTrigger);
}
} }
#endif #endif

View File

@ -24,7 +24,11 @@ namespace xna {
DirectX::Keyboard::State _state{}; DirectX::Keyboard::State _state{};
}; };
class Keyboard : public IKeyboard { struct Keyboard : public IKeyboard {
inline static void Initialize() {
_dxKeyboard = uNew<DirectX::Keyboard>();
}
private: private:
constexpr Keyboard() = default; constexpr Keyboard() = default;
constexpr Keyboard(Keyboard&&) = default; constexpr Keyboard(Keyboard&&) = default;

View File

@ -21,6 +21,10 @@ namespace xna {
}; };
struct Mouse : public IMouse { struct Mouse : public IMouse {
inline static void Initialize() {
_dxMouse = uNew<DirectX::Mouse>();
}
public: public:
inline static uptr<DirectX::Mouse> _dxMouse = nullptr; inline static uptr<DirectX::Mouse> _dxMouse = nullptr;

View File

@ -1,6 +1,7 @@
#include "window-dx.hpp" #include "window-dx.hpp"
#include "keyboard-dx.hpp" #include "keyboard-dx.hpp"
#include "mouse-dx.hpp" #include "mouse-dx.hpp"
#include "gamepad-dx.hpp"
namespace xna { namespace xna {
GameWindow::GameWindow() { GameWindow::GameWindow() {
@ -168,8 +169,13 @@ namespace xna {
case WM_MOUSEHOVER: case WM_MOUSEHOVER:
Mouse::_dxMouse->ProcessMessage(msg, wParam, lParam); Mouse::_dxMouse->ProcessMessage(msg, wParam, lParam);
break; 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); return DefWindowProc(hWnd, msg, wParam, lParam);
} }
} }

View File

@ -6,11 +6,6 @@
using namespace std; using namespace std;
using namespace xna; using namespace xna;
//int main()
//{
// cout << "Hello CMake." << endl;
// return 0;
//}
namespace xna { namespace xna {
class Game1 : public Game { class Game1 : public Game {
public: public:
@ -34,12 +29,12 @@ namespace xna {
texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err);
auto audio = AudioEngine(); auto audio = AudioEngine();
Game::LoadContent(); Game::LoadContent();
} }
void Update(GameTime const& gameTime) override { 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(); Exit();
Game::Update(gameTime); Game::Update(gameTime);