common_tasks: add RADIAL_LOOP helper macro

Helps with writing radially symmetrical patterns.

Replace this:

	int cnt = 69;
	cmplx dir = initial_dir;
	cmplx turn = cdir(M_TAU/cnt);
	for(int = 0; i < cnt; ++i, dir *= turn) {
		pew_pew(dir);
	}

with this:

	RADIAL_LOOP(l, 69, initial_dir) {
		pew_pew(l.dir);
	}
This commit is contained in:
Andrei Alexeyev 2023-08-20 15:44:11 +02:00
parent bc1267f0e8
commit 323183061b
No known key found for this signature in database
GPG key ID: 72D26128040B9690

View file

@ -165,3 +165,24 @@ DECLARE_EXTERN_TASK(
);
DECLARE_EXTERN_TASK(common_play_sfx, { const char *name; });
// TODO: move this elsewhere?
typedef struct RadialLoop {
cmplx dir, turn;
int cnt, i;
} RadialLoop;
INLINE RadialLoop _radial_loop_init(int cnt, cmplx dir) {
return (RadialLoop) {
.dir = dir,
.cnt = abs(cnt),
.turn = cdir(M_TAU/cnt),
};
}
#define RADIAL_LOOP(_loop_var, _cnt_init, _dir_init) \
for( \
RadialLoop _loop_var = _radial_loop_init(_cnt_init, _dir_init); \
_loop_var.i < _loop_var.cnt; \
++_loop_var.i, _loop_var.dir *= _loop_var.turn)