aboutsummaryrefslogtreecommitdiff
path: root/src/layer.h
blob: 378acbd87de87360c2c6e16204d764928a7715d3 (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
#ifndef __HAVE_LAYER_H
#define __HAVE_LAYER_H

#include <string.h>
#include <bsd/sys/queue.h>

#include <SDL.h>

#include <lo/lo.h>

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

extern OSCServer osc_server;

class Layer : public Mutex {
public:
	/*
	 * Every derived class must have a static CtorInfo struct "ctor_info"
	 * and a static "ctor_osc" method
	 */
	struct CtorInfo {
		const char *name;
		const char *types;
	};

	LIST_ENTRY(Layer) layers;

	char *name;

	Layer(const char *name);
	virtual ~Layer();

	/*
	 * Frame render method
	 */
	virtual void frame(SDL_Surface *target) = 0;

protected:
	inline OSCServer::MethodHandlerId *
	register_method(const char *method, const char *types,
			OSCServer::MethodHandlerCb method_cb)
	{
		return osc_server.register_method(this, method, types, method_cb);
	}
	inline void
	unregister_method(OSCServer::MethodHandlerId *hnd)
	{
		osc_server.unregister_method(hnd);
	}

	/*
	 * Default methods
	 */
	virtual void geo(SDL_Rect geo) = 0;
	virtual void alpha(float opacity) = 0;

private:
	/*
	 * OSC handler methods
	 */
	OSCServer::MethodHandlerId *geo_osc_id;
	static void
	geo_osc(Layer *obj, lo_arg **argv)
	{
		SDL_Rect geo = {
			(Sint16)argv[0]->i, (Sint16)argv[1]->i,
			(Uint16)argv[2]->i, (Uint16)argv[3]->i
		};
		obj->geo(geo);
	}
	OSCServer::MethodHandlerId *alpha_osc_id;
	static void
	alpha_osc(Layer *obj, lo_arg **argv)
	{
		obj->alpha(argv[0]->f);
	}
};

class LayerList : Mutex {
	LIST_HEAD(layers_head, Layer) head;

public:
	LayerList() : Mutex()
	{
		LIST_INIT(&head);
	}
	~LayerList();

	void insert(int pos, Layer *layer);
	void delete_layer(Layer *layer);
	void render(SDL_Surface *target);
};

#endif