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

Implementação de Keyboard e mudanças em Game

This commit is contained in:
Danilo 2024-04-16 16:13:36 -03:00
parent 3fe3cc7066
commit 3274acf478
23 changed files with 380 additions and 49 deletions

View File

@ -3,7 +3,7 @@
#
# Add source to this project's executable.
add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizerstate-dx.cpp" "platform/vertexbuffer-dx.cpp" "platform/indexbuffer-dx.cpp" "common/matrix.cpp" "platform/constbuffer-dx.cpp" "platform/databuffer-dx.cpp" "platform/samplerstate-dx.cpp" "platform/spritebatch-dx.cpp" "platform/spritefont-dx.cpp" "platform/depthstencilstate-dx.cpp")
add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizerstate-dx.cpp" "platform/vertexbuffer-dx.cpp" "platform/indexbuffer-dx.cpp" "common/matrix.cpp" "platform/constbuffer-dx.cpp" "platform/databuffer-dx.cpp" "platform/samplerstate-dx.cpp" "platform/spritebatch-dx.cpp" "platform/spritefont-dx.cpp" "platform/depthstencilstate-dx.cpp" "platform/keyboard-dx.cpp")
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET xna PROPERTY CXX_STANDARD 20)

View File

@ -87,6 +87,174 @@ namespace xna {
HiDef
};
enum class Keys {
None = 0,
Back = 8,
Tab = 9,
Enter = 13, // 0x0000000D
Pause = 19, // 0x00000013
CapsLock = 20, // 0x00000014
Kana = 21, // 0x00000015
Kanji = 25, // 0x00000019
Escape = 27, // 0x0000001B
ImeConvert = 28, // 0x0000001C
ImeNoConvert = 29, // 0x0000001D
Space = 32, // 0x00000020
PageUp = 33, // 0x00000021
PageDown = 34, // 0x00000022
End = 35, // 0x00000023
Home = 36, // 0x00000024
Left = 37, // 0x00000025
Up = 38, // 0x00000026
Right = 39, // 0x00000027
Down = 40, // 0x00000028
Select = 41, // 0x00000029
Print = 42, // 0x0000002A
Execute = 43, // 0x0000002B
PrintScreen = 44, // 0x0000002C
Insert = 45, // 0x0000002D
Delete = 46, // 0x0000002E
Help = 47, // 0x0000002F
D0 = 48, // 0x00000030
D1 = 49, // 0x00000031
D2 = 50, // 0x00000032
D3 = 51, // 0x00000033
D4 = 52, // 0x00000034
D5 = 53, // 0x00000035
D6 = 54, // 0x00000036
D7 = 55, // 0x00000037
D8 = 56, // 0x00000038
D9 = 57, // 0x00000039
A = 65, // 0x00000041
B = 66, // 0x00000042
C = 67, // 0x00000043
D = 68, // 0x00000044
E = 69, // 0x00000045
F = 70, // 0x00000046
G = 71, // 0x00000047
H = 72, // 0x00000048
I = 73, // 0x00000049
J = 74, // 0x0000004A
K = 75, // 0x0000004B
L = 76, // 0x0000004C
M = 77, // 0x0000004D
N = 78, // 0x0000004E
O = 79, // 0x0000004F
P = 80, // 0x00000050
Q = 81, // 0x00000051
R = 82, // 0x00000052
S = 83, // 0x00000053
T = 84, // 0x00000054
U = 85, // 0x00000055
V = 86, // 0x00000056
W = 87, // 0x00000057
X = 88, // 0x00000058
Y = 89, // 0x00000059
Z = 90, // 0x0000005A
LeftWindows = 91, // 0x0000005B
RightWindows = 92, // 0x0000005C
Apps = 93, // 0x0000005D
Sleep = 95, // 0x0000005F
NumPad0 = 96, // 0x00000060
NumPad1 = 97, // 0x00000061
NumPad2 = 98, // 0x00000062
NumPad3 = 99, // 0x00000063
NumPad4 = 100, // 0x00000064
NumPad5 = 101, // 0x00000065
NumPad6 = 102, // 0x00000066
NumPad7 = 103, // 0x00000067
NumPad8 = 104, // 0x00000068
NumPad9 = 105, // 0x00000069
Multiply = 106, // 0x0000006A
Add = 107, // 0x0000006B
Separator = 108, // 0x0000006C
Subtract = 109, // 0x0000006D
Decimal = 110, // 0x0000006E
Divide = 111, // 0x0000006F
F1 = 112, // 0x00000070
F2 = 113, // 0x00000071
F3 = 114, // 0x00000072
F4 = 115, // 0x00000073
F5 = 116, // 0x00000074
F6 = 117, // 0x00000075
F7 = 118, // 0x00000076
F8 = 119, // 0x00000077
F9 = 120, // 0x00000078
F10 = 121, // 0x00000079
F11 = 122, // 0x0000007A
F12 = 123, // 0x0000007B
F13 = 124, // 0x0000007C
F14 = 125, // 0x0000007D
F15 = 126, // 0x0000007E
F16 = 127, // 0x0000007F
F17 = 128, // 0x00000080
F18 = 129, // 0x00000081
F19 = 130, // 0x00000082
F20 = 131, // 0x00000083
F21 = 132, // 0x00000084
F22 = 133, // 0x00000085
F23 = 134, // 0x00000086
F24 = 135, // 0x00000087
NumLock = 144, // 0x00000090
Scroll = 145, // 0x00000091
LeftShift = 160, // 0x000000A0
RightShift = 161, // 0x000000A1
LeftControl = 162, // 0x000000A2
RightControl = 163, // 0x000000A3
LeftAlt = 164, // 0x000000A4
RightAlt = 165, // 0x000000A5
BrowserBack = 166, // 0x000000A6
BrowserForward = 167, // 0x000000A7
BrowserRefresh = 168, // 0x000000A8
BrowserStop = 169, // 0x000000A9
BrowserSearch = 170, // 0x000000AA
BrowserFavorites = 171, // 0x000000AB
BrowserHome = 172, // 0x000000AC
VolumeMute = 173, // 0x000000AD
VolumeDown = 174, // 0x000000AE
VolumeUp = 175, // 0x000000AF
MediaNextTrack = 176, // 0x000000B0
MediaPreviousTrack = 177, // 0x000000B1
MediaStop = 178, // 0x000000B2
MediaPlayPause = 179, // 0x000000B3
LaunchMail = 180, // 0x000000B4
SelectMedia = 181, // 0x000000B5
LaunchApplication1 = 182, // 0x000000B6
LaunchApplication2 = 183, // 0x000000B7
OemSemicolon = 186, // 0x000000BA
OemPlus = 187, // 0x000000BB
OemComma = 188, // 0x000000BC
OemMinus = 189, // 0x000000BD
OemPeriod = 190, // 0x000000BE
OemQuestion = 191, // 0x000000BF
OemTilde = 192, // 0x000000C0
ChatPadGreen = 202, // 0x000000CA
ChatPadOrange = 203, // 0x000000CB
OemOpenBrackets = 219, // 0x000000DB
OemPipe = 220, // 0x000000DC
OemCloseBrackets = 221, // 0x000000DD
OemQuotes = 222, // 0x000000DE
Oem8 = 223, // 0x000000DF
OemBackslash = 226, // 0x000000E2
ProcessKey = 229, // 0x000000E5
OemCopy = 242, // 0x000000F2
OemAuto = 243, // 0x000000F3
OemEnlW = 244, // 0x000000F4
Attn = 246, // 0x000000F6
Crsel = 247, // 0x000000F7
Exsel = 248, // 0x000000F8
EraseEof = 249, // 0x000000F9
Play = 250, // 0x000000FA
Zoom = 251, // 0x000000FB
Pa1 = 253, // 0x000000FD
OemClear = 254, // 0x000000FE
};
enum class KeyState {
Up,
Down,
};
enum class PresentInterval {
Default,
One,

View File

@ -131,6 +131,10 @@ namespace xna {
using pVertexShader = std::shared_ptr<VertexShader>;
struct Viewport;
using PViewport = std::shared_ptr<Viewport>;
//Input
struct KeyboardState;
using PKeyboardState = std::shared_ptr<KeyboardState>;
}
#endif

View File

@ -18,6 +18,7 @@ namespace xna {
virtual void Stop() = 0;
virtual TimeSpan ElapsedTime() = 0;
virtual TimeSpan TotalTime() = 0;
virtual bool IsActive() = 0;
};
}

