Dependencies:   mbed FATFileSystem

Committer:
ellisbhastroud
Date:
Mon Apr 08 15:10:28 2019 +0000
Revision:
4:035448357749
Parent:
3:a8960004d261
Child:
5:0b31909caf7f
Settings section of menu fully functioning.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ellisbhastroud 1:6179c2d67d19 1 #include "Menu.h"
ellisbhastroud 1:6179c2d67d19 2
ellisbhastroud 1:6179c2d67d19 3 // constructor
ellisbhastroud 1:6179c2d67d19 4
ellisbhastroud 1:6179c2d67d19 5 Menu::Menu()
ellisbhastroud 1:6179c2d67d19 6 {
ellisbhastroud 1:6179c2d67d19 7
ellisbhastroud 1:6179c2d67d19 8 }
ellisbhastroud 1:6179c2d67d19 9
ellisbhastroud 1:6179c2d67d19 10 //deconstructor
ellisbhastroud 1:6179c2d67d19 11
ellisbhastroud 1:6179c2d67d19 12 Menu::~Menu()
ellisbhastroud 1:6179c2d67d19 13 {
ellisbhastroud 1:6179c2d67d19 14
ellisbhastroud 1:6179c2d67d19 15 }
ellisbhastroud 1:6179c2d67d19 16
ellisbhastroud 1:6179c2d67d19 17 //public methods
ellisbhastroud 1:6179c2d67d19 18
ellisbhastroud 3:a8960004d261 19 void Menu::init()
ellisbhastroud 3:a8960004d261 20 {
ellisbhastroud 4:035448357749 21 _cursor_pos = 0; //cursor begins on position 0
ellisbhastroud 4:035448357749 22 _frame_rate = 30;
ellisbhastroud 4:035448357749 23 _contrast = 0.5f;
ellisbhastroud 4:035448357749 24 _brightness = 0.5f;
ellisbhastroud 3:a8960004d261 25
ellisbhastroud 3:a8960004d261 26 }
ellisbhastroud 3:a8960004d261 27 //prints welcome screen
ellisbhastroud 1:6179c2d67d19 28 void Menu::print_welcome(N5110 &lcd)
ellisbhastroud 1:6179c2d67d19 29 {
ellisbhastroud 1:6179c2d67d19 30 lcd.clear();
ellisbhastroud 1:6179c2d67d19 31 lcd.printString("Welcome to",0,0);
ellisbhastroud 1:6179c2d67d19 32 lcd.printString("GAME",0,1);
ellisbhastroud 1:6179c2d67d19 33 lcd.printString("by",0,2);
ellisbhastroud 1:6179c2d67d19 34 lcd.printString("Ellis Blackford Stroud",0,3);
ellisbhastroud 1:6179c2d67d19 35 lcd.refresh();
ellisbhastroud 3:a8960004d261 36 wait(1);
ellisbhastroud 3:a8960004d261 37 lcd.clear();
ellisbhastroud 3:a8960004d261 38 lcd.printString("Use joystick",0,1);
ellisbhastroud 3:a8960004d261 39 lcd.printString("and START/BACK",0,2);
ellisbhastroud 3:a8960004d261 40 lcd.printString("to navigate menu !",0,3);
ellisbhastroud 3:a8960004d261 41 lcd.refresh();
ellisbhastroud 3:a8960004d261 42 wait(1);
ellisbhastroud 1:6179c2d67d19 43 }
ellisbhastroud 3:a8960004d261 44 //prints main menu screen
ellisbhastroud 1:6179c2d67d19 45 void Menu::print_menu(N5110 &lcd)
ellisbhastroud 1:6179c2d67d19 46 {
ellisbhastroud 1:6179c2d67d19 47 lcd.clear();
ellisbhastroud 4:035448357749 48 lcd.printString("Start Game",10,1);
ellisbhastroud 4:035448357749 49 lcd.printString("Highscores",10,3);
ellisbhastroud 4:035448357749 50 lcd.printString("Settings",10,5);
ellisbhastroud 2:81cfa8310f55 51 }
ellisbhastroud 3:a8960004d261 52 //prints start screen and waits to start game
ellisbhastroud 3:a8960004d261 53 bool Menu::check_start(N5110 &lcd, Gamepad &pad)
ellisbhastroud 3:a8960004d261 54 {
ellisbhastroud 3:a8960004d261 55 lcd.clear();
ellisbhastroud 3:a8960004d261 56 lcd.printString("Press A ?",8,2);
ellisbhastroud 3:a8960004d261 57 lcd.printString("to start?",8,3);
ellisbhastroud 3:a8960004d261 58 lcd.refresh();
ellisbhastroud 4:035448357749 59 while(pad.check_event(Gamepad::A_PRESSED) == false) { //does nothing until a pressed
ellisbhastroud 3:a8960004d261 60 }
ellisbhastroud 3:a8960004d261 61 lcd.clear();
ellisbhastroud 4:035448357749 62 return true; //when true returned game begins
ellisbhastroud 3:a8960004d261 63 }
ellisbhastroud 3:a8960004d261 64 //prints highscores screen
ellisbhastroud 4:035448357749 65 void Menu::print_highscores(N5110 &lcd, Gamepad &pad)
ellisbhastroud 2:81cfa8310f55 66 {
ellisbhastroud 4:035448357749 67
ellisbhastroud 4:035448357749 68 while(pad.check_event(Gamepad::BACK_PRESSED) == false) {
ellisbhastroud 4:035448357749 69
ellisbhastroud 4:035448357749 70 lcd.clear();
ellisbhastroud 4:035448357749 71 lcd.printString("Highscores",0,0);
ellisbhastroud 4:035448357749 72 lcd.refresh();
ellisbhastroud 4:035448357749 73
ellisbhastroud 4:035448357749 74 }
ellisbhastroud 4:035448357749 75 print_menu(lcd);
ellisbhastroud 2:81cfa8310f55 76 }
ellisbhastroud 3:a8960004d261 77 //prints settings screen
ellisbhastroud 4:035448357749 78 void Menu::print_settings(N5110 &lcd, Gamepad &pad)
ellisbhastroud 2:81cfa8310f55 79 {
ellisbhastroud 4:035448357749 80
ellisbhastroud 4:035448357749 81 _cursor_pos = 0;
ellisbhastroud 4:035448357749 82
ellisbhastroud 2:81cfa8310f55 83 lcd.clear();
ellisbhastroud 4:035448357749 84 lcd.printString("Use pot",0,1);
ellisbhastroud 4:035448357749 85 lcd.printString("and joystick",0,2);
ellisbhastroud 4:035448357749 86 lcd.printString("to modify!",0,3);
ellisbhastroud 2:81cfa8310f55 87 lcd.refresh();
ellisbhastroud 4:035448357749 88 wait(1);
ellisbhastroud 4:035448357749 89 while(pad.check_event(Gamepad::BACK_PRESSED) == false) {
ellisbhastroud 4:035448357749 90
ellisbhastroud 4:035448357749 91 _joy_direction = pad.get_direction();
ellisbhastroud 4:035448357749 92 lcd.clear();
ellisbhastroud 4:035448357749 93 draw_settings(lcd, pad); //prints and controls settings parameters
ellisbhastroud 4:035448357749 94 move_cursor(lcd);
ellisbhastroud 4:035448357749 95 lcd.refresh();
ellisbhastroud 4:035448357749 96 wait(0.2);
ellisbhastroud 4:035448357749 97 }
ellisbhastroud 4:035448357749 98 print_menu(lcd);
ellisbhastroud 2:81cfa8310f55 99 }
ellisbhastroud 2:81cfa8310f55 100
ellisbhastroud 4:035448357749 101
ellisbhastroud 4:035448357749 102 void Menu::draw_settings(N5110 &lcd, Gamepad &pad)
ellisbhastroud 4:035448357749 103 {
ellisbhastroud 4:035448357749 104 _pot = pad.read_pot();
ellisbhastroud 4:035448357749 105 lcd.printString("LCD Contrast",0,0);
ellisbhastroud 4:035448357749 106 lcd.printString("LCD Brightness",0,2);
ellisbhastroud 4:035448357749 107 lcd.printString("Frame Rate",0,4);
ellisbhastroud 4:035448357749 108 lcd.drawRect(8,9,40,6,FILL_TRANSPARENT);
ellisbhastroud 4:035448357749 109 lcd.drawRect(8,25,40,6,FILL_TRANSPARENT);
ellisbhastroud 4:035448357749 110 lcd.drawRect(8,41,40,6,FILL_TRANSPARENT);
ellisbhastroud 4:035448357749 111 char buffer[14];
ellisbhastroud 4:035448357749 112 sprintf(buffer,"%.2f%",_contrast); //print contrast value to 2dp
ellisbhastroud 4:035448357749 113 lcd.printString(buffer,52,1);
ellisbhastroud 4:035448357749 114 sprintf(buffer,"%.2f%",_brightness); //print brightness value to 2dp
ellisbhastroud 4:035448357749 115 lcd.printString(buffer,52,3);
ellisbhastroud 4:035448357749 116 sprintf(buffer,"%ifps",_frame_rate); //prints integer value of frame rate
ellisbhastroud 4:035448357749 117 lcd.printString(buffer,52,5);
ellisbhastroud 4:035448357749 118
ellisbhastroud 4:035448357749 119 switch(_cursor_pos) {
ellisbhastroud 4:035448357749 120 case 0:
ellisbhastroud 4:035448357749 121 _contrast = _pot;
ellisbhastroud 4:035448357749 122 lcd.setContrast(_contrast);
ellisbhastroud 4:035448357749 123 lcd.drawRect(8,9,40*_contrast,6,FILL_BLACK);
ellisbhastroud 4:035448357749 124 lcd.drawRect(8,25,40*_brightness,6,FILL_BLACK);
ellisbhastroud 4:035448357749 125 lcd.drawRect(8,41,40*(_frame_rate-5.0f)/45.0f,6,FILL_BLACK);
ellisbhastroud 4:035448357749 126
ellisbhastroud 4:035448357749 127 break;
ellisbhastroud 4:035448357749 128 case 1:
ellisbhastroud 4:035448357749 129 _brightness = _pot;
ellisbhastroud 4:035448357749 130 lcd.setBrightness(_brightness);
ellisbhastroud 4:035448357749 131 lcd.drawRect(8,9,40*_contrast,6,FILL_BLACK);
ellisbhastroud 4:035448357749 132 lcd.drawRect(8,25,40*_brightness,6,FILL_BLACK);
ellisbhastroud 4:035448357749 133 lcd.drawRect(8,41,40*(_frame_rate-5.0f)/45.0f,6,FILL_BLACK);
ellisbhastroud 4:035448357749 134 break;
ellisbhastroud 4:035448357749 135 case 2:
ellisbhastroud 4:035448357749 136 _frame_rate = (_pot*45.0f)+5.0f; //scales 0-1 pot value to 5-50 fps
ellisbhastroud 4:035448357749 137 lcd.drawRect(8,9,40*_contrast,6,FILL_BLACK);
ellisbhastroud 4:035448357749 138 lcd.drawRect(8,25,40*_brightness,6,FILL_BLACK);
ellisbhastroud 4:035448357749 139 lcd.drawRect(8,41,40*(_frame_rate-5.0f)/45.0f,6,FILL_BLACK); //scales frame rate from 0-1 to draw box
ellisbhastroud 4:035448357749 140 break;
ellisbhastroud 4:035448357749 141 }
ellisbhastroud 4:035448357749 142 }
ellisbhastroud 3:a8960004d261 143 //changes menu screen using cursor position and returns true if begin start
ellisbhastroud 3:a8960004d261 144 bool Menu::menu_change(N5110 &lcd, Gamepad &pad)
ellisbhastroud 2:81cfa8310f55 145 {
ellisbhastroud 3:a8960004d261 146 int m = _cursor_pos;
ellisbhastroud 3:a8960004d261 147
ellisbhastroud 3:a8960004d261 148 switch(m) { //uses cursor position to select next screen
ellisbhastroud 3:a8960004d261 149 case 0:
ellisbhastroud 3:a8960004d261 150 //returns true if start game
ellisbhastroud 3:a8960004d261 151 {
ellisbhastroud 4:035448357749 152 bool start = check_start(lcd, pad); //waits until game start confirmed
ellisbhastroud 4:035448357749 153 return start; //causes exit of menu loop and game loop begins
ellisbhastroud 3:a8960004d261 154 }
ellisbhastroud 3:a8960004d261 155 case 1:
ellisbhastroud 4:035448357749 156 print_highscores(lcd, pad);
ellisbhastroud 3:a8960004d261 157 return false;
ellisbhastroud 3:a8960004d261 158 case 2:
ellisbhastroud 4:035448357749 159 print_settings(lcd, pad);
ellisbhastroud 3:a8960004d261 160 return false;
ellisbhastroud 3:a8960004d261 161 default: // default case
ellisbhastroud 3:a8960004d261 162 return false;
ellisbhastroud 3:a8960004d261 163 }
ellisbhastroud 3:a8960004d261 164 }
ellisbhastroud 3:a8960004d261 165
ellisbhastroud 3:a8960004d261 166 void Menu::menu_return(N5110 &lcd, Gamepad &pad)
ellisbhastroud 3:a8960004d261 167 {
ellisbhastroud 3:a8960004d261 168 while(pad.check_event(Gamepad::BACK_PRESSED) == false) {
ellisbhastroud 3:a8960004d261 169
ellisbhastroud 3:a8960004d261 170 }
ellisbhastroud 3:a8960004d261 171 print_menu(lcd);
ellisbhastroud 1:6179c2d67d19 172 }
ellisbhastroud 1:6179c2d67d19 173
ellisbhastroud 4:035448357749 174 //Uses joystick direction to select menu option and waits until start message
ellisbhastroud 3:a8960004d261 175 void Menu::menu_select(Gamepad &pad, N5110 &lcd)
ellisbhastroud 2:81cfa8310f55 176 {
ellisbhastroud 2:81cfa8310f55 177
ellisbhastroud 4:035448357749 178 while(pad.check_event(Gamepad::START_PRESSED) == false) { //loops until start pressed and choice selected
ellisbhastroud 3:a8960004d261 179
ellisbhastroud 4:035448357749 180 _joy_direction = pad.get_direction();
ellisbhastroud 4:035448357749 181 lcd.clear();
ellisbhastroud 4:035448357749 182 print_menu(lcd);
ellisbhastroud 4:035448357749 183 move_cursor(lcd);
ellisbhastroud 4:035448357749 184 lcd.refresh();
ellisbhastroud 3:a8960004d261 185 wait(0.2);
ellisbhastroud 3:a8960004d261 186 }
ellisbhastroud 2:81cfa8310f55 187
ellisbhastroud 3:a8960004d261 188 }
ellisbhastroud 3:a8960004d261 189
ellisbhastroud 4:035448357749 190 void Menu::set_frame_rate(int frame_rate)
ellisbhastroud 4:035448357749 191 {
ellisbhastroud 4:035448357749 192 _frame_rate = frame_rate;
ellisbhastroud 4:035448357749 193 }
ellisbhastroud 4:035448357749 194
ellisbhastroud 4:035448357749 195 void Menu::set_contrast(float contrast)
ellisbhastroud 4:035448357749 196 {
ellisbhastroud 4:035448357749 197 _contrast = contrast;
ellisbhastroud 4:035448357749 198 }
ellisbhastroud 4:035448357749 199
ellisbhastroud 4:035448357749 200 void Menu::set_brightness(float brightness)
ellisbhastroud 4:035448357749 201 {
ellisbhastroud 4:035448357749 202 _brightness = brightness;
ellisbhastroud 4:035448357749 203 }
ellisbhastroud 4:035448357749 204
ellisbhastroud 4:035448357749 205 int Menu::get_frame_rate() //returns frame rate 5-50
ellisbhastroud 4:035448357749 206 {
ellisbhastroud 4:035448357749 207 int frame_rate = _frame_rate;
ellisbhastroud 4:035448357749 208 return frame_rate;
ellisbhastroud 4:035448357749 209 }
ellisbhastroud 4:035448357749 210
ellisbhastroud 4:035448357749 211 float Menu::get_contrast() //returns contrast parameter 0-100
ellisbhastroud 4:035448357749 212 {
ellisbhastroud 4:035448357749 213 float contrast = _contrast;
ellisbhastroud 4:035448357749 214 return contrast;
ellisbhastroud 4:035448357749 215 }
ellisbhastroud 4:035448357749 216
ellisbhastroud 4:035448357749 217 float Menu::get_brightness() //returns brightness paramater 0-100
ellisbhastroud 4:035448357749 218 {
ellisbhastroud 4:035448357749 219 float brightness = _brightness;
ellisbhastroud 4:035448357749 220 return brightness;
ellisbhastroud 4:035448357749 221 }
ellisbhastroud 4:035448357749 222
ellisbhastroud 3:a8960004d261 223
ellisbhastroud 3:a8960004d261 224 //private methods
ellisbhastroud 3:a8960004d261 225
ellisbhastroud 3:a8960004d261 226 //draws cursor in necessary position
ellisbhastroud 3:a8960004d261 227 void Menu::draw_cursor(N5110 &lcd)
ellisbhastroud 3:a8960004d261 228 {
ellisbhastroud 4:035448357749 229
ellisbhastroud 4:035448357749 230 switch(_cursor_pos) {
ellisbhastroud 4:035448357749 231 case 0: //cursor on position 0
ellisbhastroud 4:035448357749 232 lcd.drawRect(0,9,6,6,FILL_TRANSPARENT);
ellisbhastroud 2:81cfa8310f55 233 break;
ellisbhastroud 4:035448357749 234 case 1: //cursor on position 1
ellisbhastroud 4:035448357749 235 lcd.drawRect(0,25,6,6,FILL_TRANSPARENT);
ellisbhastroud 2:81cfa8310f55 236 break;
ellisbhastroud 4:035448357749 237 case 2: //cursor on position 2
ellisbhastroud 4:035448357749 238 lcd.drawRect(0,41,6,6,FILL_TRANSPARENT);
ellisbhastroud 2:81cfa8310f55 239 break;
ellisbhastroud 3:a8960004d261 240 }
ellisbhastroud 4:035448357749 241 }
ellisbhastroud 4:035448357749 242
ellisbhastroud 4:035448357749 243 void Menu::move_cursor(N5110 &lcd)
ellisbhastroud 4:035448357749 244 {
ellisbhastroud 4:035448357749 245 switch(_cursor_pos) {
ellisbhastroud 4:035448357749 246 case 0: //cursor on position 0
ellisbhastroud 4:035448357749 247 draw_cursor(lcd);
ellisbhastroud 4:035448357749 248 switch(_joy_direction) {
ellisbhastroud 4:035448357749 249 case 0: //joystick centered
ellisbhastroud 4:035448357749 250 _cursor_pos = 0;
ellisbhastroud 4:035448357749 251 break;
ellisbhastroud 4:035448357749 252 case 1: //joystick up
ellisbhastroud 4:035448357749 253 _cursor_pos = 2;
ellisbhastroud 4:035448357749 254 break;
ellisbhastroud 4:035448357749 255 case 5: //joystick down
ellisbhastroud 4:035448357749 256 _cursor_pos = 1;
ellisbhastroud 4:035448357749 257 break;
ellisbhastroud 4:035448357749 258 }
ellisbhastroud 4:035448357749 259 break;
ellisbhastroud 4:035448357749 260 case 1: //cursor on position 1
ellisbhastroud 4:035448357749 261 draw_cursor(lcd);
ellisbhastroud 4:035448357749 262 switch(_joy_direction) {
ellisbhastroud 4:035448357749 263 case 0: //joystick centered
ellisbhastroud 4:035448357749 264 _cursor_pos = 1;
ellisbhastroud 4:035448357749 265 break;
ellisbhastroud 4:035448357749 266 case 1: //joystick up
ellisbhastroud 4:035448357749 267 _cursor_pos = 0;
ellisbhastroud 4:035448357749 268 break;
ellisbhastroud 4:035448357749 269 case 5: //joystick down
ellisbhastroud 4:035448357749 270 _cursor_pos = 2;
ellisbhastroud 4:035448357749 271 break;
ellisbhastroud 4:035448357749 272 }
ellisbhastroud 4:035448357749 273 break;
ellisbhastroud 4:035448357749 274 case 2: //cursor on position 2
ellisbhastroud 4:035448357749 275 draw_cursor(lcd);
ellisbhastroud 4:035448357749 276 switch(_joy_direction) {
ellisbhastroud 4:035448357749 277 case 0: //joystick centered
ellisbhastroud 4:035448357749 278 _cursor_pos = 2;
ellisbhastroud 4:035448357749 279 break;
ellisbhastroud 4:035448357749 280 case 1: //joystick up
ellisbhastroud 4:035448357749 281 _cursor_pos = 1;
ellisbhastroud 4:035448357749 282 break;
ellisbhastroud 4:035448357749 283 case 5: //joystick down
ellisbhastroud 4:035448357749 284 _cursor_pos = 0;
ellisbhastroud 4:035448357749 285 break;
ellisbhastroud 4:035448357749 286 }
ellisbhastroud 4:035448357749 287 break;
ellisbhastroud 4:035448357749 288 }
ellisbhastroud 4:035448357749 289 }