obs-scene.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "graphics/math-defs.h"
  15. #include "obs-scene.h"
  16. static const char *scene_getname(const char *locale)
  17. {
  18. /* TODO: locale lookup of display name */
  19. return "Scene";
  20. }
  21. static void *scene_create(const char *settings, struct obs_source *source)
  22. {
  23. struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
  24. scene->source = source;
  25. da_init(scene->items);
  26. return scene;
  27. }
  28. static void scene_destroy(void *data)
  29. {
  30. struct obs_scene *scene = data;
  31. size_t i;
  32. for (i = 0; i < scene->items.num; i++)
  33. bfree(scene->items.array[i]);
  34. da_free(scene->items);
  35. bfree(scene);
  36. }
  37. static uint32_t scene_get_output_flags(void *data)
  38. {
  39. return SOURCE_VIDEO;
  40. }
  41. static void scene_video_render(void *data)
  42. {
  43. struct obs_scene *scene = data;
  44. size_t i;
  45. for (i = scene->items.num; i > 0; i--) {
  46. struct obs_scene_item *item = scene->items.array[i-1];
  47. gs_matrix_push();
  48. gs_matrix_translate3f(item->origin.x, item->origin.y, 0.0f);
  49. gs_matrix_scale3f(item->scale.x, item->scale.y, 1.0f);
  50. gs_matrix_rotaa4f(0.0f, 0.0f, 1.0f, RAD(-item->rot));
  51. gs_matrix_translate3f(-item->pos.x, -item->pos.y, 0.0f);
  52. obs_source_video_render(item->source);
  53. gs_matrix_pop();
  54. }
  55. }
  56. static uint32_t scene_getsize(void *data)
  57. {
  58. return 0;
  59. }
  60. static bool scene_enum_children(void *data, size_t idx, obs_source_t *child)
  61. {
  62. struct obs_scene *scene = data;
  63. if (idx >= scene->items.num)
  64. return false;
  65. *child = scene->items.array[idx]->source;
  66. return true;
  67. }
  68. /* thanks for being completely worthless, microsoft. */
  69. #if 1
  70. static const struct source_info scene_info =
  71. {
  72. "scene",
  73. scene_getname,
  74. scene_create,
  75. scene_destroy,
  76. scene_get_output_flags, NULL, NULL, NULL, NULL,
  77. scene_video_render,
  78. scene_getsize,
  79. scene_getsize, NULL, NULL,
  80. scene_enum_children, NULL, NULL
  81. };
  82. #else
  83. static const struct source_info scene_info =
  84. {
  85. .name = "scene",
  86. .getname = scene_getname,
  87. .create = scene_create,
  88. .destroy = scene_destroy,
  89. .get_output_flags = scene_get_output_flags,
  90. .video_render = scene_video_render,
  91. .getwidth = scene_getsize,
  92. .getheight = scene_getsize,
  93. .enum_children = scene_enum_children
  94. };
  95. #endif
  96. obs_scene_t obs_scene_create(void)
  97. {
  98. struct obs_source *source = bmalloc(sizeof(struct obs_source));
  99. struct obs_scene *scene = scene_create(NULL, source);
  100. memset(source, 0, sizeof(struct obs_source));
  101. source->data = scene;
  102. if (!source->data) {
  103. bfree(source);
  104. return NULL;
  105. }
  106. scene->source = source;
  107. obs_source_init(source, NULL, &scene_info);
  108. memcpy(&source->callbacks, &scene_info, sizeof(struct source_info));
  109. return scene;
  110. }
  111. void obs_scene_destroy(obs_scene_t scene)
  112. {
  113. if (scene)
  114. obs_source_destroy(scene->source);
  115. }
  116. obs_source_t obs_scene_getsource(obs_scene_t scene)
  117. {
  118. return scene->source;
  119. }
  120. obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
  121. {
  122. struct obs_scene_item *item = bmalloc(sizeof(struct obs_scene_item));
  123. memset(item, 0, sizeof(struct obs_scene_item));
  124. item->source = source;
  125. item->visible = true;
  126. item->parent = scene;
  127. vec2_set(&item->scale, 1.0f, 1.0f);
  128. da_push_back(scene->items, &item);
  129. return item;
  130. }
  131. void obs_sceneitem_destroy(obs_sceneitem_t item)
  132. {
  133. if (item) {
  134. da_erase_item(item->parent->items, item);
  135. bfree(item);
  136. }
  137. }
  138. void obs_sceneitem_setpos(obs_sceneitem_t item, const struct vec2 *pos)
  139. {
  140. vec2_copy(&item->pos, pos);
  141. }
  142. void obs_sceneitem_setrot(obs_sceneitem_t item, float rot)
  143. {
  144. item->rot = rot;
  145. }
  146. void obs_sceneitem_setorigin(obs_sceneitem_t item, const struct vec2 *origin)
  147. {
  148. vec2_copy(&item->origin, origin);
  149. }
  150. void obs_sceneitem_setscale(obs_sceneitem_t item, const struct vec2 *scale)
  151. {
  152. vec2_copy(&item->scale, scale);
  153. }
  154. void obs_sceneitem_setorder(obs_sceneitem_t item, enum order_movement movement)
  155. {
  156. struct obs_scene *scene = item->parent;
  157. if (movement == ORDER_MOVE_UP) {
  158. size_t idx = da_find(scene->items, &item, 0);
  159. if (idx > 0)
  160. da_move_item(scene->items, idx, idx-1);
  161. } else if (movement == ORDER_MOVE_DOWN) {
  162. size_t idx = da_find(scene->items, &item, 0);
  163. if (idx < (scene->items.num-1))
  164. da_move_item(scene->items, idx, idx+1);
  165. } else if (movement == ORDER_MOVE_TOP) {
  166. size_t idx = da_find(scene->items, &item, 0);
  167. if (idx > 0)
  168. da_move_item(scene->items, idx, 0);
  169. } else if (movement == ORDER_MOVE_TOP) {
  170. size_t idx = da_find(scene->items, &item, 0);
  171. if (idx < (scene->items.num-1))
  172. da_move_item(scene->items, idx, scene->items.num-1);
  173. }
  174. }
  175. void obs_sceneitem_getpos(obs_sceneitem_t item, struct vec2 *pos)
  176. {
  177. vec2_copy(pos, &item->pos);
  178. }
  179. float obs_sceneitem_getrot(obs_sceneitem_t item)
  180. {
  181. return item->rot;
  182. }
  183. void obs_sceneitem_getorigin(obs_sceneitem_t item, struct vec2 *origin)
  184. {
  185. vec2_copy(origin, &item->origin);
  186. }
  187. void obs_sceneitem_getscale(obs_sceneitem_t item, struct vec2 *scale)
  188. {
  189. vec2_copy(scale, &item->scale);
  190. }