James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Committer:
JamesCummins
Date:
Thu May 09 01:09:18 2019 +0000
Revision:
38:a85bc227b907
Parent:
37:de1f584bce71
Child:
40:a1cdb6ab08af
Doxygen documentation written out and in line commenting completed. Game working and finished. Tests.h still to write out

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JamesCummins 22:4e305ff8a050 1 #include "OptionsEngine.h"
JamesCummins 22:4e305ff8a050 2
JamesCummins 38:a85bc227b907 3 //constructor
JamesCummins 22:4e305ff8a050 4 OptionsEngine::OptionsEngine(){
JamesCummins 22:4e305ff8a050 5 }
JamesCummins 22:4e305ff8a050 6
JamesCummins 38:a85bc227b907 7 //destructor
JamesCummins 22:4e305ff8a050 8 OptionsEngine::~OptionsEngine(){
JamesCummins 22:4e305ff8a050 9 }
JamesCummins 22:4e305ff8a050 10
JamesCummins 38:a85bc227b907 11 //initialiser
JamesCummins 22:4e305ff8a050 12 void OptionsEngine::init(){
JamesCummins 38:a85bc227b907 13 _state = BRIGHTNESS; //first item in menu
JamesCummins 38:a85bc227b907 14 _brightness = 0.5; //medium brightness
JamesCummins 38:a85bc227b907 15 _ball_speed = 5; // 5/10 sensitivity
JamesCummins 22:4e305ff8a050 16 }
JamesCummins 22:4e305ff8a050 17
JamesCummins 22:4e305ff8a050 18 void OptionsEngine::display_options(N5110 &lcd){
JamesCummins 38:a85bc227b907 19 lcd.printString("Options menu", 6, 0); //text for each line of display
JamesCummins 22:4e305ff8a050 20 lcd.printString("Brightness", 12, 2);
JamesCummins 22:4e305ff8a050 21 lcd.printString("Ball speed", 12, 3);
JamesCummins 22:4e305ff8a050 22 lcd.printString("High scores", 9, 4);
JamesCummins 22:4e305ff8a050 23 }
JamesCummins 22:4e305ff8a050 24
JamesCummins 22:4e305ff8a050 25 Option OptionsEngine::option_selection(Gamepad &gamepad, N5110 &lcd){
JamesCummins 38:a85bc227b907 26 OptionSelection fsm[3] = { //finite state machine to power the menu
JamesCummins 38:a85bc227b907 27 {2,{HIGH_SCORES, BALL_SPEED, BRIGHTNESS}}, //output (e.g. 2,3,4) is line to print arrows on for user interface
JamesCummins 38:a85bc227b907 28 {3,{BRIGHTNESS, HIGH_SCORES, BALL_SPEED}}, //next_state[0] is the option above current one
JamesCummins 38:a85bc227b907 29 {4,{BALL_SPEED, BRIGHTNESS, HIGH_SCORES}} //next_state[1] is the option below current one
JamesCummins 38:a85bc227b907 30 }; //next_state[2] is current state
JamesCummins 38:a85bc227b907 31 if(gamepad.get_direction() == N){ _next_state = 0; } //move arrows up
JamesCummins 38:a85bc227b907 32 else if(gamepad.get_direction() == S){ _next_state = 1; } //move arrows down
JamesCummins 38:a85bc227b907 33 else{ _next_state = 2; } //keep arrows the same (default)
JamesCummins 38:a85bc227b907 34 _state = fsm[_state].next_state[_next_state]; //calculate next state
JamesCummins 22:4e305ff8a050 35 lcd.printChar('>', 0, fsm[_state].output);
JamesCummins 38:a85bc227b907 36 lcd.printChar('<', 78, fsm[_state].output); //draw arrows
JamesCummins 22:4e305ff8a050 37 return _state;
JamesCummins 22:4e305ff8a050 38 }
JamesCummins 22:4e305ff8a050 39
JamesCummins 22:4e305ff8a050 40 void OptionsEngine::change_brightness(Gamepad &gamepad, N5110 &lcd){
JamesCummins 38:a85bc227b907 41 while(!(gamepad.check_event(gamepad.A_PRESSED))){ //check for user selection
JamesCummins 22:4e305ff8a050 42 lcd.clear();
JamesCummins 38:a85bc227b907 43 lcd.printString("Brightness", 12, 0); //text to display on each line
JamesCummins 22:4e305ff8a050 44 lcd.printString("Use L and R to", 0, 3);
JamesCummins 22:4e305ff8a050 45 lcd.printString("change", 24, 4);
JamesCummins 22:4e305ff8a050 46 lcd.printString("A = confirm", 9, 5);
JamesCummins 38:a85bc227b907 47 lcd.drawRect(10, 12, 63, 8, FILL_TRANSPARENT); //outside of slider to display the brightness controller
JamesCummins 22:4e305ff8a050 48 read_brightness_input(gamepad);
JamesCummins 22:4e305ff8a050 49 for(int i = 0; i < _brightness*10; i ++){
JamesCummins 38:a85bc227b907 50 lcd.drawRect(12+6*i, 14, 5, 4, FILL_BLACK); //draw small rectangles to represent the current brightness
JamesCummins 22:4e305ff8a050 51 }
JamesCummins 38:a85bc227b907 52 lcd.setBrightness(_brightness); //set brightness with N5110 class
JamesCummins 22:4e305ff8a050 53 lcd.refresh();
JamesCummins 38:a85bc227b907 54 wait(0.2); //avoid button bounce with large time between frames
JamesCummins 22:4e305ff8a050 55 }
JamesCummins 22:4e305ff8a050 56 }
JamesCummins 22:4e305ff8a050 57
JamesCummins 22:4e305ff8a050 58 void OptionsEngine::read_brightness_input(Gamepad &gamepad){
JamesCummins 23:61fa82f76808 59 if(gamepad.check_event(gamepad.L_PRESSED)){ _brightness -= 0.1f; } //Use of f to explicitly convert to a float (to fit declaration type in header file).
JamesCummins 22:4e305ff8a050 60 if(gamepad.check_event(gamepad.R_PRESSED)){ _brightness += 0.1f; } //Otherwise 0.1 is implicitly converted to a double (giving warning messages).
JamesCummins 38:a85bc227b907 61 if(_brightness < 0){ _brightness = 0; } //keep within range of 0 - 1
JamesCummins 22:4e305ff8a050 62 if(_brightness > 1){ _brightness = 1; }
JamesCummins 23:61fa82f76808 63 /*printf("Brightness = %f\n", _brightness);*/
JamesCummins 22:4e305ff8a050 64 }
JamesCummins 23:61fa82f76808 65
JamesCummins 23:61fa82f76808 66 void OptionsEngine::change_ball_speed(Gamepad &gamepad, N5110 &lcd, Ball &ball){
JamesCummins 38:a85bc227b907 67 while(!(gamepad.check_event(gamepad.A_PRESSED))){ //check for user selection
JamesCummins 23:61fa82f76808 68 lcd.clear();
JamesCummins 38:a85bc227b907 69 lcd.printString("Ball Speed", 12, 0); //text for each line of display
JamesCummins 23:61fa82f76808 70 lcd.printString("Use L and R to", 0, 3);
JamesCummins 23:61fa82f76808 71 lcd.printString("change", 24, 4);
JamesCummins 23:61fa82f76808 72 lcd.printString("A = confirm", 9, 5);
JamesCummins 23:61fa82f76808 73 lcd.drawRect(10, 12, 63, 8, FILL_TRANSPARENT);
JamesCummins 38:a85bc227b907 74 read_ball_speed_input(gamepad); //get values for sensitivity/ball speed
JamesCummins 23:61fa82f76808 75 for(int i = 0; i < _ball_speed; i ++){
JamesCummins 38:a85bc227b907 76 lcd.drawRect(12+6*i, 14, 5, 4, FILL_BLACK); //draw small rectangles in the same manner as brightness control
JamesCummins 23:61fa82f76808 77 }
JamesCummins 23:61fa82f76808 78 ball.set_ball_speed(_ball_speed);
JamesCummins 23:61fa82f76808 79 lcd.refresh();
JamesCummins 38:a85bc227b907 80 wait(0.2); //avoid button bounce
JamesCummins 23:61fa82f76808 81 }
JamesCummins 23:61fa82f76808 82 }
JamesCummins 23:61fa82f76808 83
JamesCummins 23:61fa82f76808 84 void OptionsEngine::read_ball_speed_input(Gamepad &gamepad){
JamesCummins 38:a85bc227b907 85 if(gamepad.check_event(gamepad.L_PRESSED)){ _ball_speed -= 1; } //increment on R press, decrement on L press
JamesCummins 23:61fa82f76808 86 if(gamepad.check_event(gamepad.R_PRESSED)){ _ball_speed += 1; }
JamesCummins 38:a85bc227b907 87 if(_ball_speed < 0){ _ball_speed = 0; } //keep within range 0 - 10
JamesCummins 23:61fa82f76808 88 if(_ball_speed > 10){ _ball_speed = 10; }
JamesCummins 24:c6415cc74b17 89 }
JamesCummins 24:c6415cc74b17 90
JamesCummins 26:0dc10374546f 91 void OptionsEngine::view_high_scores(Gamepad &gamepad, N5110 &lcd){
JamesCummins 38:a85bc227b907 92 _leaderboard = CLASSIC_MODE; //initialise to look at classic leaderboard first
JamesCummins 38:a85bc227b907 93 while(!(gamepad.check_event(gamepad.A_PRESSED))){ //check for user selection
JamesCummins 38:a85bc227b907 94 if(gamepad.check_event(gamepad.R_PRESSED)){ _leaderboard = BRICKBREAKER_MODE; } //use L and R to flick between the two leaderboards
JamesCummins 24:c6415cc74b17 95 if(gamepad.check_event(gamepad.L_PRESSED)){ _leaderboard = CLASSIC_MODE; }
JamesCummins 24:c6415cc74b17 96 lcd.clear();
JamesCummins 26:0dc10374546f 97 print_high_scores(lcd);
JamesCummins 24:c6415cc74b17 98 lcd.refresh();
JamesCummins 24:c6415cc74b17 99 wait(0.2);
JamesCummins 24:c6415cc74b17 100 }
JamesCummins 24:c6415cc74b17 101 }
JamesCummins 24:c6415cc74b17 102
JamesCummins 35:138ad0faa42b 103 void OptionsEngine::read_classic_high_scores(){
JamesCummins 38:a85bc227b907 104 FILE *fp; //open file stream
JamesCummins 38:a85bc227b907 105 fp = fopen("/sd/classichighscores.txt", "r"); //classic high scores file in read mode
JamesCummins 35:138ad0faa42b 106
JamesCummins 35:138ad0faa42b 107 if(fp == NULL){
JamesCummins 38:a85bc227b907 108 printf("Error: Could not open file"); //check open
JamesCummins 35:138ad0faa42b 109 } else {
JamesCummins 35:138ad0faa42b 110 int i = 0;
JamesCummins 38:a85bc227b907 111 rewind(fp); //reset to start of file to start reading
JamesCummins 35:138ad0faa42b 112
JamesCummins 38:a85bc227b907 113 while(fscanf(fp, "%d,%f", &_classic_index[i], &_classic_values[i]) != EOF){ //read each index and value into arrays till end of file
JamesCummins 38:a85bc227b907 114 i++; //increment counter
JamesCummins 35:138ad0faa42b 115 }
JamesCummins 38:a85bc227b907 116 fclose(fp); //close file stream
JamesCummins 26:0dc10374546f 117 }
JamesCummins 35:138ad0faa42b 118 }
JamesCummins 35:138ad0faa42b 119
JamesCummins 35:138ad0faa42b 120 void OptionsEngine::read_bb_high_scores(){
JamesCummins 38:a85bc227b907 121 FILE *fp; //open file stream
JamesCummins 38:a85bc227b907 122 fp = fopen("/sd/bbhighscores.txt", "r"); //brickbreaker high scores in read mode
JamesCummins 35:138ad0faa42b 123
JamesCummins 35:138ad0faa42b 124 if(fp == NULL){
JamesCummins 38:a85bc227b907 125 printf("Error: Could not open file"); //check open
JamesCummins 35:138ad0faa42b 126 } else {
JamesCummins 35:138ad0faa42b 127 int i = 0;
JamesCummins 38:a85bc227b907 128 rewind(fp); //reset to start of file
JamesCummins 35:138ad0faa42b 129
JamesCummins 38:a85bc227b907 130 while(fscanf(fp, "%d,%f", &_bb_index[i], &_bb_values[i]) != EOF){ //read each index and value into array till end of file
JamesCummins 38:a85bc227b907 131 i++; //increment counter
JamesCummins 35:138ad0faa42b 132 }
JamesCummins 38:a85bc227b907 133 fclose(fp); //close file stream
JamesCummins 26:0dc10374546f 134 }
JamesCummins 26:0dc10374546f 135 }
JamesCummins 26:0dc10374546f 136
JamesCummins 26:0dc10374546f 137 void OptionsEngine::print_high_scores(N5110 &lcd){
JamesCummins 38:a85bc227b907 138 if(_leaderboard == CLASSIC_MODE){ //check which leaderboard to display
JamesCummins 35:138ad0faa42b 139 read_classic_high_scores();
JamesCummins 38:a85bc227b907 140 lcd.printString("Classic", 21, 0); //text for user interface with leaderboard
JamesCummins 35:138ad0faa42b 141 lcd.printString("R>", 72, 3);
JamesCummins 35:138ad0faa42b 142 char buffer[14];
JamesCummins 38:a85bc227b907 143 for(int i = 0; i < 5; i++){ //iterate for each of 5 high scores
JamesCummins 38:a85bc227b907 144 sprintf(buffer, "%d. %.0fs", _classic_index[i], _classic_values[i]); //update the buffer for each line of high scores
JamesCummins 38:a85bc227b907 145 lcd.printString(buffer, 0, i + 1); //print the respective high score to screen
JamesCummins 35:138ad0faa42b 146 }
JamesCummins 24:c6415cc74b17 147 }
JamesCummins 38:a85bc227b907 148 if(_leaderboard == BRICKBREAKER_MODE){ //check which leaderboard to display
JamesCummins 26:0dc10374546f 149 read_bb_high_scores();
JamesCummins 38:a85bc227b907 150 lcd.printString("Brickbreak", 12, 0); //text for user interface
JamesCummins 35:138ad0faa42b 151 lcd.printString("<L", 72, 3);
JamesCummins 35:138ad0faa42b 152 char buffer[14];
JamesCummins 38:a85bc227b907 153 for(int i = 0; i < 5; i++){ //iterate for each of 5 high scores
JamesCummins 38:a85bc227b907 154 sprintf(buffer, "%d. %.0f", _bb_index[i], _bb_values[i]); //update buffer for each line of high scores
JamesCummins 38:a85bc227b907 155 lcd.printString(buffer, 0, i + 1); //print respective high score to screen
JamesCummins 26:0dc10374546f 156 }
JamesCummins 24:c6415cc74b17 157 }
JamesCummins 23:61fa82f76808 158 }