Recent

Author Topic: [SOLVED] Game speed and fps  (Read 24899 times)

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
[SOLVED] Game speed and fps
« on: September 12, 2016, 01:34:37 am »
Hi, i'm here with a new problem ( googled around without understanding ).
I created a game but it sometines goes fast and other time slow. I mean, if i play it with a slow pc, the speed of game is slow and no normal as i want. If i play with a fast pc, game is fast and so not normal. (Like a charachter should run with 5 meters/second but a slow pc make it run 2 meters/second and a fast pc make it run 10 meters/second)
You may don't understand what i mean
I need only help, i'm building and i added this in the game loop:
Code: Pascal  [Select][+][-]
  1. Repeat
  2. ...
  3. If ( SDL_GetTicks mod 1000 = 0 ) then
  4.   Begin
  5.     Fps := Frame;
  6.     Frame := 0;
  7.   End;
  8. writeln(Fps,' frames per second');
  9. Frame := frame + 1;
  10. ...
  11. Until ...;
  12.  
I'd like the speed is normal for all ( for me it tells 280 frames per second ); . I don't know if you understand me ( but i hope ); please
« Last Edit: September 18, 2016, 06:05:39 pm by Manu12x »
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Game speed and fps
« Reply #1 on: September 12, 2016, 02:55:05 am »
Assuming you wish to limit the number of frames being drawn you can apply something like the following poor man's solution:

1) before starting rendering your frame you ask for the tickcount using SDL_GetTicks()
2) you do your usual stuff, like calculating things for current frame and render the frame
3) just after rendering the frame you calculate how much time it took to calculate and render the frame, using SDL_GetTicks() again (you can simply subtract those values).
4) if the difference between the two values is < then the framerate you wish to limit to then you delay before calculating and rendering the next frame.

... and repeat the step 2 till 4

A perhaps more practical pseudo-code example makes it hopefully more clear:
Code: [Select]
var
  sleeper  : integer = 0;
  ticker   : LongWord;
begin
  ... init stuff
  ticker := SDL_GetTicks;

  // rendering loop
  while not(quit) do
  begin

    .. read keys, calculate frame and render frame

    ticker := ticker + 16; // 16 = 1000 milliseconds / 60 frames, e.g. limiting to approx. 60 frames per second
    sleeper := ticker - SDL_GetTicks;
    if sleeper >= 0 then SDL_Delay(sleeper);   
  end;

  .. close down
end;

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1225
    • Burdjia
Re: Game speed and fps
« Reply #2 on: September 12, 2016, 11:42:14 am »
Don't know if it's possible in SDL, but using Allegro's events I do that:
Code: Pascal  [Select][+][-]
  1.   REPEAT
  2.     Event := al_wait_for_event (EventQueue);
  3.     CASE Event._type OF
  4.     ALLEGRO_EVENT_DISPLAY_CLOSE:
  5.       EndGame := TRUE;
  6.     { * And any other event such as keyboard, mouse, etc. * }
  7.     ALLEGRO_EVENT_TIMER:
  8.       BEGIN
  9.         UpdateFrame;
  10.         IF al_is_event_queue_empty (fEventQueue) THEN
  11.           RenderFrame
  12.       END;
  13.     END
  14.   UNTIL EndGame
  15.  

If there's no events in SDL or you can't/don't know how to implement them but it does has timers, you may use a similar stuff, as done with Allegro 4 (that doesn't has events):
Code: Pascal  [Select][+][-]
  1. VAR
  2.   FrameCount: INTEGER;
  3.  
  4.   PROCEDURE TimerHandler; CDECL;
  5.   BEGIN
  6.     INC (FrameCount)
  7.   END;
  8.  
  9.  
  10.   BEGIN
  11. ...
  12.   { Set it to 50 frames per second. }
  13.     IF NOT al_install_int_ex (@TimerHandler, AL_BPS_TO_TIMER (50))
  14.     THEN
  15.       RAISE Exception.Create ('Can''t install timer!!!')
  16. ...
  17.     REPEAT
  18.       WHILE FrameCount > 0 DO
  19.       BEGIN
  20.         UpdateFrame;
  21.         DEC (FrameCount)
  22.       END;
  23.       RenderFrame
  24.     UNTIL EndGame
  25.   END.
  26.  
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Game speed and fps
« Reply #3 on: September 12, 2016, 12:34:51 pm »
Assuming you wish to limit the number of frames being drawn you can apply something like the following poor man's solution:

1) before starting rendering your frame you ask for the tickcount using SDL_GetTicks()
2) you do your usual stuff, like calculating things for current frame and render the frame
3) just after rendering the frame you calculate how much time it took to calculate and render the frame, using SDL_GetTicks() again (you can simply subtract those values).
4) if the difference between the two values is < then the framerate you wish to limit to then you delay before calculating and rendering the next frame.

