Skip to content

Commit

Permalink
common_tasks: add RADIAL_LOOP helper macro
Browse files Browse the repository at this point in the history
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);
	}
  • Loading branch information
Akaricchi committed Aug 20, 2023
1 parent bc1267f commit 3231830
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/common_tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 3231830

Please sign in to comment.