multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Mon Nov 16 03:01:50 2020 +0000
Revision:
23:c38680c32552
Parent:
22:1c49e1fae846
Child:
24:05eb0b0ab554
moving paddles work in progress

Who changed what in which revision?

UserRevisionLine numberNew 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 23:c38680c32552 39 int otherPlayerNum = (gs->getLocalPlayerNum() == 0) ? 1 : 0;
vsoltan 23:c38680c32552 40
vsoltan 23:c38680c32552 41 int8_t bottomPaddleRenderPos = gs->getPlayerLocation(gs->getLocalPlayerNum()).x;
vsoltan 23:c38680c32552 42 int8_t topPaddleRenderPos = gs->getPlayerLocation(otherPlayerNum).x;
vsoltan 23:c38680c32552 43
vsoltan 23:c38680c32552 44 // draw bottom paddle
vsoltan 23:c38680c32552 45 tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 23:c38680c32552 46 ELEVATION, PADDLE_WIDTH, PADDLE_COLOR);
vsoltan 23:c38680c32552 47
vsoltan 23:c38680c32552 48 // draw top paddle
vsoltan 23:c38680c32552 49 tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 23:c38680c32552 50 127 - ELEVATION, PADDLE_WIDTH, PADDLE_COLOR);
vsoltan 22:1c49e1fae846 51 }
vsoltan 22:1c49e1fae846 52
vsoltan 23:c38680c32552 53 void Graphics::erasePlayers(GameState *gs) {
vsoltan 23:c38680c32552 54 int otherPlayerNum = (gs->getLocalPlayerNum() == 0) ? 1 : 0;
vsoltan 23:c38680c32552 55
vsoltan 23:c38680c32552 56 int8_t bottomPaddleRenderPos = gs->getPlayerLocation(gs->getLocalPlayerNum()).x;
vsoltan 23:c38680c32552 57 int8_t topPaddleRenderPos = gs->getPlayerLocation(otherPlayerNum).x;
vsoltan 23:c38680c32552 58
vsoltan 23:c38680c32552 59 // draw bottom paddle
vsoltan 23:c38680c32552 60 tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 23:c38680c32552 61 ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
vsoltan 23:c38680c32552 62
vsoltan 23:c38680c32552 63 // draw top paddle
vsoltan 23:c38680c32552 64 tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
vsoltan 23:c38680c32552 65 127 - ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
vsoltan 17:32ae1f106002 66 }
vsoltan 16:7fd48cda0773 67
vsoltan 16:7fd48cda0773 68 void Graphics::renderGameState(GameState *gs) {
vsoltan 19:58cc5465f647 69 renderBall(gs);
vsoltan 23:c38680c32552 70 renderPlayers(gs);
vsoltan 23:c38680c32552 71 }
vsoltan 23:c38680c32552 72
vsoltan 23:c38680c32552 73 void Graphics::eraseGameState(GameState *gs) {
vsoltan 23:c38680c32552 74 eraseBall(gs);
vsoltan 23:c38680c32552 75 erasePlayers(gs);
vsoltan 23:c38680c32552 76 }
vsoltan 23:c38680c32552 77
vsoltan 23:c38680c32552 78 void Graphics::reset() {
vsoltan 23:c38680c32552 79 tft->fillScreen(BACKGROUND_COLOR);
vsoltan 16:7fd48cda0773 80 }