每当我尝试绘制从FPS_Counter
类返回的文本对象时,它就会崩溃。
我已经交叉引用了SFML文档,据我所知,我没有错过任何内容,而且Visual也没有给我任何关于错误代码的唠叨。
#include <SFML/Graphics.hpp>
#include <Windows.h>
#include <string>
#include <iostream>
using namespace std;
//takes frame render time and counts how many of those fits in a second
//then assigns that to a text object and returns it.
class FPS_Counter
{
private:
sf::Text text;
unsigned int count = 0;
public:
FPS_Counter() //setting up my fps counting object here
{
sf::Font font;
if (!font.loadFromFile("pixel font 1.ttf")) { throw "Cannot find font 'pixel font 1.ttf'."; }
text.setFont(font);
text.setCharacterSize(24);
text.setColor(sf::Color(255, 255, 0, 255));
}
sf::Text Count(sf::Time difference)
{
cout << "count called" << endl;
if (difference.asSeconds() != 0) //dodging a divide by zero
{
count = 1 / (float)difference.asSeconds();
text.setString("FPS: " + count);
cout << "count returned number" << endl;
return text;
}
text.setString("FPS: 0");
cout << "count returned default" << endl;
return text;
}
};
int main()
{
int mon_res_x = GetSystemMetrics(SM_CXSCREEN);
int mon_res_y = GetSystemMetrics(SM_CYSCREEN);
sf::RenderWindow window(sf::VideoMode(mon_res_x, mon_res_y), "SFML works!");
window.setPosition(sf::Vector2i(0,0));
FPS_Counter fps;
sf::Clock clock;
sf::Time time;
while (window.isOpen())
{
window.clear();
time = clock.getElapsedTime(); //get time since last frame
clock.restart();
window.draw(fps.Count(time)); //draw fps count with given frame-time
window.display();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
}
return 0;
}
答案 0 :(得分:0)
由于每帧可能只需几毫秒来渲染,因此计算秒数将始终为0 您需要在fps计数器类中创建一个计时器,并在每次此计数器达到0时打印FPS文本,每帧更改您的FPS是无用的,因为它只会每秒重新计算:
#include <SFML/Graphics.hpp>
#include <Windows.h>
#include <string>
#include <iostream>
using namespace std;
//takes frame render time and counts how many of those fits in a second
//then assigns that to a text object and returns it.
class FPS_Counter
{
private:
sf::Text text;
unsigned int count = 0;
sf::Int32 countdown = 1000;
public:
FPS_Counter() //setting up my fps counting object here
{
sf::Font font;
if (!font.loadFromFile("pixel font 1.ttf")) { throw "Cannot find font 'pixel font 1.ttf'."; }
text.setFont(font);
text.setCharacterSize(24);
text.setColor(sf::Color(255, 255, 0, 255));
}
sf::Text Count(sf::Int32 difference)
{
countdown -= difference; //Counts the time elapsed
count++; //Increment FPS
cout << "count called" << endl;
if (countdown < 0)
{
countdown += 1000;
text.setString("FPS: " + count);
cout << "count returned number" << endl;
}
return text;
}
};
int main()
{
int mon_res_x = GetSystemMetrics(SM_CXSCREEN);
int mon_res_y = GetSystemMetrics(SM_CYSCREEN);
sf::RenderWindow window(sf::VideoMode(mon_res_x, mon_res_y), "SFML works!");
window.setPosition(sf::Vector2i(0, 0));
FPS_Counter fps;
sf::Clock clock;
sf::Time time;
while (window.isOpen())
{
window.clear();
time = clock.restart(); //get time since last frame
window.draw(fps.Count(time.asMilliseconds())); //draw fps count with given frame-time
window.display();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
}
return 0;
}
另外,(IMO)我建议使用“init”函数将文本指针发送到主类一次,更容易处理字体加载失败并增加你的FPS,这也会分开你的渲染计时器。