C sprintf做什么?(was:OpenGL中的FPS计算)
对于FPS计算,我使用了一些在web上找到的代码,它运行良好。然而,我真的不明白。以下是我使用的函数:C sprintf做什么?(was:OpenGL中的FPS计算),c,std,printf,C,Std,Printf,对于FPS计算,我使用了一些在web上找到的代码,它运行良好。然而,我真的不明白。以下是我使用的函数: void computeFPS() { numberOfFramesSinceLastComputation++; currentTime = glutGet(GLUT_ELAPSED_TIME); if(currentTime - timeSinceLastFPSComputation > 1000) { char fps[256]; sprintf(
void computeFPS()
{
numberOfFramesSinceLastComputation++;
currentTime = glutGet(GLUT_ELAPSED_TIME);
if(currentTime - timeSinceLastFPSComputation > 1000)
{
char fps[256];
sprintf(fps, "FPS: %.2f", numberOfFramesSinceLastFPSComputation * 1000.0 / (currentTime . timeSinceLastFPSComputation));
glutSetWindowTitle(fps);
timeSinceLastFPSComputation = currentTime;
numberOfFramesSinceLastComputation = 0;
}
}
我的问题是,sprint调用中计算的值如何存储在fps数组中,因为我没有真正分配它。这不是关于OpenGL的问题,而是关于C标准库的问题。阅读s(n)printf的参考文档有助于: 男s(n)printf: 简而言之,snprintf获取指向用户提供的缓冲区和格式字符串的指针,并根据格式字符串和附加参数中给定的值填充缓冲区
我的建议是:如果你不得不问类似的问题,现在还不要解决OpenGL。在提供缓冲区对象数据和着色器源时,您需要熟练使用指针和缓冲区。如果你打算用C来做这件事,那就先买一本关于C的书,并彻底学习它。与C++不同,你可以在几个月内学习C到一些好的程度。 < P>这不是OpenGL的问题,而是C标准库。阅读s(n)printf的参考文档有助于: 男s(n)printf: 简而言之,snprintf获取指向用户提供的缓冲区和格式字符串的指针,并根据格式字符串和附加参数中给定的值填充缓冲区
我的建议是:如果你不得不问类似的问题,现在还不要解决OpenGL。在提供缓冲区对象数据和着色器源时,您需要熟练使用指针和缓冲区。如果你打算用C来做这件事,那就先买一本关于C的书,并彻底学习它。与C++不同,你可以在几个月内学习C到一些好的程度。 < P>这个函数被假定在你的主循环的每一个重绘(对于每一帧)。因此,它所做的是增加一个帧计数器,并获取当前显示该帧的时间。每秒一次(1000ms),它检查计数器并将其重置为0。因此,当每秒获取计数器值时,它将获取其值并将其显示为窗口的标题
/**
* This function has to be called at every frame redraw.
* It will update the window title once per second (or more) with the fps value.
*/
void computeFPS()
{
//increase the number of frames
numberOfFramesSinceLastComputation++;
//get the current time in order to check if it has been one second
currentTime = glutGet(GLUT_ELAPSED_TIME);
//the code in this if will be executed just once per second (1000ms)
if(currentTime - timeSinceLastFPSComputation > 1000)
{
//create a char string with the integer value of numberOfFramesSinceLastComputation and assign it to fps
char fps[256];
sprintf(fps, "FPS: %.2f", numberOfFramesSinceLastFPSComputation * 1000.0 / (currentTime . timeSinceLastFPSComputation));
//use fps to set the window title
glutSetWindowTitle(fps);
//saves the current time in order to know when the next second will occur
timeSinceLastFPSComputation = currentTime;
//resets the number of frames per second.
numberOfFramesSinceLastComputation = 0;
}
}
这个函数应该在主循环的每次重画(每帧)时调用。因此,它所做的是增加一个帧计数器,并获取当前显示该帧的时间。每秒一次(1000ms),它检查计数器并将其重置为0。因此,当每秒获取计数器值时,它将获取其值并将其显示为窗口的标题
/**
* This function has to be called at every frame redraw.
* It will update the window title once per second (or more) with the fps value.
*/
void computeFPS()
{
//increase the number of frames
numberOfFramesSinceLastComputation++;
//get the current time in order to check if it has been one second
currentTime = glutGet(GLUT_ELAPSED_TIME);
//the code in this if will be executed just once per second (1000ms)
if(currentTime - timeSinceLastFPSComputation > 1000)
{
//create a char string with the integer value of numberOfFramesSinceLastComputation and assign it to fps
char fps[256];
sprintf(fps, "FPS: %.2f", numberOfFramesSinceLastFPSComputation * 1000.0 / (currentTime . timeSinceLastFPSComputation));
//use fps to set the window title
glutSetWindowTitle(fps);
//saves the current time in order to know when the next second will occur
timeSinceLastFPSComputation = currentTime;
//resets the number of frames per second.
numberOfFramesSinceLastComputation = 0;
}
}