From d8fadf26db4a259ce24b958e8c254d77dd7ea80a Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 17 Jul 2019 15:46:40 +0200 Subject: [PATCH] Deal better with the user forgetting to set width/height on effects that require them. --- nageru/scene.cpp | 38 ++++++++++++++++++++++++++++++++++++++ nageru/scene.h | 2 ++ 2 files changed, 40 insertions(+) diff --git a/nageru/scene.cpp b/nageru/scene.cpp index 70bccbe..1694882 100644 --- a/nageru/scene.cpp +++ b/nageru/scene.cpp @@ -35,6 +35,16 @@ int find_index_of(const Block *block, EffectType val) return -1; } +string get_declaration_point(lua_State *L) +{ + lua_Debug ar; + lua_getstack(L, 1, &ar); + lua_getinfo(L, "nSl", &ar); + char buf[256]; + snprintf(buf, sizeof(buf), "%s:%d", ar.source, ar.currentline); + return buf; +} + Scene::Scene(Theme *theme, float aspect_nom, float aspect_denom) : theme(theme), aspect_nom(aspect_nom), aspect_denom(aspect_denom), resource_pool(theme->get_resource_pool()) {} @@ -146,6 +156,7 @@ int Scene::add_input(lua_State* L) Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene"); Block *block = new Block; + block->declaration_point = get_declaration_point(L); block->idx = scene->blocks.size(); if (lua_gettop(L) == 1) { // No parameter given, so a flexible input. @@ -222,6 +233,7 @@ int Scene::add_effect(lua_State* L) Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene"); Block *block = new Block; + block->declaration_point = get_declaration_point(L); block->idx = scene->blocks.size(); if (lua_istable(L, 2)) { @@ -258,6 +270,7 @@ int Scene::add_optional_effect(lua_State* L) Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene"); Block *block = new Block; + block->declaration_point = get_declaration_point(L); block->idx = scene->blocks.size(); EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint"); @@ -450,8 +463,19 @@ Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &inp Block *block = blocks[index_and_effect.first]; Effect *effect = index_and_effect.second; + bool missing_width = (current_type(block) == RESIZE_EFFECT || + current_type(block) == RESAMPLE_EFFECT || + current_type(block) == PADDING_EFFECT); + bool missing_height = missing_width; + // Get the effects currently set on the block. if (current_type(block) != IDENTITY_EFFECT) { // Ignore settings on optional effects. + if (block->int_parameters.count("width") && block->int_parameters["width"] > 0) { + missing_width = false; + } + if (block->int_parameters.count("height") && block->int_parameters["height"] > 0) { + missing_height = false; + } for (const auto &key_and_tuple : block->int_parameters) { int_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second); } @@ -470,6 +494,12 @@ Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &inp // so they are set afterwards. if (!block->alternatives.empty()) { EffectBlueprint *blueprint = block->alternatives[block->currently_chosen_alternative]; + if (blueprint->int_parameters.count("width") && blueprint->int_parameters["width"] > 0) { + missing_width = false; + } + if (blueprint->int_parameters.count("height") && blueprint->int_parameters["height"] > 0) { + missing_height = false; + } for (const auto &key_and_tuple : blueprint->int_parameters) { int_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second; } @@ -483,6 +513,14 @@ Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &inp vec4_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second; } } + + if (missing_width || missing_height) { + fprintf(stderr, "WARNING: Unset or nonpositive width/height for effect declared at %s " + "when getting scene for signal %u; setting to 1x1 to avoid crash.\n", + block->declaration_point.c_str(), num); + int_to_set[make_pair(effect, "width")] = 1; + int_to_set[make_pair(effect, "height")] = 1; + } } lua_pop(L, 1); diff --git a/nageru/scene.h b/nageru/scene.h index 32b3203..7715547 100644 --- a/nageru/scene.h +++ b/nageru/scene.h @@ -115,6 +115,8 @@ struct Block { std::map float_parameters; std::map> vec3_parameters; std::map> vec4_parameters; + + std::string declaration_point; // For error messages. }; int Block_display(lua_State* L); -- 2.39.2