-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
112 lines (95 loc) · 2.9 KB
/
Grid.cpp
File metadata and controls
112 lines (95 loc) · 2.9 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
106
107
108
109
110
111
112
#include "Grid.h"
Grid::Grid(int sizeC):DrawObject(-sizeC/2,-sizeC/2,1){
gridSize = sizeC;
X = -gridSize/2;
Y = -gridSize/2;
discoMode = true;
//TODO: GET FROM AN EXTERN TXT
cells = 15;
cellsSeparation = 5; //px UNITS
//BUILD
buildMatrix();
};
Grid::~Grid(){
destroyMatrix();
};
//***********************************************
// OTHER FUNCTIONS
//***********************************************
void Grid::buildMatrix(){
cellSize = (gridSize/cells) - cellsSeparation;
margin = (int) (gridSize - (cells*(cellSize + cellsSeparation)))/2; //IF STAGE WIDTH ISN'T A MULTIPLE OF CELL SIZE, AN EXTRA BORDER APPEARS
//DINAMICALLY CREATES THE GRID MATRIX
cellsArray = new int*[cells];
for(int i = 0; i < cells; i++){
*(cellsArray + i) = new int[cells];
for(int j = 0; j < cells; j++){
cellsArray[i][j] = 0;
}
}
};
void Grid::destroyMatrix(){
for(int i = 0; i < cells; i++){
delete[] *(cellsArray + i);
}
delete[] cellsArray;
};
void Grid::draw(BITMAP *dest){
CANVAS = dest;
double correctedX = X - CAMERA->getX();
double correctedY = Y - CAMERA->getY();
//CELLS
for (int i = 0; i < cells; i++){
for (int j = 0; j < cells; j++){
if (discoMode){
float _x = correctedX + margin + (i * (cellSize + cellsSeparation));
float _y = correctedY + margin + (j * (cellSize + cellsSeparation));
rectfill(dest, _x, _y, _x + cellSize, _y + cellSize, makecol(25 + cellsArray[i][j], 25 + cellsArray[i][j], 35 + cellsArray[i][j]));
//DISCO EFFECT DECAY
if (cellsArray[i][j]>0) cellsArray[i][j] -= discoFadeRatio;
}else{
rectfill(dest,
correctedX + i * cellSize + margin,
correctedY + j * cellSize + margin,
correctedX + i * cellSize + cellsSeparation,
correctedY + j * cellSize + cellsSeparation,
makecol(15,15,30));
}
}
}
//BORDER
rect(dest, correctedX, correctedY, correctedX + gridSize, correctedY + gridSize, makecol(180,180,255));
};
void Grid::highlight(int x, int y){
int cellToHighlightX = (int)((x + (gridSize/2)) / (cellSize + cellsSeparation));
int cellToHighlightY = (int)((y + (gridSize/2)) / (cellSize + cellsSeparation));
//cellToHighlight En |cellsArray|
if (cellToHighlightX >= 0 && cellToHighlightX < cells && cellToHighlightY >= 0 && cellToHighlightY < cells){
cellsArray[cellToHighlightX][cellToHighlightY] = 100;
}
};
void Grid::fade(int value){
fadeValue = value;
fading = true;
};
bool Grid::setMode(int value){
if(value >= 0 && value <= 2){
mode = value;
return true;
}
return false;
};
void Grid::moreCells(){
if(cells < 30){
destroyMatrix();
cells++;
buildMatrix();
}
};
void Grid::lessCells(){
if(cells > 1){
destroyMatrix();
cells--;
buildMatrix();
}
};