Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Options_Engine/OptionsEngine.cpp
- Committer:
- JamesCummins
- Date:
- 2019-04-23
- Revision:
- 26:0dc10374546f
- Parent:
- 25:b52aa23df120
- Child:
- 35:138ad0faa42b
File content as of revision 26:0dc10374546f:
#include "OptionsEngine.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <functional>
OptionsEngine::OptionsEngine(){
}
OptionsEngine::~OptionsEngine(){
}
void OptionsEngine::init(){
_state = BRIGHTNESS;
_brightness = 0.5;
_ball_speed = 5;
}
void OptionsEngine::display_options(N5110 &lcd){
lcd.printString("Options menu", 6, 0);
lcd.printString("Brightness", 12, 2);
lcd.printString("Ball speed", 12, 3);
lcd.printString("High scores", 9, 4);
}
Option OptionsEngine::option_selection(Gamepad &gamepad, N5110 &lcd){
OptionSelection fsm[3] = {
{2,{HIGH_SCORES, BALL_SPEED, BRIGHTNESS}},
{3,{BRIGHTNESS, HIGH_SCORES, BALL_SPEED}},
{4,{BALL_SPEED, BRIGHTNESS, HIGH_SCORES}}
};
if(gamepad.get_direction() == N){ _next_state = 0; }
else if(gamepad.get_direction() == S){ _next_state = 1; }
else{ _next_state = 2; }
_state = fsm[_state].next_state[_next_state];
lcd.printChar('>', 0, fsm[_state].output);
lcd.printChar('<', 78, fsm[_state].output);
return _state;
}
void OptionsEngine::change_brightness(Gamepad &gamepad, N5110 &lcd){
while(!(gamepad.check_event(gamepad.A_PRESSED))){
lcd.clear();
lcd.printString("Brightness", 12, 0);
lcd.printString("Use L and R to", 0, 3);
lcd.printString("change", 24, 4);
lcd.printString("A = confirm", 9, 5);
lcd.drawRect(10, 12, 63, 8, FILL_TRANSPARENT);
read_brightness_input(gamepad);
for(int i = 0; i < _brightness*10; i ++){
lcd.drawRect(12+6*i, 14, 5, 4, FILL_BLACK);
}
lcd.setBrightness(_brightness);
lcd.refresh();
wait(0.2);
}
}
void OptionsEngine::read_brightness_input(Gamepad &gamepad){
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).
if(gamepad.check_event(gamepad.R_PRESSED)){ _brightness += 0.1f; } //Otherwise 0.1 is implicitly converted to a double (giving warning messages).
if(_brightness < 0){ _brightness = 0; }
if(_brightness > 1){ _brightness = 1; }
/*printf("Brightness = %f\n", _brightness);*/
}
void OptionsEngine::change_ball_speed(Gamepad &gamepad, N5110 &lcd, Ball &ball){
while(!(gamepad.check_event(gamepad.A_PRESSED))){
lcd.clear();
lcd.printString("Ball Speed", 12, 0);
lcd.printString("Use L and R to", 0, 3);
lcd.printString("change", 24, 4);
lcd.printString("A = confirm", 9, 5);
lcd.drawRect(10, 12, 63, 8, FILL_TRANSPARENT);
read_ball_speed_input(gamepad);
for(int i = 0; i < _ball_speed; i ++){
lcd.drawRect(12+6*i, 14, 5, 4, FILL_BLACK);
}
ball.set_ball_speed(_ball_speed);
lcd.refresh();
wait(0.2);
}
}
void OptionsEngine::read_ball_speed_input(Gamepad &gamepad){
if(gamepad.check_event(gamepad.L_PRESSED)){ _ball_speed -= 1; }
if(gamepad.check_event(gamepad.R_PRESSED)){ _ball_speed += 1; }
if(_ball_speed < 0){ _ball_speed = 0; }
if(_ball_speed > 10){ _ball_speed = 10; }
}
void OptionsEngine::view_high_scores(Gamepad &gamepad, N5110 &lcd){
_leaderboard = CLASSIC_MODE;
while(!(gamepad.check_event(gamepad.A_PRESSED))){
if(gamepad.check_event(gamepad.R_PRESSED)){ _leaderboard = BRICKBREAKER_MODE; }
if(gamepad.check_event(gamepad.L_PRESSED)){ _leaderboard = CLASSIC_MODE; }
lcd.clear();
print_high_scores(lcd);
lcd.refresh();
wait(0.2);
}
}
void OptionsEngine::read_bb_high_scores(){
std::ifstream file;
file.open("/sd/bbhighscores.txt");
if(!(file.is_open())){
printf("Error: Could not open file");
}
std::string nth_score;
for(int n = 0; n < 5; n++){
std::getline(file, nth_score);
std::istringstream ipstream(nth_score);
ipstream >> _bbarray[n];
}
}
void OptionsEngine::print_high_scores(N5110 &lcd){
if(_leaderboard == CLASSIC_MODE){
lcd.printString("Classic", 21, 0);
lcd.printString("1.", 0, 1);
lcd.printString("2.", 0, 2);
lcd.printString("3.", 0, 3); lcd.printString("R>", 72, 3);
lcd.printString("4.", 0, 4);
lcd.printString("5.", 0, 5);
}
if(_leaderboard == BRICKBREAKER_MODE){
read_bb_high_scores();
lcd.printString("Brickbreak", 12, 0);
for (int i = 1; i < 6; i++){
char buffer[6];
sprintf(buffer, "%1d. %2d", i, _bbarray[i-1]);
lcd.printString(buffer, 0, i);
}
lcd.printString("<L", 72, 3);
}
}