Merge remote-tracking branch 'origin/master' into Physics

This commit is contained in:
viktorljung
2015-09-23 15:14:16 +02:00
25 changed files with 2834 additions and 6 deletions
+16 -3
View File
@@ -9,6 +9,10 @@ find_package(ZLIB REQUIRED)
find_package(PNG REQUIRED)
find_package(BGFX REQUIRED)
find_package(liquidfun REQUIRED)
# Because FindOpenAL is retarded
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${CMAKE_SOURCE_DIR}/deps/include/AL")
find_package(OpenAL REQUIRED)
# GLM
if(UNIX)
find_package(X11 REQUIRED)
@@ -25,6 +29,7 @@ include_directories(
${PNG_INCLUDE_DIRS}
${X11_INCLUDE_DIRS}
${BGFX_INCLUDE_DIRS}
${OPENAL_INCLUDE_DIR}
${liquidfun_INCLUDE_DIRS}
)
@@ -68,6 +73,12 @@ file(GLOB SOURCE_FILES_Game
)
source_group(Game FILES ${SOURCE_FILES_Game})
file(GLOB SOURCE_FILES_Sound
"${INCLUDE_PATH}/Sound/*.h"
"Sound/*.cpp"
)
source_group(Sound FILES ${SOURCE_FILES_Sound})
set(SOURCE_FILES
${SOURCE_FILES_Core}
${SOURCE_FILES_Core_Util}
@@ -75,14 +86,14 @@ set(SOURCE_FILES
${SOURCE_FILES_Rendering}
${SOURCE_FILES_Transform}
${SOURCE_FILES_Physics}
${SOURCE_FILES_Game})
${SOURCE_FILES_Game}
${SOURCE_FILES_Sound})
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
set(STATIC_LIBRARY_FLAGS "${STATIC_LIBRARY_FLAGS}")
endif()
add_definitions(-DBGFX_CONFIG_RENDERER_OPENGL=33)
add_library(game ${SOURCE_FILES})
target_link_libraries(game
${OPENGL_LIBRARIES}
@@ -93,6 +104,7 @@ target_link_libraries(game
${PNG_LIBRARIES}
${X11_LIBRARIES}
${BGFX_LIBRARIES}
${OPENAL_LIBRARY}
${liquidfun_LIBRARIES}
)
@@ -109,6 +121,7 @@ target_link_libraries(breakout
${PNG_LIBRARIES}
${X11_LIBRARIES}
${BGFX_LIBRARIES}
${OPENAL_LIBRARY}
${liquidfun_LIBRARIES}
)
+2
View File
@@ -19,6 +19,8 @@
#include "PrecompiledHeader.h"
#include "Core/World.h"
//TODO Delete this please
void dd::World::RecycleEntityID(EntityID id)
{
m_RecycledEntityIDs.push(id);
+5
View File
@@ -111,6 +111,11 @@ void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBe
transform->Scale = glm::vec3(1.6, 0.35, 0.5);
transform->Position = glm::vec3(x - 7, y + 1, -10.f);
cBrick->Score = 10 * num;
//sound
auto collisionSound = m_World->AddComponent<Components::CollisionSound>(brick);
collisionSound->filePath = "Sounds/Brick/shortbrickbreak.wav";
m_World->CommitEntity(brick);
return;
}
+88
View File
@@ -0,0 +1,88 @@
#include "Sound/Sound.h"
dd::Sound::Sound(std::string path)
{
m_Buffer = LoadFile(path);
}
ALuint dd::Sound::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;
}
+79
View File
@@ -0,0 +1,79 @@
#include "PrecompiledHeader.h"
#include "Sound/SoundSystem.h"
dd::Systems::SoundSystem::~SoundSystem()
{
alSourcei(m_Source, AL_BUFFER, 0);
alDeleteSources(1, &m_Source);
}
void dd::Systems::SoundSystem::Initialize()
{ //initialize OpenAL
ALCdevice* device = alcOpenDevice(NULL);
ALCcontext* context;
if (device) {
context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);
}
else {
LOG_ERROR("OpenAL failed to initialize.");
}
alGetError();
//Create source
m_Source = CreateSource();
alSpeedOfSound(340.29f); // Speed of sound m/s
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
const ALfloat pos[3] = {0, 0, 0};
alListenerfv(AL_POSITION, pos);
alSourcefv(m_Source, AL_POSITION, pos);
//Subscribe to events
EVENT_SUBSCRIBE_MEMBER(m_EContact, &SoundSystem::OnContact);
EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &SoundSystem::OnPlaySFX);
}
void dd::Systems::SoundSystem::Update(double dt)
{
}
bool dd::Systems::SoundSystem::OnPlaySFX(const dd::Events::PlaySFX &event)
{
//m_Source = CreateSource();
Sound *sound = ResourceManager::Load<Sound>(event.path);
ALuint buffer = sound->Buffer();
alSourcei(m_Source, AL_BUFFER, buffer);
alSourcePlay(m_Source);
}
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);
if (collisionSound == NULL) {
collisionSound = m_World->GetComponent<Components::CollisionSound>(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;
}
ALuint dd::Systems::SoundSystem::CreateSource()
{
ALuint source;
alGenSources((ALuint)1, &source);
return source;
}