绘制从类返回的文本会导致程序停止而不会出现错误
Drawing a text returned from a class causes program stop without errors
每当我试图绘制从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,这也将把你的计时器从渲染中分离出来。
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- xmake总是报告:错误:无法获取cxx的程序,为什么
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 分段错误当我试图运行程序时出错
- 显示错误输出的简单数组排序程序
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 在气泡排序程序中未声明错误功能
- 我在c ++矩阵计算器程序中找不到错误
- 叮当格式化程序多行格式配置错误
- 全局向量导致 C++ 程序结束时出现段错误
- 运行时间错误:程序跳过提示,以获取第二名和第三个名称
- Glibc 2.7 配置错误程序太旧
- 运行时错误:程序已停止响应
- OpenCV for Android:在 PATH 中找不到错误程序"sh"
- C++中的 scanf 错误 - 程序没有响应
- c++语法错误:程序以非零状态退出
- 运行时错误C++程序:检测到glibc,munmap_chunk(),指针无效
- libxml2错误程序c++