
Lewis Cheadle 201245660
Dependencies: mbed
main.cpp@13:fd290d2fd917, 2020-05-26 (annotated)
- Committer:
- ll17lrc
- Date:
- Tue May 26 22:41:23 2020 +0000
- Revision:
- 13:fd290d2fd917
- Parent:
- 8:10eb578dd754
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ll17lrc | 2:823dea76ff2e | 1 | ///////// pre-processor directives //////// |
eencae | 0:b7f1f47bb26a | 2 | #include "mbed.h" |
eencae | 0:b7f1f47bb26a | 3 | #include "Gamepad.h" |
eencae | 0:b7f1f47bb26a | 4 | #include "N5110.h" |
ll17lrc | 2:823dea76ff2e | 5 | #include "ImpossEngine.h" |
ll17lrc | 2:823dea76ff2e | 6 | |
eencae | 0:b7f1f47bb26a | 7 | |
eencae | 0:b7f1f47bb26a | 8 | |
ll17lrc | 2:823dea76ff2e | 9 | /////////////// structs ///////////////// |
ll17lrc | 2:823dea76ff2e | 10 | struct UserInput { |
ll17lrc | 2:823dea76ff2e | 11 | Direction d; |
ll17lrc | 2:823dea76ff2e | 12 | float mag; |
ll17lrc | 2:823dea76ff2e | 13 | }; |
ll17lrc | 2:823dea76ff2e | 14 | /////////////// objects /////////////// |
ll17lrc | 2:823dea76ff2e | 15 | N5110 lcd; |
eencae | 0:b7f1f47bb26a | 16 | Gamepad pad; |
ll17lrc | 4:a9d5fca3b7ba | 17 | ImpossEngine imposs; |
ll17lrc | 4:a9d5fca3b7ba | 18 | int level; |
ll17lrc | 4:a9d5fca3b7ba | 19 | int ball_x_pos; |
ll17lrc | 4:a9d5fca3b7ba | 20 | int ball_y_pos; |
ll17lrc | 2:823dea76ff2e | 21 | |
ll17lrc | 2:823dea76ff2e | 22 | ///////////// prototypes /////////////// |
ll17lrc | 2:823dea76ff2e | 23 | void init(); |
ll17lrc | 2:823dea76ff2e | 24 | void update_game(UserInput input); |
ll17lrc | 2:823dea76ff2e | 25 | void render(); |
ll17lrc | 2:823dea76ff2e | 26 | void welcome(); |
ll17lrc | 2:823dea76ff2e | 27 | void start_menu(); |
ll17lrc | 2:823dea76ff2e | 28 | |
ll17lrc | 2:823dea76ff2e | 29 | ///////////// functions //////////////// |
eencae | 0:b7f1f47bb26a | 30 | |
eencae | 0:b7f1f47bb26a | 31 | int main() |
eencae | 0:b7f1f47bb26a | 32 | { |
ll17lrc | 2:823dea76ff2e | 33 | |
ll17lrc | 2:823dea76ff2e | 34 | int fps = 6; // frames per second |
ll17lrc | 2:823dea76ff2e | 35 | |
ll17lrc | 2:823dea76ff2e | 36 | init(); // initialise and then display welcome screen... |
ll17lrc | 2:823dea76ff2e | 37 | welcome(); // waiting for the user to start |
ll17lrc | 4:a9d5fca3b7ba | 38 | imposs.complete(pad,lcd); // brings up start menu for user to select option |
ll17lrc | 2:823dea76ff2e | 39 | render(); // first draw the initial frame |
ll17lrc | 2:823dea76ff2e | 40 | wait(1.0f/fps); // and wait for one frame period |
ll17lrc | 2:823dea76ff2e | 41 | |
ll17lrc | 2:823dea76ff2e | 42 | |
ll17lrc | 2:823dea76ff2e | 43 | // game loop - read input, update the game state and render the display |
ll17lrc | 2:823dea76ff2e | 44 | while (1) { |
ll17lrc | 2:823dea76ff2e | 45 | imposs.read_input(pad); |
ll17lrc | 7:35465b3bf586 | 46 | imposs.update(pad,lcd); |
ll17lrc | 2:823dea76ff2e | 47 | render(); |
ll17lrc | 2:823dea76ff2e | 48 | wait(1.0f/fps); |
ll17lrc | 2:823dea76ff2e | 49 | } |
eencae | 0:b7f1f47bb26a | 50 | } |
eencae | 0:b7f1f47bb26a | 51 | |
ll17lrc | 2:823dea76ff2e | 52 | |
ll17lrc | 2:823dea76ff2e | 53 | // this function draws each frame on the LCD |
ll17lrc | 2:823dea76ff2e | 54 | void render() |
ll17lrc | 2:823dea76ff2e | 55 | { |
ll17lrc | 2:823dea76ff2e | 56 | // clear screen, re-draw and refresh |
ll17lrc | 2:823dea76ff2e | 57 | lcd.clear(); |
ll17lrc | 8:10eb578dd754 | 58 | imposs.draw(lcd,pad); |
ll17lrc | 2:823dea76ff2e | 59 | lcd.refresh(); |
ll17lrc | 2:823dea76ff2e | 60 | } |
ll17lrc | 2:823dea76ff2e | 61 | |
ll17lrc | 2:823dea76ff2e | 62 | // simple splash screen displayed on start-up |
ll17lrc | 2:823dea76ff2e | 63 | void welcome() { |
ll17lrc | 2:823dea76ff2e | 64 | |
ll17lrc | 2:823dea76ff2e | 65 | lcd.printString(" Welcome to ",0,1); |
ll17lrc | 2:823dea76ff2e | 66 | lcd.printString(" Impossible ",0,2); |
ll17lrc | 2:823dea76ff2e | 67 | lcd.printString(" Press Start ",0,4); |
ll17lrc | 2:823dea76ff2e | 68 | lcd.refresh(); |
ll17lrc | 2:823dea76ff2e | 69 | |
ll17lrc | 2:823dea76ff2e | 70 | // wait flashing LEDs until start button is pressed |
ll17lrc | 2:823dea76ff2e | 71 | while ( pad.start_pressed() == false) { |
ll17lrc | 2:823dea76ff2e | 72 | lcd.setContrast( pad.read_pot1()); |
ll17lrc | 2:823dea76ff2e | 73 | pad.leds_on(); |
ll17lrc | 2:823dea76ff2e | 74 | wait(0.2); |
ll17lrc | 2:823dea76ff2e | 75 | pad.leds_off(); |
ll17lrc | 2:823dea76ff2e | 76 | wait(0.2); |
ll17lrc | 2:823dea76ff2e | 77 | } |
ll17lrc | 2:823dea76ff2e | 78 | |
ll17lrc | 2:823dea76ff2e | 79 | } |
ll17lrc | 2:823dea76ff2e | 80 | |
ll17lrc | 2:823dea76ff2e | 81 | |
ll17lrc | 2:823dea76ff2e | 82 | |
ll17lrc | 3:4494e6928194 | 83 | |
ll17lrc | 3:4494e6928194 | 84 | void init() |
ll17lrc | 3:4494e6928194 | 85 | { |
ll17lrc | 3:4494e6928194 | 86 | // need to initialise LCD and Gamepad |
ll17lrc | 3:4494e6928194 | 87 | lcd.init(); |
ll17lrc | 3:4494e6928194 | 88 | pad.init(); |
ll17lrc | 3:4494e6928194 | 89 | |
ll17lrc | 2:823dea76ff2e | 90 | } |