multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
main.cpp@27:fcc5fee18a24, 2020-11-23 (annotated)
- Committer:
- vsoltan
- Date:
- Mon Nov 23 01:19:17 2020 +0000
- Revision:
- 27:fcc5fee18a24
- Parent:
- 26:ebadab157abe
- Child:
- 28:a26a43cdaea8
fixed game over logic
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsoltan | 18:32fce82690a1 | 1 | |
donatien | 1:8e1d4987eb90 | 2 | #include "mbed.h" |
donatien | 1:8e1d4987eb90 | 3 | #include "EthernetInterface.h" |
vsoltan | 11:d0a105f6743f | 4 | #include "network.h" |
vsoltan | 15:9d90f68e53da | 5 | #include "gamestate.h" |
vsoltan | 16:7fd48cda0773 | 6 | #include "graphics.h" |
denizguler | 13:95d44f7855ca | 7 | #include "DebouncedInterrupt.h" |
vsoltan | 18:32fce82690a1 | 8 | #include "MbedJSONValue.h" |
vsoltan | 12:91affff3be75 | 9 | |
vsoltan | 26:ebadab157abe | 10 | #define DEBOUNCE 25 |
vsoltan | 12:91affff3be75 | 11 | |
denizguler | 13:95d44f7855ca | 12 | DebouncedInterrupt leftButton(p21); |
denizguler | 13:95d44f7855ca | 13 | DebouncedInterrupt middleButton(p22); |
denizguler | 13:95d44f7855ca | 14 | DebouncedInterrupt rightButton(p23); |
vsoltan | 12:91affff3be75 | 15 | |
vsoltan | 12:91affff3be75 | 16 | EthernetInterface eth; |
vsoltan | 12:91affff3be75 | 17 | UDPSocket sock; |
vsoltan | 12:91affff3be75 | 18 | Endpoint nist; |
vsoltan | 12:91affff3be75 | 19 | |
denizguler | 13:95d44f7855ca | 20 | volatile int count = 0; |
vsoltan | 14:6b20a930e1cb | 21 | volatile int sendFlag = 0; |
vsoltan | 14:6b20a930e1cb | 22 | volatile int moveData = 0; |
vsoltan | 17:32ae1f106002 | 23 | volatile int menuPress = 0; |
denizguler | 13:95d44f7855ca | 24 | |
vsoltan | 17:32ae1f106002 | 25 | // interrupts service routines |
vsoltan | 17:32ae1f106002 | 26 | |
vsoltan | 17:32ae1f106002 | 27 | void pressButtonMenu(void) { |
vsoltan | 17:32ae1f106002 | 28 | menuPress = 1; |
vsoltan | 17:32ae1f106002 | 29 | } |
vsoltan | 17:32ae1f106002 | 30 | |
vsoltan | 17:32ae1f106002 | 31 | void pressLeftGame( void ) { |
vsoltan | 14:6b20a930e1cb | 32 | sendFlag = 1; |
vsoltan | 26:ebadab157abe | 33 | moveData -= 5; |
vsoltan | 12:91affff3be75 | 34 | } |
vsoltan | 12:91affff3be75 | 35 | |
vsoltan | 17:32ae1f106002 | 36 | void pressRightGame() { |
vsoltan | 14:6b20a930e1cb | 37 | sendFlag = 1; |
vsoltan | 26:ebadab157abe | 38 | moveData += 5; |
vsoltan | 12:91affff3be75 | 39 | } |
emilmont | 6:25aad2d88749 | 40 | |
emilmont | 6:25aad2d88749 | 41 | int main() { |
vsoltan | 19:58cc5465f647 | 42 | initEthernet(ð, &sock, &nist); |
vsoltan | 17:32ae1f106002 | 43 | Graphics *gfx = new Graphics(); |
vsoltan | 19:58cc5465f647 | 44 | |
vsoltan | 17:32ae1f106002 | 45 | // attach ISR |
vsoltan | 17:32ae1f106002 | 46 | leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 17:32ae1f106002 | 47 | middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 17:32ae1f106002 | 48 | rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 12:91affff3be75 | 49 | |
vsoltan | 17:32ae1f106002 | 50 | MbedJSONValue serverResponse; |
vsoltan | 24:05eb0b0ab554 | 51 | MbedJSONValue serverRequest; |
vsoltan | 15:9d90f68e53da | 52 | |
vsoltan | 18:32fce82690a1 | 53 | char connectionRequest[] = "{\"type\": \"connected\"}"; |
vsoltan | 14:6b20a930e1cb | 54 | char readTo[256]; |
vsoltan | 14:6b20a930e1cb | 55 | |
vsoltan | 17:32ae1f106002 | 56 | gfx->renderLaunchScreen(); |
vsoltan | 15:9d90f68e53da | 57 | |
vsoltan | 17:32ae1f106002 | 58 | while (1) { // keep program running |
vsoltan | 17:32ae1f106002 | 59 | if (menuPress) { |
vsoltan | 17:32ae1f106002 | 60 | GameState *gs = new GameState(); |
vsoltan | 17:32ae1f106002 | 61 | |
vsoltan | 18:32fce82690a1 | 62 | // request an open lobby from the server |
vsoltan | 18:32fce82690a1 | 63 | sock.sendTo(nist, connectionRequest, sizeof(connectionRequest) - 1); |
vsoltan | 18:32fce82690a1 | 64 | |
vsoltan | 17:32ae1f106002 | 65 | // change ISRs to game controls |
vsoltan | 17:32ae1f106002 | 66 | leftButton.attach(&pressLeftGame, IRQ_RISE, DEBOUNCE); |
vsoltan | 27:fcc5fee18a24 | 67 | middleButton.reset(); |
vsoltan | 17:32ae1f106002 | 68 | rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE); |
vsoltan | 17:32ae1f106002 | 69 | |
vsoltan | 17:32ae1f106002 | 70 | while (!gs->done()) { |
vsoltan | 17:32ae1f106002 | 71 | if (sendFlag != 0) { |
vsoltan | 24:05eb0b0ab554 | 72 | serverRequest["type"] = "move"; |
vsoltan | 24:05eb0b0ab554 | 73 | serverRequest["hash"] = "jaredyeagersflipflop"; |
vsoltan | 24:05eb0b0ab554 | 74 | serverRequest["player"] = gs->getLocalPlayerNum(); |
vsoltan | 24:05eb0b0ab554 | 75 | serverRequest["delta"] = (int)moveData; |
vsoltan | 24:05eb0b0ab554 | 76 | |
vsoltan | 24:05eb0b0ab554 | 77 | string requestContent = serverRequest.serialize(); |
vsoltan | 24:05eb0b0ab554 | 78 | int len = requestContent.size(); |
vsoltan | 24:05eb0b0ab554 | 79 | char *toSend = (char *)requestContent.c_str(); |
vsoltan | 17:32ae1f106002 | 80 | sendFlag = 0; |
vsoltan | 17:32ae1f106002 | 81 | moveData = 0; |
vsoltan | 24:05eb0b0ab554 | 82 | sock.sendTo(nist, toSend, len); |
vsoltan | 17:32ae1f106002 | 83 | } |
vsoltan | 17:32ae1f106002 | 84 | int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo)); |
vsoltan | 17:32ae1f106002 | 85 | readTo[bytesRead] = 0; |
vsoltan | 17:32ae1f106002 | 86 | if (bytesRead > 0) { |
vsoltan | 27:fcc5fee18a24 | 87 | printf("data received: %s", readTo); |
vsoltan | 17:32ae1f106002 | 88 | parse(serverResponse, readTo); |
vsoltan | 27:fcc5fee18a24 | 89 | gs->updateAndRender(&serverResponse, gfx); |
vsoltan | 17:32ae1f106002 | 90 | } |
vsoltan | 17:32ae1f106002 | 91 | wait(.1); |
vsoltan | 15:9d90f68e53da | 92 | } |
vsoltan | 27:fcc5fee18a24 | 93 | MbedJSONValue dcReq; |
vsoltan | 27:fcc5fee18a24 | 94 | dcReq["hash"] = "jaredyeagersflipflop"; |
vsoltan | 27:fcc5fee18a24 | 95 | dcReq["type"] = "disconnect"; |
vsoltan | 27:fcc5fee18a24 | 96 | |
vsoltan | 27:fcc5fee18a24 | 97 | string dcReqContent = dcReq.serialize(); |
vsoltan | 27:fcc5fee18a24 | 98 | int len = dcReqContent.size(); |
vsoltan | 27:fcc5fee18a24 | 99 | char *dcPayload = (char *)dcReqContent.c_str(); |
vsoltan | 27:fcc5fee18a24 | 100 | |
vsoltan | 27:fcc5fee18a24 | 101 | sock.sendTo(nist, dcPayload, len); |
vsoltan | 27:fcc5fee18a24 | 102 | gfx->renderGameOver(gs); |
vsoltan | 27:fcc5fee18a24 | 103 | leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 27:fcc5fee18a24 | 104 | middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 27:fcc5fee18a24 | 105 | rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 27:fcc5fee18a24 | 106 | |
vsoltan | 17:32ae1f106002 | 107 | // reset the game |
vsoltan | 17:32ae1f106002 | 108 | menuPress = 0; |
vsoltan | 14:6b20a930e1cb | 109 | } |
vsoltan | 17:32ae1f106002 | 110 | wait(0.3); |
denizguler | 13:95d44f7855ca | 111 | } |
vsoltan | 15:9d90f68e53da | 112 | // cleanupEthernet(ð, &sock); |
donatien | 1:8e1d4987eb90 | 113 | } |