【发布时间】:2017-01-30 07:27:59
【问题描述】:
简介:代码在发布模式下不起作用。在调试中工作。
这显示在我的一个断点上:
代码点是:
void Font::operator<<(std::string s)
{
this->printf(s); // this line
}
这个方法是这样调用的:
float oneframe;
oneframe = (double)elapsed/ 1000000.0;
float ffps =1. / oneframe;
int fps = ffps;
char txt[200];
sprintf(txt, "%d FPS", fps);
font << txt; // displays text
Printf 是:
void Font::printf(std::string s)
{
UINT lines = 0;
std::vector<float> offsetX;
float offsetY=0;
UINT length = s.size();
XMMATRIX M = XMMatrixScaling(m_scaling.x, m_scaling.y, m_scaling.z)*
XMMatrixTranslation(m_translation.x, m_translation.y, m_translation.z);
float fontLength=0;
float fontHeight = 60.0f / windowHeight;
float fontWidth = 60.0f / windowWidth * 0.6f;
m_deviceContext->VSSetShader(m_vertexShader, 0, 0);
m_deviceContext->IASetInputLayout(m_inputLayout);
m_deviceContext->PSSetShader(m_pixelShader, 0, 0);
m_deviceContext->PSSetShaderResources(0, 1, &m_texture);
m_deviceContext->PSSetSamplers(0, 1, &m_sampler);
m_deviceContext->OMSetDepthStencilState(m_dsOff, 1);
if (m_anchor != TOP_LEFT)
{
float offset = 0;
for (int i = 0; i < length; i++)
{
offset += m_kerning*widthMap[s[i]];
if (s[i] == '\n' || s[i] == '\r' || i == length - 1)
{
offsetX.push_back(offset);
offset = 0;
}
}
}
for (int i = 0; i < length; i++)
{
XMFLOAT3 TL(-1, 1, 0), BR(1, -1, 0);
XMVECTOR vTL, vBR;
if (s[i] == '\n' || s[i] == '\r')
{
fontLength = 0;
lines++;
continue;
}
switch (m_anchor)
{
default:
case TOP_LEFT:
vTL = XMVector3TransformCoord(XMLoadFloat3(&XMFLOAT3(fontLength, -m_leading*lines, 0)), M);
vBR = XMVector3TransformCoord(XMLoadFloat3(&XMFLOAT3(fontWidth + fontLength, -m_leading*lines - fontHeight, 0)), M);
break;
case TOP_RIGHT:
vTL = XMVector3TransformCoord(XMLoadFloat3(&XMFLOAT3(fontLength - offsetX[lines], -m_leading*lines, 0)), M);
vBR = XMVector3TransformCoord(XMLoadFloat3(&XMFLOAT3(fontWidth + fontLength - offsetX[lines], -m_leading*lines - fontHeight, 0)), M);
break;
case BOTTOM_LEFT:
offsetY = m_leading*offsetX.size();
vTL = XMVector3TransformCoord(XMLoadFloat3(&XMFLOAT3(fontLength, -m_leading*lines+ offsetY, 0)), M);
vBR = XMVector3TransformCoord(XMLoadFloat3(&XMFLOAT3(fontWidth + fontLength, -m_leading*lines - fontHeight+ offsetY, 0)), M);
break;
case BOTTOM_RIGHT:
offsetY = m_leading*offsetX.size();
vTL = XMVector3TransformCoord(XMLoadFloat3(&XMFLOAT3(fontLength - offsetX[lines], -m_leading*lines + offsetY, 0)), M);
vBR = XMVector3TransformCoord(XMLoadFloat3(&XMFLOAT3(fontWidth + fontLength - offsetX[lines], -m_leading*lines - fontHeight + offsetY, 0)), M);
break;
case CENTER:
{
offsetY = m_leading*offsetX.size() / 2;
float halfOffsetx = offsetX[lines] / 2;
vTL = XMVector3TransformCoord(XMLoadFloat3(&XMFLOAT3(fontLength - halfOffsetx, -m_leading*lines + offsetY, 0)), M);
vBR = XMVector3TransformCoord(XMLoadFloat3(&XMFLOAT3(fontWidth + fontLength - halfOffsetx, -m_leading*lines - fontHeight + offsetY, 0)), M);
break;
}
}
XMStoreFloat3(&TL, vTL);
XMStoreFloat3(&BR, vBR);
assert(updateBuffer(TL, BR, fontMap[s[i]]));
UINT stride, offset;
stride = sizeof(SimpleVertex);
offset = 0;
m_deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
m_deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_deviceContext->Draw(6, 0);
fontLength += m_kerning*widthMap[s[i]];
}
m_deviceContext->OMSetDepthStencilState(m_dsOn, 1);
}
这就是奇怪的地方。我在发布模式下收到此通知。上面的代码在调试中完全有效。无论出于何种原因,当我切换到 Release 时它都不起作用。我检查了发送到函数的txtvariable,它有文本。对我来说,似乎代码已经以某种方式进行了优化。
我还删除了 Debug 和 Release 文件夹中的所有内容并进行了新的构建。没有。我已经在没有从 VS 运行的情况下测试了可执行文件,结果相同。 Release 中的调试显示 txt 中有文本,但我无法确定会发生什么,因为调试器似乎正好漂浮在 font << txt 上方。
【问题讨论】:
-
很可能,在发布版本中
Font::operator<<被内联了,这就是你不能在其上设置断点的原因。您可能可以在Font::printf的第一行设置断点。 -
顺便说一句:你真的不应该通过值传递
std::string或std::wstring,因为它会创建很多临时对象。请改用const std::string& s。 -
@IgorTandetnik 是的,我可以查看 printf。
-
@ChuckWalbourn 我会试试看有没有什么改变。
-
编写我正在使用的课程的人告诉我它是便便,并且有太多问题。将关闭此问题。
标签: debugging visual-c++ directx-11