Skip to content

Commit 60a5a29

Browse files
committed
Fix wrap-around for text *and' control codes
Signed-off-by: Joachim Wiberg <[email protected]>
1 parent 589b1a1 commit 60a5a29

File tree

1 file changed

+15
-68
lines changed

1 file changed

+15
-68
lines changed

demo.c

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -991,12 +991,12 @@ static void build_control_map(const char *text)
991991

992992

993993
/* Apply control codes based on current scroll position */
994-
static void apply_scroll_controls(DemoContext *ctx, float scroll_offset)
994+
static void apply_scroll_controls(DemoContext *ctx, float scroll_offset, float total_width)
995995
{
996996
/* Track which control codes have been triggered */
997997
static int triggered[256] = {0};
998998
static int last_num_codes = 0;
999-
static float last_offset = 0.0f;
999+
static int last_cycle = -1;
10001000

10011001
/* Reset triggered flags if control codes changed */
10021002
if (num_control_codes != last_num_codes) {
@@ -1005,78 +1005,25 @@ static void apply_scroll_controls(DemoContext *ctx, float scroll_offset)
10051005
last_num_codes = num_control_codes;
10061006
}
10071007

1008-
/* Reset triggered flags when scroll wraps around (detect by large negative jump) */
1009-
if (scroll_offset < last_offset - 1000.0f) { /* Wrapped around */
1008+
/* Detect wrapping by tracking which cycle we're in */
1009+
int current_cycle = (int)(scroll_offset / total_width);
1010+
if (current_cycle != last_cycle && last_cycle >= 0) {
1011+
/* We've wrapped to a new cycle - reset all triggered flags */
10101012
for (int i = 0; i < 256; i++)
10111013
triggered[i] = 0;
1012-
/* Reset scroll state to defaults on wrap */
1013-
ctx->scroll_speed = 180.0f;
1014-
ctx->scroll_style = SCROLL_ROLLER_3D;
1015-
ctx->scroll_color[0] = 0;
1016-
ctx->scroll_color[1] = 0;
1017-
ctx->scroll_color[2] = 0;
1018-
1019-
/* Immediately apply control codes at position 0 after wrap */
1020-
for (int i = 0; i < num_control_codes; i++) {
1021-
ControlCode *cc = &control_codes[i];
1022-
if (cc->position == 0) {
1023-
switch (cc->type) {
1024-
case 'S':
1025-
{
1026-
char *endptr;
1027-
float new_speed = strtof(cc->data, &endptr);
1028-
if (endptr != cc->data && new_speed >= 0)
1029-
ctx->scroll_speed = new_speed;
1030-
}
1031-
break;
1032-
case 'T':
1033-
if (strcmp(cc->data, "wave") == 0)
1034-
ctx->scroll_style = SCROLL_SINE_WAVE;
1035-
else if (strcmp(cc->data, "roller") == 0)
1036-
ctx->scroll_style = SCROLL_ROLLER_3D;
1037-
else if (strcmp(cc->data, "bottom") == 0)
1038-
ctx->scroll_style = SCROLL_BOTTOM_TRADITIONAL;
1039-
break;
1040-
case 'C':
1041-
{
1042-
int r, g, b;
1043-
if (sscanf(cc->data, "%d,%d,%d", &r, &g, &b) == 3) {
1044-
ctx->scroll_color[0] = r;
1045-
ctx->scroll_color[1] = g;
1046-
ctx->scroll_color[2] = b;
1047-
}
1048-
}
1049-
break;
1050-
}
1051-
triggered[i] = 1;
1052-
}
1053-
}
10541014
}
1055-
last_offset = scroll_offset;
1015+
last_cycle = current_cycle;
1016+
1017+
/* Calculate scroll position within current cycle */
1018+
float cycle_offset = fmodf(scroll_offset, total_width);
10561019

10571020
/* Apply control codes that we've just reached (not yet triggered) */
10581021
for (int i = 0; i < num_control_codes; i++) {
10591022
ControlCode *cc = &control_codes[i];
1060-
float trigger_offset = 0.0f;
1061-
1062-
/* Different trigger offsets for different control code types */
1063-
switch (cc->type) {
1064-
case 'P': /* PAUSE */
1065-
trigger_offset = 500.0f;
1066-
break;
1067-
case 'S': /* SPEED */
1068-
trigger_offset = 500.0f;
1069-
break;
1070-
case 'T': /* STYLE */
1071-
trigger_offset = 500.0f;
1072-
break;
1073-
case 'C': /* COLOR */
1074-
trigger_offset = 500.0f;
1075-
break;
1076-
}
1023+
float trigger_offset = 500.0f;
10771024

1078-
/* Trigger when we pass the pixel position plus offset (only once) */
1079-
if (!triggered[i] && scroll_offset >= cc->pixel_position + trigger_offset) {
1025+
/* Trigger when we pass the pixel position plus offset (only once per cycle) */
1026+
if (!triggered[i] && cycle_offset >= cc->pixel_position + trigger_offset) {
10801027
triggered[i] = 1;
10811028

10821029
switch (cc->type) {
@@ -1418,7 +1365,7 @@ void render_scroll_text(DemoContext *ctx)
14181365
}
14191366

14201367
/* Apply control codes based on scroll position */
1421-
apply_scroll_controls(ctx, ctx->scroll_offset);
1368+
apply_scroll_controls(ctx, ctx->scroll_offset, total_adv);
14221369
} else if (ctx->scroll_style == SCROLL_BOTTOM_TRADITIONAL) {
14231370
/* Traditional bottom scroller - render entire line once */
14241371
static SDL_Texture *line_tex = NULL;
@@ -1494,7 +1441,7 @@ void render_scroll_text(DemoContext *ctx)
14941441
}
14951442

14961443
/* Apply control codes based on scroll position */
1497-
apply_scroll_controls(ctx, ctx->scroll_offset);
1444+
apply_scroll_controls(ctx, ctx->scroll_offset, line_w);
14981445

14991446
if (line_tex) {
15001447
int y_pos = HEIGHT - 60;

0 commit comments

Comments
 (0)