summaryrefslogtreecommitdiff
path: root/src/stage.c
blob: 4d18486d0ec0a7d32237d783cd1bdb083fda3b29 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 *  Copyright (C) 2018-2019 - Vito Caputo - <vcaputo@pengaru.com>
 *
 *  This program is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 3 as published
 *  by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <dll_h/dll.h>

#include "stage.h"


struct stage_t {
	stage_t			*parent;	/* NULL when root stage */
	dll_t			layer;		/* node on parent->layers[layer] when parent != NULL */
	dll_t			layers[STAGE_LAYERS_MAX];
	char			name[STAGE_NAME_MAX];
	float			alpha;		/* alpha for the stage */
	unsigned		active:1;	/* stage is active */
	unsigned		locked:1;	/* stage is locked */
	unsigned		dirty:1;	/* stage is dirty (since last render) */

	const stage_ops_t	*ops;		/* ops for operating on this stage's object */
	void			*object;	/* this stage's object */
};


/* minimally initialize a stage to carry an object */
static void _stage_set_object(stage_t *stage, const char *name, const stage_ops_t *ops, void *object)
{
	static	stage_ops_t	null_ops = {};

	assert(stage);
	assert(name);

	if (!ops)
		ops = &null_ops;

	strncpy(stage->name, name, sizeof(stage->name));
	stage->ops = ops;
	stage->object = object;
}


/* allocate a stage, this purely creates a stage in isolation and assigns its associated name, object and functions */
static stage_t * _stage_new(const char *name, const stage_ops_t *ops, void *object)
{
	stage_t	*stage;

	stage = calloc(1, sizeof(stage_t));
	if (!stage)
		return NULL;

	dll_init(&stage->layer);
	for (int i = 0; i < STAGE_LAYERS_MAX; i++)
		dll_init(&stage->layers[i]);

	_stage_set_object(stage, name, ops, object);

	return stage;
}


/* free a stage, no list manipulation occurs, this is purely cleanup of the stage and its object */
static void _stage_free(stage_t *stage)
{
	assert(stage);

	if (stage->ops->free_func)
		stage->ops->free_func(stage, stage->object);

	free(stage);
}


/* returns a new stage, attached at the specified layer under parent if supplied */
/* layer has no effect when parent == NULL */
stage_t * stage_new(const stage_conf_t *conf, const stage_ops_t *ops, void *object)
{
	stage_t	*stage;

	assert(conf);
	assert(conf->parent || !conf->layer);
	assert(conf->layer < STAGE_LAYERS_MAX);

	stage = _stage_new(conf->name, ops, object);
	if (!stage)
		return NULL;

	if (conf->parent) {
		stage->parent = conf->parent;
		(void) dll_add_pre(&stage->parent->layers[conf->layer], &stage->layer);
	}

	stage->alpha = conf->alpha;
	stage->active = conf->active;
	stage->locked = conf->locked;
	stage->dirty = conf->dirty;

	return stage;
}


/* replaces a given stage's object-related properties but otherwise keeping the existing state */
void stage_replace(stage_t *stage, const char *name, const stage_ops_t *ops, void *object)
{
	assert(stage);

	if (stage->ops->free_func)
		stage->ops->free_func(stage, stage->object);

	_stage_set_object(stage, name, ops, object);
}


static void _stage_free_all(stage_t *stage, int force);


/* recursively free the layers of this stage, but not the stage itself */
static void _stage_free_layers(stage_t *stage, int force)
{
	assert(stage);

	for (int i = 0; i < STAGE_LAYERS_MAX; i++) {
		stage_t	*s, *_s;

		DLL_FOR_EACH_ENTRY_SAFE(&stage->layers[i], s, _s, stage_t, layer)
			_stage_free_all(s, force);
	}
}


/* free the stage recursively, skipping locked stages if !force */
static void _stage_free_all(stage_t *stage, int force)
{
	assert(stage);

	if (stage->locked && !force)
		return;

	_stage_free_layers(stage, force);
	dll_del(&stage->layer);
	_stage_free(stage);
}


/* frees a given stage and all its descendants, removing from its
 * parent if not root.
 */
stage_t * stage_free(stage_t *stage)
{
	if (stage)
		_stage_free_all(stage, 1);

	return NULL;
}


/* replace a given stage's object only - retain render/free hooks and everything else, invalidates cache  - DOES NOT FREE OLD OBJECT */
void stage_set_object(stage_t *stage, void *object)
{
	assert(stage);

	stage->object = object;
}


void * stage_get_object(const stage_t *stage)
{
	assert(stage);

	return stage->object;
}


