diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 9becb06..ad2eb1f 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" "platform/keyboard-dx.cpp" "platform/mouse-dx.cpp" "platform/gamepad-dx.cpp" "common/vectors.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" "platform/mouse-dx.cpp" "platform/gamepad-dx.cpp" "common/vectors.cpp" "platform/soundeffect-dx.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET xna PROPERTY CXX_STANDARD 20) diff --git a/framework/audio/audioengine.hpp b/framework/audio/audioengine.hpp new file mode 100644 index 0000000..5255234 --- /dev/null +++ b/framework/audio/audioengine.hpp @@ -0,0 +1,21 @@ +#ifndef XNA_SOUND_SOUNDENGINE_HPP +#define XNA_SOUND_SOUNDENGINE_HPP + +#include "../default.hpp" + +namespace xna { + class IAudioEngine { + public: + virtual ~IAudioEngine(){} + virtual bool Reset() = 0; + virtual bool Resume() = 0; + virtual bool Suspend() = 0; + virtual bool Update() = 0; + virtual void DefaultSampleRate(int value) = 0; + virtual void MasterVolume(float value) = 0; + virtual void MasteringLimit(int limit, int loudness) = 0; + virtual void Reverb(AudioReverb value) = 0; + }; +} + +#endif \ No newline at end of file diff --git a/framework/audio/soundeffect.hpp b/framework/audio/soundeffect.hpp new file mode 100644 index 0000000..1cd3533 --- /dev/null +++ b/framework/audio/soundeffect.hpp @@ -0,0 +1,28 @@ +#ifndef XNA_SOUND_SOUNDEFFECT_HPP +#define XNA_SOUND_SOUNDEFFECT_HPP + +#include "../default.hpp" + +namespace xna { + struct ISoundEffectInstance { + public: + virtual void Play(bool loop = false) = 0; + virtual void Stop(bool immediate = true) = 0; + virtual void Pause() = 0; + virtual void Resume() = 0; + virtual void Volume(float volume) = 0; + virtual void Pitch(float pitch) = 0; + virtual void Pan(float pan) = 0; + virtual bool IsLooped() = 0; + }; + + class ISoundEffect { + public: + virtual ~ISoundEffect(){} + virtual void Play() = 0; + virtual void Play(float volume, float pitch, float pan) = 0; + virtual SoundEffectInstance CreateInstance() = 0; + }; +} + +#endif \ No newline at end of file diff --git a/framework/enums.hpp b/framework/enums.hpp index bde7867..7724e16 100644 --- a/framework/enums.hpp +++ b/framework/enums.hpp @@ -2,6 +2,41 @@ #define XNA_ENUMS_HPP namespace xna { + enum class AudioReverb { + Off, + Default, + Generic, + Forest, + PaddedCell, + Room, + Bathroom, + LivingRoom, + StoneRoom, + Auditorium, + ConcertHall, + Cave, + Arena, + Hangar, + CarpetedHallway, + Hallway, + StoneCorridor, + Alley, + City, + Mountains, + Quarry, + Plain, + ParkingLot, + SewerPipe, + Underwater, + SmallRoom, + MediumRoom, + LargeRoom, + MediumHall, + LargeHall, + Plate, + Max + }; + enum class Blend { Zero, One, diff --git a/framework/forward.hpp b/framework/forward.hpp index 53b334e..56276f0 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -7,8 +7,12 @@ namespace xna { //Audio class SoundEffect; using PSoundEffect = std::shared_ptr; + struct SoundEffectInstance; + using PSoundEffectInstance = std::shared_ptr; class AudioEngine; using PAudioEngine = std::shared_ptr; + struct WaveFormat; + using PWaveFormat = std::shared_ptr; //CShap struct TimeSpan; diff --git a/framework/platform/audioengine-dx.hpp b/framework/platform/audioengine-dx.hpp index a88ef46..8a3499d 100644 --- a/framework/platform/audioengine-dx.hpp +++ b/framework/platform/audioengine-dx.hpp @@ -1,18 +1,95 @@ #ifndef XNA_PLATFORM_SOUNDENGINE_DX_HPP #define XNA_PLATFORM_SOUNDENGINE_DX_HPP -#include "../sound/audioengine.hpp" - +#include "../audio/audioengine.hpp" +#include namespace xna { + struct WaveFormat { + Ushort FormatTag{ 0 }; + Ushort Channels{ 0 }; + Ulong SamplesPerSecond{ 0 }; + Ulong Buffer{ 0 }; + Ushort BlockAlign{ 0 }; + Ushort BitsPerSamples{ 0 }; + Ushort ByteSize{ 0 }; + }; + class AudioEngine : public IAudioEngine { public: AudioEngine() { - + _dxAudioEngine = New( +#ifdef _DEBUG + DirectX::AudioEngine_Debug +#endif + ); + } + + virtual ~AudioEngine() { + if (_dxAudioEngine) { + _dxAudioEngine->Suspend(); + } + } + + virtual bool Reset() override { + if (!_dxAudioEngine) + return false; + + return _dxAudioEngine->Reset(); + } + + virtual bool Update() override { + if (!_dxAudioEngine) + return false; + + return _dxAudioEngine->Update(); + } + + virtual bool Resume() override { + if (!_dxAudioEngine) + return false; + + _dxAudioEngine->Resume(); + + return true; + } + + virtual bool Suspend() override { + if (!_dxAudioEngine) + return false; + + _dxAudioEngine->Resume(); + + return true; + } + + virtual void DefaultSampleRate(int value) override { + if (!_dxAudioEngine) return; + + _dxAudioEngine->SetDefaultSampleRate(value); + } + + virtual void MasteringLimit(int limit, int loudness) override { + if (!_dxAudioEngine) return; + + _dxAudioEngine->SetMasteringLimit(limit, loudness); + } + + virtual void MasterVolume(float value) override { + if (!_dxAudioEngine) return; + + _dxAudioEngine->SetMasterVolume(value); + } + + virtual void Reverb(AudioReverb value) override { + if (!_dxAudioEngine) return; + + const auto reverb = static_cast(value); + _dxAudioEngine->SetMasterVolume(reverb); } public: - //static inline uptr _dxAudioEngine = uNew(); + sptr _dxAudioEngine = nullptr; }; } diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index 0edcd6a..8adbcd1 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -4,27 +4,53 @@ #include "Windows.h" #include "../game/time.hpp" #include "gdevicemanager-dx.hpp" +#include "keyboard-dx.hpp" +#include "mouse-dx.hpp" +#include "audioengine-dx.hpp" namespace xna { Game::Game() { _gameWindow = New(); _gameWindow->Color(255, 155, 55); - _gameWindow->Title("Teste de título"); + _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(); + Keyboard::_dxKeyboard = uNew(); + Mouse::_dxMouse = uNew(); } - int Game::Run() { + static void intializeAudioEngine() { + + } + + int Game::Run() { Initialize(); if (_graphicsDevice == nullptr) { - MessageBox(nullptr, "O dispositivo gráfico não foi inicializado corretamente", "Xna Game Engine", MB_OK); + MessageBox(nullptr, "O dispositivo gráfico não foi inicializado corretamente", "XN65", MB_OK); return EXIT_FAILURE; } return startLoop(); } + + void Game::Initialize() { + LoadContent(); + } + + void Game::Update(GameTime const& gameTime) { + _audioEngine->Update(); + } int Game::startLoop() { MSG msg{}; diff --git a/framework/platform/game-dx.hpp b/framework/platform/game-dx.hpp index 84cc239..c50d787 100644 --- a/framework/platform/game-dx.hpp +++ b/framework/platform/game-dx.hpp @@ -1,6 +1,7 @@ #ifndef XNA_PLATFORM_GAME_DX_HPP #define XNA_PLATFORM_GAME_DX_HPP +#include "../default.hpp" #include "../game/game.hpp" #include "clock-dx.hpp" #include "dxgi.h" @@ -27,15 +28,19 @@ namespace xna { protected: virtual void Draw(GameTime const& gameTime) override{} - virtual void Initialize() override { LoadContent(); } + + virtual void Initialize() override; + virtual void LoadContent() override{} - virtual void Update(GameTime const& gameTime) override{} + + virtual void Update(GameTime const& gameTime) override; public: PGraphicsDevice _graphicsDevice{ nullptr }; protected: PGameWindow _gameWindow{ nullptr }; + PAudioEngine _audioEngine = nullptr; GameClock _clock{}; GameTime _currentGameTime{}; diff --git a/framework/platform/soundeffect-dx.cpp b/framework/platform/soundeffect-dx.cpp new file mode 100644 index 0000000..8189480 --- /dev/null +++ b/framework/platform/soundeffect-dx.cpp @@ -0,0 +1,94 @@ +#include "soundeffect-dx.hpp" +#include "audioengine-dx.hpp" + +using DxSoundEffect = DirectX::SoundEffect; + +namespace xna { + SoundEffect::SoundEffect(AudioEngine& audioEngine, String const& fileName) { + if (!audioEngine._dxAudioEngine) + return; + + const auto file = XnaHToWString(fileName); + _dxSoundEffect = New(audioEngine._dxAudioEngine.get(), file.c_str()); + } + + void SoundEffect::Play() { + if (!_dxSoundEffect) + return; + + _dxSoundEffect->Play(); + } + + void SoundEffect::Play(float volume, float pitch, float pan) { + if (!_dxSoundEffect) + return; + + _dxSoundEffect->Play(volume, pitch, pan); + } + + SoundEffectInstance SoundEffect::CreateInstance() { + if (!_dxSoundEffect) + return SoundEffectInstance(); + + SoundEffectInstance i{}; + i._dxInstance = _dxSoundEffect->CreateInstance(); + + return i; + } + + void SoundEffectInstance::Play(bool loop) { + if (!_dxInstance) + return; + + _dxInstance->Play(loop); + } + + void SoundEffectInstance::Stop(bool immediate) { + if (!_dxInstance) + return; + + _dxInstance->Stop(immediate); + } + + void SoundEffectInstance::Pause() { + if (!_dxInstance) + return; + + _dxInstance->Pause(); + } + + void SoundEffectInstance::Resume() { + if (!_dxInstance) + return; + + _dxInstance->Resume(); + } + + void SoundEffectInstance::Volume(float volume) { + if (!_dxInstance) + return; + + _dxInstance->SetVolume(volume); + } + + void SoundEffectInstance::Pitch(float pitch) { + if (!_dxInstance) + return; + + _dxInstance->SetPitch(pitch); + } + + void SoundEffectInstance::Pan(float pan) { + if (!_dxInstance) + return; + + _dxInstance->SetPan(pan); + } + + bool SoundEffectInstance::IsLooped() { + if (!_dxInstance) + return false; + + return _dxInstance->IsLooped(); + } +} \ No newline at end of file diff --git a/framework/platform/soundeffect-dx.hpp b/framework/platform/soundeffect-dx.hpp new file mode 100644 index 0000000..a202ffb --- /dev/null +++ b/framework/platform/soundeffect-dx.hpp @@ -0,0 +1,37 @@ +#ifndef XNA_PLATFORM_SOUNDEFFECT_DX_HPP +#define XNA_PLATFORM_SOUNDEFFECT_DX_HPP + +#include "../audio/soundeffect.hpp" +#include + +namespace xna { + struct SoundEffectInstance : public ISoundEffectInstance { + public: + SoundEffectInstance() = default; + + virtual void Play(bool loop = false) override; + virtual void Stop(bool immediate = true) override; + virtual void Pause() override; + virtual void Resume() override; + virtual void Volume(float volume) override; + virtual void Pitch(float pitch) override; + virtual void Pan(float pan) override; + virtual bool IsLooped() override; + + public: + uptr _dxInstance = nullptr; + }; + + class SoundEffect : public ISoundEffect { + public: + SoundEffect(AudioEngine& audioEngine, String const& fileName); + virtual void Play() override; + virtual void Play(float volume, float pitch, float pan) override; + virtual SoundEffectInstance CreateInstance() override; + + public: + sptr _dxSoundEffect = nullptr; + }; +} + +#endif \ No newline at end of file diff --git a/framework/platform/window-dx.cpp b/framework/platform/window-dx.cpp index 67fa7d3..5352cda 100644 --- a/framework/platform/window-dx.cpp +++ b/framework/platform/window-dx.cpp @@ -134,9 +134,6 @@ namespace xna { LRESULT GameWindow::WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { - if (!Keyboard::_dxKeyboard) Keyboard::_dxKeyboard = uNew(); - if (!Mouse::_dxMouse) Mouse::_dxMouse = uNew(); - switch (msg) { case WM_DESTROY: PostQuitMessage(0); diff --git a/framework/sound/audioengine.hpp b/framework/sound/audioengine.hpp deleted file mode 100644 index 269cd0c..0000000 --- a/framework/sound/audioengine.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef XNA_SOUND_SOUNDENGINE_HPP -#define XNA_SOUND_SOUNDENGINE_HPP - -#include "../default.hpp" -//#include - -namespace xna { - class IAudioEngine { - public: - virtual ~IAudioEngine(){} - - public: - //uptr _dxAudioEngine = nullptr; - }; -} - -#endif \ No newline at end of file diff --git a/framework/sound/soundeffect.hpp b/framework/sound/soundeffect.hpp deleted file mode 100644 index b1cf0d0..0000000 --- a/framework/sound/soundeffect.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef XNA_SOUND_SOUNDEFFECT_HPP -#define XNA_SOUND_SOUNDEFFECT_HPP - -#include "../default.hpp" - -namespace xna { - -} - -#endif \ No newline at end of file diff --git a/framework/xna.cpp b/framework/xna.cpp index 150565d..ef871ce 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -23,7 +23,6 @@ public: graphics->Initialize(); Game::Initialize(); - GamePad::Initialize(); } void LoadContent() override { @@ -32,16 +31,12 @@ public: XnaErrorCode err; texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); + auto audio = AudioEngine(); + Game::LoadContent(); } - void Update(GameTime const& gameTime) override { - auto state = GamePad::GetState(PlayerIndex::One); - - if (state.IsButtonDown(Buttons::DPadRight)) - position.X += 1.0F * gameTime.ElapsedGameTime.TotalMilliseconds(); - if (state.IsButtonDown(Buttons::DPadLeft)) - position.X -= 1.0F * gameTime.ElapsedGameTime.TotalMilliseconds(); + void Update(GameTime const& gameTime) override { Game::Update(gameTime); } @@ -68,6 +63,7 @@ private: }; int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) { + auto game = Game1(); const auto result = game.Run(); return result; diff --git a/framework/xna.h b/framework/xna.h index c89ba24..7158cad 100644 --- a/framework/xna.h +++ b/framework/xna.h @@ -18,5 +18,6 @@ #include "platform/keyboard-dx.hpp" #include "platform/mouse-dx.hpp" #include "platform/gamepad-dx.hpp" +#include "platform/audioengine-dx.hpp" // TODO: Reference additional headers your program requires here.