multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
graphics.cpp@24:05eb0b0ab554, 2020-11-16 (annotated)
- Committer:
- vsoltan
- Date:
- Mon Nov 16 03:34:34 2020 +0000
- Revision:
- 24:05eb0b0ab554
- Parent:
- 23:c38680c32552
- Child:
- 26:ebadab157abe
can send move input and fixed graphics bugs;
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 | 22:1c49e1fae846 | 14 | tft->setTextCursor(15, 20); |
vsoltan | 17:32ae1f106002 | 15 | tft->printf("%s", "Multiplayer Pong"); |
vsoltan | 22:1c49e1fae846 | 16 | tft->setTextCursor(15, 80); |
vsoltan | 22:1c49e1fae846 | 17 | tft->printf("%s", "press any button"); |
vsoltan | 22:1c49e1fae846 | 18 | tft->setTextCursor(15, 90); |
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 | 22:1c49e1fae846 | 24 | tft->setTextCursor(5, 20); |
vsoltan | 18:32fce82690a1 | 25 | tft->printf("%s", "Waiting For Player"); |
vsoltan | 18:32fce82690a1 | 26 | } |
vsoltan | 18:32fce82690a1 | 27 | |
vsoltan | 17:32ae1f106002 | 28 | void Graphics::renderBall(GameState *gs) { |
vsoltan | 19:58cc5465f647 | 29 | Coord ball_loc = gs->getBallLocation(); |
vsoltan | 19:58cc5465f647 | 30 | tft->drawPixel(ball_loc.x, ball_loc.y, ST7735_WHITE); |
vsoltan | 17:32ae1f106002 | 31 | } |
vsoltan | 17:32ae1f106002 | 32 | |
vsoltan | 23:c38680c32552 | 33 | void Graphics::eraseBall(GameState *gs) { |
vsoltan | 22:1c49e1fae846 | 34 | Coord ball_loc = gs->getBallLocation(); |
vsoltan | 22:1c49e1fae846 | 35 | tft->drawPixel(ball_loc.x, ball_loc.y, BACKGROUND_COLOR); |
vsoltan | 22:1c49e1fae846 | 36 | } |
vsoltan | 22:1c49e1fae846 | 37 | |
vsoltan | 17:32ae1f106002 | 38 | void Graphics::renderPlayers(GameState *gs) { |
vsoltan | 24:05eb0b0ab554 | 39 | int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x; |
vsoltan | 24:05eb0b0ab554 | 40 | int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x; |
vsoltan | 24:05eb0b0ab554 | 41 | |
vsoltan | 24:05eb0b0ab554 | 42 | uint16_t top_color = YOUR_PADDLE_COLOR; |
vsoltan | 24:05eb0b0ab554 | 43 | uint16_t bottom_color = OTHER_PADDLE_COLOR; |
vsoltan | 23:c38680c32552 | 44 | |
vsoltan | 24:05eb0b0ab554 | 45 | if (gs->getLocalPlayerNum() == 1) { |
vsoltan | 24:05eb0b0ab554 | 46 | top_color = OTHER_PADDLE_COLOR; |
vsoltan | 24:05eb0b0ab554 | 47 | bottom_color = YOUR_PADDLE_COLOR; |
vsoltan | 24:05eb0b0ab554 | 48 | } |
vsoltan | 24:05eb0b0ab554 | 49 | |
vsoltan | 24:05eb0b0ab554 | 50 | // draw top paddle |
vsoltan | 24:05eb0b0ab554 | 51 | tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2, |
vsoltan | 24:05eb0b0ab554 | 52 | ELEVATION, PADDLE_WIDTH, top_color); |
vsoltan | 23:c38680c32552 | 53 | |
vsoltan | 23:c38680c32552 | 54 | // draw bottom paddle |
vsoltan | 23:c38680c32552 | 55 | tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2, |
vsoltan | 24:05eb0b0ab554 | 56 | 127 - ELEVATION, PADDLE_WIDTH, bottom_color); |
vsoltan | 22:1c49e1fae846 | 57 | } |
vsoltan | 22:1c49e1fae846 | 58 | |
vsoltan | 23:c38680c32552 | 59 | void Graphics::erasePlayers(GameState *gs) { |
vsoltan | 24:05eb0b0ab554 | 60 | int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x; |
vsoltan | 24:05eb0b0ab554 | 61 | int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x; |
vsoltan | 23:c38680c32552 | 62 | |
vsoltan | 24:05eb0b0ab554 | 63 | // draw top paddle |
vsoltan | 23:c38680c32552 | 64 | tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2, |
vsoltan | 23:c38680c32552 | 65 | ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR); |
vsoltan | 23:c38680c32552 | 66 | |
vsoltan | 24:05eb0b0ab554 | 67 | // draw bottom paddle |
vsoltan | 23:c38680c32552 | 68 | tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2, |
vsoltan | 23:c38680c32552 | 69 | 127 - ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR); |
vsoltan | 17:32ae1f106002 | 70 | } |
vsoltan | 16:7fd48cda0773 | 71 | |
vsoltan | 16:7fd48cda0773 | 72 | void Graphics::renderGameState(GameState *gs) { |
vsoltan | 19:58cc5465f647 | 73 | renderBall(gs); |
vsoltan | 23:c38680c32552 | 74 | renderPlayers(gs); |
vsoltan | 23:c38680c32552 | 75 | } |
vsoltan | 23:c38680c32552 | 76 | |
vsoltan | 23:c38680c32552 | 77 | void Graphics::eraseGameState(GameState *gs) { |
vsoltan | 23:c38680c32552 | 78 | eraseBall(gs); |
vsoltan | 23:c38680c32552 | 79 | erasePlayers(gs); |
vsoltan | 23:c38680c32552 | 80 | } |
vsoltan | 23:c38680c32552 | 81 | |
vsoltan | 23:c38680c32552 | 82 | void Graphics::reset() { |
vsoltan | 23:c38680c32552 | 83 | tft->fillScreen(BACKGROUND_COLOR); |
vsoltan | 16:7fd48cda0773 | 84 | } |