Adam Baker 201166301
Dependencies: mbed Gamepad N5110
main.cpp@29:b2f63d8c1268, 2019-04-22 (annotated)
- Committer:
- adambakerwa
- Date:
- Mon Apr 22 19:20:42 2019 +0000
- Revision:
- 29:b2f63d8c1268
- Parent:
- 26:3319ddd5111d
- Child:
- 31:bf765e3961f1
major work on levels
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adambakerwa | 1:a3cce487dffe | 1 | #include "mbed.h" |
adambakerwa | 1:a3cce487dffe | 2 | #include "Gamepad.h" |
adambakerwa | 1:a3cce487dffe | 3 | #include "N5110.h" |
adambakerwa | 1:a3cce487dffe | 4 | #include "Runner.h" |
adambakerwa | 6:cef9c2853315 | 5 | #include "Levels.h" |
adambakerwa | 9:ddeafaadf984 | 6 | #include "Animation.h" |
adambakerwa | 25:230e4163cb6b | 7 | #include "BlockheadEngine.h" |
adambakerwa | 1:a3cce487dffe | 8 | |
adambakerwa | 1:a3cce487dffe | 9 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
adambakerwa | 1:a3cce487dffe | 10 | Gamepad pad; |
adambakerwa | 25:230e4163cb6b | 11 | BlockheadEngine blockhead; |
adambakerwa | 13:35a459a33bbd | 12 | Ticker ticker; |
adambakerwa | 13:35a459a33bbd | 13 | |
adambakerwa | 13:35a459a33bbd | 14 | |
adambakerwa | 13:35a459a33bbd | 15 | volatile int timer_flag = 0; |
adambakerwa | 13:35a459a33bbd | 16 | |
adambakerwa | 13:35a459a33bbd | 17 | void timer_isr(); |
adambakerwa | 26:3319ddd5111d | 18 | void main_menu(N5110 &lcd, Gamepad &pad); |
adambakerwa | 26:3319ddd5111d | 19 | void print_main_menu(N5110 &lcd); |
adambakerwa | 26:3319ddd5111d | 20 | int user_input(Gamepad &pad); |
adambakerwa | 26:3319ddd5111d | 21 | void select_menu_item(N5110 &lcd, Gamepad &pad, int input); |
adambakerwa | 26:3319ddd5111d | 22 | void menu_playgame(N5110 &lcd, Gamepad &pad); |
adambakerwa | 26:3319ddd5111d | 23 | void menu_setcontrast(N5110 &lcd, Gamepad &pad); |
adambakerwa | 6:cef9c2853315 | 24 | |
adambakerwa | 13:35a459a33bbd | 25 | int main() |
adambakerwa | 13:35a459a33bbd | 26 | { |
adambakerwa | 13:35a459a33bbd | 27 | |
adambakerwa | 18:0a0976faedfb | 28 | int fps = 6; //6 |
adambakerwa | 22:8218092c2e4b | 29 | |
adambakerwa | 1:a3cce487dffe | 30 | pad.init(); |
adambakerwa | 1:a3cce487dffe | 31 | lcd.init(); |
adambakerwa | 17:ecbafe932c19 | 32 | lcd.setContrast(0.55); |
adambakerwa | 24:f5dd1f3db693 | 33 | |
adambakerwa | 13:35a459a33bbd | 34 | ticker.attach(&timer_isr,1.0f/fps); |
adambakerwa | 13:35a459a33bbd | 35 | |
adambakerwa | 26:3319ddd5111d | 36 | //int gameover = 0; |
adambakerwa | 13:35a459a33bbd | 37 | |
adambakerwa | 26:3319ddd5111d | 38 | main_menu(lcd, pad); |
adambakerwa | 13:35a459a33bbd | 39 | |
adambakerwa | 13:35a459a33bbd | 40 | |
adambakerwa | 13:35a459a33bbd | 41 | } |
adambakerwa | 13:35a459a33bbd | 42 | |
adambakerwa | 13:35a459a33bbd | 43 | void timer_isr() |
adambakerwa | 13:35a459a33bbd | 44 | { |
adambakerwa | 13:35a459a33bbd | 45 | |
adambakerwa | 13:35a459a33bbd | 46 | timer_flag = 1; // set flag in ISR |
adambakerwa | 1:a3cce487dffe | 47 | } |
adambakerwa | 1:a3cce487dffe | 48 | |
adambakerwa | 26:3319ddd5111d | 49 | void main_menu(N5110 &lcd, Gamepad &pad) |
adambakerwa | 26:3319ddd5111d | 50 | { |
adambakerwa | 26:3319ddd5111d | 51 | print_main_menu(lcd); |
adambakerwa | 26:3319ddd5111d | 52 | int input = user_input(pad); |
adambakerwa | 26:3319ddd5111d | 53 | select_menu_item(lcd, pad, input); |
adambakerwa | 26:3319ddd5111d | 54 | } |
adambakerwa | 26:3319ddd5111d | 55 | |
adambakerwa | 26:3319ddd5111d | 56 | void print_main_menu(N5110 &lcd) |
adambakerwa | 25:230e4163cb6b | 57 | { |
adambakerwa | 26:3319ddd5111d | 58 | lcd.clear(); |
adambakerwa | 26:3319ddd5111d | 59 | |
adambakerwa | 29:b2f63d8c1268 | 60 | lcd.printString("Menu",0,0); |
adambakerwa | 26:3319ddd5111d | 61 | lcd.printString("A for game",0,1); |
adambakerwa | 29:b2f63d8c1268 | 62 | lcd.printString("B for contrast",0,2); |
adambakerwa | 26:3319ddd5111d | 63 | lcd.refresh(); |
adambakerwa | 26:3319ddd5111d | 64 | |
adambakerwa | 26:3319ddd5111d | 65 | } |
adambakerwa | 26:3319ddd5111d | 66 | |
adambakerwa | 26:3319ddd5111d | 67 | |
adambakerwa | 26:3319ddd5111d | 68 | int user_input(Gamepad &pad) |
adambakerwa | 26:3319ddd5111d | 69 | { |
adambakerwa | 26:3319ddd5111d | 70 | int input = 0; |
adambakerwa | 26:3319ddd5111d | 71 | |
adambakerwa | 26:3319ddd5111d | 72 | do { |
adambakerwa | 26:3319ddd5111d | 73 | if (pad.check_event(Gamepad::A_PRESSED)) { |
adambakerwa | 26:3319ddd5111d | 74 | input = 1; |
adambakerwa | 26:3319ddd5111d | 75 | } else if (pad.check_event(Gamepad::B_PRESSED)) { |
adambakerwa | 26:3319ddd5111d | 76 | input = 2; |
adambakerwa | 26:3319ddd5111d | 77 | } |
adambakerwa | 26:3319ddd5111d | 78 | } while (input == 0); |
adambakerwa | 26:3319ddd5111d | 79 | |
adambakerwa | 25:230e4163cb6b | 80 | return input; |
adambakerwa | 25:230e4163cb6b | 81 | } |
adambakerwa | 25:230e4163cb6b | 82 | |
adambakerwa | 26:3319ddd5111d | 83 | void select_menu_item(N5110 &lcd, Gamepad &pad, int input) |
adambakerwa | 26:3319ddd5111d | 84 | { |
adambakerwa | 26:3319ddd5111d | 85 | switch (input) { |
adambakerwa | 26:3319ddd5111d | 86 | case 1: |
adambakerwa | 26:3319ddd5111d | 87 | menu_playgame(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 88 | break; |
adambakerwa | 26:3319ddd5111d | 89 | case 2: |
adambakerwa | 26:3319ddd5111d | 90 | menu_setcontrast(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 91 | break; |
adambakerwa | 26:3319ddd5111d | 92 | default: |
adambakerwa | 26:3319ddd5111d | 93 | exit(1); |
adambakerwa | 26:3319ddd5111d | 94 | break; |
adambakerwa | 26:3319ddd5111d | 95 | } |
adambakerwa | 25:230e4163cb6b | 96 | } |
adambakerwa | 25:230e4163cb6b | 97 | |
adambakerwa | 26:3319ddd5111d | 98 | void menu_playgame(N5110 &lcd, Gamepad &pad) |
adambakerwa | 26:3319ddd5111d | 99 | { |
adambakerwa | 26:3319ddd5111d | 100 | int gameover = 0; |
adambakerwa | 26:3319ddd5111d | 101 | |
adambakerwa | 26:3319ddd5111d | 102 | blockhead.init(); |
adambakerwa | 26:3319ddd5111d | 103 | |
adambakerwa | 26:3319ddd5111d | 104 | do { |
adambakerwa | 26:3319ddd5111d | 105 | |
adambakerwa | 26:3319ddd5111d | 106 | if (timer_flag == true) { |
adambakerwa | 26:3319ddd5111d | 107 | |
adambakerwa | 26:3319ddd5111d | 108 | timer_flag = 0; // if it has, clear the flag |
adambakerwa | 26:3319ddd5111d | 109 | |
adambakerwa | 26:3319ddd5111d | 110 | lcd.clear(); |
adambakerwa | 26:3319ddd5111d | 111 | |
adambakerwa | 26:3319ddd5111d | 112 | gameover = blockhead.playgame(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 113 | |
adambakerwa | 26:3319ddd5111d | 114 | lcd.refresh(); |
adambakerwa | 26:3319ddd5111d | 115 | |
adambakerwa | 26:3319ddd5111d | 116 | } else { |
adambakerwa | 26:3319ddd5111d | 117 | |
adambakerwa | 26:3319ddd5111d | 118 | sleep(); |
adambakerwa | 26:3319ddd5111d | 119 | |
adambakerwa | 26:3319ddd5111d | 120 | } |
adambakerwa | 26:3319ddd5111d | 121 | } while (gameover == 0); |
adambakerwa | 26:3319ddd5111d | 122 | |
adambakerwa | 26:3319ddd5111d | 123 | main_menu(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 124 | |
adambakerwa | 26:3319ddd5111d | 125 | |
adambakerwa | 26:3319ddd5111d | 126 | } |
adambakerwa | 26:3319ddd5111d | 127 | |
adambakerwa | 26:3319ddd5111d | 128 | void menu_setcontrast(N5110 &lcd, Gamepad &pad) |
adambakerwa | 25:230e4163cb6b | 129 | { |
adambakerwa | 25:230e4163cb6b | 130 | |
adambakerwa | 26:3319ddd5111d | 131 | int goback = 0; |
adambakerwa | 25:230e4163cb6b | 132 | |
adambakerwa | 26:3319ddd5111d | 133 | do { |
adambakerwa | 25:230e4163cb6b | 134 | |
adambakerwa | 26:3319ddd5111d | 135 | if (timer_flag == true) { |
adambakerwa | 26:3319ddd5111d | 136 | |
adambakerwa | 26:3319ddd5111d | 137 | timer_flag = 0; |
adambakerwa | 26:3319ddd5111d | 138 | |
adambakerwa | 26:3319ddd5111d | 139 | lcd.clear(); |
adambakerwa | 26:3319ddd5111d | 140 | |
adambakerwa | 29:b2f63d8c1268 | 141 | lcd.printString("contrast",0,0); |
adambakerwa | 26:3319ddd5111d | 142 | |
adambakerwa | 26:3319ddd5111d | 143 | lcd.refresh(); |
adambakerwa | 26:3319ddd5111d | 144 | |
adambakerwa | 29:b2f63d8c1268 | 145 | if (pad.check_event(Gamepad::B_PRESSED)) { |
adambakerwa | 26:3319ddd5111d | 146 | goback = 1; |
adambakerwa | 26:3319ddd5111d | 147 | } |
adambakerwa | 26:3319ddd5111d | 148 | |
adambakerwa | 26:3319ddd5111d | 149 | } else { |
adambakerwa | 26:3319ddd5111d | 150 | |
adambakerwa | 26:3319ddd5111d | 151 | sleep(); |
adambakerwa | 26:3319ddd5111d | 152 | |
adambakerwa | 26:3319ddd5111d | 153 | } |
adambakerwa | 26:3319ddd5111d | 154 | } while (goback == 0); |
adambakerwa | 26:3319ddd5111d | 155 | |
adambakerwa | 26:3319ddd5111d | 156 | main_menu(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 157 | } |
adambakerwa | 25:230e4163cb6b | 158 | |
adambakerwa | 25:230e4163cb6b | 159 | |
adambakerwa | 1:a3cce487dffe | 160 | |
adambakerwa | 13:35a459a33bbd | 161 | //rtos |
adambakerwa | 24:f5dd1f3db693 | 162 | /** |
adambakerwa | 24:f5dd1f3db693 | 163 | char buffer[14]; |
adambakerwa | 24:f5dd1f3db693 | 164 | sprintf(buffer,"%i", gameover); |
adambakerwa | 24:f5dd1f3db693 | 165 | lcd.printString(buffer,0,0); |
adambakerwa | 24:f5dd1f3db693 | 166 | */ |
adambakerwa | 26:3319ddd5111d | 167 | //NEED TO RESET POS CORDS EACH NEW LEVEL TO PREVENT UNNECERSARRY GAME OVER (have fun x) |