... and repeat the step 2 till 4

A perhaps more practical pseudo-code example makes it hopefully more clear:
Code: [Select]
var
  sleeper  : integer = 0;
  ticker   : LongWord;
begin
  ... init stuff
  ticker := SDL_GetTicks;

  // rendering loop
  while not(quit) do
  begin

    .. read keys, calculate frame and render frame

    ticker := ticker + 16; // 16 = 1000 milliseconds / 60 frames, e.g. limiting to approx. 60 frames per second
    sleeper := ticker - SDL_GetTicks;
    if sleeper >= 0 then SDL_Delay(sleeper);   
  end;

  .. close down
end;
Works.. A bit .  but thanks.
In game, it now tells 90 frames per second.. And after  5 seconds of playing, frames per second are 125.
But game was so slow ( yeah i know how to fix that ) .
I don't know why the fps change from 90 to 125..
Between this two number i saw for one second a number more high "1717".. I will try Allegro. I am a bit interested to that
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Game speed and fps
« Reply #4 on: September 12, 2016, 12:42:45 pm »
No.. I'm wrong. Then how to add a benchmark that shows the fps???
And for before.. When i start game, speed is normal and after 0.50s it is slow (like a want). Why not immediately..? Sorry for all my question..
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Handoko

  • Hero Member
  • *****
  • Posts: 5543
  • My goal: build my own game engine using Lazarus
Re: Game speed and fps
« Reply #5 on: September 12, 2016, 12:45:24 pm »
Usually game engines will provide their own solutions to solve the game speed issue, you may need to check the game engine official pages, documentations or tutorials. Or perhaps you can join their forum and ask their users.

For example, I used ZGameEditor. There has option to make it fix fps or run it as fast as the computer allowed. Godot has such setting too. And so the others I ever tried.

Game speed issue is very common thing in game development. So any good game engine should already has its own solution. And that makes it differs from graphics engine. SDL is game engine? Maybe not, it just a graphics engine if it doesn't provide that feature.

Using a good game engine, you almost don't need to think about game speed issue, how to distribute the result program,  file directory and screen resolution problems. They solve them in background, you just focus on making your game, not the technical things. Try other 'better' game engines as much as you can before you decide to seriously use it.
« Last Edit: September 12, 2016, 12:59:49 pm by Handoko »

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Game speed and fps
« Reply #6 on: September 12, 2016, 12:49:27 pm »
Usually game engines will provide their own solutions to solve the game speed issue, you may need
 to check the game engine official pages or tutorials.

For example, I used ZGameEditor. There has option to make it fix fps or run it as fast as the computer allowed. Godot has such setting too.
I want the speed is the same for all. If a pc slow doesn't reach that speed, then some frames should be skipped = lag.. Like all games! I think
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Handoko

  • Hero Member
  • *****
  • Posts: 5543
  • My goal: build my own game engine using Lazarus
Re: Game speed and fps
« Reply #7 on: September 12, 2016, 01:04:23 pm »
It won't be too noticeable if you're using the third technique I mentioned before. Because if the previous frame rendering takes too long, the next one will compensate its movement by multiple the movement speed with the delta time. Also, the game you built is simple, you won't notice the lag except you're running it on a 20 years old Pentium computer. Any 10 years old computers can run simple 3D games without noticeable lags.

If your game performance is slow, it can caused by:
- Using bad game looping
- You don't understand how to use the game engine properly
- You're a bad programmer, using not efficient solutions
- Your game is too 3D realistic, using too much lamps, objects and shaders

Previously, I saw you asking question about how to optimize the performance of your game. Honestly to say, that code was too not efficient.

Sorry, it sounds I critique too much. But what I say is truth.
« Last Edit: September 12, 2016, 01:22:28 pm by Handoko »

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Game speed and fps
« Reply #8 on: September 12, 2016, 01:39:56 pm »
It won't be too noticeable if you're using the third technique I mentioned before. Because if the previous frame rendering takes too long, the next one will compensate its movement by multiple the movement speed with the delta time. Also, the game you built is simple, you won't notice the lag except you're running it on a 20 years old Pentium computer. Any 10 years old computers can run simple 3D games without noticeable lags.

If your game performance is slow, it can caused by:
- Using bad game looping
- You don't understand how to use the game engine properly
- You're a bad programmer, using not efficient solutions
- Your game is too 3D realistic, using too much lamps, objects and shaders

Previously, I saw you asking question about how to optimize the performance of your game. Honestly to say, that code was too not efficient.

Sorry, it sounds I critique too much. But what I say is truth.
Ok then try to download this

