From 5d3df4c03eeb586d32c8a210c3ef45ede014cb98 Mon Sep 17 00:00:00 2001 From: klemek Date: Sun, 28 Sep 2025 19:32:58 +0200 Subject: [PATCH] wip state --- config/forge.cfg | 3 --- src/config.h | 4 ++++ src/forge.c | 30 +++++++++++++++--------------- src/shaders.c | 7 ++++--- src/shaders.h | 4 +++- src/state.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- src/state.h | 7 +++++++ src/types.h | 21 +++++++++++++++++++++ 8 files changed, 101 insertions(+), 23 deletions(-) diff --git a/config/forge.cfg b/config/forge.cfg index f6d7ab6..d61a46e 100644 --- a/config/forge.cfg +++ b/config/forge.cfg @@ -18,9 +18,6 @@ SUB_TYPE_COUNT=2 SUB_1_PREFIX=src_ SUB_2_PREFIX=fx_ -# TODO remove -SUB_VARIANT_COUNT=15 - # TEXTURE I/O TEX_COUNT=10 diff --git a/src/config.h b/src/config.h index 03b0cb3..86af765 100644 --- a/src/config.h +++ b/src/config.h @@ -17,4 +17,8 @@ #define MAX_FRAG 64 #endif +#ifndef UNSET_MIDI_CODE +#define UNSET_MIDI_CODE 300 +#endif + #endif /* CONFIG_H */ \ No newline at end of file diff --git a/src/forge.c b/src/forge.c index 6b94ac2..38a6ede 100644 --- a/src/forge.c +++ b/src/forge.c @@ -12,6 +12,7 @@ #include "rand.h" #include "shaders.h" #include "shared.h" +#include "state.h" #include "timer.h" #include "types.h" #include "video.h" @@ -27,6 +28,7 @@ static File common_shader_code; static Timer timer; static ConfigFile config; static MidiDevice midi; +static StateConfig state_config; static void compute_fps() { double fps; @@ -51,14 +53,6 @@ static void compute_fps() { } } -// TODO put in state file -static void randomize_context_state() { - unsigned int i; - - for (i = 0; i < program.frag_count; i++) { - context->state[i] = rand_uint(program.sub_variant_count); - } -} static void init_context(Parameters params) { unsigned int i; @@ -69,7 +63,7 @@ static void init_context(Parameters params) { memset(context->state, 0, sizeof(context->state)); if (params.demo) { - randomize_context_state(); + state_randomize(context, state_config, program.frag_count); } memset(context->seeds, 0, sizeof(context->seeds)); @@ -206,7 +200,7 @@ static void key_callback(Window *window, int key, window_close(window); } else if (window_char_key(key, action, 82)) { // R: randomize - randomize_context_state(); + state_randomize(context, state_config, program.frag_count); } else if (window_char_key(key, action, 68)) { // D: demo on/off context->demo = !context->demo; @@ -256,6 +250,8 @@ void forge_run(Parameters params) { config = config_file_read(params.config_path, false); + state_config = state_parse_config(config); + frag_count = config_file_get_int(config, "FRAG_COUNT", 6); init_files(params.frag_path, frag_count); @@ -286,8 +282,9 @@ void forge_run(Parameters params) { window_use(window_output, context); - program = shaders_init(fragment_shaders, config, context, inputs, - params.video_in_count, NULL); + program = shaders_init( + fragment_shaders, config, context, inputs, params.video_in_count, + state_config.select_page_count * state_config.select_item_count, NULL); } else { window_output = NULL; } @@ -299,9 +296,10 @@ void forge_run(Parameters params) { window_use(window_monitor, context); - program = shaders_init(fragment_shaders, config, context, inputs, - params.video_in_count, - window_output != NULL ? &program : NULL); + program = shaders_init( + fragment_shaders, config, context, inputs, params.video_in_count, + state_config.select_page_count * state_config.select_item_count, + window_output != NULL ? &program : NULL); } else { window_monitor = NULL; } @@ -347,5 +345,7 @@ void forge_run(Parameters params) { config_file_free(config); + state_free_config(state_config); + window_terminate(); } \ No newline at end of file diff --git a/src/shaders.c b/src/shaders.c index 2a72de1..cf9f349 100644 --- a/src/shaders.c +++ b/src/shaders.c @@ -366,7 +366,9 @@ static void init_programs(ShaderProgram *program, ConfigFile config) { ShaderProgram shaders_init(File *fragment_shaders, ConfigFile config, SharedContext *context, VideoCapture *inputs, - unsigned int input_count, ShaderProgram *previous) { + unsigned int input_count, + unsigned int sub_variant_count, + ShaderProgram *previous) { ShaderProgram program; if (previous == NULL) { @@ -380,8 +382,7 @@ ShaderProgram shaders_init(File *fragment_shaders, ConfigFile config, program.frag_monitor_index = config_file_get_int(config, "FRAG_MONITOR", 1) - 1; program.sub_type_count = config_file_get_int(config, "SUB_TYPE_COUNT", 0); - program.sub_variant_count = - config_file_get_int(config, "SUB_VARIANT_COUNT", 0); + program.sub_variant_count = sub_variant_count; program.in_count = config_file_get_int(config, "IN_COUNT", 0); if (program.frag_count > MAX_FRAG) { diff --git a/src/shaders.h b/src/shaders.h index be2547e..303add6 100644 --- a/src/shaders.h +++ b/src/shaders.h @@ -5,7 +5,9 @@ ShaderProgram shaders_init(File *fragment_shaders, ConfigFile config, SharedContext *context, VideoCapture *inputs, - unsigned int input_count, ShaderProgram *previous); + unsigned int input_count, + unsigned int sub_variant_count, + ShaderProgram *previous); void shaders_update(ShaderProgram program, File *fragment_shaders, unsigned int i); diff --git a/src/state.c b/src/state.c index b030e69..f0dfa5a 100644 --- a/src/state.c +++ b/src/state.c @@ -1 +1,47 @@ -#include "state.h" \ No newline at end of file +#include "state.h" +#include "config.h" +#include "config_file.h" +#include "rand.h" +#include "types.h" + +StateConfig state_parse_config(ConfigFile config) { + StateConfig state_config; + + state_config.select_page_count = + config_file_get_int(config, "SELECT_PAGE_COUNT", 0); + + // TODO parse + + state_config.select_item_count = + config_file_get_int(config, "SELECT_ITEM_COUNT", 0); + + // TODO parse + + state_config.src_count = config_file_get_int(config, "SRC_COUNT", 0); + + // TODO parse + + state_config.fader_count = config_file_get_int(config, "FADER_COUNT", 0); + + // TODO parse + + state_config.tap_tempo_code = + config_file_get_int(config, "TAP_TEMPO", UNSET_MIDI_CODE); + + return state_config; +} + +void state_free_config(StateConfig state_config) { + // TODO +} + +void state_randomize(SharedContext *context, StateConfig state_config, + unsigned int state_count) { + unsigned int i; + + for (i = 0; i < state_count; i++) { + context->state[i] = + rand_uint(state_config.select_page_count * + state_config.select_item_count); // TODO from config + } +} \ No newline at end of file diff --git a/src/state.h b/src/state.h index 7d511f1..62199de 100644 --- a/src/state.h +++ b/src/state.h @@ -3,4 +3,11 @@ #ifndef STATE_H #define STATE_H +StateConfig state_parse_config(ConfigFile config); + +void state_randomize(SharedContext *context, StateConfig state_config, + unsigned int state_count); + +void state_free_config(StateConfig state_config); + #endif /* STATE_H */ \ No newline at end of file diff --git a/src/types.h b/src/types.h index fd8b455..d329314 100644 --- a/src/types.h +++ b/src/types.h @@ -136,6 +136,27 @@ typedef struct SharedContext { bool stop; } SharedContext; +typedef struct StateConfig { + unsigned int select_page_count; + unsigned int *select_page_codes; + + unsigned int select_item_count; + unsigned int *select_item_codes; + + unsigned int *select_frag_codes; + + unsigned int src_count; + unsigned int *src_active_counts; + unsigned int *src_active; + unsigned int *src_subcounts; + unsigned int *src_codes; + + unsigned int fader_count; + unsigned int *fader_codes; + + unsigned int tap_tempo_code; +} StateConfig; + typedef struct Timer { struct timeval start; unsigned int counter;