obs-scene.c 5.4 KB
Newer Older
J
jp9000 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/******************************************************************************
    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
    the Free Software Foundation, either version 3 of the License, or
    (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
static void *scene_create(const char *settings, struct obs_source *source)
J
jp9000 已提交
22 23 24 25 26 27 28 29
{
	struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
	scene->source = source;
	da_init(scene->items);

	return scene;
}

30
static void scene_destroy(void *data)
J
jp9000 已提交
31 32 33 34 35 36 37 38 39 40 41
{
	struct obs_scene *scene = data;
	size_t i;

	for (i = 0; i < scene->items.num; i++)
		bfree(scene->items.array[i]);

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

42
static uint32_t scene_get_output_flags(void *data)
J
jp9000 已提交
43 44 45 46
{
	return SOURCE_VIDEO | SOURCE_AUDIO;
}

47
static void scene_video_render(void *data)
J
jp9000 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60
{
	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];

		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);

61
		obs_source_video_render(item->source);
J
jp9000 已提交
62 63 64 65 66

		gs_matrix_pop();
	}
}

J
jp9000 已提交
67
static uint32_t scene_getsize(void *data)
J
jp9000 已提交
68
{
J
jp9000 已提交
69
	return 0;
J
jp9000 已提交
70 71
}

72
static bool scene_enum_children(void *data, size_t idx, obs_source_t *child)
J
jp9000 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86
{
	struct obs_scene *scene = data;
	if (idx >= scene->items.num)
		return false;

	*child = scene->items.array[idx]->source;
	return true;
}

/* thanks for being completely worthless, microsoft. */
#if 1
static const struct source_info scene_info =
{
	"scene",
87 88 89 90 91 92 93
	scene_create,
	scene_destroy,
	scene_get_output_flags, NULL, NULL, NULL, NULL,
	scene_video_render,
	scene_getsize,
	scene_getsize, NULL, NULL,
	scene_enum_children, NULL, NULL
J
jp9000 已提交
94 95 96 97 98
};
#else
static const struct source_info scene_info =
{
	.name             = "scene",
99 100 101 102 103 104 105
	.create           = scene_create,
	.destroy          = scene_destroy,
	.get_output_flags = scene_get_output_flags,
	.video_render     = scene_video_render,
	.getwidth         = scene_getsize,
	.getheight        = scene_getsize,
	.enum_children    = scene_enum_children
J
jp9000 已提交
106 107 108
};
#endif

109
obs_scene_t obs_scene_create(void)
J
jp9000 已提交
110 111
{
	struct obs_source *source = bmalloc(sizeof(struct obs_source));
112
	struct obs_scene  *scene  = scene_create(NULL, source);
J
jp9000 已提交
113 114 115 116 117 118 119 120

	source->data = scene;
	if (!source->data) {
		bfree(source);
		return NULL;
	}

	scene->source = source;
121
	obs_source_init(source);
J
jp9000 已提交
122 123 124 125
	memcpy(&source->callbacks, &scene_info, sizeof(struct source_info));
	return scene;
}

126
void obs_scene_destroy(obs_scene_t scene)
J
jp9000 已提交
127 128
{
	if (scene)
129
		obs_source_destroy(scene->source);
J
jp9000 已提交
130 131
}

132
obs_source_t obs_scene_getsource(obs_scene_t scene)
J
jp9000 已提交
133 134 135 136
{
	return scene->source;
}

137
obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
J
jp9000 已提交
138 139 140 141 142 143 144 145 146 147 148 149
{
	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);

	da_push_back(scene->items, &item);
	return item;
}

J
jp9000 已提交
150
void obs_sceneitem_destroy(obs_sceneitem_t item)
J
jp9000 已提交
151 152 153 154 155 156 157
{
	if (item) {
		da_erase_item(item->parent->items, item);
		bfree(item);
	}
}

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

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

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

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

178
void obs_sceneitem_setorder(obs_sceneitem_t item, enum order_movement movement)
J
jp9000 已提交
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
{
	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);
	}
}

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

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

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

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