http://manueldicriscito.altervista.org/DinoLand.exe

Substitute this file with the original exe file and tell me differences
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Handoko

  • Hero Member
  • *****
  • Posts: 5543
  • My goal: build my own game engine using Lazarus
Re: Game speed and fps
« Reply #9 on: September 12, 2016, 02:18:53 pm »
Downloaded and tested.

What changes have you made? I compare the speed running on my computer with the video uploaded by lainz, mine is about 2x faster than that.

After I clicked the button to start playing, and I moved my right hand to touch the keyboard. The dino crashed the cactus before I landed my hand on the arrow keys. Still too fast.

I notice something not logical. When dino run forward and jump, you can press right arrow to have the dino move backward in the middle of jumping (on the sky). Solving this issue will make the game play more logically but also harder to play.

Although it still has several minor issues, but you really have made a computer game. Tomorrow, you take it to your school and collect suggestions from your friends. But be strong if you get criticism. Remember criticism will only make the programmer do it better next time.
« Last Edit: September 12, 2016, 02:28:14 pm by Handoko »

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Game speed and fps
« Reply #10 on: September 12, 2016, 02:34:20 pm »
Downloaded and tested.

What changes have you made? I compare the speed running on my computer with the video uploaded by lainz, mine is about 2x faster than that.

After I clicked the button to start playing, and I moved my right hand to touch the keyboard. The dino crashed the cactus before I landed my hand on the arrow keys. Still too fast.

I notice something not logical. When dino run forward and jump, you can press right arrow to have the dino move backward in the middle of jumping (on the sky). Solving this issue will make the game play more logically but also harder to play.

Although it still has several minor issues, but you really have made a computer game. Tomorrow, you take it to your school and collect suggestions from your friends. But be strong if you get criticism. Remember criticism will only make the programmer do it better next time.
Yes the dino can move left and right also of it is jumping together! I want this! And then.. At school.. I am at secondary education (idk how to write it on english) and my classmates know only how to use crt.. They don't know arrays.. I'm more advances.. And i don't know if also my teacher is good at advanced programming
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Game speed and fps
« Reply #11 on: September 12, 2016, 02:37:56 pm »
Downloaded and tested.

What changes have you made? I compare the speed running on my computer with the video uploaded by lainz, mine is about 2x faster than that.

After I clicked the button to start playing, and I moved my right hand to touch the keyboard. The dino crashed the cactus before I landed my hand on the arrow keys. Still too fast.

I notice something not logical. When dino run forward and jump, you can press right arrow to have the dino move backward in the middle of jumping (on the sky). Solving this issue will make the game play more logically but also harder to play.

Although it still has several minor issues, but you really have made a computer game. Tomorrow, you take it to your school and collect suggestions from your friends. But be strong if you get criticism. Remember criticism will only make the programmer do it better next time.
@Handoko do you remember that link you wrote in a reply about game looping but in C? Ok.. That's what i was searching about game speed but in pascal programming because i don't really understand C so good..
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Handoko

  • Hero Member
  • *****
  • Posts: 5543
  • My goal: build my own game engine using Lazarus
Re: Game speed and fps
« Reply #12 on: September 12, 2016, 02:49:06 pm »
I'm not good in C too. I enjoy reading the explanations. It explains well about how a game looping should be. I ever thought to learn C/C++, but I'm busy have no time for it.

If you have time, you really need to learn C/C++. Use it or love it is another thing. But you will really become super awesome in programming. Because all the computer programming topics have examples in C/C++. I regret not to learn C in the past. Don't repeat my mistake!

Quote
At school.. I am at secondary education (idk how to write it on english) and my classmates know only how to use crt.. They don't know arrays.

You're really great at this young age. Remember, always open to suggestions and criticism. And don't be proud to what you can achieve, there always someone can do better than you.

Quote
And i don't know if also my teacher is good at advanced programming

Usually teachers are teachers. They're good in teaching not doing the thing in the topic they teach. If you ever find you're better than your teacher in programming, just don't be proud, and you still have to respect him/her because it is the teacher to open the door for you to this programming world.

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Game speed and fps
« Reply #13 on: September 12, 2016, 03:17:50 pm »
I found this

http://www.gamedev.net/topic/651698-advice-to-lock-frame-rate/

Maybe that's what i was searching for! I am too bad at explain things on english so see if this can be good for me
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Handoko

  • Hero Member
  • *****
  • Posts: 5543
  • My goal: build my own game engine using Lazarus
Re: Game speed and fps
« Reply #14 on: September 12, 2016, 03:25:31 pm »
Yes, they said something about locking frame rate. You should try it, it may solve your the speed issue.

 

TinyPortal © 2005-2018