Browse Source

Merge branch 'custom_keybindings' of marioforzanini/invaders into master

Matteo Zeccoli Marazzini 2 years ago
parent
commit
caaf19c6c4
4 changed files with 52 additions and 5 deletions
  1. 2 1
      bossrush.cpp
  2. 43 2
      functions.cpp
  3. 4 0
      functions.hpp
  4. 3 2
      invaders.cpp

+ 2 - 1
bossrush.cpp

@@ -57,7 +57,8 @@ int main(int argc,char** argv)
 	int refresh_time=100;          				//how long the program waits before refreshing the screen
 	int chflag=0;						//cheat flag: 0=normal, 1=cheats allowed.
 	int num=0;
-	int commands[CMD_NUM]={'a','d','w','s','p',' ','l','m','q'};
+	int commands[CMD_NUM];
+	read_commands(commands); //0:left,1:right,2:up,3:down,4:pause,5:shoot1,6:shoot2,7:mute,8:quit
 	
 	WINDOW *Score,*BossHP;
 	

+ 43 - 2
functions.cpp

@@ -376,7 +376,7 @@ void change_commands(int* commands){
 
 		for(int i=0;i<CMD_NUM;i++){
 			if(commands[i]==' ') chcommands[i]="Barra sp.";
-			else chcommands[i]=commands[i];
+			else chcommands[i]=(char)commands[i];
 			if(i==line) wattron(change,COLOR_PAIR(9));
 			mvwprintw(change,i+5,1,"%s: %s",name[i].c_str(),chcommands[i].c_str());
 			wattron(change,COLOR_PAIR(1));
@@ -405,6 +405,7 @@ void change_commands(int* commands){
 				timeout(-1);
 				cmd=wgetch(change);
 				commands[line]=tolower(cmd);
+				dump_commands(commands);
 				wmove(change,line+5,0);
 				wclrtoeol(change);
 				box(change,ACS_VLINE,ACS_HLINE);
@@ -422,7 +423,47 @@ void change_commands(int* commands){
 	return;	
 	
 }
-	
+
+void dump_commands(int* commands)
+{
+	std::ofstream cmd;
+	std::string local_dir = getenv("HOME");
+	std::string cmd_file = local_dir + RECORD_DIR + "commands";
+	create_folder(0);
+	cmd.open(cmd_file);
+	if(cmd.is_open()) {
+		for(int i = 0; i < CMD_NUM; i++) {
+			cmd << (char)commands[i];
+		}
+		cmd << std::endl;
+		cmd.close();
+	} else {
+		printw("error: cannot open %s", cmd_file);
+		return;
+	}
+
+}
+
+void read_commands(int* commands)
+{
+	std::ifstream cmd;
+	int def[CMD_NUM] = {'a', 'd', 'w', 's', 'p', ' ', 'l', 'm', 'q'}; // default
+	std::string local_dir = getenv("HOME");
+	std::string cmd_file = local_dir + RECORD_DIR + "commands";
+	cmd.open(cmd_file);
+	if(cmd.is_open()) {
+		std::string line;
+		getline(cmd, line);
+		for(int i = 0; i < CMD_NUM; i++) {
+			commands[i] = (int)line[i];
+		}
+	} else {
+		for(int i = 0; i <CMD_NUM; i++) {
+			commands[i] = def[i];
+		}
+	}
+}
+
 void load_enemies(e_list& enemies,int enemy_num)
 {
 	enemies.clear();

+ 4 - 0
functions.hpp

@@ -68,6 +68,10 @@ void setup_level(int level, double& shootr, double& poweruprate, int& refresh_t,
 
 void change_commands(int* commands);
 
+void dump_commands(int* commands);
+
+void read_commands(int* commands);
+
 void load_enemies(e_list& enemies,int enemy_num);
 
 char playagain(WINDOW*);

+ 3 - 2
invaders.cpp

@@ -61,8 +61,9 @@ int main(int argc,char** argv)
 	int chflag=0;						//cheat flag: 0=normal, 1=cheats allowed, 2=special mode activated, 3=both special mode and cheats.
 	boss boss1;
 	WINDOW *Score,*BossHP;
-	int commands[CMD_NUM]={'a','d','w','s','p',' ','l','m','q'};	//0:left,1:right,2:up,3:down,4:pause,5:shoot1,6:shoot2,7:mute,8:quit
-	
+	int commands[CMD_NUM];
+	read_commands(commands);	//0:left,1:right,2:up,3:down,4:pause,5:shoot1,6:shoot2,7:mute,8:quit
+
 	//PARAMETERS/UTILITIES
 	double shootrate;				  	//probability of an enemy shooting a bomb 
 	double poweruprate;					//probability of a powerup being dropped