added renderer.cpp/h files
This commit is contained in:
72
src/renderer.cpp
Normal file
72
src/renderer.cpp
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
#include "renderer.h"
|
||||||
|
|
||||||
|
|
||||||
|
void Renderer::_clear_matrix() {
|
||||||
|
for (int x=0; x < MATRIX_WIDTH; x++) {
|
||||||
|
for (int y=0; y < MATRIX_HEIGHT; y++) {
|
||||||
|
_frame[y][x]= 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::render_timer(uint8_t seconds) {
|
||||||
|
this -> _clear_matrix();
|
||||||
|
|
||||||
|
for (int h=0; h < 8; h++) {
|
||||||
|
for (int w=0; w < 3; w++) {
|
||||||
|
_frame[h][w+5]= font_pong[seconds][h][w];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_matrix.renderBitmap(_frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::render_matrix() {
|
||||||
|
this -> _clear_matrix();
|
||||||
|
uint8_t p1pos= _p1.get_position();
|
||||||
|
uint8_t p2pos= _p2.get_position();
|
||||||
|
// players coords
|
||||||
|
for (int i= p1pos; i < p1pos+PADDLE_LENGTH; i++) {
|
||||||
|
_frame[i][0]= 1;
|
||||||
|
}
|
||||||
|
for (int i= p2pos; i < p2pos+PADDLE_LENGTH; i++) {
|
||||||
|
_frame[i][MATRIX_WIDTH-1]= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ball coords
|
||||||
|
uint8_t bx= _ball.get_x();
|
||||||
|
uint8_t by= _ball.get_y();
|
||||||
|
_frame[by][bx]= 1;
|
||||||
|
_matrix.renderBitmap(_frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::render_score() {
|
||||||
|
this -> _clear_matrix();
|
||||||
|
_frame[4][5]= 1;
|
||||||
|
_frame[4][6]= 1;
|
||||||
|
|
||||||
|
for (int h=0; h < 8; h++) {
|
||||||
|
for (int w=0; w < 3; w++) {
|
||||||
|
_frame[h][w+1]= font_pong[_p1.get_score()][h][w];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int h=0; h < 8; h++) {
|
||||||
|
for (int w=0; w < 3; w++) {
|
||||||
|
_frame[h][w+8]= font_pong[_p2.get_score()][h][w];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_matrix.renderBitmap(_frame, MATRIX_HEIGHT, MATRIX_WIDTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Renderer::render_winner() {
|
||||||
|
this -> _clear_matrix();
|
||||||
|
// check winner
|
||||||
|
if (_p1.get_score() > _p2.get_score()) {
|
||||||
|
Serial.println("Player 1 wins!!!");
|
||||||
|
_matrix.loadSequence(pone_wins);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("Player 2 wins!!!");
|
||||||
|
_matrix.loadSequence(ptwo_wins);
|
||||||
|
}
|
||||||
|
_matrix.play(true);
|
||||||
|
}
|
||||||
33
src/renderer.h
Normal file
33
src/renderer.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef RENDERER_H
|
||||||
|
#define RENDERER_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "Arduino_LED_Matrix.h"
|
||||||
|
#include "config.h"
|
||||||
|
#include "font.h"
|
||||||
|
#include "paddle.h"
|
||||||
|
#include "ball.h"
|
||||||
|
|
||||||
|
class Renderer {
|
||||||
|
|
||||||
|
private:
|
||||||
|
// define player coordinates
|
||||||
|
Paddle& _p1;
|
||||||
|
Paddle& _p2;
|
||||||
|
Ball& _ball;
|
||||||
|
byte (&_frame)[MATRIX_HEIGHT][MATRIX_WIDTH];
|
||||||
|
ArduinoLEDMatrix& _matrix;
|
||||||
|
|
||||||
|
void _clear_matrix();
|
||||||
|
|
||||||
|
public:
|
||||||
|
Renderer (Paddle &p1, Paddle &p2, Ball &ball, byte (&frame)[MATRIX_HEIGHT][MATRIX_WIDTH], ArduinoLEDMatrix &matrix)
|
||||||
|
: _p1(p1), _p2(p2), _ball(ball), _frame(frame), _matrix(matrix) {}
|
||||||
|
|
||||||
|
void render_timer(uint8_t seconds);
|
||||||
|
void render_matrix();
|
||||||
|
void render_score();
|
||||||
|
void render_winner();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user