multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
graphics.cpp@34:b19f39ad74ac, 2020-11-28 (annotated)
- Committer:
- vsoltan
- Date:
- Sat Nov 28 03:52:32 2020 +0000
- Revision:
- 34:b19f39ad74ac
- Parent:
- 33:98ba7b0b8c2b
- Child:
- 36:46bb54b669bc
issues with reconnecting...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsoltan | 16:7fd48cda0773 | 1 | |
vsoltan | 16:7fd48cda0773 | 2 | #include "graphics.h" |
vsoltan | 16:7fd48cda0773 | 3 | |
vsoltan | 16:7fd48cda0773 | 4 | Graphics::Graphics() { |
vsoltan | 17:32ae1f106002 | 5 | printf("\n\rInitializing Graphics: initializing TFT\n\r"); |
vsoltan | 16:7fd48cda0773 | 6 | this->tft = new Adafruit_ST7735(P_MOSI, P_MISO, P_SOCK, P_CS, P_RS, P_DC); |
vsoltan | 17:32ae1f106002 | 7 | printf("Initializing Graphics: optimizing display for resolution\n\r"); |
vsoltan | 17:32ae1f106002 | 8 | tft->initR(INITR_144GREENTAB); |
vsoltan | 17:32ae1f106002 | 9 | tft->setTextColor(ST7735_WHITE); |
vsoltan | 16:7fd48cda0773 | 10 | } |
vsoltan | 16:7fd48cda0773 | 11 | |
vsoltan | 16:7fd48cda0773 | 12 | void Graphics::renderLaunchScreen() { |
vsoltan | 22:1c49e1fae846 | 13 | tft->fillScreen(LAUNCH_SCREEN_COLOR); |
vsoltan | 29:4708bfb863cb | 14 | tft->setTextCursor(18, 20); |
vsoltan | 17:32ae1f106002 | 15 | tft->printf("%s", "Multiplayer Pong"); |
vsoltan | 29:4708bfb863cb | 16 | tft->setTextCursor(15, 60); |
vsoltan | 22:1c49e1fae846 | 17 | tft->printf("%s", "press any button"); |
vsoltan | 29:4708bfb863cb | 18 | tft->setTextCursor(15, 70); |
vsoltan | 22:1c49e1fae846 | 19 | tft->printf("%s", "to continue"); |
vsoltan | 17:32ae1f106002 | 20 | } |
vsoltan | 17:32ae1f106002 | 21 | |
vsoltan | 18:32fce82690a1 | 22 | void Graphics::renderWaitingRoom() { |
vsoltan | 22:1c49e1fae846 | 23 | tft->fillScreen(WAITING_SCREEN_COLOR); |
vsoltan | 29:4708bfb863cb | 24 | tft->setTextCursor(18, 20); |
vsoltan | 29:4708bfb863cb | 25 | tft->printf("%s", "Waiting For"); |
vsoltan | 29:4708bfb863cb | 26 | tft->setTextCursor(18, 30); |
vsoltan | 29:4708bfb863cb | 27 | tft->printf("%s", "Player..."); |
vsoltan | 18:32fce82690a1 | 28 | } |
vsoltan | 18:32fce82690a1 | 29 | |
vsoltan | 17:32ae1f106002 | 30 | void Graphics::renderBall(GameState *gs) { |
vsoltan | 19:58cc5465f647 | 31 | Coord ball_loc = gs->getBallLocation(); |
vsoltan | 19:58cc5465f647 | 32 | tft->drawPixel(ball_loc.x, ball_loc.y, ST7735_WHITE); |
vsoltan | 17:32ae1f106002 | 33 | } |
vsoltan | 17:32ae1f106002 | 34 | |
vsoltan | 23:c38680c32552 | 35 | void Graphics::eraseBall(GameState *gs) { |
vsoltan | 22:1c49e1fae846 | 36 | Coord ball_loc = gs->getBallLocation(); |
vsoltan | 22:1c49e1fae846 | 37 | tft->drawPixel(ball_loc.x, ball_loc.y, BACKGROUND_COLOR); |
vsoltan | 22:1c49e1fae846 | 38 | } |
vsoltan | 22:1c49e1fae846 | 39 | |
vsoltan | 17:32ae1f106002 | 40 | void Graphics::renderPlayers(GameState *gs) { |
vsoltan | 24:05eb0b0ab554 | 41 | int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x; |
vsoltan | 24:05eb0b0ab554 | 42 | int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x; |
vsoltan | 24:05eb0b0ab554 | 43 | |
vsoltan | 24:05eb0b0ab554 | 44 | uint16_t top_color = YOUR_PADDLE_COLOR; |
vsoltan | 24:05eb0b0ab554 | 45 | uint16_t bottom_color = OTHER_PADDLE_COLOR; |
vsoltan | 23:c38680c32552 | 46 | |
vsoltan | 24:05eb0b0ab554 | 47 | if (gs->getLocalPlayerNum() == 1) { |
vsoltan | 24:05eb0b0ab554 | 48 | top_color = OTHER_PADDLE_COLOR; |
vsoltan | 24:05eb0b0ab554 | 49 | bottom_color = YOUR_PADDLE_COLOR; |
vsoltan | 24:05eb0b0ab554 | 50 | } |
vsoltan | 24:05eb0b0ab554 | 51 | |
vsoltan | 24:05eb0b0ab554 | 52 | // draw top paddle |
vsoltan | 26:ebadab157abe | 53 | tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2, |
vsoltan | 24:05eb0b0ab554 | 54 | ELEVATION, PADDLE_WIDTH, top_color); |
vsoltan | 23:c38680c32552 | 55 | |
vsoltan | 23:c38680c32552 | 56 | // draw bottom paddle |
vsoltan | 26:ebadab157abe | 57 | tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2, |
vsoltan | 24:05eb0b0ab554 | 58 | 127 - ELEVATION, PADDLE_WIDTH, bottom_color); |
vsoltan | 22:1c49e1fae846 | 59 | } |
vsoltan | 22:1c49e1fae846 | 60 | |
vsoltan | 23:c38680c32552 | 61 | void Graphics::erasePlayers(GameState *gs) { |
vsoltan | 24:05eb0b0ab554 | 62 | int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x; |
vsoltan | 24:05eb0b0ab554 | 63 | int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x; |
vsoltan | 23:c38680c32552 | 64 | |
vsoltan | 29:4708bfb863cb | 65 | // erase top paddle |
vsoltan | 26:ebadab157abe | 66 | tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2, |
vsoltan | 23:c38680c32552 | 67 | ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR); |
vsoltan | 23:c38680c32552 | 68 | |
vsoltan | 29:4708bfb863cb | 69 | // erase bottom paddle |
vsoltan | 26:ebadab157abe | 70 | tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2, |
vsoltan | 23:c38680c32552 | 71 | 127 - ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR); |
vsoltan | 17:32ae1f106002 | 72 | } |
vsoltan | 16:7fd48cda0773 | 73 | |
vsoltan | 29:4708bfb863cb | 74 | void Graphics::renderScore(GameState *gs) { |
vsoltan | 29:4708bfb863cb | 75 | tft->setTextColor(ST7735_WHITE); |
vsoltan | 29:4708bfb863cb | 76 | tft->setTextCursor(120, 50); |
vsoltan | 29:4708bfb863cb | 77 | tft->printf("%i", gs->getPlayerOneScore()); |
vsoltan | 29:4708bfb863cb | 78 | tft->setTextCursor(120, 77); |
vsoltan | 29:4708bfb863cb | 79 | tft->printf("%i", gs->getPlayerTwoScore()); |
vsoltan | 29:4708bfb863cb | 80 | } |
vsoltan | 29:4708bfb863cb | 81 | |
vsoltan | 27:fcc5fee18a24 | 82 | void Graphics::eraseScore(GameState *gs) { |
vsoltan | 27:fcc5fee18a24 | 83 | tft->setTextColor(BACKGROUND_COLOR); |
vsoltan | 27:fcc5fee18a24 | 84 | tft->setTextCursor(120, 50); |
vsoltan | 27:fcc5fee18a24 | 85 | tft->printf("%i", gs->getPlayerOneScore()); |
vsoltan | 27:fcc5fee18a24 | 86 | tft->setTextCursor(120, 77); |
vsoltan | 27:fcc5fee18a24 | 87 | tft->printf("%i", gs->getPlayerTwoScore()); |
vsoltan | 27:fcc5fee18a24 | 88 | } |
vsoltan | 27:fcc5fee18a24 | 89 | |
vsoltan | 27:fcc5fee18a24 | 90 | void Graphics::renderGameOver(GameState *gs) { |
vsoltan | 27:fcc5fee18a24 | 91 | reset(); |
vsoltan | 33:98ba7b0b8c2b | 92 | tft->setTextColor(DEFAULT_TEXT_COLOR); |
vsoltan | 27:fcc5fee18a24 | 93 | tft->setTextCursor(15, 20); |
vsoltan | 27:fcc5fee18a24 | 94 | tft->printf("Game Over"); |
vsoltan | 27:fcc5fee18a24 | 95 | tft->setTextCursor(15, 40); |
vsoltan | 33:98ba7b0b8c2b | 96 | |
vsoltan | 33:98ba7b0b8c2b | 97 | bool winner = gs->getPlayerOneScore() > gs->getPlayerTwoScore() ? 0 : 1; |
vsoltan | 33:98ba7b0b8c2b | 98 | |
vsoltan | 33:98ba7b0b8c2b | 99 | if (winner == gs->getLocalPlayerNum()) { |
vsoltan | 33:98ba7b0b8c2b | 100 | tft->printf("You won :)"); |
vsoltan | 33:98ba7b0b8c2b | 101 | } else { |
vsoltan | 33:98ba7b0b8c2b | 102 | printf("You lost :("); |
vsoltan | 33:98ba7b0b8c2b | 103 | } |
vsoltan | 33:98ba7b0b8c2b | 104 | |
vsoltan | 27:fcc5fee18a24 | 105 | tft->setTextCursor(15, 60); |
vsoltan | 33:98ba7b0b8c2b | 106 | tft->printf("Press any button to play again!"); |
vsoltan | 27:fcc5fee18a24 | 107 | } |
vsoltan | 27:fcc5fee18a24 | 108 | |
vsoltan | 29:4708bfb863cb | 109 | void Graphics::renderCountdown(GameState *gs) { |
vsoltan | 29:4708bfb863cb | 110 | tft->setTextColor(DEFAULT_TEXT_COLOR); |
vsoltan | 29:4708bfb863cb | 111 | tft->setTextCursor(63, 63); |
vsoltan | 29:4708bfb863cb | 112 | tft->printf("%i", gs->getCountdown()); |
vsoltan | 29:4708bfb863cb | 113 | } |
vsoltan | 29:4708bfb863cb | 114 | |
vsoltan | 29:4708bfb863cb | 115 | void Graphics::eraseCountdown(GameState *gs) { |
vsoltan | 29:4708bfb863cb | 116 | tft->setTextColor(BACKGROUND_COLOR); |
vsoltan | 29:4708bfb863cb | 117 | tft->setTextCursor(63, 63); |
vsoltan | 29:4708bfb863cb | 118 | tft->printf("%i", gs->getCountdown()); |
vsoltan | 29:4708bfb863cb | 119 | } |
vsoltan | 29:4708bfb863cb | 120 | |
vsoltan | 16:7fd48cda0773 | 121 | void Graphics::renderGameState(GameState *gs) { |
vsoltan | 19:58cc5465f647 | 122 | renderBall(gs); |
vsoltan | 23:c38680c32552 | 123 | renderPlayers(gs); |
vsoltan | 27:fcc5fee18a24 | 124 | renderScore(gs); |
vsoltan | 23:c38680c32552 | 125 | } |
vsoltan | 23:c38680c32552 | 126 | |
vsoltan | 23:c38680c32552 | 127 | void Graphics::eraseGameState(GameState *gs) { |
vsoltan | 23:c38680c32552 | 128 | eraseBall(gs); |
vsoltan | 23:c38680c32552 | 129 | erasePlayers(gs); |
vsoltan | 27:fcc5fee18a24 | 130 | eraseScore(gs); |
vsoltan | 23:c38680c32552 | 131 | } |
vsoltan | 23:c38680c32552 | 132 | |
vsoltan | 23:c38680c32552 | 133 | void Graphics::reset() { |
vsoltan | 23:c38680c32552 | 134 | tft->fillScreen(BACKGROUND_COLOR); |
vsoltan | 16:7fd48cda0773 | 135 | } |