绘制从类返回的文本会导致程序停止而不会出现错误

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

本文关键字:错误 程序 返回 文本 绘制      更新时间:2023-10-16

每当我试图绘制从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。
你需要在你的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;
}

另外,(在我看来)我建议用"init"函数向你的主类发送一个文本指针,这将更容易处理字体加载失败并提高你的FPS,这也将把你的计时器从渲染中分离出来。