aboutsummaryrefslogtreecommitdiff
path: root/layer.cpp
blob: 882083419b0b5a4a27701bf789005c4c6fb1ee15 (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
#include <string.h>
#include <bsd/sys/queue.h>

#include <SDL.h>

#include "osc_graphics.h"
#include "osc_server.h"
#include "layer.h"

Layer::Layer(const char *name)
{
	mutex = SDL_CreateMutex();
	Layer::name = strdup(name);

	geo_osc_id = register_method("geo", GEO_TYPES, geo_osc);
	alpha_osc_id = register_method("alpha", "f", alpha_osc);
}

void
LayerList::insert(int pos, Layer *layer)
{
	Layer *cur, **prev;

	lock();

	SLIST_FOREACH_PREVPTR(cur, prev, &head, layers)
		if (!pos--)
			break;

	SLIST_NEXT(layer, layers) = cur;
	*prev = layer;

	unlock();
}

bool
LayerList::delete_by_name(const char *name)
{
	Layer *cur, **prev;

	lock();

	SLIST_FOREACH_PREVPTR(cur, prev, &head, layers)
		if (!strcmp(cur->name, name)) {
			*prev = SLIST_NEXT(cur, layers);
			delete cur;
			break;
		}

	unlock();

	return cur == NULL;
}

void
LayerList::render(SDL_Surface *target)
{
	SDL_FillRect(target, NULL, SDL_MapRGB(target->format, 0, 0, 0));

	lock();

	Layer *cur;
	SLIST_FOREACH(cur, &head, layers) {
		cur->lock();
		cur->frame(target);
		cur->unlock();
	}

	unlock();
}

Layer::~Layer()
{
	unregister_method(alpha_osc_id);
	unregister_method(geo_osc_id);

	free(name);
	SDL_DestroyMutex(mutex);
}