Aaldert 9 роки тому
батько
коміт
0afbca8ffc
4 змінених файлів з 25 додано та 10 видалено
  1. 5 2
      Enemy.cpp
  2. 3 0
      Enemy.h
  3. 13 7
      OpenAL.cpp
  4. 4 1
      OpenAL.h

+ 5 - 2
Enemy.cpp

@@ -5,7 +5,6 @@
 #include "Model.h"
 #include <iostream>
 
-
 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;
 

+ 3 - 0
Enemy.h

@@ -3,6 +3,7 @@
 #include "Entity.h"
 #include <string>
 #include "Vector.h"
+#include "OpenAL.h"
 
 class Enemy : public Entity
 {
@@ -16,5 +17,7 @@ public:
 
 	void update(float);
 	void draw();
+private:
+	OpenAL *openal;
 };
 

+ 13 - 7
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()
 {

+ 4 - 1
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;
 };