【问题标题】:Method in "Release" doesnt work correctly“Release”中的方法无法正常工作
【发布时间】:2021-08-18 18:00:15
【问题描述】:

我是 C++ 新手,所以我遇到了问题。在“调试”模式下,我得到了这张照片,这是正确的:所有的墙壁都是透明的

但在“发布”模式下,我得到了这个:

我确定我搞砸了我的墙类并创建了对象:

std::vector<Wall> glassWalls = {
    Wall(Vec2(2.0f, 1.0f), Vec2(2.0f, 2.0f),true,255, 0, 0,  0.5f),
    Wall(Vec2(2.0f, 2.0f), Vec2(3.0f, 1.0f),true, 255, 0, 0, 0.5f),
    Wall(Vec2(3.0f, 1.0f), Vec2(2.0f, 1.0f), true, 255, 0, 0, 0.5f),
    Wall(Vec2(1.0f, 3.0f), Vec2(3.0f, 7.0f), true, 0, 255, 0, 0.5f),
    Wall(Vec2(3.0f, 7.0f), Vec2(3.0f, 4.0f), true, 0, 255, 0, 0.5f),
    Wall(Vec2(3.0f, 4.0f), Vec2(1.0f, 3.0f), true, 0, 255, 0, 0.5f),
    Wall(Vec2(5.0f, 7.0f), Vec2(6.0f, 7.0f), true, 255, 255, 0, 0.5f),
    Wall(Vec2(6.0f, 7.0f), Vec2(6.0f, 6.0f), true, 255, 255, 0, 0.5f),
    Wall(Vec2(6.0f, 6.0f), Vec2(5.0f, 7.0f), true, 255, 255, 0, 0.5f),
    Wall(Vec2(5.0f, 1.0f), Vec2(5.0f, 4.0f), true, 255, 0, 255, 0.5f),
    Wall(Vec2(5.0f, 4.0f), Vec2(7.0f, 5.0f), true, 255, 0, 255, 0.5f),
    Wall(Vec2(7.0f, 5.0f), Vec2(5.0f, 1.0f), true, 255, 0, 255, 0.5f)
};

这是我的方法,应该通过 dist 返回颜色

int red = std::min((int)(std::max((-colorR / WALL_VISIBILITY * dist + colorR + emmision * 255.0f), 0.0f)), colorR);
    int green = std::min((int)(std::max((-colorG / WALL_VISIBILITY * dist + colorG + emmision * 255.0f), 0.0f)), colorG);
    int blue = std::min((int)(std::max((-colorB / WALL_VISIBILITY * dist + colorB + emmision * 255.0f), 0.0f)), colorB);
    if (isGlass) {
        sf::Color newCol(red, green, blue, std::max(std::min((int)(transparency * 255), 255), 0));
        return newCol;
    }
    else {
        float k = std::max(std::min(transparency, 1.0f), 0.0f);
        return sf::Color((int)(red * k), (int)(green * k), (int)(blue * k));
    }

【问题讨论】:

  • 你有很多除法和乘法。您确定不会出现任何类型的下溢/溢出情况吗?尤其是转换为int?
  • 顺便说一句,在 C++ 中,您不应该真正使用 C 风格的转换进行转换。使用 static_cast 等 C++ 原语(除非隐式转换规则已经做了正确的事情)。使用 C 风格转换几乎总是表明您做错了会导致未定义行为(如果代码在进行优化发布构建时表现不同,可能会发生这种情况)。
  • 为什么不使用调试器来查看您正在使用的值?或者干脆打印出中间值,看看它们在调试和发布之间是否相同?
  • 造成这种差异的最常见原因可能是未初始化变量。

标签: c++ visual-studio debugging sfml release


【解决方案1】:

molbdnilo 是对的,我搞砸了我的构造函数。类有字段“isGlass”,构造函数也有“isGlass”。

【讨论】:

  • 很高兴您得到了解决方案,感谢您的分享,如果您将它们标记为答案,我将不胜感激,这将对其他社区有益。
猜你喜欢
  • 2018-09-27
  • 2021-03-07
  • 2019-06-02
  • 2021-12-29
  • 2014-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多