Hi, I'm new to both SFML and C++ development (from C#/Java background) and I want to know if I'm doing something wrong that is negatively affecting my frame time.
I have a method that draws the current FPS in a new sf::Text, and I call this method ~100 times per frame. This brings the app to 100 FPS. However, when I was drawing the sf::Text object 1 time per frame, I was seeing 500 FPS.
This FPS drop is way more dramatic than I expected. If this were a 2D tile-based game that has 10x10 tiles in your line-of-sight, that could be 100 sprites drawn per second and when you consider the Tile layer, Item layer, Player layer, Effects layer... I don't see how this would handle it. That is unless drawing Sprites is somehow much cheaper/faster.
I'm using Windows XP, Visual C++ 2010 Express.
Dell Inspiron 6000 laptop (7 years old?), Intel processor 1.73GHz, integrated graphics.
Are CPU and GPU specs my problem? :-X
#include <cstdlib>
#include "stdafx.h"
#include "SFML\Window.hpp"
#include "SFML\Graphics.hpp"
#include "SFML\System.hpp"
#include "SFML\System\Time.hpp"
using namespace std;
void drawString(std::string s, float x, float y, sf::RenderWindow* window)
{
sf::String sfString(s);
sf::Text sfText;
sfText.setString(sfString);
sfText.setPosition(x,y);
(*window).draw(sfText);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
sf::RenderWindow *pwindow = &window;
window.setVerticalSyncEnabled(true); // call it once, after creating the window
sf::Clock fps_clock;
while(window.isOpen())
{
window.clear(sf::Color::Black);
// show fps
char *fps_str = new char[30];
char *ct_str = new char[30];
float frame_time = fps_clock.restart().asSeconds();
float fps = 1.f / frame_time;
sprintf(fps_str, "FPS: %.1f", fps);
// draw text a few times...
for(int i=0; i<19; i++)
for(int j=0; j<5; j++)
drawString(fps_str,j*160,i*30, pwindow);
delete fps_str;
delete ct_str;
window.display();
sf::sleep(sf::milliseconds(0));
}
delete pwindow;
return 0;
}
In debug mode, I get around 115 fps with that code. Compiled with optimizations I get around 1400 fps.
If I stop allocating and deallocating boatloads of dynamic memory and also stop constructing and copying a std::string, sf::String and sf::Text 100 times per frame, I get around 3500 fps.
Btw, deleting stuff you didn't allocate with new is bad mojo.
#include <sstream>
#include "SFML\Graphics.hpp"
const std::string fontName = "sketchflow.ttf" ;
sf::Font font ;
void drawText(sf::Text& text, float x, float y, sf::RenderWindow& window)
{
text.setPosition(x,y) ;
window.draw(text) ;
}
template <typename T>
std::string to_string(const T& value)
{
std::ostringstream os ;
os << value ;
return os.str() ;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
sf::Clock fps_clock;
font.loadFromFile(fontName) ;
sf::Text fpsDrawable ;
fpsDrawable.setFont(font) ;
std::string fps ;
while(window.isOpen())
{
sf::Event event ;
while ( window.pollEvent(event) )
if ( event.type == sf::Event::Closed )
window.close() ;
fps = to_string(1.0f / (fps_clock.restart().asSeconds())) ;
fpsDrawable.setString(fps) ;
window.clear(sf::Color::Black);
for(int i=0; i<19; i++)
for(int j=0; j<5; j++)
drawText(fpsDrawable,j*160,i*30, window);
window.display();
}
return 0;
}
Since the default font was removed from SFML, I added code to load a font. You may have to modify that to run this code (or just remove it altogether if you're using an older version that still has the default font and it actually works for you!)