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