refactor: store init params in main program
This commit is contained in:
+34
-33
@@ -31,14 +31,14 @@ static MidiDevice midi;
|
||||
static bool trace_midi;
|
||||
static Project project;
|
||||
|
||||
static void compute_fps(bool trace_fps) {
|
||||
static void compute_fps() {
|
||||
double fps;
|
||||
char title[STR_LEN];
|
||||
|
||||
if (timer_inc(&timer)) {
|
||||
fps = timer_reset(&timer);
|
||||
|
||||
if (trace_fps) {
|
||||
if (init_params.trace_fps) {
|
||||
log_trace("(main) %.2ffps", fps);
|
||||
}
|
||||
|
||||
@@ -56,13 +56,14 @@ static void compute_fps(bool trace_fps) {
|
||||
}
|
||||
}
|
||||
|
||||
static void init_context(const Parameters *params) {
|
||||
static void init_context() {
|
||||
context = shared_init_context("/" PACKAGE "_context");
|
||||
|
||||
context->stop = false;
|
||||
|
||||
state_init(context, &project.state_config, params->demo, params->auto_random,
|
||||
params->auto_random_cycle, params->base_tempo, params->load_state);
|
||||
state_init(context, &project.state_config, init_params.demo,
|
||||
init_params.auto_random, init_params.auto_random_cycle,
|
||||
init_params.base_tempo, init_params.load_state);
|
||||
|
||||
memset(context->input_resolutions, 0, sizeof(context->input_resolutions));
|
||||
}
|
||||
@@ -74,11 +75,12 @@ static void reload_shader(unsigned int i) {
|
||||
}
|
||||
|
||||
#ifdef VIDEO_IN
|
||||
static void init_inputs(const StringArray *video_in, unsigned int video_size) {
|
||||
context->inputs.length = video_in->length;
|
||||
static void init_inputs() {
|
||||
context->inputs.length = init_params.video_in.length;
|
||||
|
||||
for (unsigned int i = 0; i < video_in->length; i++) {
|
||||
video_init(&context->inputs.values[i], video_in->values[i], video_size);
|
||||
for (unsigned int i = 0; i < init_params.video_in.length; i++) {
|
||||
video_init(&context->inputs.values[i], init_params.video_in.values[i],
|
||||
init_params.video_size);
|
||||
|
||||
if (!context->inputs.values[i].error) {
|
||||
context->input_resolutions[i][0] = context->inputs.values[i].width;
|
||||
@@ -89,18 +91,18 @@ static void init_inputs(const StringArray *video_in, unsigned int video_size) {
|
||||
}
|
||||
}
|
||||
|
||||
static bool reconnect_video_captures(const StringArray *video_in,
|
||||
unsigned int video_size, bool trace_fps) {
|
||||
for (unsigned int i = 0; i < video_in->length; i++) {
|
||||
static bool reconnect_video_captures() {
|
||||
for (unsigned int i = 0; i < init_params.video_in.length; i++) {
|
||||
if (context->inputs.values[i].disconnected) {
|
||||
video_init(&context->inputs.values[i], video_in->values[i], video_size);
|
||||
video_init(&context->inputs.values[i], init_params.video_in.values[i],
|
||||
init_params.video_size);
|
||||
|
||||
if (!context->inputs.values[i].error) {
|
||||
context->input_resolutions[i][0] = context->inputs.values[i].width;
|
||||
context->input_resolutions[i][1] = context->inputs.values[i].height;
|
||||
|
||||
if (!video_background_read(&context->inputs.values[i], context, i,
|
||||
trace_fps)) {
|
||||
init_params.trace_fps)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -112,12 +114,12 @@ static bool reconnect_video_captures(const StringArray *video_in,
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool start_video_captures(unsigned int video_count, bool trace_fps) {
|
||||
static bool start_video_captures() {
|
||||
pid_t pid;
|
||||
for (unsigned int i = 0; i < video_count; i++) {
|
||||
for (unsigned int i = 0; i < context->inputs.length; i++) {
|
||||
if (!context->inputs.values[i].error &&
|
||||
!video_background_read(&context->inputs.values[i], context, i,
|
||||
trace_fps)) {
|
||||
init_params.trace_fps)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -135,16 +137,15 @@ static bool start_video_captures(unsigned int video_count, bool trace_fps) {
|
||||
log_info("background reconnect acquisition started (pid: %d)", pid);
|
||||
while (!context->stop) {
|
||||
sleep(1);
|
||||
if (!reconnect_video_captures(&init_params.video_in, init_params.video_size,
|
||||
init_params.trace_fps)) {
|
||||
if (!reconnect_video_captures()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static void free_video_captures(unsigned int video_count) {
|
||||
for (unsigned int i = 0; i < video_count; i++) {
|
||||
static void free_video_captures() {
|
||||
for (unsigned int i = 0; i < context->inputs.length; i++) {
|
||||
shaders_free_input(&program, i);
|
||||
|
||||
video_free(&context->inputs.values[i]);
|
||||
@@ -190,12 +191,12 @@ static bool init(const Parameters *params) {
|
||||
return false;
|
||||
}
|
||||
|
||||
init_context(params);
|
||||
init_context();
|
||||
|
||||
#ifdef VIDEO_IN
|
||||
init_inputs(¶ms->video_in, params->video_size);
|
||||
init_inputs();
|
||||
|
||||
if (!start_video_captures(params->video_in.length, params->trace_fps)) {
|
||||
if (!start_video_captures()) {
|
||||
return false;
|
||||
}
|
||||
#endif /* VIDEO_IN */
|
||||
@@ -265,8 +266,8 @@ static bool should_close() {
|
||||
(window_monitor != NULL && window_should_close(window_monitor));
|
||||
}
|
||||
|
||||
static bool loop(bool hr, bool trace_fps) {
|
||||
if (hr) {
|
||||
static bool loop() {
|
||||
if (init_params.hot_reload) {
|
||||
project_reload(&project, reload_shader);
|
||||
}
|
||||
|
||||
@@ -281,7 +282,7 @@ static bool loop(bool hr, bool trace_fps) {
|
||||
}
|
||||
#endif /* VIDEO_IN */
|
||||
|
||||
compute_fps(trace_fps);
|
||||
compute_fps();
|
||||
|
||||
context->time = window_get_time();
|
||||
context->tempo_total = (float)tempo_total(&context->tempo);
|
||||
@@ -307,10 +308,10 @@ static bool loop(bool hr, bool trace_fps) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static void shutdown(const Parameters *params) {
|
||||
static void shutdown() {
|
||||
context->stop = true;
|
||||
|
||||
if (params->save_state) {
|
||||
if (init_params.save_state) {
|
||||
state_save(context, &project.state_config);
|
||||
}
|
||||
|
||||
@@ -325,11 +326,11 @@ static void shutdown(const Parameters *params) {
|
||||
if (window_monitor != NULL) {
|
||||
window_use(window_monitor, context);
|
||||
|
||||
shaders_free_window(&program, params->output);
|
||||
shaders_free_window(&program, init_params.output);
|
||||
}
|
||||
|
||||
#ifdef VIDEO_IN
|
||||
free_video_captures(params->video_in.length);
|
||||
free_video_captures();
|
||||
#endif /* VIDEO_IN */
|
||||
|
||||
free_context();
|
||||
@@ -345,10 +346,10 @@ void forge_run(const Parameters *params) {
|
||||
}
|
||||
|
||||
while (!should_close()) {
|
||||
if (!loop(params->hot_reload, params->trace_fps)) {
|
||||
if (!loop()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
shutdown(params);
|
||||
shutdown();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user