diff --git a/Enemy.cpp b/Enemy.cpp index 3ee1faf..730835a 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -5,7 +5,6 @@ #include "Model.h" #include - Enemy::Enemy(const std::string &fileName, const Vec3f &position, Vec3f &rotation, @@ -21,6 +20,7 @@ Enemy::Enemy(const std::string &fileName, speed = 1; radius = 10; hasTarget = false; + openal = new OpenAL(); } @@ -51,9 +51,12 @@ void Enemy::draw() void Enemy::update(float delta) { + if (!openal->isMusicPlaying()) { + openal->playMusic(); + } if (hasTarget) { - OpenAL(); + //just 2d walking float dx, dz, length; diff --git a/Enemy.h b/Enemy.h index 9c14fb9..9d60def 100644 --- a/Enemy.h +++ b/Enemy.h @@ -3,6 +3,7 @@ #include "Entity.h" #include #include "Vector.h" +#include "OpenAL.h" class Enemy : public Entity { @@ -16,5 +17,7 @@ public: void update(float); void draw(); +private: + OpenAL *openal; }; diff --git a/OpenAL.cpp b/OpenAL.cpp index 092411f..138fe80 100644 --- a/OpenAL.cpp +++ b/OpenAL.cpp @@ -7,7 +7,6 @@ OpenAL::OpenAL() { - Init(); } @@ -20,8 +19,10 @@ int OpenAL::EndWithError(char* msg) return error; } -int OpenAL::Init() -{ +int OpenAL::playMusic(void) { + //start nieuwe thread + //speel sound; + isPlaying = true; //Loading of the WAVE file FILE *fp = NULL; //Create FILE pointer for the WAVE file fp = fopen("WAVE/Sound.wav", "rb"); //Open the WAVE file @@ -77,11 +78,11 @@ int OpenAL::Init() 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 - //number of bytes loaded. - //Should be the same as the Data Size if OK + //number of bytes loaded. + //Should be the same as the Data Size if OK - //Now OpenAL needs to be initialized + //Now OpenAL needs to be initialized ALCdevice *device; //Create an OpenAL Device ALCcontext *context; //And an OpenAL Context device = alcOpenDevice(NULL); //Open the device @@ -143,7 +144,7 @@ int OpenAL::Init() //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 + system("PAUSE"); //Pause to let the sound play //Clean-up fclose(fp); //Close the WAVE file @@ -154,9 +155,14 @@ int OpenAL::Init() alcDestroyContext(context); //Destroy the OpenAL Context alcCloseDevice(device); //Close the OpenAL Device + isPlaying = false; return EXIT_SUCCESS; } +bool OpenAL::isMusicPlaying() +{ + return isPlaying; +} OpenAL::~OpenAL() { diff --git a/OpenAL.h b/OpenAL.h index 6b16ffa..1b04099 100644 --- a/OpenAL.h +++ b/OpenAL.h @@ -4,6 +4,9 @@ class OpenAL public: OpenAL(); int EndWithError(char* msg); - int Init(); + int playMusic(void); + bool isMusicPlaying(); ~OpenAL(); +private: + bool isPlaying; };