James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OptionsEngine.cpp Source File

OptionsEngine.cpp

00001 #include "OptionsEngine.h"
00002 
00003 //constructor
00004 OptionsEngine::OptionsEngine(){
00005 }
00006 
00007 //destructor
00008 OptionsEngine::~OptionsEngine(){
00009 }
00010 
00011 //initialiser
00012 void OptionsEngine::init(){
00013     _state = BRIGHTNESS;        //first item in menu
00014     _brightness = 0.5;          //medium brightness
00015     _ball_speed = 5;            // 5/10 sensitivity
00016 }
00017 
00018 void OptionsEngine::display_options(N5110 &lcd){
00019     lcd.printString("Options menu", 6, 0);      //text for each line of display
00020     lcd.printString("Brightness", 12, 2);
00021     lcd.printString("Ball speed", 12, 3);
00022     lcd.printString("High scores", 9, 4);
00023 }
00024 
00025 Option OptionsEngine::option_selection(Gamepad &gamepad, N5110 &lcd){
00026     OptionSelection fsm[3] = {      //finite state machine to power the menu
00027         {2,{HIGH_SCORES, BALL_SPEED, BRIGHTNESS}},    //output (e.g. 2,3,4) is line to print arrows on for user interface
00028         {3,{BRIGHTNESS, HIGH_SCORES, BALL_SPEED}},      //next_state[0] is the option above current one
00029         {4,{BALL_SPEED, BRIGHTNESS, HIGH_SCORES}}       //next_state[1] is the option below current one
00030     };                                                  //next_state[2] is current state
00031     if(gamepad.get_direction() == N){ _next_state = 0; }        //move arrows up
00032     else if(gamepad.get_direction() == S){ _next_state = 1; }   //move arrows down
00033     else{ _next_state = 2; }        //keep arrows the same (default)
00034     _state = fsm[_state].next_state[_next_state];       //calculate next state
00035     lcd.printChar('>', 0, fsm[_state].output);
00036     lcd.printChar('<', 78, fsm[_state].output);         //draw arrows
00037     return _state;
00038 }
00039 
00040 void OptionsEngine::change_brightness(Gamepad &gamepad, N5110 &lcd){
00041     while(!(gamepad.check_event(gamepad.A_PRESSED))){       //check for user selection
00042         lcd.clear();
00043         lcd.printString("Brightness", 12, 0);       //text to display on each line
00044         lcd.printString("Use L and R to", 0, 3);
00045         lcd.printString("change", 24, 4);
00046         lcd.printString("A = confirm", 9, 5);
00047         lcd.drawRect(10, 12, 63, 8, FILL_TRANSPARENT);      //outside of slider to display the brightness controller
00048         read_brightness_input(gamepad);
00049         for(int i = 0; i < _brightness*10; i ++){
00050             lcd.drawRect(12+6*i, 14, 5, 4, FILL_BLACK);     //draw small rectangles to represent the current brightness
00051         }
00052         lcd.setBrightness(_brightness);     //set brightness with N5110 class
00053         lcd.refresh();
00054         wait(0.2);      //avoid button bounce with large time between frames
00055     }
00056 }
00057 
00058 void OptionsEngine::read_brightness_input(Gamepad &gamepad){
00059     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).
00060     if(gamepad.check_event(gamepad.R_PRESSED)){ _brightness += 0.1f; }      //Otherwise 0.1 is implicitly converted to a double (giving warning messages).
00061     if(_brightness < 0){ _brightness = 0; }     //keep within range of 0 - 1
00062     if(_brightness > 1){ _brightness = 1; }
00063 }
00064 
00065 void OptionsEngine::change_ball_speed(Gamepad &gamepad, N5110 &lcd, Ball &ball){
00066     while(!(gamepad.check_event(gamepad.A_PRESSED))){       //check for user selection
00067         lcd.clear();
00068         lcd.printString("Ball Speed", 12, 0);           //text for each line of display
00069         lcd.printString("Use L and R to", 0, 3);
00070         lcd.printString("change", 24, 4);
00071         lcd.printString("A = confirm", 9, 5);
00072         lcd.drawRect(10, 12, 63, 8, FILL_TRANSPARENT);
00073         read_ball_speed_input(gamepad);                 //get values for sensitivity/ball speed
00074         for(int i = 0; i < _ball_speed; i ++){
00075             lcd.drawRect(12+6*i, 14, 5, 4, FILL_BLACK); //draw small rectangles in the same manner as brightness control
00076         }
00077         ball.set_ball_speed(_ball_speed);
00078         lcd.refresh();
00079         wait(0.2);      //avoid button bounce
00080     }
00081 }
00082 
00083 void OptionsEngine::read_ball_speed_input(Gamepad &gamepad){
00084     if(gamepad.check_event(gamepad.L_PRESSED)){ _ball_speed -= 1; }     //increment on R press, decrement on L press
00085     if(gamepad.check_event(gamepad.R_PRESSED)){ _ball_speed += 1; }
00086     if(_ball_speed < 0){ _ball_speed = 0; }     //keep within range 0 - 10
00087     if(_ball_speed > 10){ _ball_speed = 10; }
00088 }
00089 
00090 void OptionsEngine::view_high_scores(Gamepad &gamepad, N5110 &lcd){
00091     _leaderboard = CLASSIC_MODE;        //initialise to look at classic leaderboard first
00092     while(!(gamepad.check_event(gamepad.A_PRESSED))){       //check for user selection
00093         if(gamepad.check_event(gamepad.R_PRESSED)){ _leaderboard = BRICKBREAKER_MODE; }     //use L and R to flick between the two leaderboards
00094         if(gamepad.check_event(gamepad.L_PRESSED)){ _leaderboard = CLASSIC_MODE; }
00095         lcd.clear();
00096         print_high_scores(lcd);
00097         lcd.refresh();
00098         wait(0.2);
00099     }
00100 }
00101 
00102 void OptionsEngine::read_classic_high_scores(){
00103     FILE *fp;       //open file stream
00104     fp = fopen("/sd/classichighscores.txt", "r");       //classic high scores file in read mode
00105     
00106     if(fp == NULL){
00107         printf("Error: Could not open file");       //check open
00108     } else {
00109         int i = 0;
00110         rewind(fp);         //reset to start of file to start reading
00111         
00112         while(fscanf(fp, "%d,%f", &_classic_index[i], &_classic_values[i]) != EOF){     //read each index and value into arrays till end of file
00113             i++;        //increment counter
00114         }
00115         fclose(fp);         //close file stream
00116     }
00117 }
00118 
00119 void OptionsEngine::read_bb_high_scores(){
00120     FILE *fp;       //open file stream
00121     fp = fopen("/sd/bbhighscores.txt", "r");        //brickbreaker high scores in read mode
00122     
00123     if(fp == NULL){
00124         printf("Error: Could not open file");       //check open
00125     } else {
00126         int i = 0;
00127         rewind(fp);     //reset to start of file
00128         
00129         while(fscanf(fp, "%d,%f", &_bb_index[i], &_bb_values[i]) != EOF){       //read each index and value into array till end of file
00130             i++;        //increment counter
00131         }
00132         fclose(fp);     //close file stream
00133     }
00134 }
00135 
00136 void OptionsEngine::print_high_scores(N5110 &lcd){
00137     if(_leaderboard == CLASSIC_MODE){       //check which leaderboard to display
00138         read_classic_high_scores();
00139         lcd.printString("Classic", 21, 0);      //text for user interface with leaderboard
00140         lcd.printString("R>", 72, 3);
00141         char buffer[14];
00142         for(int i = 0; i < 5; i++){             //iterate for each of 5 high scores
00143             sprintf(buffer, "%d. %.0fs", _classic_index[i], _classic_values[i]);        //update the buffer for each line of high scores
00144             lcd.printString(buffer, 0, i + 1);                                          //print the respective high score to screen
00145         }   
00146     }
00147     if(_leaderboard == BRICKBREAKER_MODE){      //check which leaderboard to display
00148         read_bb_high_scores();
00149         lcd.printString("Brickbreak", 12, 0);       //text for user interface
00150         lcd.printString("<L", 72, 3);
00151         char buffer[14];
00152         for(int i = 0; i < 5; i++){             //iterate for each of 5 high scores
00153             sprintf(buffer, "%d. %.0f", _bb_index[i], _bb_values[i]);       //update buffer for each line of high scores
00154             lcd.printString(buffer, 0, i + 1);                              //print respective high score to screen
00155         }
00156     }
00157 }