View File

@ -20,6 +20,7 @@ namespace xna {
protected:
virtual void Draw(GameTime const& gameTime) = 0;
virtual void Initialize() = 0;
virtual void LoadContent() = 0;
virtual void Update(GameTime const& gameTime) = 0;
};
}

View File

@ -10,6 +10,7 @@ namespace xna {
public:
virtual ~IGraphicsDeviceManager(){}
virtual void ApplyChanges() = 0;
virtual bool Initialize() = 0;
virtual void ToggleFullScreen() = 0;
virtual Int PreferredBackBufferWidth() const = 0;
virtual Int PreferredBackBufferHeight() const = 0;

View File

@ -16,6 +16,7 @@ namespace xna {
public:
virtual ~IGraphicsDevice() {}
virtual void Clear() = 0;
virtual void Clear(Color const& color) = 0;
virtual bool Initialize(GameWindow& gameWindow) = 0;
virtual bool Present() = 0;
virtual PGraphicsAdapter Adapter() const = 0;

View File

@ -7,8 +7,6 @@
namespace xna {
class ISpriteBatch {
public:
static constexpr Matrix _identity = Matrix::Identity();
virtual ~ISpriteBatch(){}
virtual void Begin(
SpriteSortMode sortMode = SpriteSortMode::Deferred,
@ -17,7 +15,7 @@ namespace xna {
DepthStencilState * depthStencil = nullptr,
RasterizerState* rasterizerState = nullptr,
//Effect
Matrix const& transformMatrix = _identity
Matrix const& transformMatrix = Matrix::Identity()
) = 0;
virtual void End() = 0;
virtual void Draw(Texture2D& texture, Vector2 const& position, Color const& color) = 0;

View File

@ -0,0 +1,21 @@
#ifndef XNA_INPUT_KEYBOARD_HPP
#define XNA_INPUT_KEYBOARD_HPP
#include "../default.hpp"
namespace xna {
struct IKeyboardState {
public:
virtual bool IsKeyDown(Keys key) const = 0;
virtual bool IsKeyUp(Keys key) const = 0;
};
class IKeyboard {
public:
virtual ~IKeyboard(){}
static KeyboardState GetState();
static bool IsConnected();
};
}
#endif

View File

@ -17,6 +17,10 @@ namespace xna {
virtual void Stop() override;
virtual TimeSpan ElapsedTime() override;
virtual TimeSpan TotalTime() override;
virtual constexpr bool IsActive() override {
return !_suspended && !_stopped;
}
inline SteadyClock::time_point ClockNow() {
return SteadyClock::now();
@ -40,8 +44,7 @@ namespace xna {
SteadyClock::time_point _end;
long long _total{ 0 };
bool _suspended{ false };
bool _stopped { false };
static constexpr double _millisecondMask = 100'000'000.0;
bool _stopped { true };
};
}

View File

@ -5,6 +5,7 @@
#include "adapter-dx.hpp"
#include "blendstate-dx.hpp"
#include "gdeviceinfo-dx.hpp"
#include "../common/color.hpp"
namespace xna {
GraphicsDevice::GraphicsDevice() {
@ -148,4 +149,15 @@ namespace xna {
void GraphicsDevice::Clear() {
_context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor);
}
void GraphicsDevice::Clear(Color const& color) {
const auto v4 = color.ToVector4();
_backgroundColor[0] = v4.X;
_backgroundColor[1] = v4.Y;
_backgroundColor[2] = v4.Z;
_backgroundColor[3] = v4.W;
_context->ClearRenderTargetView(_renderTarget2D->_renderTargetView, _backgroundColor);
}
}

View File

@ -30,6 +30,7 @@ namespace xna {
}
virtual void Clear() override;
virtual void Clear(Color const& color) override;
virtual bool Initialize(GameWindow& gameWindow) override;
virtual bool Present() override;
@ -78,7 +79,7 @@ namespace xna {
PRenderTarget2D _renderTarget2D{ nullptr };
xna::Viewport _viewport{};
PBlendState _blendState{ nullptr };
bool _usevsync{ false };
bool _usevsync{ true };
bool createDevice();
};

View File

@ -1,4 +1,6 @@
#include "dxgi.h"
#include "d3d11.h"
#include <d3dcompiler.h>
#include <DirectXMath.h>
#include <DirectXMath.h>
#define NOMINMAX
#include <Windows.h>

View File

@ -16,8 +16,10 @@ namespace xna {
}
int Game::Run() {
Initialize();
if (_graphicsDevice == nullptr) {
MessageBox(nullptr, "O dispositivo gráfico não foi inicializar corretamente", "Xna Game Engine", MB_OK);
MessageBox(nullptr, "O dispositivo gráfico não foi inicializado corretamente", "Xna Game Engine", MB_OK);
return EXIT_FAILURE;
}
@ -25,38 +27,37 @@ namespace xna {
}
int Game::startLoop() {
MSG msg{};
MSG msg{};
_clock.Start();
GameTime gameTime{};
TimeSpan endElapsedTime{};
do {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
{
TranslateMessage(&msg);
DispatchMessage(&msg);
DispatchMessage(&msg);
}
else {
auto elapsed = _clock.ElapsedTime();
gameTime.ElapsedGameTime = elapsed - endElapsedTime;
gameTime.TotalGameTime = _clock.TotalTime();
this->Update(gameTime);
elapsed = _clock.ElapsedTime();
gameTime.ElapsedGameTime = elapsed - endElapsedTime;
gameTime.TotalGameTime = _clock.TotalTime();
this->Draw(gameTime);
_graphicsDevice->Present();
endElapsedTime = _clock.ElapsedTime();
tick();
}
} while (msg.message != WM_QUIT);
return static_cast<int>(msg.wParam);
}
void Game::tick() {
_clock.Reset();
this->Update(_currentGameTime);
_currentGameTime.ElapsedGameTime = _clock.ElapsedTime();
_currentGameTime.TotalGameTime = _clock.TotalTime();
this->Draw(_currentGameTime);
_graphicsDevice->Present();
_currentGameTime.ElapsedGameTime = _clock.ElapsedTime();
_currentGameTime.TotalGameTime = _clock.TotalTime();
}
}

View File

@ -27,7 +27,8 @@ namespace xna {
protected:
virtual void Draw(GameTime const& gameTime) override{}
virtual void Initialize() override{}
virtual void Initialize() override { LoadContent(); }
virtual void LoadContent() override{}
virtual void Update(GameTime const& gameTime) override{}
public:
@ -39,7 +40,9 @@ namespace xna {
GameClock _clock{};
GameTime _currentGameTime{};
int startLoop();
private:
int startLoop();
void tick();
};
}

View File

@ -5,21 +5,26 @@
#include "gdeviceinfo-dx.hpp"
namespace xna {
GraphicsDeviceManager::GraphicsDeviceManager(Game* game) : _game(game){
GraphicsDeviceManager::GraphicsDeviceManager(Game*& game) : _game(game) {
}
bool GraphicsDeviceManager::Initialize() {
GraphicsDeviceInformation information;
const auto adp = GraphicsAdapter::DefaultAdapter();
information.Adapter(adp);
information.GraphicsProfile(xna::GraphicsProfile::HiDef);
PresentationParameters parameters;
parameters.BackBufferWidth = _backBufferWidth;
parameters.BackBufferHeight = _backBufferHeight;
information.PresentationParameters(parameters);
information.Window(game->Window());
information.Window(_game->Window());
CreateDevice(information);
return true;
}
void GraphicsDeviceManager::ApplyChanges() {
@ -31,7 +36,8 @@ namespace xna {
void GraphicsDeviceManager::CreateDevice(GraphicsDeviceInformation const& info) {
_device = New<GraphicsDevice>(info);
auto window = _game->Window();
//auto window = _game->Window();
auto window = info.Window();
if (!window->Create()) {
MessageBox(nullptr, "Falha na criação da janela", "Xna Game Engine", MB_OK);

View File

@ -6,11 +6,12 @@
namespace xna {
class GraphicsDeviceManager : public IGraphicsDeviceManager {
public:
GraphicsDeviceManager(Game* game);
GraphicsDeviceManager(Game*& game);
virtual ~GraphicsDeviceManager() override{}
virtual void ApplyChanges() override;
virtual bool Initialize() override;
virtual void ToggleFullScreen() override;
virtual constexpr Int PreferredBackBufferWidth() const {
@ -36,8 +37,8 @@ namespace xna {
virtual void ChangeDevice() override;
public:
static constexpr int DefaultBackBufferWidth = 800;
static constexpr int DefaultBackBufferHeight = 480;
static constexpr int DefaultBackBufferWidth = 1280;//800;
static constexpr int DefaultBackBufferHeight = 720;// 480;
private:
Game*& _game;
@ -45,7 +46,6 @@ namespace xna {
Int _backBufferHeight{ DefaultBackBufferHeight };
bool _isDeviceDirty{ false };
PGraphicsDevice _device;
};
}

View File

@ -0,0 +1 @@
#include "keyboard-dx.hpp"

View File

@ -0,0 +1,53 @@
#ifndef XNA_PLATFORM_KEYBOARD_DX_HPP
#define XNA_PLATFORM_KEYBOARD_DX_HPP
#include "../input/keyboard.hpp"
#include <Keyboard.h>
namespace xna {
struct KeyboardState : public IKeyboardState {
public:
KeyboardState() = default;
KeyboardState(DirectX::Keyboard::State const& state) :_state(state) {}
virtual bool IsKeyDown(Keys key) const override {
const auto k = static_cast<DirectX::Keyboard::Keys>(key);
return _state.IsKeyDown(k);
}
virtual bool IsKeyUp(Keys key) const override {
const auto k = static_cast<DirectX::Keyboard::Keys>(key);
return _state.IsKeyUp(k);
}
private:
DirectX::Keyboard::State _state{};
};
class Keyboard : public IKeyboard {
public:
friend class IKeyboard;
friend class GameWindow;
Keyboard() = default;
private:
inline static sptr<DirectX::Keyboard> initializeKeyboard() {
return New<DirectX::Keyboard>();
}
inline static sptr<DirectX::Keyboard> _dxKeyboard = initializeKeyboard();
};
inline KeyboardState IKeyboard::GetState() {
const auto state = Keyboard::_dxKeyboard->GetState();
return KeyboardState(state);
}
inline bool IKeyboard::IsConnected() {
return Keyboard::_dxKeyboard->IsConnected();
}
}
#endif

View File

@ -23,7 +23,7 @@ namespace xna {
DepthStencilState* depthStencil = nullptr,
RasterizerState* rasterizerState = nullptr,
//Effect
Matrix const& transformMatrix = _identity
Matrix const& transformMatrix = Matrix::Identity()
) override;
virtual void End() override;

View File

@ -1,4 +1,5 @@
#include "window-dx.hpp"
#include "keyboard-dx.hpp"
namespace xna {
GameWindow::GameWindow() {
@ -136,6 +137,20 @@ namespace xna {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_ACTIVATE:
case WM_ACTIVATEAPP:
Keyboard::_dxKeyboard->ProcessMessage(msg, wParam, lParam);
break;
case WM_SYSKEYDOWN:
if (!(wParam == VK_RETURN && (lParam & 0x60000000) == 0x20000000)) {
Keyboard::_dxKeyboard->ProcessMessage(msg, wParam, lParam);
}
break;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
Keyboard::_dxKeyboard->ProcessMessage(msg, wParam, lParam);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);

View File

@ -15,24 +15,60 @@ using namespace xna;
class Game1 : public Game {
public:
Game1() {
graphics = New<GraphicsDeviceManager>(this);
auto _game = reinterpret_cast<Game*>(this);
graphics = New<GraphicsDeviceManager>(_game);
}
void Initialize() override {
graphics->Initialize();
Game::Initialize();
}
void LoadContent() override {
spriteBatch = New<SpriteBatch>(*_graphicsDevice);
XnaErrorCode err;
texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err);
spriteBatch = New<SpriteBatch>(*_graphicsDevice);
}
Game::LoadContent();
}
virtual void Update(GameTime const& gameTime) override {
void Update(GameTime const& gameTime) override {
const auto state = Keyboard::GetState();
if (state.IsKeyDown(Keys::Right)) {
position.X += 1 * gameTime.ElapsedGameTime.TotalMilliseconds();
}
if (state.IsKeyDown(Keys::Left)) {
position.X -= 1 * gameTime.ElapsedGameTime.TotalMilliseconds();
}
if (state.IsKeyDown(Keys::Up)) {
position.Y -= 1 * gameTime.ElapsedGameTime.TotalMilliseconds();
}
if (state.IsKeyDown(Keys::Down)) {
position.Y += 1 * gameTime.ElapsedGameTime.TotalMilliseconds();
}
/*if (position.X > 1280 || position.X < 0)
vel *= -1;
if (gameTime.ElapsedGameTime.TotalMilliseconds() > 1) {
}
position.X += 0.05 * (gameTime.ElapsedGameTime.TotalMilliseconds() * vel);*/
//position.X += 2 * vel;
Game::Update(gameTime);
}
virtual void Draw(GameTime const& gameTime) override {
_graphicsDevice->Clear();
void Draw(GameTime const& gameTime) override {
_graphicsDevice->Clear(Colors::CornflowerBlue);
spriteBatch->Begin();
spriteBatch->Draw(*texture, Vector2(20, 20), nullptr, Colors::White, 0, {0,0}, 0.5F, SpriteEffects::None, 0);
spriteBatch->Draw(*texture, position, nullptr, Colors::White, 0, { 0,0 }, 0.5F, SpriteEffects::None, 0);
spriteBatch->End();
Game::Draw(gameTime);
@ -42,6 +78,8 @@ private:
PGraphicsDeviceManager graphics = nullptr;
PSpriteBatch spriteBatch = nullptr;
PTexture2D texture = nullptr;
Vector2 position{};
float vel = 1;
};
int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {

View File

@ -15,5 +15,6 @@
#include "platform/texture-dx.hpp"
#include "platform/spritebatch-dx.hpp"
#include "common/color.hpp"
#include "platform/keyboard-dx.hpp"
// TODO: Reference additional headers your program requires here.