1
General / Re: Confused About Performance
« on: February 05, 2014, 06:13:54 pm »
Vsync is disabled. The CPU is an A6-4400M (2.7Ghz). The GPU is a dual-graphics setup, with an HD 7520G + HD 7470 M. But, regardless, my issue isn't that SFML is performing poorly (200 FPS is fine and dandy); it's that it is performing extremely poorly relative to another program that should, in theory, be slower, despite the fact that it is actually doing even less. I will post the relevant sections of code below. Some things are commented out because they are disabled for testing purposes.
Initialization
Basic game loop
Draw function
FPS counter
Nothing else effects the code in a meaningful way. a Game object is created and .play() is called.
Initialization
#include "Game.h"
#include <iostream>
#include "GameObject.h"
#include "Player.h"
#include <sstream>
Game::Game() :
state(State::PLAYING),
window(sf::VideoMode(640, 480), "SFML window"),
renderTexture(),
renderSprite(),
tickRate(30),
fpsLimit(0),
fpsUpdateTicker(0),
fpsUpdateRate(0.5),
fpsDrawLoopCounter(0),
vSyncOn(false),
gameUpdateTicker(0),
secondsPassed(0),
isRunning(true),
gameClock(),
world(),
lastReportedFPS(0) {
window.setFramerateLimit(fpsLimit);
window.setVerticalSyncEnabled(vSyncOn);
systemFont.loadFromFile("fonts/Arcadepix Plus.ttf");
}
#include <iostream>
#include "GameObject.h"
#include "Player.h"
#include <sstream>
Game::Game() :
state(State::PLAYING),
window(sf::VideoMode(640, 480), "SFML window"),
renderTexture(),
renderSprite(),
tickRate(30),
fpsLimit(0),
fpsUpdateTicker(0),
fpsUpdateRate(0.5),
fpsDrawLoopCounter(0),
vSyncOn(false),
gameUpdateTicker(0),
secondsPassed(0),
isRunning(true),
gameClock(),
world(),
lastReportedFPS(0) {
window.setFramerateLimit(fpsLimit);
window.setVerticalSyncEnabled(vSyncOn);
systemFont.loadFromFile("fonts/Arcadepix Plus.ttf");
}
Basic game loop
void Game::play() {
state = State::PLAYING;
while(isRunning) {
secondsPassed = gameClock.getElapsedTime().asSeconds();
gameClock.restart();
//input();
if (state == State::PLAYING) {
gameUpdateTicker += secondsPassed;
while (gameUpdateTicker >= 1.0 / tickRate) {
gameUpdateTicker -= 1.0 / tickRate;
//update();
}
} else if (state == State::PAUSED) {
//pause the game
}
draw();
fpsUpdate();
}
quit();
}
state = State::PLAYING;
while(isRunning) {
secondsPassed = gameClock.getElapsedTime().asSeconds();
gameClock.restart();
//input();
if (state == State::PLAYING) {
gameUpdateTicker += secondsPassed;
while (gameUpdateTicker >= 1.0 / tickRate) {
gameUpdateTicker -= 1.0 / tickRate;
//update();
}
} else if (state == State::PAUSED) {
//pause the game
}
draw();
fpsUpdate();
}
quit();
}
Draw function
void Game::draw() {
fpsDrawLoopCounter++;
window.clear();
std::vector<GameObject *>::const_iterator it = world.get_objectList().begin();
while (it != world.get_objectList().end()) {
window.draw((**it).get_sprite());
++it;
}
std::stringstream ss;
ss << "FPS: " << lastReportedFPS << std::endl;
if (state == State::PAUSED) {
ss << "PAUSED" << std::endl;
}
sf::Text fpsText(ss.str(), systemFont, 16);
fpsText.setPosition(10, 10);
window.draw(fpsText);
window.display();
}
fpsDrawLoopCounter++;
window.clear();
std::vector<GameObject *>::const_iterator it = world.get_objectList().begin();
while (it != world.get_objectList().end()) {
window.draw((**it).get_sprite());
++it;
}
std::stringstream ss;
ss << "FPS: " << lastReportedFPS << std::endl;
if (state == State::PAUSED) {
ss << "PAUSED" << std::endl;
}
sf::Text fpsText(ss.str(), systemFont, 16);
fpsText.setPosition(10, 10);
window.draw(fpsText);
window.display();
}
FPS counter
void Game::fpsUpdate() {
fpsUpdateTicker += secondsPassed;
if (fpsUpdateTicker > fpsUpdateRate) {
fpsUpdateTicker -= fpsUpdateRate;
lastReportedFPS = fpsDrawLoopCounter / fpsUpdateRate;
fpsDrawLoopCounter = 0;
}
}
fpsUpdateTicker += secondsPassed;
if (fpsUpdateTicker > fpsUpdateRate) {
fpsUpdateTicker -= fpsUpdateRate;
lastReportedFPS = fpsDrawLoopCounter / fpsUpdateRate;
fpsDrawLoopCounter = 0;
}
}
Nothing else effects the code in a meaningful way. a Game object is created and .play() is called.