-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.c
168 lines (128 loc) · 2.97 KB
/
simulator.c
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
#include "universe.h"
#include "physics.h"
#include "render.h"
#include "simulator.h"
#include "input.h"
#include <SDL2/SDL.h>
#include <getopt.h>
#include <stdio.h>
#include <unistd.h>
struct simulation *simulationInit();
void simulateMode(struct renderstate *, struct universe *, struct simulation *);
int main(int argc, char **argv)
{
// args setup
const char *shortOpts = "f:";
struct option longOpts[] = {
{"file", required_argument, 0, 'f'},
{0, 0, 0, 0}
};
char c = 0;
int optionIndex = 0;
// file for loading universe
char *universeFilePath = 0;
FILE *universeFile = 0;
// state information
struct universe *univ = 0;
struct renderstate *render = 0;
struct simulation *sim = 0;
// process arguments
while ((c =
getopt_long(argc, argv, shortOpts, longOpts,
&optionIndex)) != -1) {
switch (c) {
case 'f':
universeFilePath = optarg;
break;
default:
return -1;
}
}
// attempt to open file
if (universeFilePath) {
universeFile = fopen(universeFilePath, "r");
if (!universeFile) {
fprintf(stderr, "Failed to open file %s\n",
universeFilePath);
return -2;
}
}
// create universe and renderer
if (!universeFile) {
// new universe
univ = universeInit(10);
// new renderstate
render = renderstateInit("Particle Simulator", 640, 480);
} else {
render = renderstateInitFromFile(universeFile);
univ = universeInitFromFile(universeFile);
if (render == 0) {
fprintf(stderr, "Error loading rendering struct.\n");
}
if (univ == 0) {
fprintf(stderr, "Error loading universe struct.\n");
}
fclose(universeFile);
}
// check for errors
if (!univ) {
fprintf(stderr, "Failed to initialize universe\n");
return -3;
}
if (!render) {
fprintf(stderr, "Failed to initialize the renderer\n");
return -4;
}
// Create the simulation state
sim = simulationInit();
// run simulation
simulateMode(render, univ, sim);
// finished
freeUniverse(univ);
freeRenderstate(render);
return 0;
}
void
simulateMode(struct renderstate *render, struct universe *univ,
struct simulation *sim)
{
sim->running = 1;
// main process loop
while (sim->running) {
handleInput(sim, univ, render);
if (sim->paused == 0) {
physicsApply(univ);
}
renderUniverse(render, sim, univ);
}
}
struct simulation *simulationInit()
{
struct simulation *sim =
(struct simulation *)malloc(sizeof(struct simulation));
sim->running = 0;
sim->paused = 1;
sim->state = SIMULATION_NORMAL;
sim->hotParticle = -1;
sim->lastClickX = -1;
sim->lastClickX = -1;
sim->savingTime = -1;
return sim;
}
void saveToFile(struct renderstate *render, struct universe *univ)
{
char fileName[20] = { 0 };
FILE *file = 0;
int fileCount;
for (fileCount = 1; fileCount < 999; fileCount++) {
sprintf(fileName, "universe-%d.save", fileCount);
if (access(fileName, F_OK) == -1) {
// file does not exist
break;
}
}
file = fopen(fileName, "w");
renderstateToFile(render, file);
universeToFile(univ, file);
fclose(file);
}