-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphicsdisplay.cc
More file actions
106 lines (91 loc) · 3.68 KB
/
graphicsdisplay.cc
File metadata and controls
106 lines (91 loc) · 3.68 KB
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
#include "graphicsdisplay.h"
#include "window.h"
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// Creates a GrpahicsDisplay for an n by n grid
GraphicsDisplay::GraphicsDisplay(int width, int height,
int cellSize,
int leftOffset, int topOffset,
int rightOffset, int bottomOffset):
cellSize{cellSize},
leftOffset{leftOffset}, topOffset{topOffset},
rightOffset{rightOffset}, bottomOffset{bottomOffset},
theDisplay{new Xwindow(leftOffset + width * cellSize + rightOffset,
topOffset + height * cellSize + bottomOffset)}{}
void GraphicsDisplay::setGrid(vector<vector<Cell>> * board){
this->board = board;
}
// Draws a cell at the given row at column with specified colour
void GraphicsDisplay::drawCell(int row, int col, int colour){
//std::cout << row << ", " << col << ", " << colour << std::endl;
theDisplay->fillRectangle(col * cellSize + leftOffset,
row * cellSize + topOffset,
cellSize-1, cellSize-1, colour);
}
void GraphicsDisplay::drawText(int row, int col, std::string txt){
theDisplay->fillRectangle(leftOffset + (WIDTH + col + 1)*cellSize,
topOffset + cellSize * row,
cellSize*4, cellSize, Xwindow::White);
theDisplay->drawString(leftOffset + (WIDTH + col + 1)*cellSize,
topOffset + cellSize * (row + 1), txt);
}
void GraphicsDisplay::drawText(int row, int col, int i){
std::stringstream ss; ss << i;
drawText(row, col, ss.str());
}
// Shows score info
void GraphicsDisplay::showInfo(int lvl, int total, int MaxSofar){
theDisplay->fillRectangle((WIDTH+1) * cellSize + leftOffset,
(promptRow-1) * cellSize + topOffset,
cellSize*(WIDTH+2), cellSize*(HEIGHT+2), Xwindow::White);
string level_str;
switch (lvl) {
case 0: level_str = "Level: I "; break;
case 1: level_str = "Level: II "; break;
case 2: level_str = "Level: III"; break;
case 3: level_str = "Level: IV "; break;
case 4: level_str = "Level: V "; break;
}
drawText(0, 0, level_str);
drawText(1, 0, "Score: " + to_string(total));
drawText(2, 0, "High Score: " + to_string(MaxSofar));
}
// Shows score info
void GraphicsDisplay::showNextBlock(Block b){
promptRow = std::min((HEIGHT - 1) - b.getHeight() - 2,
HEIGHT - 1 - 4);
drawText(promptRow, 0, "Next:");
for(auto c : b.getArea()){
int row = (c.row - b.getRow()) + (HEIGHT - 2);
int col = WIDTH + 2 + c.col;
drawCell(row, col, getColour(c.content));
}
}
int GraphicsDisplay::getColour(Content c){
switch(c){
case Content::Hint : return Xwindow::Pink;
case Content::Empty : return Xwindow::Grey;
case Content::Extra : return Xwindow::Brown;
case Content::I : return Xwindow::Red;
case Content::J : return Xwindow::Green;
case Content::O : return Xwindow::Blue;
case Content::L : return Xwindow::Yellow;
case Content::S : return Xwindow::Purple;
case Content::Z : return Xwindow::Orange;
case Content::T : return Xwindow::Black;
} return Xwindow::White;
}
void GraphicsDisplay::showGameOver(){
drawText((promptRow + 3)/2, 1, "Game over!");
}
ostream &operator<<(std::ostream &out, GraphicsDisplay &gd){
for (int row=HEIGHT-1; row>=0; row--)
for (int col=0; col<WIDTH; col++)
if(gd.prev.size() == 0 ||
(*(gd.board))[row][col].content != gd.prev[row][col].content)
gd.drawCell(row, col, gd.getColour((*(gd.board))[row][col].content));
gd.prev = *(gd.board);
return out;
}