Hi everyone,
I wanted to add an FPS visual in my game so I converted a script I saw in the XNA 3.0 book to SFML. I'm bascially just wondering if there's any way to optimize the performance as it sometimes eats up more CPU than I would have thought it should. Here's the code:
Main.cpp
[source lang="cpp"]#include <SFML/Graphics.hpp>#include <iostream>#include "FPS.h"#define ScreenWidth 500#define ScreenHeight 300int main(){ sf::RenderWindow window(sf::VideoMode(ScreenWidth, ScreenHeight, 32), "SFML Template"); window.SetFramerateLimit(200); FPS fps; fps.Initialize(); while(window.IsOpened()) { sf::Event Event; while(window.GetEvent(Event)) { if(Event.Type == sf::Event::Closed || Event.Key.Code == sf::Key::Escape) window.Close(); } window.Clear(); // Draw Game Components fps.Draw(window, (ScreenWidth / 2) - 25); window.Display(); } return 0;}[/source]
FPS.h
[source lang="cpp"]#pragma once#include <SFML/Graphics.hpp>#include <iostream>#include <sstream>class FPS{private:float fps;float updateInterval;float timeSinceLastUpdate;float frameCount;sf::String string;std::stringstream ss;public:FPS(void);~FPS(void);void Initialize();void Update(sf::RenderWindow &window);void Draw(sf::RenderWindow &window, float cameraX);};[/source]
FPS.cpp
[source lang="cpp"]#include "FPS.h"FPS::FPS(void){}FPS::~FPS(void){}void FPS::Initialize(){float updateInterval = 1.0f;float timeSinceLastUpdate = 0.0f;float frameCount = 0.0f;string.SetSize(12);string.SetColor(sf::Color::White);}void FPS::Update(sf::RenderWindow &window){}void FPS::Draw(sf::RenderWindow &window, float cameraX){frameCount++;timeSinceLastUpdate = window.GetFrameTime();if(timeSinceLastUpdate > updateInterval){ fps = frameCount / timeSinceLastUpdate; ss << "FPS: " << fps; string.SetText(ss.str()); string.SetPosition(cameraX, 20); window.Draw(string); ss.str(""); frameCount = 0; timeSinceLastUpdate -= updateInterval;}}[/source]
0026sd
Member Since 20 Sep 2012Offline Last Active Oct 31 2012 12:11 PM

Find content
Not Telling