-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameBoard.cpp
113 lines (89 loc) · 3.17 KB
/
GameBoard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Created by vojta on 1/12/20.
//
#include "GameBoard.h"
GameBoard::GameBoard(SDL2pp::Renderer &renderer, Map *map) {
this->renderer = &renderer;
this->blockSize = 40;
this->map = map;
}
GameBoard::~GameBoard() = default;
void GameBoard::RenderShape(Shape *shape) {
for (auto &point : shape->GetBlocks()) {
DrawBlockOutline((shape->position.x + point.x) * blockSize, (shape->position.y + point.y) * blockSize,
shape->color);
}
}
void GameBoard::DrawBlock(int x, int y, SDL_Color color) {
SDL2pp::Rect target(x, y, blockSize, blockSize);
renderer->SetDrawColor(color);
renderer->FillRect(target);
}
void GameBoard::DrawBlockOutline(int x, int y, SDL_Color color) {
SDL2pp::Rect target(x, y, blockSize, blockSize);
SDL2pp::Rect target2(x + 3, y + 3, blockSize - 6, blockSize - 6);
renderer->SetDrawColor(0, 0, 0, 255);
renderer->FillRect(target);
renderer->SetDrawColor(color.r, color.g, color.b, 120);
renderer->FillRect(target);
renderer->SetDrawColor(color);
renderer->FillRect(target2);
}
void GameBoard::RenderBackground() {
renderer->SetDrawColor({0x4a, 0x4a, 0x4a, 255});
for (int x = 40; x <= 400; x += 40) {
renderer->DrawLine(x, 0, x, 800);
}
for (int y = 40; y < 800; y += 40) {
renderer->DrawLine(0, y, 400, y);
}
}
void GameBoard::RenderMap() {
for (int i = 0; i < 10; i++) {
for (int x = 0; x < 20; x++) {
if (map->mapData[i][x].hasBlock) {
DrawBlockOutline(i * blockSize, x * blockSize, map->mapData[i][x].color);
}
}
}
}
void GameBoard::RenderDestination(Shape *shape) {
int i = GetFinalDestination(shape);
for (auto &p : shape->GetBlocks()) {
SDL_Color c = {shape->color.r, shape->color.g, shape->color.b, 100};
DrawBlock((shape->position.x + p.x) * blockSize, (i + p.y) * blockSize, c);
}
}
int GameBoard::GetFinalDestination(Shape *shape) {
for (int i = shape->position.y; i < 20; i++) {
for (auto &point : shape->GetBlocks()) {
if (map->HasBlock(shape->position.x + point.x, i + point.y + 1) ||
i + point.y >= 19) {
return i;
}
}
}
}
void GameBoard::RenderGuidelines(Shape *shape) {
int maxX = 0;
int minX = 999;
int minY = 0;
for (auto &point : shape->GetBlocks()) {
if (maxX <= shape->position.x + point.x) {
maxX = shape->position.x + point.x + 1;
}
if (minX >= shape->position.x + point.x) {
minX = shape->position.x + point.x;
}
if (minY <= shape->position.y + point.y) {
minY = shape->position.y + point.y + 1;
}
}
renderer->SetDrawColor(shape->color.r, shape->color.g, shape->color.b, 170);
renderer->DrawLine(minX * blockSize, shape->position.y * blockSize, minX * blockSize, 800);
renderer->DrawLine(maxX * blockSize, shape->position.y * blockSize, maxX * blockSize, 800);
renderer->DrawLine(minX * blockSize, shape->position.y * blockSize, maxX * blockSize,
shape->position.y * blockSize);
}
void GameBoard::RenderNextShape(Shape *shape) {
}