Skip to content

Commit 589b1a1

Browse files
committed
Attempt to add {SKIP:N} control code
Signed-off-by: Joachim Wiberg <[email protected]>
1 parent 074022a commit 589b1a1

File tree

4 files changed

+112
-7
lines changed

4 files changed

+112
-7
lines changed

demo.c

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -912,11 +912,17 @@ void render_raining_logo(DemoContext *ctx)
912912
}
913913
}
914914

915+
/* Helper to calculate spaces generated by SKIP command */
916+
static int calculate_skip_spaces(float skip_screens)
917+
{
918+
return (int)(skip_screens * (WIDTH / 35.0f));
919+
}
920+
915921
/* Build a map of control code positions in the stripped text */
916922
typedef struct {
917923
int position; /* Character position in stripped text */
918924
float pixel_position; /* Pixel position based on actual glyph widths */
919-
char type; /* P=pause, S=speed, T=style, C=color */
925+
char type; /* P=pause, S=speed, T=style, C=color, K=skip */
920926
char data[64]; /* Parameter data */
921927
} ControlCode;
922928

@@ -963,6 +969,14 @@ static void build_control_map(const char *text)
963969
cc->type = 'C';
964970
strncpy(cc->data, cmd + 6, 63);
965971
num_control_codes++;
972+
} else if (strncmp(cmd, "SKIP:", 5) == 0) {
973+
/* SKIP is added to control codes for pixel position calculation */
974+
cc->type = 'K';
975+
strncpy(cc->data, cmd + 5, 63);
976+
num_control_codes++;
977+
/* Advance char_pos by the spaces that SKIP will insert */
978+
float skip_screens = atof(cmd + 5);
979+
char_pos += calculate_skip_spaces(skip_screens);
966980
}
967981
}
968982
p = end + 1;
@@ -1041,11 +1055,25 @@ static void apply_scroll_controls(DemoContext *ctx, float scroll_offset)
10411055
last_offset = scroll_offset;
10421056

10431057
/* Apply control codes that we've just reached (not yet triggered) */
1044-
/* Offset to delay trigger - adjust this value to find the right timing */
1045-
const float trigger_offset = 500.0f; /* Try WIDTH as starting point */
1046-
10471058
for (int i = 0; i < num_control_codes; i++) {
10481059
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+
}
10491077

10501078
/* Trigger when we pass the pixel position plus offset (only once) */
10511079
if (!triggered[i] && scroll_offset >= cc->pixel_position + trigger_offset) {
@@ -1102,18 +1130,57 @@ static char *strip_control_codes(const char *text)
11021130
if (!text)
11031131
return NULL;
11041132

1105-
int len = strlen(text);
1106-
char *result = malloc(len + 1);
1133+
/* First pass: calculate required size accounting for SKIP expansions */
1134+
int expanded_len = 0;
1135+
const char *p = text;
1136+
while (*p) {
1137+
if (*p == '{') {
1138+
const char *end = strchr(p, '}');
1139+
if (end) {
1140+
int cmd_len = end - p - 1;
1141+
char cmd[64] = {0};
1142+
if (cmd_len > 0 && cmd_len < 64) {
1143+
strncpy(cmd, p + 1, cmd_len);
1144+
/* Check if this is a SKIP command */
1145+
if (strncmp(cmd, "SKIP:", 5) == 0) {
1146+
float skip_screens = atof(cmd + 5);
1147+
expanded_len += calculate_skip_spaces(skip_screens);
1148+
}
1149+
}
1150+
p = end + 1;
1151+
continue;
1152+
}
1153+
}
1154+
expanded_len++;
1155+
p++;
1156+
}
1157+
1158+
/* Allocate result with room for expansions */
1159+
char *result = malloc(expanded_len + 1);
11071160
if (!result)
11081161
return NULL;
11091162

1163+
/* Second pass: copy text and expand SKIP commands */
11101164
const char *src = text;
11111165
char *dst = result;
11121166

11131167
while (*src) {
11141168
if (*src == '{') {
11151169
const char *end = strchr(src, '}');
11161170
if (end) {
1171+
int cmd_len = end - src - 1;
1172+
char cmd[64] = {0};
1173+
if (cmd_len > 0 && cmd_len < 64) {
1174+
strncpy(cmd, src + 1, cmd_len);
1175+
/* Expand SKIP into spaces */
1176+
if (strncmp(cmd, "SKIP:", 5) == 0) {
1177+
float skip_screens = atof(cmd + 5);
1178+
int spaces = calculate_skip_spaces(skip_screens);
1179+
for (int i = 0; i < spaces; i++) {
1180+
*dst++ = ' ';
1181+
}
1182+
}
1183+
}
11171184
src = end + 1;
11181185
continue;
11191186
}

scroll.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{STYLE:bottom}{SPEED:400}
1+
{SPEED:400}{STYLE:bottom}
22

33

44

scroll2.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{STYLE:bottom}Welcome to... the Infix Demo!{PAUSE:2}
2+
3+
4+
5+
Now scrolling... FAST!
6+
{SKIP:1}
7+
{STYLE:roller}Now with 3D roller!{PAUSE:1.5}
8+
{SKIP:1}
9+
{COLOR:255,0,0}
10+
RED
11+
{SKIP:1}
12+
{COLOR:0,255,0}
13+
GREEN
14+
{SKIP:1}
15+
{COLOR:0,0,255}
16+
BLUE
17+
{SKIP:1}
18+
{COLOR:0,0,0}{STYLE:wave}Back to waves... Greetings to the demoscene! <3

scroll3.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{STYLE:bottom}Welcome to... the Infix Demo!{PAUSE:2}
2+
{SPEED:400}
3+
4+
5+
6+
Now scrolling... FAST!{SPEED:800}
7+
{SKIP:1}
8+
{SPEED:400}
9+
{STYLE:roller}Now with 3D roller!{PAUSE:1.5}
10+
{SKIP:1}
11+
{COLOR:255,0,0}
12+
RED
13+
{SKIP:1}
14+
{COLOR:0,255,0}
15+
GREEN
16+
{SKIP:1}
17+
{COLOR:0,0,255}
18+
BLUE
19+
{SKIP:1}
20+
{COLOR:0,0,0}{STYLE:wave}Back to waves... Greetings to the demoscene! <3

0 commit comments

Comments
 (0)