Repository navigation
Expand file tree
/
Copy pathesempioAC.cpp
More file actions
154 lines (126 loc) · 3.04 KB
/
Copy pathesempioAC.cpp
File metadata and controls
154 lines (126 loc) · 3.04 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <allegro.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
// File di prova per Automi Cellulari
// Usa dati di setup dal file Configuration.txt
int **readMatrix;
int **writeMatrix;
int numStep;
int numRows;
int numCols;
int rowCentro;
int colCentro;
int raggio;
int black, white;
int dir = 1;
BITMAP *buffer;
void initAllegro() {
allegro_init();
install_keyboard();
set_color_depth(24);
buffer = create_bitmap(numCols, numRows);
set_gfx_mode( GFX_AUTODETECT_WINDOWED, numCols, numRows, 0, 0);
black = makecol(0, 0, 0);
white = makecol(255, 255, 255);
}
void readConfigFile() {
char str[20];
FILE *file;
file = fopen("Configuration.txt", "r");
if (file) {
fscanf(file, "%s", str);
numRows = atoi(str);
fscanf(file, "%s", str);
numCols = atoi(str);
fscanf(file, "%s", str);
rowCentro = atoi(str);
fscanf(file, "%s", str);
colCentro = atoi(str);
fscanf(file, "%s", str);
raggio = atoi(str);
fscanf(file, "%s", str);
numStep = atoi(str);
fclose(file);
}
}
void createMatrix(int **&m, int numRows, int numCols) {
m = new int*[numRows];
for (int row = 0; row < numRows; ++row) {
m[row] = new int[numCols];
}
}
void initModel() {
createMatrix(readMatrix, numRows, numCols);
createMatrix(writeMatrix, numRows, numCols);
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
if (sqrt(pow((row - rowCentro), 2) + pow((col - colCentro), 2))
<= raggio) {
readMatrix[row][col] = 1;
writeMatrix[row][col] = 1;
} else {
readMatrix[row][col] = 0;
writeMatrix[row][col] = 0;
}
}
}
}
void drawWithAllegro(int step) {
//draw every pixel with a color depending on the state
for (int row = 0; row < numRows; row++)
for (int col = 0; col < numCols; col++)
switch (readMatrix[row][col]) {
case 0:
putpixel(buffer, col, row, black);
break;
case 1:
putpixel(buffer, col, row, white);
break;
}
textprintf_ex(buffer, font, 0, 0, white, black, "step: %i", step);
//Move the modified buffer to the screen
// printf("numRows=%d numCols=%d\n",numRows,numCols);
blit(buffer, screen, 0, 0, 0, 0, numRows, numCols);
}
int incMod(int num, int inc, int dim){
return (num+inc+dim)%dim;
}
void transitionFunction(int row, int col) {
writeMatrix[row][col]= readMatrix[row][incMod(col,dir,numCols)];
}
void executeTransitionFunction() {
for (int row = 0; row < numRows; row++)
for (int col = 0; col < numCols; col++)
transitionFunction(row, col);
}
void swapMatrices() {
int **p = readMatrix;
readMatrix = writeMatrix;
// for (int row = 0; row < numRows; row++)
// for (int col = 0; col < numCols; col++)
// readMatrix[row][col]=writeMatrix[row][col];
writeMatrix = p;
}
void controlLoop() {
readConfigFile();
initAllegro();
initModel();
for (int step = 0; step < numStep; ++step) {
drawWithAllegro(step);
readkey();
if (key[KEY_LEFT])
dir = 1;
if (key[KEY_RIGHT])
dir = -1;
executeTransitionFunction();
swapMatrices();
}
readkey();
}
int main() {
controlLoop();
return 0;
}
END_OF_MAIN()