绘制从 class 返回的文本会导致程序无错停止

Drawing a text returned from a class causes program stop without errors

每当我尝试绘制从 FPS_Counter class.

返回的文本对象时,它就会崩溃

我已经交叉引用了 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。
您需要在您的 fps 计数器中设置一个计时器 class,并在每次该计数器达到 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" 函数将文本指针发送到主 class 一次,这样会更容易处理字体加载失败,并且增加您的 FPS,这也会将您的计时器与渲染分开。