From 999df5e6e5235bd5179a9a6e6102eb77c44e111e Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Mon, 1 Oct 2018 00:54:18 +0200 Subject: [PATCH] gfx-source-texture: Refactoring --- source/gfx-source-texture.cpp | 78 ++++++++++++++++++----------------- source/gfx-source-texture.h | 6 +-- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/source/gfx-source-texture.cpp b/source/gfx-source-texture.cpp index 4be7d5ee..fbcbd26b 100644 --- a/source/gfx-source-texture.cpp +++ b/source/gfx-source-texture.cpp @@ -19,57 +19,59 @@ gfx::source_texture::~source_texture() { - obs_source_remove_active_child(target, source); - if (source) { - obs_source_release(source); - source = nullptr; + if (child) { + if (parent) { + obs_source_remove_active_child(parent, child); + } + obs_source_release(child); } - m_rt = nullptr; } -gfx::source_texture::source_texture(obs_source_t* parent) +gfx::source_texture::source_texture(obs_source_t* _parent) { - m_rt = std::make_shared(GS_RGBA, GS_ZS_NONE); - target = parent; + render_target = std::make_shared(GS_RGBA, GS_ZS_NONE); + parent = _parent; +} + +gfx::source_texture::source_texture(const char* _name, obs_source_t* _parent) : source_texture(_parent) +{ + child = obs_get_source_by_name(_name); + if (!child) { + throw std::invalid_argument("No such source."); + } + if (!obs_source_add_active_child(parent, child)) { + throw std::runtime_error("Recursion is not allowed."); + } +} + +gfx::source_texture::source_texture(std::string _name, obs_source_t* _parent) : source_texture(_name.c_str(), _parent) +{} + +gfx::source_texture::source_texture(obs_source_t* _source, obs_source_t* _parent) : source_texture(_parent) +{ + child = _source; + if (!child) { + throw std::invalid_argument("No such source."); + } + if (!obs_source_add_active_child(parent, child)) { + throw std::runtime_error("Recursion is not allowed."); + } + obs_source_addref(child); } obs_source_t* gfx::source_texture::get_object() { - return source; + return child; } obs_source_t* gfx::source_texture::get_parent() { - return target; -} - -gfx::source_texture::source_texture(const char* name, obs_source_t* parent) : source_texture(parent) -{ - source = obs_get_source_by_name(name); - if (!source) { - throw std::invalid_argument("No such source."); - } - if (!obs_source_add_active_child(target, source)) { - throw std::runtime_error("Recursion is not allowed."); - } -} - -gfx::source_texture::source_texture(std::string name, obs_source_t* parent) : source_texture(name.c_str(), parent) {} - -gfx::source_texture::source_texture(obs_source_t* src, obs_source_t* parent) : source_texture(parent) -{ - source = src; - if (!source) { - throw std::invalid_argument("No such source."); - } - if (!obs_source_add_active_child(target, source)) { - throw std::runtime_error("Recursion is not allowed."); - } + return parent; } std::shared_ptr gfx::source_texture::render(size_t width, size_t height) { - if (!source) { + if (!child) { throw std::invalid_argument("Missing source to render."); } if ((width == 0) || (width >= 16384)) { @@ -80,15 +82,15 @@ std::shared_ptr gfx::source_texture::render(size_t width, size_t he } { - auto op = m_rt->render((uint32_t)width, (uint32_t)height); + auto op = render_target->render((uint32_t)width, (uint32_t)height); vec4 black; vec4_zero(&black); gs_ortho(0, (float_t)width, 0, (float_t)height, 0, 1); gs_clear(GS_CLEAR_COLOR, &black, 0, 0); - obs_source_video_render(source); + obs_source_video_render(child); } std::shared_ptr tex; - m_rt->get_texture(tex); + render_target->get_texture(tex); return tex; } diff --git a/source/gfx-source-texture.h b/source/gfx-source-texture.h index 63fa59c0..4b17c6b7 100644 --- a/source/gfx-source-texture.h +++ b/source/gfx-source-texture.h @@ -27,10 +27,10 @@ extern "C" { namespace gfx { class source_texture { - obs_source_t* source; - obs_source_t* target; + obs_source_t* parent; + obs_source_t* child; - std::shared_ptr m_rt; + std::shared_ptr render_target; source_texture(obs_source_t* parent);