-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
216 lines (167 loc) · 5.26 KB
/
main.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#define GL_GLEXT_PROTOTYPES
#include <stdio.h>
#include <GL/glut.h>
#include <vector>
#include <algorithm> // min
#include "atomos.hpp"
#include "volumes/environment.hpp"
#include "pipe.hpp"
#include "time.hpp"
bool enableTimingPrintouts = false;
GLuint window;
int window_wd = 300;
int window_ht = 300;
uint frameCount = 0;
const int simulation_wd = 100;
const int simulation_ht = 100;
GLubyte pixelData[simulation_ht * simulation_wd * 4];
std::vector<Environment*> volumes;
std::vector<Pipe*> pipes;
// void mouseHandler(int, int state, int, int) {
// if (state == GLUT_DOWN) {
// printf("mousepressed\n");
// }
// // glutPostRedisplay();
// }
void cleanUp() {
printf("starting clean up\n");
glutDestroyWindow(window);
// note: delete pipes before volumes
for (Pipe* p : pipes) {
delete p;
}
pipes.clear();
for (Environment* e : volumes) {
delete e;
}
volumes.clear();
printf("finished clean up\n");
}
void reshapeHandler(int wd, int ht) {
window_wd = wd;
window_ht = ht;
float scale = std::min(
(float)window_wd / simulation_wd,
(float)window_ht / simulation_ht
);
glPixelZoom(scale, scale);
}
// Input
void keyHandler(unsigned char key, int, int) {
switch (key) {
case 27: // Escape -> exit
case 'q':
cleanUp();
exit (0);
break; // lol
}
}
// prints something identifyable and the time in miliseconds since the last call of this function
void doTiming(const char *description) {
if (enableTimingPrintouts) {
printf("[ %s: %5.1fms ] ", description, time_dt()*1000.0);
}
}
std::vector<Molecule_ptr> &getMolecules(unsigned x, unsigned y) {
return volumes.at(y * simulation_wd + x)->molecules;
}
void printVolume(unsigned x, unsigned y) {
for (const Molecule_ptr &mol : getMolecules(x,y)) {
printf((mol)? "1" : "0");
}
printf("\n");
}
// the body of the main loop
void tick() {
doTiming("outside of tick()");
frameCount += 1;
// printf("frame %d\n", frameCount);
// for (int i=0; i<5; ++i) {
Atomos::getInstance().tick(); // where the magic happens
// }
doTiming("repeated atomos::tick()");
// interpret the volumes as a pixel grid
for (int y=0; y<simulation_ht; ++y){
for (int x=0; x<simulation_wd; ++x) {
int index = y * simulation_wd + x;
std::vector<Molecule_ptr> &mols = getMolecules(x, y);
unsigned molCount = 0;
for (const Molecule_ptr &mol : mols) {
if (mol) {
molCount += 1;
}
}
// red if completely full
// black if completely empty
pixelData[4 * index] = (
1.0 * (molCount * 255) / mols.size() +
0.0 * pixelData[4 * index]
);
}
}
doTiming("pixelData filling");
// do a debug print of the one volume
// printVolume(simulation_wd*0.5, simulation_ht*0.5);
// draw the pixel data
// we are using old openGL but this is quick, simple and dirty
glClearColor(0.1, 0.1, 0.1, 1.0); // have a dark grey background
glClear(GL_COLOR_BUFFER_BIT);
glDrawPixels(simulation_wd, simulation_ht, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
glutSwapBuffers();
glutPostRedisplay(); // active rendering
doTiming("draw");
if (enableTimingPrintouts) {
printf("\n");
}
}
int main(int argc, char** argv) {
printf("hello world\n");
time_init();
// create the atmos grid
for (int y=0; y<simulation_ht; ++y) {
for (int x=0; x<simulation_wd; ++x) {
Environment *newEnv = new Environment(8);
volumes.push_back(newEnv);
// add connections to existing volumes in a grid
if (x > 0) { // horizontal
Pipe* p = new Pipe();
newEnv->takeControlOf(&p->A),
volumes.at(y*simulation_wd + x-1)->takeControlOf(&p->B),
p->checkConnections();
pipes.push_back(p);
}
if (y > 0) { // vertical
Pipe* p = new Pipe();
newEnv->takeControlOf(&p->A);
volumes.at((y-1)*simulation_wd + x)->takeControlOf(&p->B);
p->checkConnections();
pipes.push_back(p);
}
}
}
doTiming("graph creation");
// fill a region of the grid to its capacity
for (unsigned y=simulation_ht*0.25; y<=simulation_ht*0.5; ++y) {
for (unsigned x=simulation_wd*0.25; x<=simulation_wd*0.5; ++x) {
std::vector<Molecule_ptr> &mols = getMolecules(x, y);
for (unsigned i=0; i < mols.size(); ++i) {
mols.at(i).reset(new Molecule(1337));
}
}
}
doTiming("molecule creation");
// set up the window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(window_wd, window_ht);
window = glutCreateWindow("atomos");
glutDisplayFunc(tick);
glutReshapeFunc(reshapeHandler);
// glutIdleFunc(AnimateScene);
glutKeyboardFunc(keyHandler);
// glutMouseFunc(mouseHandler);
doTiming("window setup");
printf("\n");
glutMainLoop();
return 0;
}