   Yet another FPS (FrameRate) question
  Ok, well first of all, this is a pretty newbie question.So far I have made a Pong, Tetris and Space Invaders clone. I am now working on a bigger step up... Pacman clone, using tiles and AI (only used VERY basic AI in pong and space invaders) and trying to make it as finished as possible.Now in an attempt to be OOP and efficient I have being programming it in modules, some of which I can use in other basic 2d games, as a form of mini-engine.Oh and I'm using C++ and SDLI have a Tileset class, for loading a bitmap containing all the tiles, then splitting them into an array of single tiles, ready for drawing.I have a font class for loading a font and drawing strings with it.I have a basic sprite class for loading sprite animations and displaying the sprite.I have a log class that records key events and such for analysis and testing and stuff.I have a basic Framerate class that calculates average FPS, displays it, gives me time values for frame-rate-independant movement (time based) and other time/frame related purposes.So anyway I have the basic structure of my Pacman game set out, its only like 5-10% complete, but I wanted to get the FPS and stuff set up. Now it's been working pretty well (I get about 50-60fps, see later in the post)But for some reason, I came back to my project a week later (I started a new job, 70hrs a week or so, don't have so much time for coding at the moment) and now I'm showing 0fps nearly all the time, after 1 second of running it jumps to about 50fps but then falls back down to 0fps for the remainder.My code is really newbie and theres a load of stuff I know you guys will suggest that I can improve but here it is at the moment.First, my FrameRate.h file: #ifndef __FRAMERATE_H__#define __FRAMERATE_H__#include <windows.h>class CFrameRate{public:CFrameRate();~CFrameRate();doubleUpdate();doublegetFPS() { return m_fps; };doublegetHighFPS() { return m_highFPS; };doublegetAvFPS() { return m_avFPS; };private:doublem_dt;DWORDm_lastFrame;doublem_fps;doublem_highFPS;doublem_avFPS;doublem_avTotal;intm_avCounter;};#endifUpdate is my main function, I call it every iteration of the game loop. m_dt is the time difference between the last frame. If I remember from the article I read, I can get the fps by using 1/dt. m_fps stores the current fps. m_highFPS stores the highestFPS achieved since program start. m_avFPS stores the average FPS from the past 10-20frames (haven't decided a final value yet). m_avTotal and m_avCounter are using in the calculation of the averageFPS.So, my FrameRate.cpp file is as follows:   #include "FrameRate.h"CFrameRate::CFrameRate(){m_dt = 0;m_lastFrame = GetTickCount();m_fps = 0;m_highFPS = m_fps;m_avFPS = 0;m_avTotal = 0;m_avCounter = 0;}CFrameRate::~CFrameRate(){}double CFrameRate::Update(){m_dt = (GetTickCount() - m_lastFrame) * 0.001;   // Difference in milliseconds (i think) since last framem_lastFrame = GetTickCount();   // Set now to be the time for last frame, for next time update is calledm_fps = 1 / m_dt;   // get the fpsif(m_fps > m_highFPS) { m_highFPS = m_fps; }   // if the current fps is higher than the highest so far, store this as highest   // if 10 frames have passedif(m_avCounter == 10){m_avFPS = m_avTotal / 10;m_avCounter = 0;m_avTotal = 0;} else{m_avTotal += m_fps;m_avCounter++;}return m_dt;}Ok, so the update function returns the m_dt time difference. Now, as part of my CGame class, I have:   void CGame::playFrame(){m_dt = m_frameRate.Update();   // m_dt is also a member of CGame so all objects can access the time difference, for time-based movement instead of frame-based movementhandleInput();switch(getState()){case STATE_PLAYING:break;case STATE_PAUSED:break;case STATE_LOADING:break;case STATE_TITLE:showTitleScreen();break;case STATE_GAMEOVER:break;default:break;}displayFPS(5,5);}void CGame::displayFPS(int x, int y){m_fonts[0].drawString(screen,  x,  y,  "FPS: %d",  (int)m_frameRate.getFPS());}This is run once every frame, and is where I put all the calls that need to be made. Now in this state, it draws the string "FPS: ..." where ... is a number. But naturally, as the fps changes every frame, it flashes between numbers to quickly to even read. So I changed displayFPS function to use m_frameRate.getAvFPS() instead of m_frameRate.getFPS() so it should be using hte average fps (which changes every 10-20 frames).So yeah, now it just shows 0fps most of the time apart from the quick blip at the start. And also, in my log, I write the highest FPS just before the program closes, and that is 0. So the highest FPS I acheive is 0? Obviously something I'm doing wrong.Can anyone help? What should I change or improve? Can you see the problem? Do you have a framerate class or something I could see/read to try and get a better understanding of what I should be doing?Also, isn't 60fps a bit low, when all I'm doing is showing a blank screen with the fps on it. As far as I'm aware I've turned off VSync if that makes a difference. Then again, I am running on a GeForce 2 MX.Another concern I have is my games take up like 8mb of RAM or so even when they are just a blank screen, isn't this a bit extreme?EDIT: Also is it worth dumping SDL and switching to OpenGL? I found it so hard before  and couldn't find any useful/good tutorials on 2d/orthographic mode [edited by - sanguineraven on February 4, 2004 11:36:10 AM]
  No help? I''ve mainly been relying on GD articles and forum searches but need something more object/class based. ;  I do it like this: (I''ll post some code if you can''t figure it out)Calculate the fpsAdd the fps to average fpsIncrement frame count by oneCalculate averaged fps by dividing average fps with frame countIf certain amount is passed, refresh the counter by dividing average fps and framecount with twoI do the refreshing because otherwise the counter will become more and more stiff and doesn''t react at all to sudden fps changes  Business is business and Moses is Moses[Homesite]   ;  In the first line of your functionm_dt = (GetTickCount() - m_lastFrame) * 0.001;GetTickCount() returns milliseconds, IIRC, so multiplying by 0.001 is giving _really_ small values, which can lead to floating point imprecision errors and whatnot. ;  I think the problem is that you have the line:m_fps = 1 / m_dt;   // get the fpsI think it should be:m_fps = 1000 / m_dt;  This is because m_dt is in units of milliseconds (ms).  Therefore, doing 1/m_dt gives you units of frames/ms, but what you want is frames/sec, so you have to multiply by 1000. frames/sec = (1 frame/m_fps ms) * (1000 ms/1 s).  If you write that equation down you can see easier how the units cancel out.  Also, you might want to check on GetTickCount().  I believe ''ticks'' are in units of ms, so there''s no need to divide by 1000 (or in your case, multiply by 0.001).  Try that, and see if it helps. 