aboutsummaryrefslogtreecommitdiff
path: root/layer.cpp
blob: 89f437fd54f09b8908f3930c79b0013817f0dffc (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
#include <string.h>

#include <SDL.h>

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

#define FOREACH_LAYER(VAR) \
	for (Layer *VAR = head; VAR; VAR = VAR->next)

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

	lock();

	for (cur = head; cur && pos; prev = cur, cur = cur->next, pos--);

	layer->next = cur;
	if (prev)
		prev->next = layer;
	else
		head = layer;

	unlock();
}

bool
LayerList::delete_by_name(const char *name)
{
	Layer *prev = NULL;

	lock();

	FOREACH_LAYER(cur) {
		if (!strcmp(cur->name, name)) {
			if (prev)
				prev->next = cur->next;
			else
				head = cur->next;
			delete cur;

			unlock();
			return false;
		}

		prev = cur;
	}

	unlock();

	return true;
}

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

	lock();

	FOREACH_LAYER(cur) {
		cur->lock();
		cur->frame(target);
		cur->unlock();
	}

	unlock();
}