From 6bc6d66bbe4b644c958723b02564cc7d64b8266d Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sun, 20 Jun 2021 15:36:42 +0200 Subject: Add param cursor for sq1; fix bug in line drawing --- src/renderer.c | 42 ++++------- src/sequencer.c | 221 +++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 154 insertions(+), 109 deletions(-) (limited to 'src') diff --git a/src/renderer.c b/src/renderer.c index 0ce3107..07d79d6 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -128,35 +128,21 @@ draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) { } } else { // Diagonal line. - int x_step = x0 > x1 ? -1 : 1; - int y_step = y0 > y1 ? -1 : 1; - size_t dx = x0 > x1 ? x0 - x1 : x1 - x0; - size_t dy = y0 > y1 ? y0 - y1 : y1 - y0; - size_t x = x0; - size_t y = y0; - if (dx >= dy) { - // Positive slope. - int diff = 2 * dy - dx; - for (size_t i = 0; i <= dx; ++i) { - draw_pixel(x, y, clr); - if (diff >= 0) { - y += y_step; - diff -= 2 * dx; - } - x += x_step; - diff += 2 * dy; + int dx = x0 > x1 ? x0 - x1 : x1 - x0; + int dy = y0 > y1 ? y1 - y0 : y0 - y1; + int x_step = x0 < x1 ? 1 : -1; + int y_step = y0 < y1 ? 1 : -1; + int err = dx + dy; + while (!(x0 == x1 && y0 == y1)) { + draw_pixel(x0, y0, clr); + int diff = 2 * err; + if (diff >= dy) { + err += dy; + x0 += x_step; } - } else { - // Negative slope. - int diff = 2 * dx - dy; - for (size_t i = 0; i <= dy; ++i) { - draw_pixel(x, y, clr); - if (diff >= 0) { - x += x_step; - diff -= 2 * dy; - } - y -= y_step; - diff += 2 * dx; + if (diff <= dx) { + err += dx; + y0 += y_step; } } } diff --git a/src/sequencer.c b/src/sequencer.c index 2d9ca4f..2978e9b 100644 --- a/src/sequencer.c +++ b/src/sequencer.c @@ -610,9 +610,9 @@ draw_params_cursor_wave(size_t i, u8 clr) { 20, 20, 20, 20, // Default wave B. 20, 20, 20, 20, - // Mode seleciton. + // Mode selection. 20, - // Volume seleciton. + // Volume selection. 0, }; size_t cursor_length = 0; @@ -628,14 +628,40 @@ draw_params_cursor_wave(size_t i, u8 clr) { draw_line(x, y, x + cursor_length, y, clr); } +void +draw_params_cursor_sq1(size_t i, u8 clr) { + u8 x_positions[] = { + 0, // Duty. + 31, // Env. Vol. + 59, // Env. Direction. + 87, // Env. Time. + 118, // Sweep Number. + 146, // Sweep Time. + 132, // Sweep Direction. + }; + u8 y_positions[] = { + 20, // Duty. + 20, // Env. Vol. + 20, // Env. Direction. + 20, // Env. Time. + 20, // Sweep Number. + 20, // Sweep Time. + 0, // Sweep Direction. + }; + size_t cursor_length = 24; + size_t x = PARAMS_START_X + x_positions[i]; + size_t y = PARAMS_START_Y + PARAMS_H - 23 + y_positions[i]; + draw_line(x, y, x + cursor_length, y, clr); +} + void draw_params_cursor(size_t i, u8 clr) { switch (channel_selection_loc) { case 0: { - // draw_params_cursor_ch1(i, clr); + draw_params_cursor_sq1(i, clr); } break; case 1: { - // draw_params_cursor_ch2(i, clr); + // draw_params_cursor_sq2(i, clr); } break; case 2: { draw_params_cursor_wave(i, clr); @@ -884,17 +910,23 @@ draw_parameters_square(ChannelSquareParams *params, bool sweep) { size_t x = PARAMS_START_X + 43; size_t y = PARAMS_START_Y + PARAMS_H - 41; size_t x0 = x; - size_t y0 = y + 16 - params->env_volume; + size_t y0 = y + 15 - params->env_volume; size_t x1 = x + 8 * params->env_time; - size_t y1 = params->env_direction == 0 ? y : y + 16; + size_t y1 = params->env_direction == 0 ? y + 15 : y; size_t x2 = x + 8 * 7; size_t y2 = y1; + // Axis. draw_line(x, y - 2, x, y + 16, COL_CYAN); draw_line(x, y + 16, x + 8 * 7 + 2, y + 16, COL_CYAN); + // Env. - draw_line(x0, y0, x1, y1, COL_CYAN); - draw_line(x1, y1, x2, y2, COL_CYAN); + if (params->env_time == 0) { + draw_line(x1, y0, x2, y0, COL_CYAN); + } else { + draw_line(x0, y0, x1, y1, COL_CYAN); + draw_line(x1, y1, x2, y2, COL_CYAN); + } } // Env. volume. @@ -975,10 +1007,10 @@ draw_parameters_square(ChannelSquareParams *params, bool sweep) { char arr_down[2] = { 0x18, 0 }; switch (params->env_direction) { case 0: { - txt_drawf(arr_down, x + 9, y + 11, 6, COL_FG); + txt_drawf(arr_up, x + 9, y + 11, 6, COL_FG); } break; case 1: { - txt_drawf(arr_up, x + 9, y + 11, 6, COL_FG); + txt_drawf(arr_down, x + 9, y + 11, 6, COL_FG); } break; } } @@ -1090,7 +1122,7 @@ draw_parameters_square(ChannelSquareParams *params, bool sweep) { char arr_up[2] = { 0x19, 0 }; char arr_down[2] = { 0x18, 0 }; - switch (params->env_direction) { + switch (params->sweep_direction) { case 0: { txt_drawf(arr_up, x + 9, y + 11, 6, COL_FG); } break; @@ -1248,8 +1280,8 @@ void (*input_handler)(void); void handle_trigger_selection(void); void handle_channel_selection(void); -void handle_param_selection_ch1(void); -void handle_param_selection_ch2(void); +void handle_param_selection_sq1(void); +void handle_param_selection_sq2(void); void handle_param_selection_wave(void); void @@ -1299,80 +1331,103 @@ handle_channel_selection(void) { } void -handle_param_selection_ch1(void) { +handle_param_selection_sq1(void) { + // Go back to trigger selection. if (key_tap(KEY_A)) { - // TODO: draw param cursor. + draw_params_cursor(param_selection_loc, COL_BG); input_handler = handle_trigger_selection; - draw_trig_cursor(trig_selection_loc, COL_CURRENT_TRIG); + draw_trig_cursor(trig_selection_loc, COL_CURSOR); } - // // Move through the selected synth parameters. - // if (key_tap(KEY_LEFT)) { - // int max_param = 6; - // if (channel_selection_loc == 1) { - // max_param = 3; - // } - // if (param_selection_loc == 0) { - // param_selection_loc = max_param; - // } else { - // param_selection_loc = MAX(param_selection_loc - 1, 0); - // } - // } - // if (key_tap(KEY_RIGHT)) { - // int max_param = 6; - // if (channel_selection_loc == 1) { - // max_param = 3; - // } - // if (param_selection_loc == max_param) { - // param_selection_loc = 0; - // } else { - // param_selection_loc = MIN(param_selection_loc + 1, max_param); - // } - // } - // // Adjust the parameters up or down. - // if (key_tap(KEY_L) || key_tap(KEY_R)) { - // int inc; - // if (key_tap(KEY_L)) { - // inc = -1; - // } else { - // inc = 1; - // } - // switch (param_selection_loc) { - // case 0: { - // trig->env_volume = CLAMP(trig->env_volume + inc, 0, 15); - // } break; - // case 1: { - // trig->env_time = CLAMP(trig->env_time + inc, 0, 7); - // } break; - // case 2: { - // trig->env_direction ^= 1; - // } break; - // case 3: { - // trig->duty_cycle = CLAMP(trig->duty_cycle + inc, 0, 3); - // } break; - // case 4: { - // trig->sweep_number = CLAMP(trig->sweep_number + inc, 0, 7); - // } break; - // case 5: { - // trig->sweep_time = CLAMP(trig->sweep_time + inc, 0, 7); - // } break; - // case 6: { - // if (trig->sweep_direction == 0) { - // trig->sweep_direction = 1; - // } else { - // trig->sweep_direction = 0; - // } - // } break; - // } - // } + // Cursor movement. + if (key_tap(KEY_LEFT) || key_tap(KEY_RIGHT)) { + int inc = 0; + int loc = param_selection_loc; + if (key_tap(KEY_RIGHT)) { + if (loc < 5) { + inc = 1; + } else if (loc == 6) { + inc = -1; + } + } else { + if (loc <= 5) { + inc = -1; + } else if (loc == 6) { + inc = -2; + } + } + draw_params_cursor_sq1(param_selection_loc, COL_BG); + param_selection_loc = CLAMP(loc + inc, 0, 6); + draw_params_cursor_sq1(param_selection_loc, COL_CURSOR); + } + if (key_tap(KEY_UP) || key_tap(KEY_DOWN)) { + int inc = 0; + int loc = param_selection_loc; + if (key_tap(KEY_UP)) { + if (loc == 4) { + inc = 2; + } else if (loc == 5) { + inc = 1; + } else if (loc == 6) { + inc = -1; + } + } else { + if (loc == 4) { + inc = 2; + } else if (loc == 5) { + inc = 1; + } else if (loc == 6) { + inc = -1; + } + } + draw_params_cursor_sq1(param_selection_loc, COL_BG); + param_selection_loc = CLAMP(loc + inc, 0, 6); + draw_params_cursor_sq1(param_selection_loc, COL_CURSOR); + } + + // Adjust parameter. + if (key_tap(KEY_R) || key_tap(KEY_L)) { + int inc; + if (key_tap(KEY_L)) { + inc = -1; + } else { + inc = 1; + } + ChannelSquareParams *params = &ch1.params[trig_selection_loc]; + switch (param_selection_loc) { + case 0: { + params->duty_cycle = CLAMP(params->duty_cycle + inc, 0, 3); + } break; + case 1: { + params->env_volume = CLAMP(params->env_volume + inc, 0, 15); + } break; + case 2: { + params->env_direction ^= 1; + } break; + case 3: { + params->env_time = CLAMP(params->env_time + inc, 0, 7); + } break; + case 4: { + params->sweep_number = CLAMP(params->sweep_number + inc, 0, 3); + } break; + case 5: { + params->sweep_time = CLAMP(params->sweep_time + inc, 0, 3); + } break; + case 6: { + params->sweep_direction ^= 1; + } break; + } + draw_parameters(); + draw_params_cursor(param_selection_loc, COL_CURSOR); + } } void -handle_param_selection_ch2(void) { +handle_param_selection_sq2(void) { if (key_tap(KEY_A)) { - // TODO: draw param cursor. + draw_params_cursor(param_selection_loc, COL_BG); input_handler = handle_trigger_selection; - draw_trig_cursor(trig_selection_loc, COL_CURRENT_TRIG); + draw_trig_cursor(trig_selection_loc, COL_CURSOR); } // // Move through the selected synth parameters. // if (key_tap(KEY_LEFT)) { @@ -1438,12 +1493,14 @@ handle_param_selection_ch2(void) { void handle_param_selection_wave(void) { + // Go back to trigger selection. if (key_tap(KEY_A)) { - // Go back to trigger selection. draw_params_cursor(param_selection_loc, COL_BG); input_handler = handle_trigger_selection; draw_trig_cursor(trig_selection_loc, COL_CURSOR); } + + // Cursor movement. if (key_tap(KEY_LEFT) || key_tap(KEY_RIGHT)) { int inc = 0; int loc = param_selection_loc; @@ -1526,6 +1583,8 @@ handle_param_selection_wave(void) { param_selection_loc = CLAMP(loc + inc, 0, 73); draw_params_cursor_wave(param_selection_loc, COL_CURSOR); } + + // Adjust parameter. if (key_tap(KEY_R) || key_tap(KEY_L)) { int odd = param_selection_loc & 0x1; int inc; @@ -1675,10 +1734,10 @@ handle_trigger_selection(void) { // Switch to parameter selection. switch (channel_selection_loc) { case 0: { - input_handler = handle_param_selection_ch1; + input_handler = handle_param_selection_sq1; } break; case 1: { - input_handler = handle_param_selection_ch2; + input_handler = handle_param_selection_sq2; } break; case 2: { input_handler = handle_param_selection_wave; -- cgit v1.2.1