Resourcing working on sounds. Added class Sound resource.
This commit is contained in:
@@ -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<std::string, ALuint> m_BufferCache;
|
||||||
|
ALuint LoadFile(std::string path);
|
||||||
|
|
||||||
|
ALuint m_Buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef DAYDREAM_SOUND_H
|
#ifndef DAYDREAM_SOUND_H
|
||||||
#define DAYDREAM_SOUND_H
|
#define DAYDREAM_SOUND_H
|
||||||
|
|
||||||
@@ -24,7 +6,7 @@
|
|||||||
#include "AL/alc.h"
|
#include "AL/alc.h"
|
||||||
#include "Core/System.h"
|
#include "Core/System.h"
|
||||||
#include "Core/World.h"
|
#include "Core/World.h"
|
||||||
#include "Core/EKeyDown.h"
|
#include "Sound.h"
|
||||||
#include "Sound/EPlaySFX.h"
|
#include "Sound/EPlaySFX.h"
|
||||||
#include "Physics/EContact.h"
|
#include "Physics/EContact.h"
|
||||||
#include "Core/EventBroker.h"
|
#include "Core/EventBroker.h"
|
||||||
@@ -32,6 +14,7 @@
|
|||||||
#include "Game/CBrick.h"
|
#include "Game/CBrick.h"
|
||||||
#include "Game/CPad.h"
|
#include "Game/CPad.h"
|
||||||
#include "Sound/CCollisionSound.h"
|
#include "Sound/CCollisionSound.h"
|
||||||
|
#include "Core/ResourceManager.h"
|
||||||
|
|
||||||
|
|
||||||
namespace dd
|
namespace dd
|
||||||
@@ -58,28 +41,15 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
//Events
|
//Events
|
||||||
dd::EventRelay<SoundSystem, dd::Events::KeyDown> m_EKeyDown;
|
|
||||||
dd::EventRelay<SoundSystem, dd::Events::PlaySFX> m_EPlaySFX;
|
dd::EventRelay<SoundSystem, dd::Events::PlaySFX> m_EPlaySFX;
|
||||||
dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact;
|
dd::EventRelay<SoundSystem, dd::Events::Contact> m_EContact;
|
||||||
|
|
||||||
bool OnKeyDown(const dd::Events::KeyDown &event);
|
|
||||||
bool OnPlaySFX(const dd::Events::PlaySFX &event);
|
bool OnPlaySFX(const dd::Events::PlaySFX &event);
|
||||||
bool OnContact(const dd::Events::Contact &event);
|
bool OnContact(const dd::Events::Contact &event);
|
||||||
|
|
||||||
ALuint LoadFile(std::string fileName);
|
ALuint LoadFile(std::string fileName);
|
||||||
ALuint CreateSource();
|
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<Component*, ALuint> m_Sources;
|
//std::map<Component*, ALuint> m_Sources;
|
||||||
std::map<std::string, ALuint> m_BufferCache;
|
|
||||||
|
|
||||||
|
|
||||||
//Temp
|
//Temp
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#include "PrecompiledHeader.h"
|
#include "PrecompiledHeader.h"
|
||||||
#include "Sound/SoundSystem.h"
|
#include "Sound/SoundSystem.h"
|
||||||
|
|
||||||
@@ -44,7 +25,6 @@ void dd::Systems::SoundSystem::Initialize()
|
|||||||
|
|
||||||
//Subscribe to events
|
//Subscribe to events
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact);
|
EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact);
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &SoundSystem::OnKeyDown);
|
|
||||||
EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySFX);
|
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)
|
bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event)
|
||||||
{
|
{
|
||||||
m_Source = CreateSource();
|
m_Source = CreateSource();
|
||||||
ALuint buffer = LoadFile(event.path);
|
//ALuint buffer = LoadFile(event.path); //change to resourcemanager load
|
||||||
|
Sound *sound = ResourceManager::Load<Sound>(event.path);
|
||||||
|
ALuint buffer = sound->Buffer();
|
||||||
|
|
||||||
alSourcei(m_Source, AL_BUFFER, buffer);
|
alSourcei(m_Source, AL_BUFFER, buffer);
|
||||||
alSourcePlay(m_Source);
|
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)
|
bool dd::Systems::SoundSystem::OnContact(const dd::Events::Contact &event)
|
||||||
{
|
{
|
||||||
|
//Check which entity has the collisionSound component.
|
||||||
auto collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity1);
|
auto collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity1);
|
||||||
if (collisionSound == NULL) {
|
if (collisionSound == NULL) {
|
||||||
collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity2);
|
collisionSound = m_World->GetComponent<Components::CollisionSound>(event.Entity2);
|
||||||
if (collisionSound == NULL) {
|
if (collisionSound == NULL) {
|
||||||
|
//None of the entities had a collisionSound component.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Send play-sound event
|
||||||
dd::Events::PlaySFX e;
|
dd::Events::PlaySFX e;
|
||||||
e.path = collisionSound->filePath;
|
e.path = collisionSound->filePath;
|
||||||
EventBroker->Publish(e);
|
EventBroker->Publish(e);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
|
||||||
/*int localBallEntID = 0;
|
|
||||||
//Make sure a ball is colliding
|
|
||||||
auto ball = m_World->GetComponent<Components::Ball>(event.Entity1);
|
|
||||||
if (ball == NULL) {
|
|
||||||
ball = m_World->GetComponent<Components::Ball>(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<Components::Brick>(collObject);
|
|
||||||
if (collisionBrick != NULL) {
|
|
||||||
dd::Events::PlaySFX e;
|
|
||||||
e.path = "Sounds/Brick/brickBreak.wav";
|
|
||||||
EventBroker->Publish(e);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto collisionPad = m_World->GetComponent<Components::Pad>(collObject);
|
|
||||||
if (collisionPad != NULL) {
|
|
||||||
dd::Events::PlaySFX e;
|
|
||||||
e.path = "Sounds/Metal/impact.wav";
|
|
||||||
EventBroker->Publish(e);
|
|
||||||
return true;
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ALuint dd::Systems::SoundSystem::CreateSource()
|
ALuint dd::Systems::SoundSystem::CreateSource()
|
||||||
@@ -144,82 +76,5 @@ ALuint dd::Systems::SoundSystem::CreateSource()
|
|||||||
|
|
||||||
ALuint dd::Systems::SoundSystem::LoadFile(std::string path)
|
ALuint dd::Systems::SoundSystem::LoadFile(std::string path)
|
||||||
{
|
{
|
||||||
if (m_BufferCache.find(path) != m_BufferCache.end()) {
|
/*Moved code*/
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user