Chen Zirui
/
ELEC2645_Project_el18zc
Chen Zirui Project 201235448
main.cpp@5:7207c9b70108, 2020-05-27 (annotated)
- Committer:
- ChenZirui
- Date:
- Wed May 27 21:13:59 2020 +0000
- Revision:
- 5:7207c9b70108
- Parent:
- 1:86da5130732b
- Child:
- 6:b393cfe4e0a7
problems
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ChenZirui | 5:7207c9b70108 | 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" |
ChenZirui | 5:7207c9b70108 | 5 | #include "Touch.h" |
ChenZirui | 5:7207c9b70108 | 6 | #ifdef WITH_TESTING |
ChenZirui | 5:7207c9b70108 | 7 | #include "test.h" |
ChenZirui | 5:7207c9b70108 | 8 | #endif |
eencae | 0:b7f1f47bb26a | 9 | |
ChenZirui | 5:7207c9b70108 | 10 | #define PADDLE_WIDTH 2 |
ChenZirui | 5:7207c9b70108 | 11 | #define PADDLE_HEIGHT 8 |
ChenZirui | 5:7207c9b70108 | 12 | #define BALL_SIZE 2 |
ChenZirui | 5:7207c9b70108 | 13 | #define BALL_SPEED 3 |
eencae | 0:b7f1f47bb26a | 14 | |
ChenZirui | 5:7207c9b70108 | 15 | /////////////// structs ///////////////// |
ChenZirui | 5:7207c9b70108 | 16 | struct UserInput { |
ChenZirui | 5:7207c9b70108 | 17 | Direction d; |
ChenZirui | 5:7207c9b70108 | 18 | float mag; |
ChenZirui | 5:7207c9b70108 | 19 | }; |
ChenZirui | 5:7207c9b70108 | 20 | /////////////// objects /////////////// |
ChenZirui | 5:7207c9b70108 | 21 | N5110 lcd; |
eencae | 0:b7f1f47bb26a | 22 | Gamepad pad; |
ChenZirui | 5:7207c9b70108 | 23 | Touch touch; |
eencae | 0:b7f1f47bb26a | 24 | |
ChenZirui | 5:7207c9b70108 | 25 | ///////////// prototypes /////////////// |
ChenZirui | 5:7207c9b70108 | 26 | void init(); |
ChenZirui | 5:7207c9b70108 | 27 | void update_game(UserInput input); |
ChenZirui | 5:7207c9b70108 | 28 | void render(); |
ChenZirui | 5:7207c9b70108 | 29 | void welcome(); |
ChenZirui | 5:7207c9b70108 | 30 | |
ChenZirui | 5:7207c9b70108 | 31 | ///////////// functions //////////////// |
eencae | 0:b7f1f47bb26a | 32 | int main() |
eencae | 0:b7f1f47bb26a | 33 | { |
ChenZirui | 5:7207c9b70108 | 34 | #ifdef WITH_TESTING |
ChenZirui | 5:7207c9b70108 | 35 | int number_of_failures = run_all_tests(); |
ChenZirui | 5:7207c9b70108 | 36 | |
ChenZirui | 5:7207c9b70108 | 37 | if(number_of_failures > 0) return number_of_failures; |
ChenZirui | 5:7207c9b70108 | 38 | #endif |
ChenZirui | 5:7207c9b70108 | 39 | int fps = 6; // frames per second |
ChenZirui | 5:7207c9b70108 | 40 | init(); // initialise and then display welcome screen... |
ChenZirui | 5:7207c9b70108 | 41 | welcome(); // waiting for the user to start; |
ChenZirui | 5:7207c9b70108 | 42 | render(); // first draw the initial frame |
ChenZirui | 5:7207c9b70108 | 43 | wait(1.0f/fps); // and wait for one frame period |
ChenZirui | 5:7207c9b70108 | 44 | |
ChenZirui | 5:7207c9b70108 | 45 | //lcd.drawRect(0,0,84,24,FILL_BLACK); |
ChenZirui | 5:7207c9b70108 | 46 | // game loop - read input, update the game state and render the display |
ChenZirui | 5:7207c9b70108 | 47 | while (1) { |
ChenZirui | 5:7207c9b70108 | 48 | touch.read_input(pad); |
ChenZirui | 5:7207c9b70108 | 49 | touch.update(pad,lcd); |
ChenZirui | 5:7207c9b70108 | 50 | render(); |
ChenZirui | 5:7207c9b70108 | 51 | //lcd.drawRect(0,0,84,24,FILL_BLACK); |
ChenZirui | 5:7207c9b70108 | 52 | wait(1.0f/fps); |
ChenZirui | 5:7207c9b70108 | 53 | } |
eencae | 0:b7f1f47bb26a | 54 | } |
eencae | 0:b7f1f47bb26a | 55 | |
ChenZirui | 5:7207c9b70108 | 56 | // initialies all classes and libraries |
ChenZirui | 5:7207c9b70108 | 57 | void init() |
ChenZirui | 5:7207c9b70108 | 58 | { |
ChenZirui | 5:7207c9b70108 | 59 | // need to initialise LCD and Gamepad |
ChenZirui | 5:7207c9b70108 | 60 | lcd.init(); |
ChenZirui | 5:7207c9b70108 | 61 | pad.init(); |
ChenZirui | 5:7207c9b70108 | 62 | |
ChenZirui | 5:7207c9b70108 | 63 | // initialise the game with correct ball and paddle sizes |
ChenZirui | 5:7207c9b70108 | 64 | pong.init(PADDLE_WIDTH,PADDLE_HEIGHT,BALL_SIZE,BALL_SPEED,lcd); |
ChenZirui | 5:7207c9b70108 | 65 | |
ChenZirui | 5:7207c9b70108 | 66 | } |
ChenZirui | 5:7207c9b70108 | 67 | |
ChenZirui | 5:7207c9b70108 | 68 | // this function draws each frame on the LCD |
ChenZirui | 5:7207c9b70108 | 69 | void render() |
ChenZirui | 5:7207c9b70108 | 70 | { |
ChenZirui | 5:7207c9b70108 | 71 | // clear screen, re-draw and refresh |
ChenZirui | 5:7207c9b70108 | 72 | lcd.clear(); |
ChenZirui | 5:7207c9b70108 | 73 | touch.draw(lcd); |
ChenZirui | 5:7207c9b70108 | 74 | lcd.refresh(); |
ChenZirui | 5:7207c9b70108 | 75 | } |
ChenZirui | 5:7207c9b70108 | 76 | |
ChenZirui | 5:7207c9b70108 | 77 | // simple splash screen displayed on start-up |
ChenZirui | 5:7207c9b70108 | 78 | void welcome() { |
ChenZirui | 5:7207c9b70108 | 79 | |
ChenZirui | 5:7207c9b70108 | 80 | lcd.printString(" touch! ",0,1); |
ChenZirui | 5:7207c9b70108 | 81 | lcd.printString(" Press Start ",0,4); |
ChenZirui | 5:7207c9b70108 | 82 | lcd.refresh(); |
ChenZirui | 5:7207c9b70108 | 83 | |
ChenZirui | 5:7207c9b70108 | 84 | // wait flashing LEDs until start button is pressed |
ChenZirui | 5:7207c9b70108 | 85 | while ( pad.start_pressed() == false) { |
ChenZirui | 5:7207c9b70108 | 86 | lcd.setContrast( pad.read_pot1()); |
ChenZirui | 5:7207c9b70108 | 87 | pad.leds_on(); |
ChenZirui | 5:7207c9b70108 | 88 | wait(0.1); |
ChenZirui | 5:7207c9b70108 | 89 | pad.leds_off(); |
ChenZirui | 5:7207c9b70108 | 90 | wait(0.1); |
ChenZirui | 5:7207c9b70108 | 91 | } |
ChenZirui | 5:7207c9b70108 | 92 | |
ChenZirui | 5:7207c9b70108 | 93 | } |