在向量数组中重复的字符串
编辑:更改了标题以更好地反映当前问题。
现在我知道问题的根源在于函数的文本分割部分。我现在记得我做了什么,我更改了分割文本,因为我的教程返回了错误。
for(const char *c=text;*c;c++)
{
if(*c=='\n') {
string line;
for(const char *n=start_line;n<c;n++) line.append(1,*n);
lines.push_back(line);
start_line=c+1;
}
}
if(start_line)
{
string line;
for(const char *n=start_line; n < c;n++) line.append(1,*n);
lines.push_back(line);
}
'c' 未声明地返回,并且没有提及任何其他 c,所以我猜它指的是上面 for 循环中的指针。虽然将“if (start_line)”带入第一个代码块,但不断返回文本中的每个字符,而不是整个内容。
所以我将代码更改为以下内容:
for(const char *c=text;*c;c++)
{
if(*c=='\n')
{
string line;
for(const char *n=start_line;n<c;n++) line.append(1,*n);
lines.push_back(line);
start_line=c+1;
if(start_line)
{
string line;
for(const char *n=start_line;n<c;n++) line.append(1,*n);
lines.push_back(line);
}
}
else if (*c == *start_line)
{
lines.push_back(text);
}
}
我很确定“else if (*c == *start_line)”比较是导致我出现问题的原因。但不确定用什么来代替它。我想虽然因为我没有使用任何换行符或者不打算这样做,所以我可以选择:
for(const char *c=text;*c;c++)
{
lines.push_back(text);
break;
}
但知道我是否出错了仍然会很高兴。 *注意:上面的代码现在可以正常工作,没有问题,并且效果加倍。所以我确信这是我的文本分割代码。
Edit: Changed the title to better reflect the current issue.
Right i know now were the source of the issue lies, it's with the text splitting part of the function. I remember now what i did, i changed the splitting text because the tutorial for me was returning an error.
for(const char *c=text;*c;c++)
{
if(*c=='\n') {
string line;
for(const char *n=start_line;n<c;n++) line.append(1,*n);
lines.push_back(line);
start_line=c+1;
}
}
if(start_line)
{
string line;
for(const char *n=start_line; n < c;n++) line.append(1,*n);
lines.push_back(line);
}
The 'c' was returning undeclared, and there's no mention for any other c, so i guess it's referring to the pointer in the for loop above. Though bring the "if (start_line)" into the first code block, kept returning me each character in the text, instead of just the whole thing.
So i changed the code to the following:
for(const char *c=text;*c;c++)
{
if(*c=='\n')
{
string line;
for(const char *n=start_line;n<c;n++) line.append(1,*n);
lines.push_back(line);
start_line=c+1;
if(start_line)
{
string line;
for(const char *n=start_line;n<c;n++) line.append(1,*n);
lines.push_back(line);
}
}
else if (*c == *start_line)
{
lines.push_back(text);
}
}
I pretty sure that the "else if (*c == *start_line)" comparsion is what's causing me the issue. Unsure though what to replace it with. I guess though because i'm not using any newlines or don't plan to i can just go with:
for(const char *c=text;*c;c++)
{
lines.push_back(text);
break;
}
But it would still be nice to know were i was going wrong. *Note: That the above code works fine now, no issue with that and the doubling effect. So i'm sure that it was my text splitting code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一个想法:在文本渲染方法中,添加一个静态计数器并使用它来设置每个渲染字符串的颜色。由于每帧似乎没有那么多字符串,因此您可以使用一种颜色分量(例如红色)的 8 位作为计数器,并将其他 2 个分量设置为 255。如果您有超过 255 个字符串,您可以仍然通过 2 或 3 个颜色分量对计数器值进行编码。
有了这个小调试帮助,您将能够看到每段文本的呈现顺序。您可以使用 pixie 和/或 zoomin 以查看“实时”像素值。否则,只需截取屏幕截图并检查结果。
Here's an idea for you: in your text rendering method, add a static counter and use it to set the color of each string rendered. Since you don't seem to have that many strings per frame, you can use the 8 bits of one color component (e.g. red) for the counter and set the 2 other components to 255. If you had more than 255 strings, you could still encode the counter value over 2 or 3 color components.
With this little debug aid, you will be able to see in which order each piece of text is rendered. You can use pixie and/or zoomin to see the pixel values "live". Otherwise, just take a screenshot and examine the result.
看起来该捕获中错误绘制的文本是“50b”,我怀疑它是通常出现在游戏中的字符串。看起来您正在绘制的东西通常是空字符串,但有时会拾取垃圾值 - 换句话说,未定义的行为。
当然,我不能确定,因为我根本没有足够的信息来找到您的问题。你的 glClear 对我来说看起来很好,所以你可以放心,额外的文本是在与你想要的文本相同的框架中绘制的。
It looks like the erroneously drawn text in that capture is "50b" which I doubt is a string that would normally appear in your game. It looks like you're drawing something that's normally an empty string, but sometimes picks up junk values - in other words, undefined behavior.
I can't be sure, of course, because I simply don't have enough information to find your problem. Your glClear looks fine to me, so you can be assured that the extra text is being drawn in the same frame as your intended text.