-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
140 lines (107 loc) · 3.22 KB
/
main.cpp
File metadata and controls
140 lines (107 loc) · 3.22 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
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <SDL.h>
#include "CSDLManagerLite.h"
#include "CSDLInputManagerLite.h"
#include "defs.h"
#include "Turmite.h"
using namespace std;
struct Color {
int r, g, b;
Color() {
r = 0;
g = 0;
b = 0;
}
Color(int rr, int gg, int bb) {
r = rr;
g = gg;
b = bb;
}
};
int cells[HEIGHT/CELLSIZE][WIDTH/CELLSIZE];
int rows, cols;
int numStates;
string movementPattern;
bool pause = false, keydown = false, toToggle = false;
Color* palette;
Color termiteColor = Color(255, 0, 0);
void update() {
CSDLManagerLite::getInstance() -> drawFrame();
//CSDLManagerLite::getInstance() -> background();
CSDLInputManagerLite::getInstance() -> update();
}
void iteration(Turmite* t) {
int tempColor = cells[t->r][t->c];
cells[t->r][t->c]++;
if (cells[t->r][t->c] == numStates)
cells[t->r][t->c] = 0;
t -> move(tempColor);
}
void redrawGrid(){
CSDLManagerLite::getInstance() -> background();
for (int r = 0; r < rows; r++){
for (int c = 0; c < cols; c++){
if (cells[r][c] > 0){
int temp = cells[r][c];
CSDLManagerLite::getInstance() -> setColor(palette[temp].r, palette[temp].g, palette[temp].b, 255);
CSDLManagerLite::getInstance() -> drawRectTLCorner(c*CELLSIZE, r*CELLSIZE, CELLSIZE, CELLSIZE);
}
}
}
}
int main(int argc, char *argv[]) {
rows = HEIGHT/CELLSIZE;
cols = WIDTH/CELLSIZE;
cout << "Please enter turmite movement pattern: ";
getline(cin, movementPattern);
numStates = movementPattern.length();
palette = new Color[numStates];
Turmite* bob = new Turmite(rows/2, cols/2, 0, rows, cols, movementPattern);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
cells[r][c] = 0;
}
}
srand(time(NULL)); //Seed
palette[0] = Color(0,0,0);
for (int k = 1; k < numStates; k++) {
palette[k] = Color(rand()%256, rand()%256, rand()%256);
}
CSDLManagerLite::getInstance()->initializeSDL(WIDTH, HEIGHT, TITLE);
int steps = 0;
/*while (SDL_GetTicks() < 1000){
update();
continue;
} */ //useful when recording
while (!CSDLInputManagerLite::getInstance() -> isExit()) {
if (CSDLInputManagerLite::getInstance() -> isKeyDown(SDL_SCANCODE_SPACE)){
keydown = true;
toToggle = true;
} else{
keydown = false;
}
if (toToggle && !keydown){
pause = !pause;
toToggle = false;
}
if (pause){
CSDLInputManagerLite::getInstance() -> update();
continue;
}
iteration(bob);
if (steps%STEPSPERFRAME == 0){
redrawGrid();
CSDLManagerLite::getInstance() -> setColor(termiteColor.r, termiteColor.g, termiteColor.b, 255);
CSDLManagerLite::getInstance() -> drawRectTLCorner(bob->c*CELLSIZE, bob->r*CELLSIZE, CELLSIZE, CELLSIZE);
update();
}
steps++;
}
cout << "steps taken: " + steps << endl;
CSDLManagerLite::getInstance()->clean();
delete bob;
delete[] palette;
return 0;
}