-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathropesim.c
executable file
·169 lines (151 loc) · 5.19 KB
/
ropesim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#if 0
cc -std=c99 -Wall -Werror $0 `sdl2-config --cflags --libs` || exit 1
exec ./a.out
#endif
// rope simulation as in AoC 2022, day 9
// - use mouse to pull rope
// - cursor keys left/right = adjust rope length
// - cursor keys up/down = zoom in/out
#define MAX_ROPE_LENGTH 1000
#define INITIAL_GRID_SIZE 10
#define INITIAL_ROPE_LENGTH 20
#define INITIAL_WINDOW_WIDTH 1024
#define INITIAL_WINDOW_HEIGHT 768
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include <SDL.h>
typedef struct _knot {
int x, y;
} Knot;
SDL_Window *win;
SDL_Renderer *render;
int grid_size = INITIAL_GRID_SIZE;
int rope_length = INITIAL_ROPE_LENGTH;
Knot rope[MAX_ROPE_LENGTH];
static inline Uint8 clip_color(int x) {
return (x < 0) ? 0u : (x > 255) ? 255u : (Uint8)x;
}
///////////////////////////////////////////////////////////////////////////////
void move_rope(int x, int y) {
rope[0].x = x;
rope[0].y = y;
for (int k = 1; k < MAX_ROPE_LENGTH; ++k) {
int dx = rope[k-1].x - rope[k].x;
int dy = rope[k-1].y - rope[k].y;
if ((abs(dx) > 1) || (abs(dy) > 1)) {
rope[k].x += (dx > 0) ? 1 : (dx < 0) ? (-1) : 0;
rope[k].y += (dy > 0) ? 1 : (dy < 0) ? (-1) : 0;
}
}
}
void move_to_pixel(int x, int y) {
int nx = x / grid_size, ny = y / grid_size;
int ox = rope[0].x, oy = rope[0].y;
int adx = abs(ox - nx), ady = abs(oy - ny);
int delta = (adx > ady) ? adx : ady;
if (delta <= 1) {
move_rope(nx, ny);
return;
}
// larger delta -> interpolate coordinates
for (int t = 1; t <= delta; ++t) {
move_rope((ox * (delta - t) + nx * t + (delta >> 1)) / delta,
(oy * (delta - t) + ny * t + (delta >> 1)) / delta);
}
}
///////////////////////////////////////////////////////////////////////////////
int main(void) {
int res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
assert(res == 0);
win = SDL_CreateWindow(
"AoC Rope Simulation",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
INITIAL_WINDOW_WIDTH, INITIAL_WINDOW_HEIGHT,
SDL_WINDOW_RESIZABLE);
assert(win);
render = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC);
assert(render);
bool active = true;
while (active) {
SDL_Event ev;
bool redraw = true;
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_WINDOWEVENT:
switch (ev.window.event) {
case SDL_WINDOWEVENT_EXPOSED:
case SDL_WINDOWEVENT_RESIZED:
case SDL_WINDOWEVENT_SIZE_CHANGED:
redraw = true;
break;
default:
break;
}
break;
case SDL_KEYDOWN:
switch (ev.key.keysym.sym) {
case SDLK_q:
case SDLK_ESCAPE:
active = false;
break;
case SDLK_LEFT:
if (rope_length > 1) {
rope_length--;
redraw = true;
}
break;
case SDLK_RIGHT:
if (rope_length < MAX_ROPE_LENGTH) {
rope_length++;
redraw = true;
}
break;
case SDLK_UP:
grid_size++;
redraw = true;
break;
case SDLK_DOWN:
if (grid_size > 1) {
grid_size--;
redraw = true;
}
break;
default:
break;
}
break;
case SDL_MOUSEMOTION:
move_to_pixel(ev.motion.x, ev.motion.y);
redraw = true;
break;
case SDL_QUIT:
active = false;
break;
default:
break;
}
}
if (redraw) {
SDL_SetRenderDrawColor(render, 0, 0, 0, 255);
SDL_RenderClear(render);
for (int k = rope_length - 1; k >= 0; --k) {
SDL_Rect r;
r.x = rope[k].x * grid_size;
r.y = rope[k].y * grid_size;
r.w = r.h = grid_size;
int color = 768 - (k * 768 / rope_length);
if (color > 0) {
SDL_SetRenderDrawColor(render, clip_color(color), clip_color(color - 256), clip_color(color - 512), 255u);
SDL_RenderFillRect(render, &r);
}
}
SDL_RenderPresent(render);
}
SDL_WaitEvent(NULL);
}
SDL_DestroyRenderer(render);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}