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:
parent
aa63d7cef9
commit
aef3e85b5d
@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -5,4 +5,7 @@
|
||||
#include <d3dcompiler.h>
|
||||
#include <DirectXMath.h>
|
||||
#define NOMINMAX
|
||||
#include <Windows.h>
|
||||
#include <Windows.h>
|
||||
#include <Windows.Foundation.h>
|
||||
#include <wrl\wrappers\corewrappers.h>
|
||||
#include <wrl\client.h>
|
@ -8,30 +8,17 @@
|
||||
#include "keyboard-dx.hpp"
|
||||
#include "mouse-dx.hpp"
|
||||
#include "window-dx.hpp"
|
||||
#include "Windows.h"
|
||||
#include <Windows.h>
|
||||
|
||||
namespace xna {
|
||||
Game::Game() {
|
||||
Keyboard::_dxKeyboard = uNew<DirectX::Keyboard>();
|
||||
Mouse::_dxMouse = uNew<DirectX::Mouse>();
|
||||
GamePad::_dxGamePad = uNew<DirectX::GamePad>();
|
||||
|
||||
_gameWindow = New<GameWindow>();
|
||||
_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<AudioEngine>();
|
||||
}
|
||||
}
|
||||
|
||||
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<AudioEngine>();
|
||||
|
||||
LoadContent();
|
||||
}
|
||||
|
||||
@ -78,19 +84,19 @@ namespace xna {
|
||||
} while (msg.message != WM_QUIT);
|
||||
|
||||
return static_cast<int>(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);
|
||||
|
@ -3,7 +3,6 @@
|
||||
|
||||
#include "../default.hpp"
|
||||
#include "../game/game.hpp"
|
||||
#include "clock-dx.hpp"
|
||||
#include "dxheaders.hpp"
|
||||
#include "dx/StepTimer.hpp"
|
||||
|
||||
|
@ -1,6 +1,39 @@
|
||||
#include "gamepad-dx.hpp"
|
||||
#include <GamePad.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -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<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:
|
||||
constexpr GamePad() = default;
|
||||
constexpr GamePad(GamePad&&) = default;
|
||||
constexpr GamePad(const GamePad&) = default;
|
||||
public:
|
||||
uptr<DirectX::GamePad> _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<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);
|
||||
}
|
||||
inline static _GamePad GamePad = _GamePad();
|
||||
}
|
||||
|
||||
#endif
|
@ -24,7 +24,11 @@ namespace xna {
|
||||
DirectX::Keyboard::State _state{};
|
||||
};
|
||||
|
||||
class Keyboard : public IKeyboard {
|
||||
struct Keyboard : public IKeyboard {
|
||||
inline static void Initialize() {
|
||||
_dxKeyboard = uNew<DirectX::Keyboard>();
|
||||
}
|
||||
|
||||
private:
|
||||
constexpr Keyboard() = default;
|
||||
constexpr Keyboard(Keyboard&&) = default;
|
||||
|
@ -21,6 +21,10 @@ namespace xna {
|
||||
};
|
||||
|
||||
struct Mouse : public IMouse {
|
||||
inline static void Initialize() {
|
||||
_dxMouse = uNew<DirectX::Mouse>();
|
||||
}
|
||||
|
||||
public:
|
||||
inline static uptr<DirectX::Mouse> _dxMouse = nullptr;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user