diff --git a/include/Sound/Sound.h b/include/Sound/Sound.h new file mode 100644 index 0000000..328925a --- /dev/null +++ b/include/Sound/Sound.h @@ -0,0 +1,36 @@ +#ifndef SOUND_H__ +#define SOUND_H__ + +#include "AL/al.h" +#include "AL/alc.h" + +#include "Core/ResourceManager.h" + +namespace dd +{ + +class Sound : public Resource +{ + friend class ResourceManager; +public: + ALuint Buffer() { return m_Buffer; }; +private: + Sound(std::string path); + + //File-info + char m_Type[4]; + unsigned long m_Size, m_ChunkSize; + short m_FormatType, m_Channels; + unsigned long m_SampleRate, m_AvgBytesPerSec; + short m_BytesPerSample, m_BitsPerSample; + unsigned long m_DataSize; + std::map m_BufferCache; + ALuint LoadFile(std::string path); + + ALuint m_Buffer; +}; + + +} + +#endif diff --git a/include/Sound/SoundSystem.h b/include/Sound/SoundSystem.h index d3d247d..254b989 100644 --- a/include/Sound/SoundSystem.h +++ b/include/Sound/SoundSystem.h @@ -1,21 +1,3 @@ -/* - This file is part of Daydream Engine. - Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung - - Daydream Engine is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Daydream Engine is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with Daydream Engine. If not, see . -*/ - #ifndef DAYDREAM_SOUND_H #define DAYDREAM_SOUND_H @@ -24,7 +6,7 @@ #include "AL/alc.h" #include "Core/System.h" #include "Core/World.h" -#include "Core/EKeyDown.h" +#include "Sound.h" #include "Sound/EPlaySFX.h" #include "Physics/EContact.h" #include "Core/EventBroker.h" @@ -32,6 +14,7 @@ #include "Game/CBrick.h" #include "Game/CPad.h" #include "Sound/CCollisionSound.h" +#include "Core/ResourceManager.h" namespace dd @@ -58,28 +41,15 @@ public: private: //Events - dd::EventRelay m_EKeyDown; dd::EventRelay m_EPlaySFX; dd::EventRelay m_EContact; - - bool OnKeyDown(const dd::Events::KeyDown &event); bool OnPlaySFX(const dd::Events::PlaySFX &event); bool OnContact(const dd::Events::Contact &event); ALuint LoadFile(std::string fileName); ALuint CreateSource(); - //File-info - char m_Type[4]; - unsigned long m_Size, m_ChunkSize; - short m_FormatType, m_Channels; - unsigned long m_SampleRate, m_AvgBytesPerSec; - short m_BytesPerSample, m_BitsPerSample; - unsigned long m_DataSize; - - //std::map m_Sources; - std::map m_BufferCache; //Temp diff --git a/src/game/Sound/Sound.cpp b/src/game/Sound/Sound.cpp new file mode 100644 index 0000000..f38ddf7 --- /dev/null +++ b/src/game/Sound/Sound.cpp @@ -0,0 +1,90 @@ +#include "Sound/Sound.h" + +dd::Sound::Sound(std::string path) +{ + m_Buffer = LoadFile(path); +} + +ALuint dd::Sound::LoadFile(std::string path) +{ + + LOG_ERROR("Detta meddelande ska du bara fĺ 1 gĺng! -----------------------------"); + if (m_BufferCache.find(path) != m_BufferCache.end()) { + return m_BufferCache[path]; + } + + //Open file + FILE *fp = fopen(path.c_str(), "rb"); + if (!fp) { + LOG_ERROR("Failed to open file %s, no such file exists", path.c_str()); + return 0; + } + + //CHECK FOR VALID WAVE-FILE + fread(m_Type, sizeof(char), 4, fp); + if(m_Type[0] != 'R' || m_Type[1] != 'I' || m_Type[2] != 'F' || m_Type[3] != 'F') { + LOG_ERROR("ERROR: No RIFF in WAVE-file"); + return 0; + } + + fread(&m_Size, sizeof(unsigned long), 1, fp); + fread(m_Type, sizeof(char), 4, fp); + if (m_Type[0] != 'W' || m_Type[1] != 'A' || m_Type[2] != 'V' || m_Type[3]!= 'E') { + LOG_ERROR("ERROR: Not WAVE-file"); + return 0; + } + + fread(m_Type, sizeof(char), 4, fp); + if (m_Type[0] != 'f' || m_Type[1] != 'm' || m_Type[2] != 't' || m_Type[3] != ' ') { + LOG_ERROR("ERROR: No fmt in WAVE-file"); + return 0; + } + + //READ THE DATA FROM WAVE-FILE + fread(&m_ChunkSize, sizeof(unsigned long), 1, fp); + fread(&m_FormatType, sizeof(short), 1, fp); + fread(&m_Channels, sizeof(short), 1, fp); + fread(&m_SampleRate, sizeof(unsigned long), 1, fp); + fread(&m_AvgBytesPerSec, sizeof(unsigned long), 1, fp); + fread(&m_BytesPerSample, sizeof(short), 1, fp); + fread(&m_BitsPerSample, sizeof(short), 1, fp); + + fread(m_Type, sizeof(char), 4, fp); + if (m_Type[0] !='d' || m_Type[1] != 'a' || m_Type[2] != 't' || m_Type[3] != 'a') { + LOG_ERROR("ERROR: WAVE-file Missing data"); + return 0; + } + + fread(&m_DataSize, sizeof(unsigned long), 1, fp); + + unsigned char* buf = new unsigned char[m_DataSize]; + fread(buf, sizeof(unsigned char), m_DataSize, fp); + fclose(fp); + + // Create buffer + ALuint format = 0; + if (m_BitsPerSample == 8) { + if (m_Channels == 1) { + format = AL_FORMAT_MONO8; + } + else if (m_Channels == 2) { + format = AL_FORMAT_STEREO8; + } + } + if (m_BitsPerSample == 16) { + if (m_Channels == 1) { + format = AL_FORMAT_MONO16; + } + else if (m_Channels == 2) { + format = AL_FORMAT_STEREO16; + } + } + + ALuint buffer; + alGenBuffers(1, &buffer); + alBufferData(buffer, format, buf, m_DataSize, m_SampleRate); + delete[] buf; + + m_BufferCache[path] = buffer; + return buffer; +} \ No newline at end of file diff --git a/src/game/Sound/SoundSystem.cpp b/src/game/Sound/SoundSystem.cpp index 3a7903a..9ba277f 100644 --- a/src/game/Sound/SoundSystem.cpp +++ b/src/game/Sound/SoundSystem.cpp @@ -1,22 +1,3 @@ -/* - This file is part of Daydream Engine. - Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung - - Daydream Engine is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Daydream Engine is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with Daydream Engine. If not, see . -*/ - - #include "PrecompiledHeader.h" #include "Sound/SoundSystem.h" @@ -44,7 +25,6 @@ void dd::Systems::SoundSystem::Initialize() //Subscribe to events EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact); - EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &SoundSystem::OnKeyDown); EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySFX); } @@ -59,79 +39,31 @@ void dd::Systems::SoundSystem::Update(double dt) bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event) { m_Source = CreateSource(); - ALuint buffer = LoadFile(event.path); + //ALuint buffer = LoadFile(event.path); //change to resourcemanager load + Sound *sound = ResourceManager::Load(event.path); + ALuint buffer = sound->Buffer(); alSourcei(m_Source, AL_BUFFER, buffer); alSourcePlay(m_Source); } -bool dd::Systems::SoundSystem::OnKeyDown(const dd::Events::KeyDown &event) -{ - // #define GLFW_KEY_S 83 - /*if (event.KeyCode == 83) { - - }*/ - if (event.KeyCode == 83) { - - } -} - bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event) { + //Check which entity has the collisionSound component. auto collisionSound = m_World->GetComponent(event.Entity1); if (collisionSound == NULL) { collisionSound = m_World->GetComponent(event.Entity2); if (collisionSound == NULL) { + //None of the entities had a collisionSound component. return false; } } + //Send play-sound event dd::Events::PlaySFX e; e.path = collisionSound->filePath; EventBroker->Publish(e); return true; - - - /*int localBallEntID = 0; - //Make sure a ball is colliding - auto ball = m_World->GetComponent(event.Entity1); - if (ball == NULL) { - ball = m_World->GetComponent(event.Entity2); - if (ball == NULL) { - return false; - } - else { - localBallEntID = 2; - } - } - else { - localBallEntID = 1; - } - - //Determine which event.Entity is not t - EntityID collObject; - if (localBallEntID == 1) { - collObject = event.Entity2; - } - else if (localBallEntID == 2) { - collObject = event.Entity1; - } - - auto collisionBrick = m_World->GetComponent(collObject); - if (collisionBrick != NULL) { - dd::Events::PlaySFX e; - e.path = "Sounds/Brick/brickBreak.wav"; - EventBroker->Publish(e); - return true; - } - - auto collisionPad = m_World->GetComponent(collObject); - if (collisionPad != NULL) { - dd::Events::PlaySFX e; - e.path = "Sounds/Metal/impact.wav"; - EventBroker->Publish(e); - return true; - }*/ } ALuint dd::Systems::SoundSystem::CreateSource() @@ -144,82 +76,5 @@ ALuint dd::Systems::SoundSystem::CreateSource() ALuint dd::Systems::SoundSystem::LoadFile(std::string path) { - if (m_BufferCache.find(path) != m_BufferCache.end()) { - return m_BufferCache[path]; - } - - //Open file - FILE *fp = fopen(path.c_str(), "rb"); - if (!fp) { - LOG_ERROR("Failed to open file %s, no such file exists", path.c_str()); - return 0; - } - - //CHECK FOR VALID WAVE-FILE - fread(m_Type, sizeof(char), 4, fp); - if(m_Type[0] != 'R' || m_Type[1] != 'I' || m_Type[2] != 'F' || m_Type[3] != 'F') { - LOG_ERROR("ERROR: No RIFF in WAVE-file"); - return 0; - } - - fread(&m_Size, sizeof(unsigned long), 1, fp); - fread(m_Type, sizeof(char), 4, fp); - if (m_Type[0] != 'W' || m_Type[1] != 'A' || m_Type[2] != 'V' || m_Type[3]!= 'E') { - LOG_ERROR("ERROR: Not WAVE-file"); - return 0; - } - - fread(m_Type, sizeof(char), 4, fp); - if (m_Type[0] != 'f' || m_Type[1] != 'm' || m_Type[2] != 't' || m_Type[3] != ' ') { - LOG_ERROR("ERROR: No fmt in WAVE-file"); - return 0; - } - - //READ THE DATA FROM WAVE-FILE - fread(&m_ChunkSize, sizeof(unsigned long), 1, fp); - fread(&m_FormatType, sizeof(short), 1, fp); - fread(&m_Channels, sizeof(short), 1, fp); - fread(&m_SampleRate, sizeof(unsigned long), 1, fp); - fread(&m_AvgBytesPerSec, sizeof(unsigned long), 1, fp); - fread(&m_BytesPerSample, sizeof(short), 1, fp); - fread(&m_BitsPerSample, sizeof(short), 1, fp); - - fread(m_Type, sizeof(char), 4, fp); - if (m_Type[0] !='d' || m_Type[1] != 'a' || m_Type[2] != 't' || m_Type[3] != 'a') { - LOG_ERROR("ERROR: WAVE-file Missing data"); - return 0; - } - - fread(&m_DataSize, sizeof(unsigned long), 1, fp); - - unsigned char* buf = new unsigned char[m_DataSize]; - fread(buf, sizeof(unsigned char), m_DataSize, fp); - fclose(fp); - - // Create buffer - ALuint format = 0; - if (m_BitsPerSample == 8) { - if (m_Channels == 1) { - format = AL_FORMAT_MONO8; - } - else if (m_Channels == 2) { - format = AL_FORMAT_STEREO8; - } - } - if (m_BitsPerSample == 16) { - if (m_Channels == 1) { - format = AL_FORMAT_MONO16; - } - else if (m_Channels == 2) { - format = AL_FORMAT_STEREO16; - } - } - - ALuint buffer; - alGenBuffers(1, &buffer); - alBufferData(buffer, format, buf, m_DataSize, m_SampleRate); - delete[] buf; - - m_BufferCache[path] = buffer; - return buffer; + /*Moved code*/ }