|
1 | 1 | #include "scene.h" |
| 2 | +#include "dsk.h" |
| 3 | +#include <map> |
2 | 4 |
|
3 | 5 | Scene::Scene(std::string &id, Settings *settings) : |
4 | 6 | id(id), |
5 | 7 | settings(settings), |
6 | | - obs_scene(createObsScene(id)) { |
| 8 | + obs_scene(createObsScene(id)), |
| 9 | + obs_output_scene(nullptr) { |
| 10 | +} |
| 11 | + |
| 12 | +Scene::~Scene() { |
| 13 | + if (obs_scene) { |
| 14 | + obs_scene_release(obs_scene); |
| 15 | + } |
| 16 | + if (obs_output_scene) { |
| 17 | + obs_scene_release(obs_scene); |
| 18 | + } |
7 | 19 | } |
8 | 20 |
|
9 | 21 | void Scene::addSource(std::string &sourceId, SourceType sourceType, std::string &sourceUrl) { |
@@ -47,6 +59,65 @@ obs_scene_t *Scene::createObsScene(std::string &sceneId) { |
47 | 59 | return scene; |
48 | 60 | } |
49 | 61 |
|
| 62 | +obs_scene_t *Scene::getObsOutputScene(std::map<std::string, Dsk*> &dsks) { |
| 63 | + if (obs_output_scene) { |
| 64 | + obs_scene_release(obs_output_scene); |
| 65 | + obs_output_scene = nullptr; |
| 66 | + } |
| 67 | + |
| 68 | + // create duplicate scene |
| 69 | + obs_output_scene = obs_scene_duplicate(obs_scene, (id + "_output").c_str(), OBS_SCENE_DUP_REFS); |
| 70 | + |
| 71 | + // add dsks |
| 72 | + for (auto &dsk : dsks) { |
| 73 | + // Add the source to the scene |
| 74 | + obs_scene_item *obs_scene_item = obs_scene_add(obs_output_scene, dsk.second->getObsSource()); |
| 75 | + if (!obs_scene_item) { |
| 76 | + throw std::runtime_error("Failed to add scene item."); |
| 77 | + } |
| 78 | + |
| 79 | + // set position |
| 80 | + struct vec2 pos = {}; |
| 81 | + pos.x = (float)dsk.second->getLeft(); |
| 82 | + pos.y = (float)dsk.second->getTop(); |
| 83 | + obs_sceneitem_set_pos(obs_scene_item, &pos); |
| 84 | + |
| 85 | + // set align |
| 86 | + std::string position = dsk.second->getPosition(); |
| 87 | + uint32_t align = 0; |
| 88 | + if (position == "top") { |
| 89 | + align = OBS_ALIGN_TOP; |
| 90 | + } else if (position == "top-right") { |
| 91 | + align = OBS_ALIGN_TOP + OBS_ALIGN_RIGHT; |
| 92 | + } else if (position == "right") { |
| 93 | + align = OBS_ALIGN_RIGHT; |
| 94 | + } else if (position == "bottom-right") { |
| 95 | + align = OBS_ALIGN_BOTTOM + OBS_ALIGN_RIGHT; |
| 96 | + } else if (position == "bottom") { |
| 97 | + align = OBS_ALIGN_BOTTOM; |
| 98 | + } else if (position == "bottom-left") { |
| 99 | + align = OBS_ALIGN_BOTTOM + OBS_ALIGN_LEFT; |
| 100 | + } else if (position == "left") { |
| 101 | + align = OBS_ALIGN_LEFT; |
| 102 | + } else if (position == "top-left") { |
| 103 | + align = OBS_ALIGN_TOP + OBS_ALIGN_LEFT; |
| 104 | + } |
| 105 | + obs_sceneitem_set_bounds_alignment(obs_scene_item, align); |
| 106 | + |
| 107 | + // set size |
| 108 | + struct vec2 bounds = {}; |
| 109 | + bounds.x = (float)dsk.second->getWidth(); |
| 110 | + bounds.y = (float)dsk.second->getHeight(); |
| 111 | + obs_sceneitem_set_bounds_type(obs_scene_item, OBS_BOUNDS_SCALE_INNER); |
| 112 | + obs_sceneitem_set_bounds(obs_scene_item, &bounds); |
| 113 | + |
| 114 | + // set top most |
| 115 | + obs_sceneitem_set_order(obs_scene_item, OBS_ORDER_MOVE_TOP); |
| 116 | + } |
| 117 | + |
| 118 | + return obs_output_scene; |
| 119 | +} |
| 120 | + |
50 | 121 | Napi::Object Scene::getNapiScene(const Napi::Env &env) { |
51 | 122 | auto napiScene = Napi::Object::New(env); |
52 | 123 | auto napiSources = Napi::Array::New(env, sources.size()); |
|
0 commit comments