obs-scene.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 void *scene_create(const char *settings, struct obs_source *source)
  17. {
  18. struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
  19. scene->source = source;
  20. da_init(scene->items);
  21. return scene;
  22. }
  23. static void scene_destroy(void *data)
  24. {
  25. struct obs_scene *scene = data;
  26. size_t i;
  27. for (i = 0; i < scene->items.num; i++)
  28. bfree(scene->items.array[i]);
  29. da_free(scene->items);
  30. bfree(scene);
  31. }
  32. static uint32_t scene_get_output_flags(void *data)
  33. {
  34. return SOURCE_VIDEO;
  35. }
  36. static void scene_video_render(void *data)
  37. {
  38. struct obs_scene *scene = data;
  39. size_t i;
  40. for (i = scene->items.num; i > 0; i--) {
  41. struct obs_scene_item *item = scene->items.array[i-1];
  42. gs_matrix_push();
  43. gs_matrix_translate3f(item->origin.x, item->origin.y, 0.0f);
  44. gs_matrix_scale3f(item->scale.x, item->scale.y, 1.0f);
  45. gs_matrix_rotaa4f(0.0f, 0.0f, 1.0f, RAD(-item->rot));
  46. gs_matrix_translate3f(-item->pos.x, -item->pos.y, 0.0f);
  47. obs_source_video_render(item->source);
  48. gs_matrix_pop();
  49. }
  50. }
  51. static uint32_t scene_getsize(void *data)
  52. {
  53. return 0;
  54. }
  55. static bool scene_enum_children(void *data, size_t idx, obs_source_t *child)
  56. {
  57. struct obs_scene *scene = data;
  58. if (idx >= scene->items.num)
  59. return false;
  60. *child = scene->items.array[idx]->source;
  61. return true;
  62. }
  63. /* thanks for being completely worthless, microsoft. */
  64. #if 1
  65. static const struct source_info scene_info =
  66. {
  67. "scene",
  68. scene_create,
  69. scene_destroy,
  70. scene_get_output_flags, NULL, NULL, NULL, NULL,
  71. scene_video_render,
  72. scene_getsize,
  73. scene_getsize, NULL, NULL,
  74. scene_enum_children, NULL, NULL
  75. };
  76. #else
  77. static const struct source_info scene_info =
  78. {
  79. .name = "scene",
  80. .create = scene_create,
  81. .destroy = scene_destroy,
  82. .get_output_flags = scene_get_output_flags,
  83. .video_render = scene_video_render,
  84. .getwidth = scene_getsize,
  85. .getheight = scene_getsize,
  86. .enum_children = scene_enum_children
  87. };
  88. #endif
  89. obs_scene_t obs_scene_create(void)
  90. {
  91. struct obs_source *source = bmalloc(sizeof(struct obs_source));
  92. struct obs_scene *scene = scene_create(NULL, source);
  93. memset(source, 0, sizeof(struct obs_source));
  94. source->data = scene;
  95. if (!source->data) {
  96. bfree(source);
  97. return NULL;
  98. }
  99. scene->source = source;
  100. obs_source_init(source, NULL, &scene_info);
  101. memcpy(&source->callbacks, &scene_info, sizeof(struct source_info));
  102. return scene;
  103. }
  104. void obs_scene_destroy(obs_scene_t scene)
  105. {
  106. if (scene)
  107. obs_source_destroy(scene->source);
  108. }
  109. obs_source_t obs_scene_getsource(obs_scene_t scene)
  110. {
  111. return scene->source;
  112. }
  113. obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
  114. {
  115. struct obs_scene_item *item = bmalloc(sizeof(struct obs_scene_item));
  116. memset(item, 0, sizeof(struct obs_scene_item));
  117. item->source = source;
  118. item->visible = true;
  119. item->parent = scene;
  120. vec2_set(&item->scale, 1.0f, 1.0f);
  121. da_push_back(scene->items, &item);
  122. return item;
  123. }
  124. void obs_sceneitem_destroy(obs_sceneitem_t item)
  125. {
  126. if (item) {
  127. da_erase_item(item->parent->items, item);
  128. bfree(item);
  129. }
  130. }
  131. void obs_sceneitem_setpos(obs_sceneitem_t item, const struct vec2 *pos)
  132. {
  133. vec2_copy(&item->pos, pos);
  134. }
  135. void obs_sceneitem_setrot(obs_sceneitem_t item, float rot)
  136. {
  137. item->rot = rot;
  138. }
  139. void obs_sceneitem_setorigin(obs_sceneitem_t item, const struct vec2 *origin)
  140. {
  141. vec2_copy(&item->origin, origin);
  142. }
  143. void obs_sceneitem_setscale(obs_sceneitem_t item, const struct vec2 *scale)
  144. {
  145. vec2_copy(&item->scale, scale);
  146. }
  147. void obs_sceneitem_setorder(obs_sceneitem_t item, enum order_movement movement)
  148. {
  149. struct obs_scene *scene = item->parent;
  150. if (movement == ORDER_MOVE_UP) {
  151. size_t idx = da_find(scene->items, &item, 0);
  152. if (idx > 0)
  153. da_move_item(scene->items, idx, idx-1);
  154. } else if (movement == ORDER_MOVE_DOWN) {
  155. size_t idx = da_find(scene->items, &item, 0);
  156. if (idx < (scene->items.num-1))
  157. da_move_item(scene->items, idx, idx+1);
  158. } else if (movement == ORDER_MOVE_TOP) {
  159. size_t idx = da_find(scene->items, &item, 0);
  160. if (idx > 0)
  161. da_move_item(scene->items, idx, 0);
  162. } else if (movement == ORDER_MOVE_TOP) {
  163. size_t idx = da_find(scene->items, &item, 0);
  164. if (idx < (scene->items.num-1))
  165. da_move_item(scene->items, idx, scene->items.num-1);
  166. }
  167. }
  168. void obs_sceneitem_getpos(obs_sceneitem_t item, struct vec2 *pos)
  169. {
  170. vec2_copy(pos, &item->pos);
  171. }
  172. float obs_sceneitem_getrot(obs_sceneitem_t item)
  173. {
  174. return item->rot;
  175. }
  176. void obs_sceneitem_getorigin(obs_sceneitem_t item, struct vec2 *origin)
  177. {
  178. vec2_copy(origin, &item->origin);
  179. }
  180. void obs_sceneitem_getscale(obs_sceneitem_t item, struct vec2 *scale)
  181. {
  182. vec2_copy(scale, &item->scale);
  183. }