The following lines of code do not produce the correct values in release builds in MSVC 2005. Note that m_rect is a RECT and its members have been set to zero prior to this. Vector3 is a struct with 3 floats x,y and z. These lines do work in debug builds.
Code:
Vector3 pos = m_pFrame->GetPosition();
Vector3 scale = m_pFrame->GetScale();
m_rect.left = static_cast<LONG>(pos.x - (scale.x * 0.5f));
m_rect.right = m_rect.left + static_cast<LONG>(scale.x);
m_rect.top = static_cast<LONG>(pos.y - (scale.y * 0.5f));
m_rect.bottom = m_rect.top + static_cast<LONG>(scale.y);
These lines work in release builds and debug builds.
Code:
Vector3 pos = m_pFrame->GetPosition();
Vector3 scale = m_pFrame->GetScale();
float scaleX2 = scale.x * 0.5f;
float scaleY2 = scale.y * 0.5f;
float left = pos.x - scaleX2;
float top = pos.y - scaleY2;
float right = left + scale.x;
float bottom = top + scale.y;
m_rect.left = static_cast<LONG>(left);
m_rect.right = static_cast<LONG>(right);
m_rect.top = static_cast<LONG>(top);
m_rect.bottom = static_cast<LONG>(bottom);
Why does the first example only work in debug builds and not release builds? Are they not syntactically the same lines of code? Is my FPU possibly in a bad state prior to executing these lines and somehow debug is resetting the state so they work or is it something else?
Here is an example output in release using the first block of code:
Cell rectangle: PosX: 75.000000 - PosY: 487.500000 - ScaleX: 100.000000 - ScaleY: 15.000000, Top: -1012 - Left: 74 - Right: 124 - Bottom: -997
Here is an example output in release using the second block of code:
Cell rectangle: PosX: 75.000000 - PosY: 487.500000 - ScaleX: 100.000000 - ScaleY: 15.000000, Top: 480 - Left: 25 - Right: 125 - Bottom: 495
Any ideas?