multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

graphics.cpp

Committer:
vsoltan
Date:
2020-11-16
Revision:
24:05eb0b0ab554
Parent:
23:c38680c32552
Child:
26:ebadab157abe

File content as of revision 24:05eb0b0ab554:


#include "graphics.h"

Graphics::Graphics() {
    printf("\n\rInitializing Graphics: initializing TFT\n\r");
    this->tft = new Adafruit_ST7735(P_MOSI, P_MISO, P_SOCK, P_CS, P_RS, P_DC);
    printf("Initializing Graphics: optimizing display for resolution\n\r");
    tft->initR(INITR_144GREENTAB); 
    tft->setTextColor(ST7735_WHITE);
}

void Graphics::renderLaunchScreen() {
    tft->fillScreen(LAUNCH_SCREEN_COLOR);
    tft->setTextCursor(15, 20); 
    tft->printf("%s", "Multiplayer Pong");
    tft->setTextCursor(15, 80); 
    tft->printf("%s", "press any button"); 
    tft->setTextCursor(15, 90); 
    tft->printf("%s", "to continue"); 
}

void Graphics::renderWaitingRoom() {
    tft->fillScreen(WAITING_SCREEN_COLOR);
    tft->setTextCursor(5, 20); 
    tft->printf("%s", "Waiting For Player");
}

void Graphics::renderBall(GameState *gs) {
    Coord ball_loc = gs->getBallLocation(); 
    tft->drawPixel(ball_loc.x, ball_loc.y, ST7735_WHITE); 
}

void Graphics::eraseBall(GameState *gs) {
    Coord ball_loc = gs->getBallLocation(); 
    tft->drawPixel(ball_loc.x, ball_loc.y, BACKGROUND_COLOR); 
}

void Graphics::renderPlayers(GameState *gs) {
    int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x;    
    int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
    
    uint16_t top_color = YOUR_PADDLE_COLOR;
    uint16_t bottom_color = OTHER_PADDLE_COLOR; 
    
    if (gs->getLocalPlayerNum() == 1) {
        top_color = OTHER_PADDLE_COLOR; 
        bottom_color = YOUR_PADDLE_COLOR; 
    }
    
    // draw top paddle
    tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
        ELEVATION, PADDLE_WIDTH, top_color);
    
    // draw bottom paddle
    tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
        127 - ELEVATION, PADDLE_WIDTH, bottom_color);
}

void Graphics::erasePlayers(GameState *gs) {
    int8_t topPaddleRenderPos = gs->getPlayerLocation(0).x;    
    int8_t bottomPaddleRenderPos = gs->getPlayerLocation(1).x;
    
    // draw top paddle
    tft->drawFastHLine(bottomPaddleRenderPos - PADDLE_WIDTH / 2,
        ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
    
    // draw bottom paddle
    tft->drawFastHLine(topPaddleRenderPos - PADDLE_WIDTH / 2,
        127 - ELEVATION, PADDLE_WIDTH, BACKGROUND_COLOR);
}

void Graphics::renderGameState(GameState *gs) {
    renderBall(gs); 
    renderPlayers(gs);
}

void Graphics::eraseGameState(GameState *gs) {
    eraseBall(gs); 
    erasePlayers(gs); 
}

void Graphics::reset() {
    tft->fillScreen(BACKGROUND_COLOR);
}