summaryrefslogtreecommitdiff
path: root/src/modules/stars/stars.c
blob: 3712e335f864837d70d39b9138e3ca476277db7e (plain)
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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>

#include "draw.h"
#include "fb.h"
#include "rototiller.h"
#include "starslib.h"

/* Copyright (C) 2017 Philip J. Freeman <elektron@halo.nu> */

static void stars_render_fragment(void *context, fb_fragment_t *fragment)
{
	static int	initialized, z;
	static struct	universe* u;

	struct		return_point rp;
	int		x, y, width = fragment->width, height = fragment->height;

	if (!initialized) {
		z = 128;
		srand(time(NULL) + getpid());

		// Initialize the stars lib (and pre-add a bunch of stars)
		new_universe(&u, width, height, z);
		for(y=0; y<z; y++) {
			while (process_point(u, &rp) != 0);
			for(x=0; x<rand()%128; x++){
				new_point(u);
			}
		}
		initialized = 1;
	}

	// draw space (or blank the frame, if you prefer)
	fb_fragment_zero(fragment);

	// draw stars
	for (;;) {
		int ret = process_point( u, &rp  );
		if (ret==0) break;
		if (ret==1) fb_fragment_put_pixel_unchecked(fragment, rp.x+(width/2), rp.y+(height/2),
				       makergb(0xFF, 0xFF, 0xFF, (float)rp.opacity/OPACITY_MAX)
				      );
	}

	// add stars at horizon
	for (x=0; x<rand()%128; x++) {
		new_point(u);
	}
}

rototiller_module_t	stars_module = {
	.render_fragment = stars_render_fragment,
	.name = "stars",
	.description = "basic starfield",
	.author = "Philip J Freeman <elektron@halo.nu>",
	.license = "GPLv2",
};
© All Rights Reserved