summaryrefslogtreecommitdiff
path: root/src/modules/sparkler/sparkler.c
blob: b673d0c4b60b5cae34bc1e54288adc56cc621f4a (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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "fb.h"
#include "rototiller.h"
#include "util.h"

#include "particles.h"

/* particle system gadget (C) Vito Caputo <vcaputo@pengaru.com> 2/15/2014 */
/* 1/10/2015 added octree bsp (though not yet leveraged) */
/* 11/25/2016 refactor and begun adapting to rototiller */

#define INIT_PARTS 100

typedef struct sparkler_context_t {
	particles_t	*particles;
	unsigned	n_cpus;
} sparkler_context_t;

extern particle_ops_t	simple_ops;

static particles_conf_t	sparkler_conf;

static void * sparkler_create_context(unsigned ticks, unsigned num_cpus)
{
	static int		initialized;
	sparkler_context_t	*ctxt;

	if (!initialized) {
		srand(time(NULL) + getpid());
		initialized = 1;
	}

	ctxt = calloc(1, sizeof(sparkler_context_t));
	if (!ctxt)
		return NULL;

	ctxt->particles = particles_new(&sparkler_conf);
	if (!ctxt->particles) {
		free(ctxt);
		return NULL;
	}

	particles_add_particles(ctxt->particles, NULL, &simple_ops, INIT_PARTS);

	return ctxt;
}


static void sparkler_destroy_context(void *context)
{
	sparkler_context_t	*ctxt = context;

	particles_free(ctxt->particles);
	free(ctxt);
}


static int sparkler_fragmenter(void *context, const fb_fragment_t *fragment, unsigned number, fb_fragment_t *res_fragment)
{
	sparkler_context_t	*ctxt = context;

	return fb_fragment_slice_single(fragment, ctxt->n_cpus, number, res_fragment);
}

static void sparkler_prepare_frame(void *context, unsigned ticks, unsigned ncpus, fb_fragment_t *fragment, rototiller_fragmenter_t *res_fragmenter)
{
	sparkler_context_t	*ctxt = context;

	*res_fragmenter = sparkler_fragmenter;
	ctxt->n_cpus = ncpus;

	if (sparkler_conf.show_bsp_matches)
		fb_fragment_zero(fragment);

	particles_sim(ctxt->particles, fragment);
	particles_add_particles(ctxt->particles, NULL, &simple_ops, INIT_PARTS / 4);
	particles_age(ctxt->particles);
}


/* Render a 3D particle system */
static void sparkler_render_fragment(void *context, unsigned ticks, unsigned cpu, fb_fragment_t *fragment)
{
	sparkler_context_t	*ctxt = context;

	if (!sparkler_conf.show_bsp_matches)
		fb_fragment_zero(fragment);

	particles_draw(ctxt->particles, fragment);
}


/* Settings hooks for configurable variables */
static int sparkler_setup(const settings_t *settings, setting_desc_t **next_setting)
{
	const char	*show_bsp_leafs;
	const char	*show_bsp_matches;
	const char	*values[] = {
				"off",
				"on",
				NULL
			};

	/* TODO: return -EINVAL on parse errors? */

	show_bsp_leafs = settings_get_value(settings, "show_bsp_leafs");
	if (!show_bsp_leafs) {
		int	r;

		r = setting_desc_clone(&(setting_desc_t){
						.name = "Show BSP Leaf Node Bounding Boxes",
						.key = "show_bsp_leafs",
						.preferred = "off",
						.values = values,
					}, next_setting);
		if (r < 0)
			return r;

		return 1;
	}

	if (!strcasecmp(show_bsp_leafs, "on")) {
		const char	*show_bsp_leafs_min_depth;

		sparkler_conf.show_bsp_leafs = 1;

		show_bsp_leafs_min_depth = settings_get_value(settings, "show_bsp_leafs_min_depth");
		if (!show_bsp_leafs_min_depth) {
			const char	*depth_values[] = {
						"0",
						"4",
						"6",
						"8",
						"10",
						NULL
					};
			int	r;

			r = setting_desc_clone(&(setting_desc_t){
							.name = "Show BSP Leaf Node Bounding Boxes Minimum Depth",
							.key = "show_bsp_leafs_min_depth",
							.preferred = "8",
							.values = depth_values,
						}, next_setting);
			if (r < 0)
				return r;

			return 1;
		}

		sscanf(show_bsp_leafs_min_depth, "%u", &sparkler_conf.show_bsp_leafs_min_depth);
	} else {
		sparkler_conf.show_bsp_leafs = 0;
	}

	show_bsp_matches = settings_get_value(settings, "show_bsp_matches");
	if (!show_bsp_matches) {
		int	r;

		r = setting_desc_clone(&(setting_desc_t){
						.name = "Show BSP Search Matches",
						.key = "show_bsp_matches",
						.preferred = "off",
						.values = values,
					}, next_setting);
		if (r < 0)
			return r;

		return 1;
	}

	if (!strcasecmp(show_bsp_matches, "on"))
		sparkler_conf.show_bsp_matches = 1;
	else
		sparkler_conf.show_bsp_matches = 0;

	if (!strcasecmp(show_bsp_matches, "on")) {
		const char	*show_bsp_matches_affected_only;

		show_bsp_matches_affected_only = settings_get_value(settings, "show_bsp_matches_affected_only");
		if (!show_bsp_matches_affected_only) {
			int	r;

			r = setting_desc_clone(&(setting_desc_t){
							.name = "Show Only Affected BSP Search Matches",
							.key = "show_bsp_matches_affected_only",
							.preferred = "off",
							.values = values,
						}, next_setting);
			if (r < 0)
				return r;

			return 1;
		}

		if (!strcasecmp(show_bsp_matches_affected_only, "on"))
			sparkler_conf.show_bsp_matches_affected_only = 1;
		else
			sparkler_conf.show_bsp_matches_affected_only = 0;
	}

	return 0;
}


rototiller_module_t	sparkler_module = {
	.create_context = sparkler_create_context,
	.destroy_context = sparkler_destroy_context,
	.prepare_frame = sparkler_prepare_frame,
	.render_fragment = sparkler_render_fragment,
	.setup = sparkler_setup,
	.name = "sparkler",
	.description = "Particle system with spatial interactions (threaded (poorly))",
	.author = "Vito Caputo <vcaputo@pengaru.com>",
	.license = "GPLv2",
};
© All Rights Reserved