   [SDL + C++] Frame rate capping
  First off, hello everybody. I'm new to the forums, and I'm pretty new to programming.I recently started to learn how to program games with C++, using SDL. My IDE is VC++  2008 express edition, and I'm using tutorials and a book I bought on C++.anyway.... frame rate capping:I have a header file called "timer.h", that looks like this:#include "SDL.h" class timer{public:timer();~timer();int start();int stop();int getticks();private:bool running;int startticks;};timer::timer(){running = false;startticks = 0;}timer::~timer(){}int timer::start(){if(running == false){running = true;startticks = SDL_GetTicks();}return 0;}int timer::stop(){if(running == true){running = false;}return 0;}int timer::getticks(){if (running == true){return SDL_GetTicks() - startticks;}else {return 0;}}In my gameloop file, I placed, of course, the header "# include timer.h".just before the gameloop itself, I wrote "timer fps;", which creates a timer called fps. then, the first thing in the gameloop, right before I handle events, it calls "fps.start();". and at the end of the whole loop, I wrote if(fps.getticks() < 1000 / FPSECOND) { //Delay the remaining frame time SDL_Delay( ( 1000 / FPSECOND ) - fps.getticks() ); } The full code of the game function is as follow:void newgame(){const int FPSECOND = 2;Uint8* keys = SDL_GetKeyState(NULL);surf_background = ImageLoad("background.bmp");bool done = false;int frame = 0;timer fps;//Creates the player (Soldier class)Soldier Player1(150,200);while(done == false){fps.start();SDL_Event event;while(SDL_PollEvent(&event)){if (event.type == SDL_KEYDOWN){if(event.key.keysym.sym == SDLK_ESCAPE){done = true;}if(event.key.keysym.sym == SDLK_q){Player1.switchweapon();}if(event.key.keysym.sym == SDLK_e){Player1.thrownade();}if(event.key.keysym.sym == SDLK_r){Player1.reload();}}} if(keys[SDLK_s]){Player1.crouch();}if(keys[SDLK_w]) {Player1.jump();}if(keys[SDLK_a]){Player1.moveleft();}if(keys[SDLK_d]){Player1.moveright();}if(keys[SDLK_j]){Player1.fire();}if(keys[SDLK_k]){Player1.altfire();}SDL_FillRect(surf_screen, NULL,   SDL_MapRGB(surf_screen->format,0,0,0));apply_surface(0,0,surf_background,surf_screen,NULL);apply_surface(Player1.getx(),Player1.gety(),surf_soldier,surf_screen, NULL);SDL_Flip(surf_screen);frame++;if(fps.getticks() < 1000 / FPSECOND) { //Delay the remaining frame time SDL_Delay( ( 1000 / FPSECOND ) - fps.getticks() ); } }}I know the "goto" isn't preferred, but I couldnt think of any other way to do it without windows giving an error when I quit. What this code SHOULD do, is cap FPS at 2 frames per second. I just chose 2 because that way, it'd be easily noticable if the code works or not. What this code does is... well... run the game I programmed and completely ignore the FPS capping feature. Computer mutany?Well, that was my problem, I hope you can help me with it. Oh and if you think that for whatever reason my code looks sloppy, just tell me. I'm really new so I have a lot to learn.You'll see more of me when I try to add tile based maps, collision detection and other basic, yet complicated things.EDIT: thanks to nhatkthanh, I ditched my goto command, yay.[Edited by - Poitertjuh on May 5, 2009 2:50:56 PM]
  I can't read without indentation of the brackets, put your code in [.code] ... [./code] block, remove the . after the bracket.Also you shouldn't need the goto, use your done flag, when it's done it should exit out of the loop and end the game.   ;  hmm... I added the [.code] and [./code] (without the dots), but it doesnt put all that in a neat white little window. it just shows it as it is now....   ;  This line:  if(fps.getticks() < 1000 / FPSECOND)Will only trigger once. After then, getticks() will always return a larger value.To find out how to use different forum tags, click the 'FAQ' link near the top-right of this page.   ;  so if I changeif(fps.getticks() < 1000 / FPSECOND) { //Delay the remaining frame time SDL_Delay( ( 1000 / FPSECOND ) - fps.getticks() ); } into[if(fps.getticks() < 1000 / FPSECOND) { //Delay the remaining frame time SDL_Delay( ( 1000 / FPSECOND ) - fps.getticks() );   fps.stop();} it should work just fine?I'm in school right now, not working on my own PC, so I can't check it. I'll check out the FAQ, thank you.   ;  I'm not answering that question for you - you should be able to prove it to yourself by thinking about what the values would be. You should also realise what happens to those values if frames overrun and you choose not to stop the timer.   ;  Well fps.stop(); worked, thanks for pointing me in the right direction.I've got the animations of the player working too, so I'm happy.Thanks again!;  use[.source] .....[./source]   