changes but linker error
This commit is contained in:
+4
-5
@@ -36,13 +36,12 @@ struct Ball {
|
||||
};
|
||||
|
||||
struct Paddle {
|
||||
Paddle(int size) : _size(size), m_hearts(3) {};
|
||||
~Paddle();
|
||||
Paddle();
|
||||
int _size;
|
||||
int m_hearts;
|
||||
vec2** m_nodes;
|
||||
int m_hearts { 3 };
|
||||
std::array<vec2, 3> m_nodes;
|
||||
enum Direction m_direction;
|
||||
void determine_new_position(vec2* (*board)[COL]);
|
||||
void determine_new_position(char (*board)[COL]);
|
||||
void check_collision(Ball &ball);
|
||||
};
|
||||
|
||||
|
||||
+2
-2
@@ -3,8 +3,8 @@
|
||||
#include "globals.hpp"
|
||||
|
||||
struct Window {
|
||||
vec2* m_board[ROW][COL] = {};
|
||||
char view[ROW][COL] = {};
|
||||
void clear_display();
|
||||
void draw_display(int fps);
|
||||
void update_board(vec2** buffer, int buffer_size);
|
||||
void update_board(Paddle paddle, Ball ball);
|
||||
};
|
||||
|
||||
+79
-111
@@ -1,100 +1,85 @@
|
||||
#include <engine.hpp>
|
||||
#include "globals.hpp"
|
||||
|
||||
Paddle create_new_paddle(vec2** buffer, int starting_array_index, int size, int row, char symbol) {
|
||||
Paddle paddle { size };
|
||||
paddle.m_nodes = new vec2*[size];
|
||||
for(int i = 0; i < size; i++) {
|
||||
paddle.m_nodes[i] = new vec2 { row, ((COL / 2) - (size / 2)) + i, symbol };
|
||||
buffer[i + starting_array_index] = paddle.m_nodes[i];
|
||||
}
|
||||
// Block** create_block_buffer(int starting_symbol, int rows, int cols, int length) {
|
||||
// Block** block_buffer = new Block*[rows * cols];
|
||||
// int row, int_symbol, col;
|
||||
// char char_symbol;
|
||||
// for(int index = 0; index < rows * cols; index++) {
|
||||
// row = index / 5;
|
||||
// int_symbol = (starting_symbol - '0') - row;
|
||||
// char_symbol = '0' + int_symbol;
|
||||
// col = (index % 5) * 3;
|
||||
// block_buffer[index] = new Block(length, row, col, char_symbol);
|
||||
// }
|
||||
// return block_buffer;
|
||||
// }
|
||||
//
|
||||
// bool is_block_buffer_empty(Block** block_buffer, int block_buffer_size) {
|
||||
// int total_clear_blocks = 0;
|
||||
// for(int block_index = 0; block_index < block_buffer_size; block_index++) {
|
||||
// if(block_buffer[block_index] != nullptr) continue;
|
||||
// total_clear_blocks++;
|
||||
// }
|
||||
//
|
||||
// if(total_clear_blocks == block_buffer_size) return true;
|
||||
// return false;
|
||||
// }
|
||||
|
||||
return paddle;
|
||||
}
|
||||
// void cleanup(Block** block_buffer, int block_buffer_size) {
|
||||
// for(int i = 0; i < block_buffer_size; i++) {
|
||||
// delete block_buffer[i];
|
||||
// }
|
||||
// delete[] block_buffer;
|
||||
// }
|
||||
//
|
||||
// void rasterize_buffer(vec2** buffer, Block** block_buffer, int offset) {
|
||||
// for(int block_index = 0; block_index < 15; block_index++) {
|
||||
// if(block_buffer[block_index] == nullptr) continue;
|
||||
// for(int node_index = 0; node_index < block_buffer[block_index]->_size; node_index++) {
|
||||
// buffer[offset + node_index] = block_buffer[block_index]->m_nodes[node_index];
|
||||
// }
|
||||
// offset += 3;
|
||||
// }
|
||||
// }
|
||||
|
||||
Block** create_block_buffer(int starting_symbol, int rows, int cols, int length) {
|
||||
Block** block_buffer = new Block*[rows * cols];
|
||||
int row, int_symbol, col;
|
||||
char char_symbol;
|
||||
for(int index = 0; index < rows * cols; index++) {
|
||||
row = index / 5;
|
||||
int_symbol = (starting_symbol - '0') - row;
|
||||
char_symbol = '0' + int_symbol;
|
||||
col = (index % 5) * 3;
|
||||
block_buffer[index] = new Block(length, row, col, char_symbol);
|
||||
}
|
||||
return block_buffer;
|
||||
}
|
||||
// void trim_block_buffer_on_collision(vec2** buffer, const int buffer_size, Block** block_buffer, const int block_buffer_size, Ball &ball) {
|
||||
// for(int i = 0; i < block_buffer_size; i++) {
|
||||
// if(block_buffer[i] == nullptr) continue;
|
||||
// if(block_buffer[i]->check_collision(ball)) {
|
||||
// ball.change_velocity('y');
|
||||
// for(int a = 0; a < block_buffer[i]->_size; a++) {
|
||||
// vec2* doomed = block_buffer[i]->m_nodes[a];
|
||||
// for(int j = 0; j < buffer_size; j++) {
|
||||
// if(buffer[j] == doomed) {
|
||||
// buffer[j] = nullptr;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// delete block_buffer[i];
|
||||
// block_buffer[i] = nullptr;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
bool is_block_buffer_empty(Block** block_buffer, int block_buffer_size) {
|
||||
int total_clear_blocks = 0;
|
||||
for(int block_index = 0; block_index < block_buffer_size; block_index++) {
|
||||
if(block_buffer[block_index] != nullptr) continue;
|
||||
total_clear_blocks++;
|
||||
}
|
||||
|
||||
if(total_clear_blocks == block_buffer_size) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void cleanup(Block** block_buffer, int block_buffer_size) {
|
||||
for(int i = 0; i < block_buffer_size; i++) {
|
||||
delete block_buffer[i];
|
||||
}
|
||||
delete[] block_buffer;
|
||||
}
|
||||
|
||||
void rasterize_buffer(vec2** buffer, Block** block_buffer, int offset) {
|
||||
for(int block_index = 0; block_index < 15; block_index++) {
|
||||
if(block_buffer[block_index] == nullptr) continue;
|
||||
for(int node_index = 0; node_index < block_buffer[block_index]->_size; node_index++) {
|
||||
buffer[offset + node_index] = block_buffer[block_index]->m_nodes[node_index];
|
||||
}
|
||||
offset += 3;
|
||||
Paddle::Paddle() {
|
||||
for(int i = 0; i < m_nodes.size(); i++) {
|
||||
m_nodes[i].set(COL -2 , (ROW / 2) - (m_nodes.size() - i));
|
||||
m_nodes[i].m_symbol = '-';
|
||||
}
|
||||
}
|
||||
|
||||
void trim_block_buffer_on_collision(vec2** buffer, const int buffer_size, Block** block_buffer, const int block_buffer_size, Ball &ball) {
|
||||
for(int i = 0; i < block_buffer_size; i++) {
|
||||
if(block_buffer[i] == nullptr) continue;
|
||||
if(block_buffer[i]->check_collision(ball)) {
|
||||
ball.change_velocity('y');
|
||||
for(int a = 0; a < block_buffer[i]->_size; a++) {
|
||||
vec2* doomed = block_buffer[i]->m_nodes[a];
|
||||
for(int j = 0; j < buffer_size; j++) {
|
||||
if(buffer[j] == doomed) {
|
||||
buffer[j] = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
delete block_buffer[i];
|
||||
block_buffer[i] = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Paddle::~Paddle() {
|
||||
if(m_nodes) {
|
||||
for(int i = 0; i < _size; i++) {
|
||||
delete m_nodes[i];
|
||||
m_nodes[i] = nullptr;
|
||||
}
|
||||
delete[] m_nodes;
|
||||
m_nodes = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Paddle::determine_new_position(vec2* (*board)[COL]) {
|
||||
if(m_direction == LEFT && m_nodes[0]->x - 1 >= 0) {
|
||||
m_nodes[0]->x--;
|
||||
void Paddle::determine_new_position(char (*board)[COL]) {
|
||||
if(m_direction == LEFT && m_nodes[0].x - 1 >= 0) {
|
||||
m_nodes[0].x--;
|
||||
for(int i = 1; i < _size; i++) {
|
||||
m_nodes[i]->x = m_nodes[0]->x + i;
|
||||
m_nodes[i].x = m_nodes[0].x + i;
|
||||
}
|
||||
}
|
||||
else if(m_direction == RIGHT && m_nodes[_size - 1]->x + 1 < ROW) {
|
||||
m_nodes[_size - 1]->x++;
|
||||
else if(m_direction == RIGHT && m_nodes[_size - 1].x + 1 < ROW) {
|
||||
m_nodes[_size - 1].x++;
|
||||
for(int i = 1; i < _size; i++) {
|
||||
m_nodes[_size - (i + 1)]->x = m_nodes[_size - 1]->x - i;
|
||||
m_nodes[_size - (i + 1)].x = m_nodes[_size - 1].x - i;
|
||||
}
|
||||
}
|
||||
m_direction = NONE;
|
||||
@@ -102,9 +87,8 @@ void Paddle::determine_new_position(vec2* (*board)[COL]) {
|
||||
|
||||
void Paddle::check_collision(Ball &ball) {
|
||||
for(int i = 0; i < _size; i++) {
|
||||
if(ball.m_origin->y != m_nodes[i]->y) continue;
|
||||
if(ball.m_origin->x != m_nodes[i]->x) continue;
|
||||
ball.change_velocity('y');
|
||||
if(ball.m_origin != m_nodes[i]) continue;
|
||||
ball.vel.y *= -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,32 +101,17 @@ void Ball::change_velocity(char d) {
|
||||
}
|
||||
|
||||
void Ball::determine_new_position(int &paddle_lives, int reset_ball_x, int reset_ball_y) {
|
||||
if(m_origin->y <= 0) {
|
||||
vy = 1;
|
||||
} else if(m_origin->y >= ROW - 1) {
|
||||
if(m_origin.y <= 0) {
|
||||
vel.y = 1;
|
||||
} else if(m_origin.y >= ROW - 1) {
|
||||
paddle_lives--;
|
||||
m_origin->y = reset_ball_y;
|
||||
m_origin->x = reset_ball_x;
|
||||
m_origin.set(reset_ball_x, reset_ball_y);
|
||||
}
|
||||
if(m_origin->x <= 0) {
|
||||
vx = 1;
|
||||
} else if(m_origin->x >= COL - 1) {
|
||||
vx = -1;
|
||||
}
|
||||
|
||||
m_origin->x += vx;
|
||||
m_origin->y += vy;
|
||||
}
|
||||
|
||||
Block::~Block() {
|
||||
if(m_nodes) {
|
||||
for(int i = 0; i < _size; i++) {
|
||||
delete m_nodes[i];
|
||||
m_nodes[i] = nullptr;
|
||||
}
|
||||
delete[] m_nodes;
|
||||
m_nodes = nullptr;
|
||||
if(m_origin.x <= 0 || m_origin.x > COL - 1) {
|
||||
vel.x *= -1;
|
||||
}
|
||||
m_origin.x += vel.x;
|
||||
m_origin.y += vel.y;
|
||||
}
|
||||
|
||||
Block::Block(vec2 pos, char symbol) {
|
||||
@@ -154,7 +123,6 @@ Block::Block(vec2 pos, char symbol) {
|
||||
|
||||
bool Block::check_collision(Ball &ball) {
|
||||
for(int i = 0; i < m_nodes.size(); i++) {
|
||||
if(m_nodes[i] != ball.m_origin) continue;
|
||||
if(m_nodes[i] != ball.m_origin) continue;
|
||||
return true;
|
||||
}
|
||||
|
||||
+16
-14
@@ -6,40 +6,42 @@
|
||||
#include <window.hpp>
|
||||
|
||||
int main() {
|
||||
const int buffer_size = 50, block_buffer_size = 15;
|
||||
// const int buffer_size = 50, block_buffer_size = 15;
|
||||
bool running = true;
|
||||
vec2* buffer[buffer_size] = {};
|
||||
// vec2* buffer[buffer_size] = {};
|
||||
Input input;
|
||||
Window window;
|
||||
|
||||
Ball ball = create_new_ball(buffer, 0, 1, -1, (ROW - 5), (COL / 2), '*' );
|
||||
Paddle paddle = create_new_paddle(buffer, 1, 3, ROW - 2, '-');
|
||||
Block** block_buffer = create_block_buffer('3', 3, 5, 3);
|
||||
rasterize_buffer(buffer, block_buffer, 4);
|
||||
// Ball ball = create_new_ball(buffer, 0, 1, -1, (ROW - 5), (COL / 2), '*' );
|
||||
// Paddle paddle = create_new_paddle(buffer, 1, 3, ROW - 2, '-');
|
||||
// Block** block_buffer = create_block_buffer('3', 3, 5, 3);
|
||||
// rasterize_buffer(buffer, block_buffer, 4);
|
||||
Paddle paddle;
|
||||
Ball ball;
|
||||
|
||||
std::thread input_thread([&] { input.get_input(running, paddle.m_direction); } );
|
||||
|
||||
while (running) {
|
||||
paddle.determine_new_position(window.m_board);
|
||||
paddle.determine_new_position(window.view);
|
||||
paddle.check_collision(ball);
|
||||
trim_block_buffer_on_collision(buffer, buffer_size, block_buffer, block_buffer_size, ball);
|
||||
// trim_block_buffer_on_collision(buffer, buffer_size, block_buffer, block_buffer_size, ball);
|
||||
ball.determine_new_position(paddle.m_hearts, (ROW - 5), (COL / 2));
|
||||
window.clear_display();
|
||||
window.update_board(buffer, buffer_size);
|
||||
window.update_board(paddle, ball);
|
||||
window.draw_display(FPMS);
|
||||
if(paddle.m_hearts <= 0) {
|
||||
running = false;
|
||||
std::cout << "No Hearts left! You lose!\n";
|
||||
}
|
||||
if(is_block_buffer_empty(block_buffer, block_buffer_size)) {
|
||||
running = false;
|
||||
std::cout << "No Blocks left! You win!\n";
|
||||
}
|
||||
// if(is_block_buffer_empty(block_buffer, block_buffer_size)) {
|
||||
// running = false;
|
||||
// std::cout << "No Blocks left! You win!\n";
|
||||
// }
|
||||
std::cout << "Hearts left: " << paddle.m_hearts << "\n";
|
||||
}
|
||||
|
||||
input_thread.join();
|
||||
cleanup(block_buffer, block_buffer_size);
|
||||
// cleanup(block_buffer, block_buffer_size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+6
-6
@@ -6,15 +6,15 @@
|
||||
void Window::clear_display() {
|
||||
for(int i = 0; i < ROW; i++) {
|
||||
for(int j = 0; j < COL; j++) {
|
||||
m_board[i][j] = nullptr;
|
||||
view[i][j] = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Window::update_board(vec2** buffer, int buffer_size) {
|
||||
for(int i = buffer_size - 1; i >= 0; i--) {
|
||||
if(buffer[i] == nullptr) continue;
|
||||
m_board[buffer[i]->y][buffer[i]->x] = buffer[i];
|
||||
void Window::update_board(Paddle paddle, Ball ball) {
|
||||
for(int i = 0; i < paddle.m_nodes.size(); i++) {
|
||||
view[paddle.m_nodes[i].y][paddle.m_nodes[i].x] = paddle.m_nodes[i].m_symbol;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ void Window::draw_display(int fps) {
|
||||
std::cout << "\033[H" << std::flush;
|
||||
for(int i = 0; i < ROW; i++) {
|
||||
for(int j = 0; j < COL; j++) {
|
||||
if(m_board[i][j] != nullptr) std::cout << m_board[i][j]->m_symbol << " ";
|
||||
if(view[i][j] != '.') std::cout << view[i][j] << " ";
|
||||
else std::cout << ". ";
|
||||
}
|
||||
std::cout << "\r\n";
|
||||
|
||||
Reference in New Issue
Block a user