-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
201 lines (185 loc) · 6.75 KB
/
common.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
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
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <GLFW/glfw3.h>
#include "common.h"
#include "linmath.h"
#define FPS_BUFF_SIZE 256
#define DEFAULT_CONFIG_FILE "constel.conf"
#define CONFIG_STARS "Stars"
#define CONFIG_GALAXY_DENSITY "GalaxyDens"
#define CONFIG_STAR_SPEED "StarSpeed"
#define CONFIG_GRAVITY "Gravity"
#define CONFIG_EPSILON "Epsilon"
#define CONFIG_ACCURACY "Accuracy"
#define CONFIG_SPEED "Speed"
#define CONFIG_MIN_FPS "MinFPS"
#define CONFIG_MAX_FPS "MaxFPS"
#define CONFIG_DEFAULT_ZOOM "DefaultZoom"
#define CONFIG_MSAA "MSAA"
#define CONFIG_SHOW_STATUS "ShowStatus"
#define CONFIG_FONT "Font"
#define CONFIG_TEXT_SIZE "TextSize"
#define CONFIG_TEXT_COLOR "TextColor"
vec2* disp_star_position = NULL; // display coordinates, float
vec3* disp_star_color = NULL; // star colors
// Must be freed by the caller
char* read_file(const char *filename, int *length)
{
FILE *file = fopen(filename, "r");
if (!file) {
fprintf(stderr, "Cannot open '%s': %s\n", filename, strerror(errno));
*length = 0;
return NULL;
}
fseek(file, 0, SEEK_END);
*length = ftell(file);
rewind(file);
char* buffer = (char*) malloc((*length + 1) * sizeof(char));
*length = fread(buffer, 1, *length, file);
fclose(file);
buffer[*length] = '\0';
return buffer;
}
// returns actual frame duration
double frame_sleep()
{
static double last_time = NAN;
if (isnan(last_time))
last_time = glfwGetTime() - 1.0/config.max_fps;
double last_interval = glfwGetTime() - last_time;
double sleep_interval = 1.0/config.max_fps - last_interval;
if (sleep_interval > 0) {
double intpart;
const struct timespec sleep_ts = { sleep_interval, 1e+9*modf(sleep_interval,&intpart) };
nanosleep(&sleep_ts, NULL);
}
last_interval = glfwGetTime() - last_time;
last_time += last_interval;
set_fps(1 / last_interval);
return last_interval;
}
// ============================== Configuration ===============================
static float text_color[] = { 0, 1, 0, 1 };
struct config config = {
.stars = 7000,
.galaxy_density = 10,
.star_speed = 1.4,
.gravity = 0.002,
.epsilon = 2,
.accuracy = 0.7,
.speed = 1,
.min_fps = 40,
.max_fps = 60,
.default_zoom = 25,
.show_status = true,
.font = "/usr/share/fonts/TTF/FreeSans.ttf",
.text_size = 14,
.text_color = &text_color,
};
static char* config_lines[1024] = { NULL }; // TODO: config saving
static size_t config_line_lengths[1024] = { 0 };
void finalize_config()
{
for (int i = 0; i < sizeof(config_lines)/sizeof(config_lines[0]); i++)
free(config_lines[i]);
}
void init_config(const char* filename)
{
if (!filename)
filename = DEFAULT_CONFIG_FILE;
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "Cannot open '%s': %s\n", filename, strerror(errno));
return;
}
for (int n = 0; getline(&config_lines[n], &config_line_lengths[n], file) >= 0; n++) {
char* key = config_lines[n];
while(*key == ' ' || *key == '\t')
key++;
if (*key == '#' || *key == '\n' || *key == '\0')
continue;
char* value = key;
while(*value != ' ' && *value != '\t' && *value != '\n' && *value != '\0')
value++;
while(*value == ' ' || *value == '\t')
value++;
if (*value == '\n' || *value == '\0')
continue;
char* value_end = value; // remove trailing newline
while (*value_end)
value_end++;
if (*(--value_end) == '\n')
*value_end = '\0';
if (!strncmp(key, CONFIG_STARS, sizeof(CONFIG_STARS)-1)) {
sscanf(value, "%d", &config.stars);
} else if (!strncmp(key, CONFIG_GALAXY_DENSITY, sizeof(CONFIG_GALAXY_DENSITY)-1)) {
sscanf(value, "%lf", &config.galaxy_density);
} else if (!strncmp(key, CONFIG_STAR_SPEED, sizeof(CONFIG_STAR_SPEED)-1)) {
sscanf(value, "%lf", &config.star_speed);
} else if (!strncmp(key, CONFIG_GRAVITY, sizeof(CONFIG_GRAVITY)-1)) {
sscanf(value, "%lf", &config.gravity);
} else if (!strncmp(key, CONFIG_EPSILON, sizeof(CONFIG_EPSILON)-1)) {
sscanf(value, "%lf", &config.epsilon);
} else if (!strncmp(key, CONFIG_ACCURACY, sizeof(CONFIG_ACCURACY)-1)) {
sscanf(value, "%lf", &config.accuracy);
} else if (!strncmp(key, CONFIG_SPEED, sizeof(CONFIG_SPEED)-1)) {
sscanf(value, "%lf", &config.speed);
} else if (!strncmp(key, CONFIG_MIN_FPS, sizeof(CONFIG_MIN_FPS)-1)) {
sscanf(value, "%lf", &config.min_fps);
} else if (!strncmp(key, CONFIG_MAX_FPS, sizeof(CONFIG_MAX_FPS)-1)) {
sscanf(value, "%lf", &config.max_fps);
} else if (!strncmp(key, CONFIG_DEFAULT_ZOOM, sizeof(CONFIG_DEFAULT_ZOOM)-1)) {
sscanf(value, "%lf", &config.default_zoom);
} else if (!strncmp(key, CONFIG_MSAA, sizeof(CONFIG_MSAA)-1)) {
sscanf(value, "%d", &config.msaa);
} else if (!strncmp(key, CONFIG_SHOW_STATUS, sizeof(CONFIG_SHOW_STATUS)-1)) {
config.show_status = !strncmp(value, "true", 4)
|| !strncmp(value, "True", 4)
|| !strncmp(value, "1", 1);
} else if (!strncmp(key, CONFIG_FONT, sizeof(CONFIG_FONT)-1)) {
config.font = value;
} else if (!strncmp(key, CONFIG_TEXT_SIZE, sizeof(CONFIG_TEXT_SIZE)-1)) {
sscanf(value, "%lf", &config.text_size);
} else if (!strncmp(key, CONFIG_TEXT_COLOR, sizeof(CONFIG_TEXT_COLOR)-1)) {
sscanf(value, "%f %f %f %f", &text_color[0], &text_color[1], &text_color[2], &text_color[3]);
}
}
fclose(file);
}
// =========================== Performance counters ===========================
static float fps_history[FPS_BUFF_SIZE];
static int fps_count = 0;
static int fps_pointer = 0;
// Get mean FPS among the last [frame] values
float get_fps(int frame)
{
if (fps_count == 0)
return 0;
if (frame < 1)
frame = 1;
if (frame > fps_count)
frame = fps_count;
float sum = 0;
for (int i = (fps_pointer - frame + FPS_BUFF_SIZE) % FPS_BUFF_SIZE;
i != fps_pointer; i = (i + 1) % FPS_BUFF_SIZE)
sum += fps_history[i];
return sum / frame;
}
// Get mean FPS for the last [period] seconds, according to the last FPS value
float get_fps_period(float period)
{
return get_fps(period * fps_history[(fps_pointer - 1 + FPS_BUFF_SIZE) % FPS_BUFF_SIZE]);
}
// Append a new FPS value
void set_fps(float value)
{
fps_history[fps_pointer] = value;
fps_pointer++;
fps_pointer %= FPS_BUFF_SIZE;
if (fps_count < FPS_BUFF_SIZE)
fps_count++;
}