From 83b3d508575124fb83c823ca2439f728d43c7647 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 16 Jun 2019 19:12:11 +0200 Subject: [PATCH] Refactor out find_inputs_for_block(). --- nageru/scene.cpp | 59 ++++++++++++++++++++++++++---------------------- nageru/scene.h | 1 + 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/nageru/scene.cpp b/nageru/scene.cpp index ccb3b1b..fded0e0 100644 --- a/nageru/scene.cpp +++ b/nageru/scene.cpp @@ -109,6 +109,37 @@ int Scene::add_input(lua_State* L) return wrap_lua_existing_object_nonowned(L, "Block", block); } +void Scene::find_inputs_for_block(lua_State *L, Scene *scene, Block *block) +{ + if (lua_gettop(L) == 2) { + // Implicitly the last added effect. + assert(!scene->blocks.empty()); + block->inputs.push_back(scene->blocks.size() - 1); + return; + } + + for (int idx = 3; idx <= lua_gettop(L); ++idx) { + Block *input_block = nullptr; + if (luaL_testudata(L, idx, "Block")) { + input_block = *(Block **)luaL_checkudata(L, idx, "Block"); + } else { + EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint"); + + // Search through all the blocks to figure out which one contains this effect. + for (Block *block : scene->blocks) { + if (find(block->alternatives.begin(), block->alternatives.end(), blueprint) != block->alternatives.end()) { + input_block = block; + break; + } + } + if (input_block == nullptr) { + luaL_error(L, "Input effect in parameter #%d has not been added to this scene", idx - 1); + } + } + block->inputs.push_back(input_block->idx); + } +} + int Scene::add_effect(lua_State* L) { assert(lua_gettop(L) >= 2); @@ -130,33 +161,7 @@ int Scene::add_effect(lua_State* L) block->alternatives.push_back(blueprint); } - // Find the inputs. - if (lua_gettop(L) == 2) { - assert(!scene->blocks.empty()); - block->inputs.push_back(scene->blocks.size() - 1); - } else { - for (int idx = 3; idx <= lua_gettop(L); ++idx) { - Block *input_block = nullptr; - if (luaL_testudata(L, idx, "Block")) { - input_block = *(Block **)luaL_checkudata(L, idx, "Block"); - } else { - EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint"); - - // Search through all the blocks to figure out which one contains this effect. - for (Block *block : scene->blocks) { - if (find(block->alternatives.begin(), block->alternatives.end(), blueprint) != block->alternatives.end()) { - input_block = block; - break; - } - } - if (input_block == nullptr) { - luaL_error(L, "Input effect in parameter #%d has not been added to this scene", idx - 1); - } - } - block->inputs.push_back(input_block->idx); - } - } - + find_inputs_for_block(L, scene, block); scene->blocks.push_back(block); return wrap_lua_existing_object_nonowned(L, "Block", block); diff --git a/nageru/scene.h b/nageru/scene.h index 7d83a27..bdd5d7c 100644 --- a/nageru/scene.h +++ b/nageru/scene.h @@ -134,6 +134,7 @@ private: movit::Effect *instantiate_effects(const Block *block, size_t chain_idx, Instantiation *instantiation); size_t compute_chain_number_for_block(size_t block_idx) const; + static void find_inputs_for_block(lua_State *L, Scene *scene, Block *block); public: Scene(Theme *theme, float aspect_nom, float aspect_denom); -- 2.39.2