gpt4 book ai didi

连接 char 和 float 以在 opengl/glut 中打印出来

转载 作者:行者123 更新时间:2023-11-30 19:21:13 26 4
gpt4 key购买 nike

我正在尝试在窗口中打印我的程序的 fps。

所以我尝试输出这样的内容:Fps: 16.72

我创建了一个方法来处理显示文本:

void displayText( float x, float y, char *string ) {
char * ch;

glColor3f( 0.0f, 0.0f, 0.0f );
glRasterPos3f( x, y, 0.0 );

for( ch = string; *ch; ch++ ) {
glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_24, (int)*ch );
}
}

调用方式为:

displayText( 900, 900, str);

我正在尝试将 float fps 转换为字符以传递给此方法。我尝试过的各种方法:

char * str = "Fps: "; // and the fps is a float instantiated by another method

char * newFPS = (char)fps; // I tried to cast
strcat(str,newFPS); // and then concatenate them
snprintf(str, sizeof(str), "%f", fps); // googled example
sprintf( str, "%f", (char)fps); // another one

我不太了解 C,但它的功能足以知道我哪里出错了。

最佳答案

试试这个例子:

char buf[100] = {0};
snprintf(buf, 100, "FPS: %.1f", fps);
displayText( 900, 900, buf);

关于连接 char 和 float 以在 opengl/glut 中打印出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20412022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com