/* set the alpha on a stage */
void stage_set_alpha(stage_t *stage, float alpha)
{
	assert(stage);

	stage->alpha = alpha;
}


/* get the current alpha from a stage */
float stage_get_alpha(const stage_t *stage)
{
	assert(stage);

	return stage->alpha;
}


/* set a stage's active state (participates in rendering) */
void stage_set_active(stage_t *stage, int active)
{
	assert(stage);

	stage->active = active;
}


/* get a stage's active state  */
int stage_get_active(const stage_t *stage)
{
	assert(stage);

	return stage->active;
}


/* set a stage's locked state (doesn't get freed by clears) */
void stage_set_locked(stage_t *stage, int locked)
{
	assert(stage);

	stage->locked = locked;
}


/* get a stage's locked state */
int stage_get_locked(const stage_t *stage)
{
	assert(stage);

	return stage->locked;
}


/* set a stage's layer (must not be root stage) */
/* TODO: maybe support supplying a parent here for switching parents? */
void stage_set_layer(stage_t *stage, int layer)
{
	assert(stage);
	assert(stage->parent);

	(void) dll_del(&stage->layer);
	(void) dll_add_pre(&stage->parent->layers[layer], &stage->layer);
}


/* free everything in the stage, but keep the stage around */
/* probably good hygiene to clear everything when there's no intention of
 * reusing stages at the start of a new context, in case something is left
 * around unintentionally. */
void stage_clear(stage_t *stage)
{
	_stage_free_layers(stage, 0);
}


static void _prepare_stage(stage_t *stage, float alpha, void *render_ctxt)
{
	float	a = alpha * stage->alpha;

	assert(stage);

	if (stage->ops->prepare_func)
		stage->ops->prepare_func(stage, stage->object, a, render_ctxt);

	for (int i = 0; i < STAGE_LAYERS_MAX; i++) {
		stage_t	*s;

		DLL_FOR_EACH_ENTRY(&stage->layers[i], s, stage_t, layer) {
			if (!s->active)
				continue;

			_prepare_stage(s, a, render_ctxt);
		}
	}
}


static void _render_stage(const stage_t *stage, float alpha, void *render_ctxt)
{
	float	a = alpha * stage->alpha;

	assert(stage);

	if (stage->ops->render_func)
		stage->ops->render_func(stage, stage->object, a, render_ctxt);

	for (int i = 0; i < STAGE_LAYERS_MAX; i++) {
		stage_t	*s;

		DLL_FOR_EACH_ENTRY(&stage->layers[i], s, stage_t, layer) {
			if (!s->active)
				continue;

			_render_stage(s, a, render_ctxt);
		}
	}
}


/* recursively render the supplied stage tree if dirty, skipping inactive branches */
/* returns wether the stage was dirty or not, for influence of page flipping etc. */
int stage_render(stage_t *stage, void *render_ctxt)
{
	assert(stage);
	assert(!stage->parent);	/* XXX: Current dirty implementation is global and only
				 * maintains the dirty bit in the root node.  So assert
				 * that render is only called on the root to not break
				 * dirty detection.
				 */

	/* we must always enter the prepare pass, as some nodes may dirty the stage
	 * in the prepare stage.
	 */
	if (stage->active)
		_prepare_stage(stage, 1.f, render_ctxt);

	if (stage->dirty) {
		if (stage->active)
			_render_stage(stage, 1.f, render_ctxt);

		stage->dirty = 0;

		return 1;
	}

	return 0;
}


/* mark a stage as dirty */
/* as the current stage_render() implementation only checks the dirty flag of
 * the supplied stage, which is presumed to be the root, this currently
 * walks up the chain of parents if any to update the root flag.
 * TODO: perhaps stage_render() should assert that it's called on the root.
 */
void stage_dirty(stage_t *stage)
{
	assert(stage);

	while (stage->parent)
		stage = stage->parent;

	stage->dirty = 1;
}


/* lookup a stage from a name, returns first hit */
stage_t * stage_lookup_name(stage_t *stage, const char *name)
{
	assert(stage);
	assert(name);

	if (!strncmp(stage->name, name, sizeof(stage->name)))
		return stage;

	/* FIXME: this should probably search layers in descending order */
	for (int i = 0; i < STAGE_LAYERS_MAX; i++) {
		stage_t	*s;

		DLL_FOR_EACH_ENTRY(&stage->layers[i], s, stage_t, layer) {
			s = stage_lookup_name(s, name);
			if (s)
				return s;
		}
	}

	return NULL;
}
© All Rights Reserved