Browse Source

Uncomment sound code

Mario Forzanini 2 years ago
parent
commit
e25edc636b
4 changed files with 20 additions and 18 deletions
  1. 2 2
      definitions.hpp
  2. 2 2
      functions.cpp
  3. 2 2
      functions.hpp
  4. 14 12
      invaders.cpp

+ 2 - 2
definitions.hpp

@@ -21,7 +21,7 @@
 #include <vector>
 #include <cmath>
 #include <unistd.h>
-//#include <pthread.h>	//for sounds
+#include <pthread.h>	//for sounds
 #include <sys/stat.h>	//for making directories
 #include <ncurses.h>	//for graphics
 #include <mutex>
@@ -67,7 +67,7 @@
 #define ROCKET_SPRITE '^'
 
 //THREAD DEFINES
-//#define THREADS_NUM 5
+#define THREADS_NUM 5
 
 //COLORS
 #define red "\033[31m" 

+ 2 - 2
functions.cpp

@@ -1342,7 +1342,7 @@ void print_scores(){
 }
 
 
-/*//=========== THREADS FUNCTIONS ==============================
+//=========== THREADS FUNCTIONS ==============================
 
 void *pmusic(void *arg){
 	while(1){
@@ -1370,7 +1370,7 @@ void *plose_theme(void *arg){
 void pkill_music(pthread_t thread){
 	pthread_cancel(thread);
 	system("beep -f 1 2> /dev/null");
-}*/
+}
 
 
 				

+ 2 - 2
functions.hpp

@@ -129,7 +129,7 @@ void create_folder(int);
 
 void kill_music(int);*/
 
-/*//PTHREAD FUNCTIONS
+//PTHREAD FUNCTIONS
 void *pmusic(void *arg);
 
 void *pshoot_sound(void* arg);
@@ -140,7 +140,7 @@ void *pwin_theme(void *arg);
 
 void *plose_theme(void* arg);
 
-void pkill_music(pthread_t);*/
+void pkill_music(pthread_t);
 
 
 #endif

+ 14 - 12
invaders.cpp

@@ -66,12 +66,14 @@ int main(int argc,char** argv)
 	int level=1;						//difficulty level
 	//bool sound=false;					
 	
-	/*//PTHREAD STUFF
+	//PTHREAD STUFF
 	pthread_t thread[THREADS_NUM];
 	int bkgd_music;			//#0
 	int enshoot_sound;		//#1
 	int win_theme;			//#2
-	int lose_theme;			//#3
+	/* This is not used
+	  *  int lose_theme;			//#3
+	  */
 	int shoot_sound;		//#4
 	
 	if(argc>1){		//./invaders -m/m/mute -> the game starts with no sounds.
@@ -121,8 +123,8 @@ int main(int argc,char** argv)
 
 	/////////ENTERING MAIN LOOP
 	
-	/*if(sound)
-		bkgd_music = pthread_create(&thread[0],NULL,pmusic,NULL); 	//executing background music in parallel thread*/
+	if(sound)
+		bkgd_music = pthread_create(&thread[0],NULL,pmusic,NULL); 	//executing background music in parallel thread
 	
 	if(argc>1){
 		if(strcmp(argv[1],"--uber-debug")==0){ player1.weaponclass=5; player1.weaponclassrkt=5; player1.rocketlauncher=true; player1.length=MAX_LENGTH; }
@@ -140,7 +142,7 @@ int main(int argc,char** argv)
 		    		           
 	    	if(command == commands[8]){ 						//q = exit game
 	    		endwin();
-	    		//pkill_music(thread[0]);
+	    		pkill_music(thread[0]);
 	    		cout<<lightgreen<<"Game exited correctly."<<none<<endl;
 	    		return 1;
 	    	}  
@@ -164,16 +166,16 @@ int main(int argc,char** argv)
 	    	}	    
 		    		
 		if(command==commands[5] || command==commands[6]){                		//spacebar = shoot!
-			//if(sound) shoot_sound = pthread_create(&thread[4],NULL,pshoot_sound,NULL);
+			if(sound) shoot_sound = pthread_create(&thread[4],NULL,pshoot_sound,NULL);
 			player1.shoot(bullets);
 			if(player1.rocketlauncher) player1.shootrkt(rockets);
 		}
 		
-		/*if(command==commands[7]){				//mute/unmute
+		if(command==commands[7]){				//mute/unmute
 			if(sound) pkill_music(thread[0]);
 			else	bkgd_music = pthread_create(&thread[0],NULL,pmusic,NULL); 
 			sound=!sound;
-		}*/
+		}
 		
 		if(enemyalive(enemies))					//if there's at least one enemy alive:
 		{
@@ -188,7 +190,7 @@ int main(int argc,char** argv)
 				//mvaddch(it->y,it->x,' ');
 				it->next_pos();                                        //evaluate new positions
 				if(it->alive && (double)rand()/RAND_MAX<(shootrate*enemy_num/n_enemies)){     //try a bomb-dropping
-						//if(sound) shoot_sound = pthread_create(&thread[1],NULL,penshoot_sound,NULL);
+						if(sound) enshoot_sound = pthread_create(&thread[1],NULL,penshoot_sound,NULL);
 						it->shoot(bombs);
 				}
 			}
@@ -204,7 +206,7 @@ int main(int argc,char** argv)
 			}
 			boss1.next_pos();
 			if((double)rand()/RAND_MAX<shootrate*25.){		//boss's shootrate is 25 times the one of normal enemies
-				//if(sound) shoot_sound = pthread_create(&thread[1],NULL,penshoot_sound,NULL);
+				if(sound) enshoot_sound = pthread_create(&thread[1],NULL,penshoot_sound,NULL);
 				boss1.shoot(bombs);	
 			}
 		}
@@ -247,10 +249,10 @@ int main(int argc,char** argv)
 		///////ENDGAME CHECKS               
 		if(boss1.health<1 || gameover(player1,bombs))
 		{
-			/*if(sound){
+			if(sound){
 				pkill_music(thread[0]);	
 				win_theme = pthread_create(&thread[2],NULL,pwin_theme,NULL);
-			}*/
+			}
 
 			WINDOW *replay;
 			if(boss1.health<1)	// YOU WON