forked from its007Kevin/tetris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphicsdisplay.cc
131 lines (120 loc) · 4.52 KB
/
graphicsdisplay.cc
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include "graphicsdisplay.h"
#include "window.h"
#include "info.h"
#include "subject.h"
using namespace std;
GraphicsDisplay::GraphicsDisplay(int gridSize, int winSize): gridSize{gridSize}, winSize{winSize}, xw{winSize, winSize} {
//xw.fillRectangleRGB(0, 0, winSize, winSize, 250000, 250000, 250000);
//xw.fillRectangle(0, 0, winSize, winSize, Xwindow::White);
//grid lines
int x = 8 * (winSize / gridSize) - offset;
while (x < winSize - offset) {
xw.fillRectangleRGB(x, offset - 4, 1, winSize - 3 * winSize / gridSize + 9, 250000, 250000, 250000);
x += winSize / gridSize;
}
int y = offset + winSize / gridSize;
while (y < winSize - 3 * winSize / gridSize + offset + 1) {
xw.fillRectangleRGB(7 * (winSize/gridSize) - 4 - offset, y, winSize - 7 * (winSize / gridSize) + 5, 1, 250000, 250000, 250000);
y += winSize / gridSize;
}
//border
//left
xw.fillRectangle(7 * (winSize/gridSize) - 4 - offset, offset - 4,
4, winSize - 3 * winSize / gridSize + 9,
Xwindow::Black);
//right
xw.fillRectangle(winSize - offset + 1, offset - 4,
4, winSize - 3 * winSize / gridSize + 9,
Xwindow::Black);
//top
xw.fillRectangle(7 * (winSize/gridSize) - 4 - offset, offset - 4,
winSize - 7 * (winSize / gridSize) + 5, 4,
Xwindow::Black);
//bottom
xw.fillRectangle(7 * (winSize/gridSize) - 4 - offset, winSize - 3 * winSize / gridSize + offset + 1,
winSize - 7 * (winSize / gridSize) + 5, 4,
Xwindow::Black);
//xw.fillRectangle(0, 0, winSize / gridSize * 7, winSize, Xwindow::Black);
xw.drawString(offset , offset, "QUADRIS");
xw.drawString(offset , winSize - 40, "Created by:");
xw.drawString(offset , winSize - 20, "Eric Wang, Bob Zhang, Kevin Zeng");
xw.drawString(offset, 5 * winSize / gridSize, "Next: ");
xw.drawString(offset, offset + 8 * winSize / gridSize, "Level: ");
xw.drawString(offset, offset + 10 * winSize / gridSize, "Current Score: ");
xw.drawString(offset, offset + 12 * winSize / gridSize, "Highscore: ");
}
void GraphicsDisplay::notify(Subject &whoNotified) {
auto info = whoNotified.getInfo();
if (info.row <= 2) return;
fillCell(info.col + 7, info.row - 3, info.data);
}
void GraphicsDisplay::fillCell(int x, int y, char type) {
int cellSize = winSize / gridSize;
x = x * cellSize - offset;
y = y * cellSize + offset;
int colour = Xwindow::White;
switch(type) {
case 'I':
colour = Xwindow::Cyan;
break;
case 'J':
colour = Xwindow::Orange;
break;
case 'L':
colour = Xwindow::Blue;
break;
case 'O':
colour = Xwindow::Yellow;
break;
case 'S':
colour = Xwindow::Red;
break;
case 'Z':
colour = Xwindow::Green;
break;
case 'T':
colour = Xwindow::Magenta;
break;
case '*':
colour = Xwindow::Brown;
break;
default:
colour = Xwindow::White;
break;
}
xw.fillRectangle(x, y, cellSize, cellSize, colour);
xw.fillRectangleRGB(x, y, cellSize, 1, 250000, 250000, 250000);
xw.fillRectangleRGB(x, y, 1, cellSize, 250000, 250000, 250000);
xw.fillRectangleRGB(x + cellSize, y, 1, cellSize, 250000, 250000, 250000);
xw.fillRectangleRGB(x, y + cellSize, cellSize, 1, 250000, 250000, 250000);
}
void GraphicsDisplay::setLevel(int level) {
xw.fillRectangle(offset, offset + 4 + 8 * winSize / gridSize, 100, 16, Xwindow::White);
xw.drawString(offset, offset + 20 + 8 * winSize / gridSize, to_string(level));
}
void GraphicsDisplay::setNextPiece(std::vector<std::vector<char>> nextPiece) {
for (int i = 0; i < nextPiece.size(); i++) {
for (int j = 0; j < nextPiece[i].size(); j++) {
fillCell(j + 1, i + 5, nextPiece[i][j]);
}
}
}
void GraphicsDisplay::setHoldPiece(std::vector<std::vector<char>> holdPiece) {
for (int i = 0; i < holdPiece.size(); i++) {
for (int j = 0; j < holdPiece[i].size(); j++) {
fillCell(j + 1, i + 1, holdPiece[i][j]);
}
}
}
void GraphicsDisplay::setScore(int score) {
xw.fillRectangle(offset, offset + 4 + 10 * winSize / gridSize, 100, 16, Xwindow::White);
xw.drawString(offset, offset + 20 + 10 * winSize / gridSize, to_string(score));
}
void GraphicsDisplay::setHighScore(int highScore) {
xw.fillRectangle(offset, offset + 4 + 12 * winSize / gridSize, 100, 16, Xwindow::White);
xw.drawString(offset, offset + 20 + 12 * winSize / gridSize, to_string(highScore));
}
void GraphicsDisplay::enableHold() {
xw.drawString(offset, 40, "Hold: ");
}