【发布时间】: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