OpenAl Werkt! en Setup enzo

This commit is contained in:
Aaldert
2016-06-05 23:49:06 +02:00
parent 47b4d9570d
commit 117010b9fe
18 changed files with 320 additions and 237 deletions
+4
View File
@@ -9,6 +9,8 @@
int CrystalPoint::width = 0;
int CrystalPoint::height = 0;
SoundSystem CrystalPoint::sound_system;
void CrystalPoint::init()
{
player = Player::getInstance();
@@ -90,6 +92,8 @@ void CrystalPoint::update()
mouseOffset = Vec2f(0, 0);
prevKeyboardState = keyboardState;
glutPostRedisplay();
sound_system.SetListener(player->position, Vec3f(), Vec3f());
}
+6
View File
@@ -1,8 +1,10 @@
#pragma once
class WorldHandler;
class SoundSystem;
class Player;
#include "Vector.h"
#include "SoundSystem.h"
class KeyboardState
{
@@ -33,4 +35,8 @@ public:
float lastFrameTime;
static SoundSystem& GetSoundSystem() { return sound_system; }
private:
static SoundSystem sound_system;
};
+8 -4
View File
@@ -89,12 +89,13 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile />
<AdditionalIncludeDirectories>freeglut/include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>freeglut/include; openAL/include</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>freeglut/lib</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>freeglut/lib; openAL/libs/Win32</AdditionalLibraryDirectories>
<AdditionalDependencies>kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;OpenAL32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -163,7 +164,8 @@
<ClCompile Include="LevelObject.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="Model.cpp" />
<ClCompile Include="OpenAL.cpp" />
<ClCompile Include="Sound.cpp" />
<ClCompile Include="SoundSystem.cpp" />
<ClCompile Include="Player.cpp" />
<ClCompile Include="Vector.cpp" />
<ClCompile Include="Vertex.cpp" />
@@ -181,7 +183,8 @@
<ClInclude Include="LevelObject.h" />
<ClInclude Include="Main.h" />
<ClInclude Include="Model.h" />
<ClInclude Include="OpenAL.h" />
<ClInclude Include="Sound.h" />
<ClInclude Include="SoundSystem.h" />
<ClInclude Include="Player.h" />
<ClInclude Include="stb_image.h" />
<ClInclude Include="vector.h" />
@@ -196,6 +199,7 @@
<None Include="worlds\worlds.json" />
</ItemGroup>
<ItemGroup>
<Media Include="WAVE\bond.wav" />
<Media Include="WAVE\Sound.wav" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+15
View File
@@ -69,6 +69,12 @@
<ClCompile Include="Interface.cpp">
<Filter>Source Files\World</Filter>
</ClCompile>
<ClCompile Include="SoundSystem.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Sound.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="World.h">
@@ -119,6 +125,12 @@
<ClInclude Include="vector.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SoundSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Sound.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="worlds\worlds.json">
@@ -138,5 +150,8 @@
<Media Include="WAVE\Sound.wav">
<Filter>Resource Files</Filter>
</Media>
<Media Include="WAVE\bond.wav">
<Filter>Resource Files</Filter>
</Media>
</ItemGroup>
</Project>
+11 -7
View File
@@ -1,8 +1,8 @@
#define _USE_MATH_DEFINES
#include <cmath>
#include "OpenAL.h"
#include "Enemy.h"
#include "Model.h"
#include "CrystalPoint.h"
#include <iostream>
Enemy::Enemy(const std::string &fileName,
@@ -19,7 +19,7 @@ Enemy::Enemy(const std::string &fileName,
speed = 1;
radius = 10;
hasTarget = false;
// openal = new OpenAL();
hit_sound_id = CrystalPoint::GetSoundSystem().LoadSound("WAVE/Sound.wav", false);
}
@@ -61,14 +61,11 @@ void Enemy::inEyeSight(Vec3f & TargetPosition)
bool Enemy::hasCollison(Vec3f &)
{
return false;
}
void Enemy::update(float delta)
{
// if (!openal->isMusicPlaying()) {
// openal->playMusic();
// }
if (hasTarget)
{
@@ -91,5 +88,12 @@ void Enemy::update(float delta)
position.z += dz;
}
rotation.y = atan2f(dx, dz) * 180 / M_PI;
}
}
if (false)
{
Sound* sound = CrystalPoint::GetSoundSystem().GetSound(hit_sound_id);
sound->SetPos(position, Vec3f());
sound->Play();
}
}
+3 -1
View File
@@ -3,7 +3,6 @@
#include "Entity.h"
#include <string>
#include "Vector.h"
#include "OpenAL.h"
class Enemy : public Entity
{
@@ -20,5 +19,8 @@ public:
void inEyeSight(Vec3f &);
bool hasCollison(Vec3f &);
private:
int hit_sound_id;
};
+2
View File
@@ -54,6 +54,8 @@ int main(int argc, char* argv[])
CrystalPoint::width = GLUT_WINDOW_WIDTH;
glutMainLoop();
delete app;
return 0;
}
-180
View File
@@ -1,180 +0,0 @@
#include "OpenAL.h"
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <al.h>
#include <alc.h>
SoundSystem::SoundSystem():
device(nullptr),
context(nullptr)
{
device = alcOpenDevice(nullptr);
if (!device)
return;
context = alcCreateContext(device, nullptr);
if (!context)
return;
alcMakeContextCurrent(context);
}
SoundSystem::~SoundSystem()
{
alcMakeContextCurrent(nullptr); //Make no context current
alcDestroyContext(context); //Destroy the OpenAL Context
alcCloseDevice(device); //Close the OpenAL Device
}
void SoundSystem::Play(ESoundID inID)
{
ALuint buffer;
alGenBuffers(1, &buffer); //Generate one OpenAL Buffer and link to "buffer"
ALuint source;
alGenSources(1, &source); //Generate one OpenAL Source and link to "source"
if (alGetError() != AL_NO_ERROR) return ; //Error during buffer/source generation
//Figure out the format of the WAVE file
ALenum format = 0; //The audio format (bits per sample, number of channels)
if (bitsPerSample == 8)
{
if (channels == 1)
format = AL_FORMAT_MONO8;
else if (channels == 2)
format = AL_FORMAT_STEREO8;
}
else if (bitsPerSample == 16)
{
if (channels == 1)
format = AL_FORMAT_MONO16;
else if (channels == 2)
format = AL_FORMAT_STEREO16;
}
if (!format) return; //Not valid format
ALuint frequency = sampleRate; //The Sample Rate of the WAVE file
alBufferData(buffer, format, buf, dataSize, frequency); //Store the sound data in the OpenAL Buffer
if (alGetError() != AL_NO_ERROR)
return EndWithError("Error loading ALBuffer"); //Error during buffer loading
//Sound setting variables
ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; //Position of the source sound
ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; //Velocity of the source sound
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; //Position of the listener
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; //Velocity of the listener
ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }; //Orientation of the listener
//First direction vector, then vector pointing up)
//Listener
alListenerfv(AL_POSITION, ListenerPos); //Set position of the listener
alListenerfv(AL_VELOCITY, ListenerVel); //Set velocity of the listener
alListenerfv(AL_ORIENTATION, ListenerOri); //Set orientation of the listener
//Source
alSourcei(source, AL_BUFFER, buffer); //Link the buffer to the source
alSourcef(source, AL_PITCH, 1.0f); //Set the pitch of the source
alSourcef(source, AL_GAIN, 1.0f); //Set the gain of the source
alSourcefv(source, AL_POSITION, SourcePos); //Set the position of the source
alSourcefv(source, AL_VELOCITY, SourceVel); //Set the velocity of the source
alSourcei(source, AL_LOOPING, AL_FALSE); //Set if source is looping sound
//PLAY
alSourcePlay(source); //Play the sound buffer linked to the source
if (alGetError() != AL_NO_ERROR) return EndWithError("Error playing sound"); //Error when playing sound
system("PAUSE"); //Pause to let the sound play
//Clean-up
fclose(fp); //Close the WAVE file
delete[] buf; //Delete the sound data buffer
alDeleteSources(1, &source); //Delete the OpenAL Source
alDeleteBuffers(1, &buffer); //Delete the OpenAL Buffer
return ;
}
void SoundSystem::Pause(ESoundID inID)
{
}
void SoundSystem::Stop(ESoundID inID)
{
}
const char* SoundSystem::GetWaveFile(ESoundID inID)
{
}
bool SoundSystem::LoadWave(ESoundID inID)
{
const char* path = GetWaveFile(inID);
FILE *fp = fopen(path, "rb"); //Open the WAVE file
if (!fp)
return false; // Could not open file
//Check that the WAVE file is OK
char type[4];
fread(type, sizeof(char), 4, fp); // Reads the first bytes in the file
if (type[0] != 'R' || type[1] != 'I' || type[2] != 'F' || type[3] != 'F') // Should be "RIFF"
return false; // Not RIFF
DWORD size;
fread(&size, sizeof(DWORD), 1, fp); // Continue to read the file
fread(type, sizeof(char), 4, fp); // Continue to read the file
if (type[0] != 'W' || type[1] != 'A' || type[2] != 'V' || type[3] != 'E') //This part should be "WAVE"
return false; //Not WAVE
fread(type, sizeof(char), 4, fp); //Continue to read the file
if (type[0] != 'f' || type[1] != 'm' || type[2] != 't' || type[3] != ' ') //This part should be "fmt "
return false; //Not fmt
//Now we know that the file is a acceptable WAVE file
//Info about the WAVE data is now read and stored
DWORD chunkSize;
fread(&chunkSize, sizeof(DWORD), 1, fp);
short formatType;
fread(&formatType, sizeof(short), 1, fp);
short channels;
fread(&channels, sizeof(short), 1, fp);
DWORD sampleRate;
fread(&sampleRate, sizeof(DWORD), 1, fp);
DWORD avgBytesPerSec;
fread(&avgBytesPerSec, sizeof(DWORD), 1, fp);
short bytesPerSample;
fread(&bytesPerSample, sizeof(short), 1, fp);
short bitsPerSample;
fread(&bitsPerSample, sizeof(short), 1, fp);
fread(type, sizeof(char), 4, fp);
if (type[0] != 'd' || type[1] != 'a' || type[2] != 't' || type[3] != 'a') //This part should be "data"
return false; //not data
DWORD dataSize;
fread(&dataSize, sizeof(DWORD), 1, fp); //The size of the sound data is read
//Display the info about the WAVE file
std::cout << "Chunk Size: " << chunkSize << "\n";
std::cout << "Format Type: " << formatType << "\n";
std::cout << "Channels: " << channels << "\n";
std::cout << "Sample Rate: " << sampleRate << "\n";
std::cout << "Average Bytes Per Second: " << avgBytesPerSec << "\n";
std::cout << "Bytes Per Sample: " << bytesPerSample << "\n";
std::cout << "Bits Per Sample: " << bitsPerSample << "\n";
std::cout << "Data Size: " << dataSize << "\n";
unsigned char* buf = new unsigned char[dataSize]; //Allocate memory for the sound data
std::cout << fread(buf, sizeof(BYTE), dataSize, fp) << " bytes loaded\n"; //Read the sound data and display the
return true;
}
-29
View File
@@ -1,29 +0,0 @@
#pragma once
struct ALCdevice;
struct ALCcontext;
class SoundSystem
{
public:
enum ESoundID
{
ES_Music1,
ES_Music2
};
SoundSystem();
~SoundSystem();
void Play(ESoundID inID);
void Pause(ESoundID inID);
void Stop(ESoundID inID);
private:
const char* GetWaveFile(ESoundID inID);
bool LoadWave(ESoundID inID);
ALCdevice* device;
ALCcontext* context;
};
+139
View File
@@ -0,0 +1,139 @@
#include "Sound.h"
#include <iostream>
#include <windows.h>
#include <al.h>
Sound::Sound(const char* inWavPath, bool inLooping):
buffer_id(0),
source_id(0),
is_looping(inLooping)
{
const char* path = inWavPath;
FILE *fp = fopen(path, "rb"); // Open the WAVE file
if (!fp)
return; // Could not open file
// Check that the WAVE file is OK
char type[4];
fread(type, sizeof(char), 4, fp); // Reads the first bytes in the file
if (type[0] != 'R' || type[1] != 'I' || type[2] != 'F' || type[3] != 'F') // Should be "RIFF"
return; // Not RIFF
DWORD size;
fread(&size, sizeof(DWORD), 1, fp); // Continue to read the file
fread(type, sizeof(char), 4, fp); // Continue to read the file
if (type[0] != 'W' || type[1] != 'A' || type[2] != 'V' || type[3] != 'E') // This part should be "WAVE"
return; // Not WAVE
fread(type, sizeof(char), 4, fp); // Continue to read the file
if (type[0] != 'f' || type[1] != 'm' || type[2] != 't' || type[3] != ' ') // This part should be "fmt "
return; // Not fmt
// Now we know that the file is a acceptable WAVE file
// Info about the WAVE data is now read and stored
DWORD chunkSize;
fread(&chunkSize, sizeof(DWORD), 1, fp);
short formatType;
fread(&formatType, sizeof(short), 1, fp);
short channels;
fread(&channels, sizeof(short), 1, fp);
DWORD sampleRate;
fread(&sampleRate, sizeof(DWORD), 1, fp);
DWORD avgBytesPerSec;
fread(&avgBytesPerSec, sizeof(DWORD), 1, fp);
short bytesPerSample;
fread(&bytesPerSample, sizeof(short), 1, fp);
short bitsPerSample;
fread(&bitsPerSample, sizeof(short), 1, fp);
ALenum format = 0; // The audio format (bits per sample, number of channels)
if (bitsPerSample == 8)
{
if (channels == 1)
format = AL_FORMAT_MONO8;
else if (channels == 2)
format = AL_FORMAT_STEREO8;
}
else if (bitsPerSample == 16)
{
if (channels == 1)
format = AL_FORMAT_MONO16;
else if (channels == 2)
format = AL_FORMAT_STEREO16;
}
if (!format)
return; // Not valid format
fread(type, sizeof(char), 4, fp);
if (type[0] != 'd' || type[1] != 'a' || type[2] != 't' || type[3] != 'a') // This part should be "data"
return; // not data
DWORD dataSize;
fread(&dataSize, sizeof(DWORD), 1, fp); // The size of the sound data is read
// Display the info about the WAVE file
std::cout << "Chunk Size: " << chunkSize << "\n";
std::cout << "Format Type: " << formatType << "\n";
std::cout << "Channels: " << channels << "\n";
std::cout << "Sample Rate: " << sampleRate << "\n";
std::cout << "Average Bytes Per Second: " << avgBytesPerSec << "\n";
std::cout << "Bytes Per Sample: " << bytesPerSample << "\n";
std::cout << "Bits Per Sample: " << bitsPerSample << "\n";
std::cout << "Data Size: " << dataSize << "\n";
unsigned char* buf = new unsigned char[dataSize]; // Allocate memory for the sound data
std::cout << fread(buf, sizeof(BYTE), dataSize, fp) << " bytes loaded\n"; // Read the sound data and display the
fclose(fp);
alGenBuffers(1, &buffer_id); // Generate one OpenAL Buffer and link to "buffer"
alGenSources(1, &source_id); // Generate one OpenAL Source and link to "source"
if (alGetError() != AL_NO_ERROR)
return; // Error during buffer/source generation
alBufferData(buffer_id, format, buf, dataSize, sampleRate); // Store the sound data in the OpenAL Buffer
if (alGetError() != AL_NO_ERROR)
return; // Error during buffer loading
delete[] buf; // Delete the sound data buffer
}
Sound::~Sound()
{
alSourceStop(source_id);
alDeleteSources(1, &source_id); // Delete the OpenAL Source
alDeleteBuffers(1, &buffer_id); // Delete the OpenAL Buffer
}
void Sound::SetPos(const Vec3f& inPos, const Vec3f& inVel)
{
alSourcei(source_id, AL_BUFFER, buffer_id); // Link the buffer to the source
alSourcef(source_id, AL_PITCH, 1.0f); // Set the pitch of the source
alSourcef(source_id, AL_GAIN, 1.0f); // Set the gain of the source
alSourcefv(source_id, AL_POSITION, inPos.v); // Set the position of the source
alSourcefv(source_id, AL_VELOCITY, inVel.v); // Set the velocity of the source
alSourcei(source_id, AL_LOOPING, is_looping ? AL_TRUE : AL_FALSE); // Set if source is looping sound
}
void Sound::Play()
{
alSourcePlay(source_id);
int e = alGetError(); // != AL_NO_ERROR) return;
}
void Sound::Pause()
{
alSourcePause(source_id);
}
void Sound::Stop()
{
alSourceStop(source_id);
}
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include "vector.h"
class Sound
{
public:
Sound(const char* inWavPath, bool inLooping);
~Sound();
void SetPos(const Vec3f& inPos, const Vec3f& inVel);
void Play();
void Pause();
void Stop();
private:
unsigned int buffer_id;
unsigned int source_id;
bool is_looping;
};
+56
View File
@@ -0,0 +1,56 @@
#include "SoundSystem.h"
#include <cstdlib>
#include <iostream>
#include <windows.h>
SoundSystem::SoundSystem():
device(nullptr),
context(nullptr)
{
device = alcOpenDevice(nullptr);
if (!device)
return;
context = alcCreateContext(device, nullptr);
if (!context)
return;
alcMakeContextCurrent(context);
}
SoundSystem::~SoundSystem()
{
for (auto sound : sounds)
delete sound;
alcMakeContextCurrent(nullptr);
alcDestroyContext(context);
alcCloseDevice(device);
}
void SoundSystem::SetListener(const Vec3f& inPos, const Vec3f& inVel, const Vec3f& inOri)
{
ALfloat orientation[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 };
alListenerfv(AL_POSITION, inPos.v);
alListenerfv(AL_VELOCITY, inVel.v);
alListenerfv(AL_ORIENTATION, orientation);
}
unsigned int SoundSystem::LoadSound(const char* inWavPath, bool inLooping)
{
Sound* sound = new Sound(inWavPath, inLooping);
sounds.push_back(sound);
return sounds.size() - 1;
}
Sound* SoundSystem::GetSound(unsigned int inID)
{
if (inID > sounds.size())
return nullptr;
return sounds[inID];
}
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include <vector>
#include <al.h>
#include <alc.h>
#include "vector.h"
#include "Sound.h"
class SoundSystem
{
public:
SoundSystem();
~SoundSystem();
void SetListener(const Vec3f& inPos, const Vec3f& inVel, const Vec3f& inOri);
unsigned int LoadSound(const char* inWavPath, bool inLooping);
Sound* GetSound(unsigned int inID);
private:
ALCdevice* device;
ALCcontext* context;
std::vector<Sound*> sounds;
};
BIN
View File
Binary file not shown.
+11 -1
View File
@@ -7,7 +7,8 @@
#include <fstream>
#include <iostream>
World::World(const std::string &fileName)
World::World(const std::string &fileName):
music_id(-1)
{
//Store player instance
player = Player::getInstance();
@@ -113,6 +114,15 @@ World::World(const std::string &fileName)
enemies.push_back(new Enemy(e["file"].asString(), position, rotation, scale));
}
if (!v["world"]["music"].isNull())
{
music_id = CrystalPoint::GetSoundSystem().LoadSound(v["world"]["music"].asString().c_str(), true);
Sound* music = CrystalPoint::GetSoundSystem().GetSound(music_id);
music->SetPos(Vec3f(), Vec3f());
music->Play();
}
}
+1
View File
@@ -17,6 +17,7 @@ private:
Player* player;
HeightMap* heightmap;
Interface* interface;
int music_id;
std::vector<Entity*> entities;
std::vector<Enemy*> enemies;
+1
View File
@@ -97,6 +97,7 @@ bool WorldHandler::isPlayerPositionValid(void)
{
if(!loadingWorld)
return world->isPlayerPositionValid();
return false;
}
float WorldHandler::getHeight(float x, float y)
+16 -15
View File
@@ -1,17 +1,18 @@
{
"world": {
"heightmap": "worlds/small.png",
"object-templates": [
{
"color":100,
"file": "models/boom/Boom.obj",
"collision": false
}
]
},
"player": {
"startposition": [ 20, 5, 20 ]
},
"objects": [],
"enemies": []
"world": {
"heightmap": "worlds/small.png",
"object-templates": [
{
"color": 100,
"file": "models/boom/Boom.obj",
"collision": false
}
],
"music": "WAVE/bond.wav"
},
"player": {
"startposition": [ 20, 5, 20 ]
},
"objects": [],
"enemies": []
}