From 3274acf47879381d15c59d51da49a34519b8319a Mon Sep 17 00:00:00 2001 From: Danilo Date: Tue, 16 Apr 2024 16:13:36 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=A3o=20de=20Keyboard=20e=20m?= =?UTF-8?q?udan=C3=A7as=20em=20Game?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/CMakeLists.txt | 2 +- framework/enums.hpp | 168 +++++++++++++++++++++++ framework/forward.hpp | 4 + framework/game/clock.hpp | 1 + framework/game/game.hpp | 1 + framework/game/gdevicemanager.hpp | 1 + framework/graphics/device.hpp | 1 + framework/graphics/spritebatch.hpp | 4 +- framework/input/keyboard.hpp | 21 +++ framework/platform/clock-dx.hpp | 7 +- framework/platform/device-dx.cpp | 12 ++ framework/platform/device-dx.hpp | 3 +- framework/platform/dxheaders.hpp | 4 +- framework/platform/game-dx.cpp | 45 +++--- framework/platform/game-dx.hpp | 7 +- framework/platform/gdevicemanager-dx.cpp | 16 ++- framework/platform/gdevicemanager-dx.hpp | 8 +- framework/platform/keyboard-dx.cpp | 1 + framework/platform/keyboard-dx.hpp | 53 +++++++ framework/platform/spritebatch-dx.hpp | 2 +- framework/platform/window-dx.cpp | 15 ++ framework/xna.cpp | 52 ++++++- framework/xna.h | 1 + 23 files changed, 380 insertions(+), 49 deletions(-) create mode 100644 framework/input/keyboard.hpp create mode 100644 framework/platform/keyboard-dx.cpp create mode 100644 framework/platform/keyboard-dx.hpp diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 0c130c3..a738f63 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -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) diff --git a/framework/enums.hpp b/framework/enums.hpp index cb535da..866dc86 100644 --- a/framework/enums.hpp +++ b/framework/enums.hpp @@ -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, diff --git a/framework/forward.hpp b/framework/forward.hpp index 133284a..980256f 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -131,6 +131,10 @@ namespace xna { using pVertexShader = std::shared_ptr; struct Viewport; using PViewport = std::shared_ptr; + + //Input + struct KeyboardState; + using PKeyboardState = std::shared_ptr; } #endif \ No newline at end of file diff --git a/framework/game/clock.hpp b/framework/game/clock.hpp index c899e82..7804874 100644 --- a/framework/game/clock.hpp +++ b/framework/game/clock.hpp @@ -18,6 +18,7 @@ namespace xna { virtual void Stop() = 0; virtual TimeSpan ElapsedTime() = 0; virtual TimeSpan TotalTime() = 0; + virtual bool IsActive() = 0; }; } diff --git a/framework/game/game.hpp b/framework/game/game.hpp index c05451d..0aa121a 100644 --- a/framework/game/game.hpp +++ b/framework/game/game.hpp @@ -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; }; } diff --git a/framework/game/gdevicemanager.hpp b/framework/game/gdevicemanager.hpp index d5e671b..bbbc540 100644 --- a/framework/game/gdevicemanager.hpp +++ b/framework/game/gdevicemanager.hpp @@ -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; diff --git a/framework/graphics/device.hpp b/framework/graphics/device.hpp index 69fe8e1..0ab1229 100644 --- a/framework/graphics/device.hpp +++ b/framework/graphics/device.hpp @@ -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; diff --git a/framework/graphics/spritebatch.hpp b/framework/graphics/spritebatch.hpp index f47d4e3..6299aea 100644 --- a/framework/graphics/spritebatch.hpp +++ b/framework/graphics/spritebatch.hpp @@ -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; diff --git a/framework/input/keyboard.hpp b/framework/input/keyboard.hpp new file mode 100644 index 0000000..30dc205 --- /dev/null +++ b/framework/input/keyboard.hpp @@ -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 \ No newline at end of file diff --git a/framework/platform/clock-dx.hpp b/framework/platform/clock-dx.hpp index e8addd1..9f00b4f 100644 --- a/framework/platform/clock-dx.hpp +++ b/framework/platform/clock-dx.hpp @@ -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 }; }; } diff --git a/framework/platform/device-dx.cpp b/framework/platform/device-dx.cpp index a2d93d5..11e40fb 100644 --- a/framework/platform/device-dx.cpp +++ b/framework/platform/device-dx.cpp @@ -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); + } } \ No newline at end of file diff --git a/framework/platform/device-dx.hpp b/framework/platform/device-dx.hpp index e0bf2ad..83567cf 100644 --- a/framework/platform/device-dx.hpp +++ b/framework/platform/device-dx.hpp @@ -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(); }; diff --git a/framework/platform/dxheaders.hpp b/framework/platform/dxheaders.hpp index 02ce37a..27c5d19 100644 --- a/framework/platform/dxheaders.hpp +++ b/framework/platform/dxheaders.hpp @@ -1,4 +1,6 @@ #include "dxgi.h" #include "d3d11.h" #include -#include \ No newline at end of file +#include +#define NOMINMAX +#include \ No newline at end of file diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index fe72d4b..0edcd6a 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -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(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(); } } diff --git a/framework/platform/game-dx.hpp b/framework/platform/game-dx.hpp index 0e5c475..84cc239 100644 --- a/framework/platform/game-dx.hpp +++ b/framework/platform/game-dx.hpp @@ -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(); }; } diff --git a/framework/platform/gdevicemanager-dx.cpp b/framework/platform/gdevicemanager-dx.cpp index 9fa3019..83ba5fe 100644 --- a/framework/platform/gdevicemanager-dx.cpp +++ b/framework/platform/gdevicemanager-dx.cpp @@ -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(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); diff --git a/framework/platform/gdevicemanager-dx.hpp b/framework/platform/gdevicemanager-dx.hpp index 8744bb1..d4c4227 100644 --- a/framework/platform/gdevicemanager-dx.hpp +++ b/framework/platform/gdevicemanager-dx.hpp @@ -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; - }; } diff --git a/framework/platform/keyboard-dx.cpp b/framework/platform/keyboard-dx.cpp new file mode 100644 index 0000000..c3931de --- /dev/null +++ b/framework/platform/keyboard-dx.cpp @@ -0,0 +1 @@ +#include "keyboard-dx.hpp" \ No newline at end of file diff --git a/framework/platform/keyboard-dx.hpp b/framework/platform/keyboard-dx.hpp new file mode 100644 index 0000000..7849f7b --- /dev/null +++ b/framework/platform/keyboard-dx.hpp @@ -0,0 +1,53 @@ +#ifndef XNA_PLATFORM_KEYBOARD_DX_HPP +#define XNA_PLATFORM_KEYBOARD_DX_HPP + +#include "../input/keyboard.hpp" +#include + +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(key); + return _state.IsKeyDown(k); + } + + virtual bool IsKeyUp(Keys key) const override { + const auto k = static_cast(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 initializeKeyboard() { + return New(); + } + + inline static sptr _dxKeyboard = initializeKeyboard(); + }; + + inline KeyboardState IKeyboard::GetState() { + + const auto state = Keyboard::_dxKeyboard->GetState(); + return KeyboardState(state); + } + + inline bool IKeyboard::IsConnected() { + return Keyboard::_dxKeyboard->IsConnected(); + } +} + +#endif \ No newline at end of file diff --git a/framework/platform/spritebatch-dx.hpp b/framework/platform/spritebatch-dx.hpp index 9f80a57..984ed1c 100644 --- a/framework/platform/spritebatch-dx.hpp +++ b/framework/platform/spritebatch-dx.hpp @@ -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; diff --git a/framework/platform/window-dx.cpp b/framework/platform/window-dx.cpp index 815700d..92e1c2e 100644 --- a/framework/platform/window-dx.cpp +++ b/framework/platform/window-dx.cpp @@ -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); diff --git a/framework/xna.cpp b/framework/xna.cpp index 88ba3e6..ddccd00 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -15,24 +15,60 @@ using namespace xna; class Game1 : public Game { public: Game1() { - graphics = New(this); + auto _game = reinterpret_cast(this); + graphics = New(_game); + } + + void Initialize() override { + graphics->Initialize(); + + Game::Initialize(); + } + + void LoadContent() override { + spriteBatch = New(*_graphicsDevice); XnaErrorCode err; texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); - spriteBatch = New(*_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) { diff --git a/framework/xna.h b/framework/xna.h index 06cbcad..31abf07 100644 --- a/framework/xna.h +++ b/framework/xna.h @@ -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.