obs-scene.c 5.3 KB
Newer Older
J
jp9000 已提交
1 2 3 4 5
/******************************************************************************
    Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
J
jp9000 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20
    (at your option) any later version.

    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 "graphics/math-defs.h"
#include "obs-scene.h"

21 22 23 24 25 26
static const char *scene_getname(const char *locale)
{
	/* TODO: locale lookup of display name */
	return "Scene";
}

27
static void *scene_create(const char *settings, struct obs_source *source)
J
jp9000 已提交
28 29 30 31 32 33 34 35
{
	struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
	scene->source = source;
	da_init(scene->items);

	return scene;
}

36
static void scene_destroy(void *data)
J
jp9000 已提交
37 38 39 40
{
	struct obs_scene *scene = data;
	size_t i;

41 42 43 44 45 46
	for (i = 0; i < scene->items.num; i++) {
		struct obs_scene_item *item = scene->items.array[i];
		if (item->source)
			obs_source_release(item->source);
		bfree(item);
	}
J
jp9000 已提交
47 48 49 50 51

	da_free(scene->items);
	bfree(scene);
}

52
static uint32_t scene_get_output_flags(void *data)
J
jp9000 已提交
53
{
54
	return SOURCE_VIDEO;
J
jp9000 已提交
55 56
}

57
static void scene_video_render(void *data)
J
jp9000 已提交
58 59 60 61 62 63 64
{
	struct obs_scene *scene = data;
	size_t i;

	for (i = scene->items.num; i > 0; i--) {
		struct obs_scene_item *item = scene->items.array[i-1];

65 66 67 68 69 70
		if (obs_source_removed(item->source)) {
			obs_source_release(item->source);
			da_erase(scene->items, i--);
			continue;
		}

J
jp9000 已提交
71 72 73 74 75 76
		gs_matrix_push();
		gs_matrix_translate3f(item->origin.x, item->origin.y, 0.0f);
		gs_matrix_scale3f(item->scale.x, item->scale.y, 1.0f);
		gs_matrix_rotaa4f(0.0f, 0.0f, 1.0f, RAD(-item->rot));
		gs_matrix_translate3f(-item->pos.x, -item->pos.y, 0.0f);

77
		obs_source_video_render(item->source);
J
jp9000 已提交
78 79 80 81 82

		gs_matrix_pop();
	}
}

J
jp9000 已提交
83
static uint32_t scene_getsize(void *data)
J
jp9000 已提交
84
{
J
jp9000 已提交
85
	return 0;
J
jp9000 已提交
86 87 88 89 90
}

static const struct source_info scene_info =
{
	.name             = "scene",
91
	.getname          = scene_getname,
92 93 94 95 96 97
	.create           = scene_create,
	.destroy          = scene_destroy,
	.get_output_flags = scene_get_output_flags,
	.video_render     = scene_video_render,
	.getwidth         = scene_getsize,
	.getheight        = scene_getsize,
J
jp9000 已提交
98 99
};

100
obs_scene_t obs_scene_create(void)
J
jp9000 已提交
101 102
{
	struct obs_source *source = bmalloc(sizeof(struct obs_source));
103
	struct obs_scene  *scene  = scene_create(NULL, source);
J
jp9000 已提交
104

105 106
	memset(source, 0, sizeof(struct obs_source));

J
jp9000 已提交
107 108 109 110 111 112 113
	source->data = scene;
	if (!source->data) {
		bfree(source);
		return NULL;
	}

	scene->source = source;
114
	obs_source_init(source, NULL, &scene_info);
J
jp9000 已提交
115 116 117 118
	memcpy(&source->callbacks, &scene_info, sizeof(struct source_info));
	return scene;
}

119
void obs_scene_destroy(obs_scene_t scene)
J
jp9000 已提交
120 121
{
	if (scene)
122
		obs_source_release(scene->source);
J
jp9000 已提交
123 124
}

125
obs_source_t obs_scene_getsource(obs_scene_t scene)
J
jp9000 已提交
126 127 128 129
{
	return scene->source;
}

130
obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
J
jp9000 已提交
131 132 133 134 135 136 137 138
{
	struct obs_scene_item *item = bmalloc(sizeof(struct obs_scene_item));
	memset(item, 0, sizeof(struct obs_scene_item));
	item->source  = source;
	item->visible = true;
	item->parent  = scene;
	vec2_set(&item->scale, 1.0f, 1.0f);

139 140 141
	if (source)
		obs_source_addref(source);

J
jp9000 已提交
142 143 144 145
	da_push_back(scene->items, &item);
	return item;
}

J
jp9000 已提交
146
void obs_sceneitem_destroy(obs_sceneitem_t item)
J
jp9000 已提交
147 148
{
	if (item) {
149 150 151
		if (item->source)
			obs_source_release(item->source);

J
jp9000 已提交
152 153 154 155 156
		da_erase_item(item->parent->items, item);
		bfree(item);
	}
}

157
void obs_sceneitem_setpos(obs_sceneitem_t item, const struct vec2 *pos)
J
jp9000 已提交
158 159 160 161
{
	vec2_copy(&item->pos, pos);
}

162
void obs_sceneitem_setrot(obs_sceneitem_t item, float rot)
J
jp9000 已提交
163 164 165 166
{
	item->rot = rot;
}

167
void obs_sceneitem_setorigin(obs_sceneitem_t item, const struct vec2 *origin)
J
jp9000 已提交
168 169 170 171
{
	vec2_copy(&item->origin, origin);
}

172
void obs_sceneitem_setscale(obs_sceneitem_t item, const struct vec2 *scale)
J
jp9000 已提交
173 174 175 176
{
	vec2_copy(&item->scale, scale);
}

177
void obs_sceneitem_setorder(obs_sceneitem_t item, enum order_movement movement)
J
jp9000 已提交
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
{
	struct obs_scene *scene = item->parent;

	if (movement == ORDER_MOVE_UP) {
		size_t idx = da_find(scene->items, &item, 0);
		if (idx > 0)
			da_move_item(scene->items, idx, idx-1);

	} else if (movement == ORDER_MOVE_DOWN) {
		size_t idx = da_find(scene->items, &item, 0);
		if (idx < (scene->items.num-1))
			da_move_item(scene->items, idx, idx+1);

	} else if (movement == ORDER_MOVE_TOP) {
		size_t idx = da_find(scene->items, &item, 0);
		if (idx > 0)
			da_move_item(scene->items, idx, 0);

	} else if (movement == ORDER_MOVE_TOP) {
		size_t idx = da_find(scene->items, &item, 0);
		if (idx < (scene->items.num-1))
			da_move_item(scene->items, idx, scene->items.num-1);
	}
}

203
void obs_sceneitem_getpos(obs_sceneitem_t item, struct vec2 *pos)
J
jp9000 已提交
204 205 206 207
{
	vec2_copy(pos, &item->pos);
}

208
float obs_sceneitem_getrot(obs_sceneitem_t item)
J
jp9000 已提交
209 210 211 212
{
	return item->rot;
}

213
void obs_sceneitem_getorigin(obs_sceneitem_t item, struct vec2 *origin)
J
jp9000 已提交
214 215 216 217
{
	vec2_copy(origin, &item->origin);
}

218
void obs_sceneitem_getscale(obs_sceneitem_t item, struct vec2 *scale)
J
jp9000 已提交
219 220 221
{
	vec2_copy(scale, &item->scale);
}