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

Implementa Soun e SoundEffect

This commit is contained in:
Danilo 2024-04-20 13:39:19 -03:00
parent e73633b88f
commit 40e0c00e79
15 changed files with 342 additions and 48 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" "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)

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -7,8 +7,12 @@ namespace xna {
//Audio
class SoundEffect;
using PSoundEffect = std::shared_ptr<SoundEffect>;
struct SoundEffectInstance;
using PSoundEffectInstance = std::shared_ptr<SoundEffectInstance>;
class AudioEngine;
using PAudioEngine = std::shared_ptr<AudioEngine>;
struct WaveFormat;
using PWaveFormat = std::shared_ptr<WaveFormat>;
//CShap
struct TimeSpan;

View File

@ -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 <Audio.h>
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<DirectX::AudioEngine>(
#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<DirectX::AUDIO_ENGINE_REVERB>(value);
_dxAudioEngine->SetMasterVolume(reverb);
}
public:
//static inline uptr<DirectX::AudioEngine> _dxAudioEngine = uNew<DirectX::AudioEngine>();
sptr<DirectX::AudioEngine> _dxAudioEngine = nullptr;
};
}

View File

@ -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>();
_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<AudioEngine>();
Keyboard::_dxKeyboard = uNew<DirectX::Keyboard>();
Mouse::_dxMouse = uNew<DirectX::Mouse>();
}
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{};

View File

@ -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{};

View File

@ -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<DxSoundEffect>(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();
}
}

View File

@ -0,0 +1,37 @@
#ifndef XNA_PLATFORM_SOUNDEFFECT_DX_HPP
#define XNA_PLATFORM_SOUNDEFFECT_DX_HPP
#include "../audio/soundeffect.hpp"
#include <Audio.h>
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<DirectX::SoundEffectInstance> _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<DirectX::SoundEffect> _dxSoundEffect = nullptr;
};
}
#endif

View File

@ -134,9 +134,6 @@ namespace xna {
LRESULT GameWindow::WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (!Keyboard::_dxKeyboard) Keyboard::_dxKeyboard = uNew<DirectX::Keyboard>();
if (!Mouse::_dxMouse) Mouse::_dxMouse = uNew<DirectX::Mouse>();
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);

View File

@ -1,17 +0,0 @@
#ifndef XNA_SOUND_SOUNDENGINE_HPP
#define XNA_SOUND_SOUNDENGINE_HPP
#include "../default.hpp"
//#include <Audio.h>
namespace xna {
class IAudioEngine {
public:
virtual ~IAudioEngine(){}
public:
//uptr<DirectX::AudioEngine> _dxAudioEngine = nullptr;
};
}
#endif

View File

@ -1,10 +0,0 @@
#ifndef XNA_SOUND_SOUNDEFFECT_HPP
#define XNA_SOUND_SOUNDEFFECT_HPP
#include "../default.hpp"
namespace xna {
}
#endif

View File

@ -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;

View File

@ -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.