Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

0026sd

Member Since 20 Sep 2012
Offline Last Active Oct 31 2012 12:11 PM
-----

Topics I've Started

SFML Frame Per Second feedback

31 October 2012 - 07:38 AM

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]

Games with menus

20 September 2012 - 12:03 PM

Hi everyone,

Just a general question. Are game menus (Start menu, pause menu, etc) typically put into their own class where all the drawing and logic would take place or are the menus all programmed in the main game logic class?

A lot of the tutorials and books I'm reading all have the start menu in the main game class however these are very simple menus (An image that says press enter to start game). I want to make complex menus with character stat selection and pause menus with options. Surely menus with so much logic wouldn't all be crammed into the main game class...?

Thanks,
Scott

PARTNERS