Recent

Author Topic: How to fix awesome lags?  (Read 5142 times)

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
How to fix awesome lags?
« on: August 28, 2016, 06:04:11 pm »
I am creating a small game like an advanced version of the dinosaur game of google chrome while the user is not connected to the internet. Ok, While i was creatig that game, some lags appeared so i fixed them. But this time, the lag is very eccessive(bad english). Without the function i putted, the game doesn't work very well.
I think the problem is this where there are the conditions  checking if the dino touches the cactus:
Code: Pascal  [Select][+][-]
  1. procedure Die;
  2.         begin
  3.                 if up = false then begin
  4.  
  5.                 for i4 := 1 to 100 do begin
  6.                 cactus1^.x := CCactus[i4];
  7.                 if ( dino123rect^.x >= cactus1^.x ) and ( dino123rect^.x <= cactus1^.x + cactus1^.w ) or
  8.                    ( dino123rect^.x + dino123rect^.w >= cactus1^.x ) and ( dino123rect^.x + dino123rect^.w <= cactus1^.x + cactus1^.w ) then
  9.                         if ( dino123rect^.y >= cactus1^.y ) and ( dino123rect^.y <= cactus1^.y + cactus1^.h ) or
  10.                            ( dino123rect^.y + dino123rect^.h >= cactus1^.y ) and ( dino123rect^.y + dino123rect^.h <= cactus1^.y + cactus1^.h ) then death := true;
  11.                 writeln( death );
  12.                 end;
  13.  
  14.                 end;
  15.         end;
Here is it the full game :
http://manueldicriscito.altervista.org/GoogleGame.zip
« Last Edit: August 28, 2016, 06:08:23 pm by Manu12x »
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: How to fix awesome lags?
« Reply #1 on: August 28, 2016, 06:19:55 pm »
If you use Boolean logic, why the intermediate "If..then"? I do not understand that. That should be an AND operation too.... Agree ;)
There is no logic in that, especially since Boolean shortcut evaluation will bail out at any point.
Continuing in Boolean style instead of forcing a jump might help.
Write it so that all boolean truths are satisfied up until the determination of dead.
This should show an improvement:
Code: Pascal  [Select][+][-]
  1.                 for i4 := 1 to 100 do begin
  2.                 cactus1^.x := CCactus[i4];
  3.                 if (( dino123rect^.x >= cactus1^.x ) and ( dino123rect^.x <= cactus1^.x + cactus1^.w ) or
  4.                    ( dino123rect^.x + dino123rect^.w >= cactus1^.x ) and ( dino123rect^.x + dino123rect^.w <= cactus1^.x + cactus1^.w )) and
  5.                    (( dino123rect^.y >= cactus1^.y ) and ( dino123rect^.y <= cactus1^.y + cactus1^.h ) or
  6.                    ( dino123rect^.y + dino123rect^.h >= cactus1^.y ) and ( dino123rect^.y + dino123rect^.h <= cactus1^.y + cactus1^.h )) then death := true;
  7.  

You should also look at impossibilities in your algorithm, btw.
It may be more efficient to bring one or more  variables outside of the loop.
« Last Edit: August 28, 2016, 06:33:25 pm by Thaddy »
Specialize a type, not a var.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: How to fix awesome lags?
« Reply #2 on: August 28, 2016, 06:30:44 pm »
@Manu12x

I can't be sure to say it will work. But I think it may help a bit.

You code tests all the positions to know if the player hit the cactus, if yes then it die. It can be optimized by checking if it the player is outside the cactus position and stop to test to current cactus but move to the next loop.

How it can speed up the performance? Because most of the time, the player is alive, so by checking not dying and continue to the next loop is better. You use continue command the quit the the testing and move to the next looping.

As usual I won't provide the code, but a draft of the concept:

Code: [Select]
for i = 1 to something do begin
  CurrentCactus := Cactus[i];
  if (PlayerX < CurrenctCactus.x) then continue;
  if (PlayerX > CurrenctCuctus.x + CactusWidth) then continue;
  ...

The code still can be further improved if you know how.

More info about continue command:
http://www.freepascal.org/docs-html/rtl/system/continue.html

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: How to fix awesome lags?
« Reply #3 on: August 28, 2016, 06:36:32 pm »
@Manu12x
I can't be sure to say it will work. But I think it may help a bit.
No. He needs to do what I wrote and bring variables that are constant (it seems at first glance half of them are known values  *before* the loop is entered...) inside the loop outside of the loop and remove the if/then and replace it with full boolean logic.
Normally your advice is very helpful. This is, however, about speed. In the future FPC can do this kind of optimization by itself. There is work being done in that direction.

OTOH you are right that he can test for NOT, but in the case of boolean shortcut evaluation that is pretty much the same.
« Last Edit: August 28, 2016, 06:59:21 pm by Thaddy »
Specialize a type, not a var.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: How to fix awesome lags?
« Reply #4 on: August 28, 2016, 06:58:16 pm »
Quote
bring variables that are constant inside the loop outside
This is what I meant about the 'can be further improved'.

@Manu12x

Speed optimization is an art that may need years to learn. Most of the tricks can be learned from books and reading other programmers' code. But the best teacher is learning from experience. Usually if I meet something similar to your case, I will add some codes to do performance benchmark and test to optimize the code or probably using different solutions to find out which one is the best. Now, there already has 3 suggestions, so if you want to improve your programming skill especially on performance optimization, please test all the suggestions to find out which one is the best. Some or all of them may combined.

1. Move some calculations inside the loop to prior of the loop (by Thaddy)
2. Avoid jumping (if-then-else) but use full boolean (by Thaddy)
3. Use continue command to skip the current loop and do the next (by me)

@Thaddy

I'm not sure he will understand how to do the things we said.
« Last Edit: August 28, 2016, 07:01:56 pm by Handoko »

Thaddy

  • Hero Member
  • *****
  • Posts: 14159
  • Probably until I exterminate Putin.
Re: How to fix awesome lags?
« Reply #5 on: August 28, 2016, 07:00:24 pm »
He will learn ;)
I will add example for outside the loop: for OP: it simply means you are making calculations every time you loop (100 times) when these only need to be calculated once.
Later I will show you what I mean.

[edit]
Like so:
Code: Pascal  [Select][+][-]
  1. // declare intermediate variables with values that don't change inside the loop
  2. var
  3.  DinoXw:Integer;
  4.  DinoYh:Integer;
  5.  CactusXw:integer;
  6.  CactusYh:Integer;
  7.  .......
  8.                 DinoXw := dino123rect^.x + dino123rect^.w ; // saves 99 calculations
  9.                 DinoYh :=dino123rect^.y + dino123rect^.h;  // saves another 99 calculations
  10.                 for i4 := 1 to 100 do begin
  11.                 cactus1^.x := CCactus[i4];
  12.                 CactusYh := cactus1^.y + cactus1^.h;  // Saves just a bit
  13.                 CactusXw := cactus1^.x + cactus1^.w ; //Saves just a bit
  14.                      ------
  15.  

You can now substitute all these calculations with a variable that already has the correct value. In a loop that is a HUGE speed gain.

« Last Edit: August 28, 2016, 07:33:29 pm by Thaddy »
Specialize a type, not a var.

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: How to fix awesome lags?
« Reply #6 on: August 28, 2016, 08:12:57 pm »
Ok i'm not good at English so i will write in italian language so please translate it from google or other.


Ok. Del consiglio che @Thaddy mi ha dato, ne sono consapevole ma preferivo nel mio modo così da organizzarmi meglio, e poi provandoci, non trovavo molte differenze inserendo AND. Poi, il comando "CONTINUE" non l' ho mai sentito e magari da questo potrei imparare qualcosa. E poi se qualche volta non capisco le risposte che mi date, la causa è il mio pessimo inglese. Io proverò e vi dirò se tutto è andato bene, grazie per la comprensione e le risposte!! ::)
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: How to fix awesome lags?
« Reply #7 on: August 28, 2016, 08:31:32 pm »
OK i tried!!! Thank you @Handoko you helped me a lot reducing the code a lot using the continue command too. Thanks again. See you ( not really see ) next time!
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: How to fix awesome lags?
« Reply #8 on: August 29, 2016, 01:30:25 am »
This:
Code: Pascal  [Select][+][-]
  1. ( dino123rect^.x >= cactus1^.x ) and ( dino123rect^.x <= cactus1^.x + cactus1^.w )
  2. or ( dino123rect^.x + dino123rect^.w >= cactus1^.x )
  3. and ( dino123rect^.x + dino123rect^.w <= cactus1^.x + cactus1^.w )
is logically equal to:
Code: Pascal  [Select][+][-]
  1. ( dino123rect^.x + dino123rect^.w >= cactus1^.x )
  2. and ( dino123rect^.x <= cactus1^.x + cactus1^.w )

the same goes for dino123rect^.y .... (the Y function); so, you could cut the calculation by more than half (in the worst case) and also moving dino123rect^.x + dino123rect^.w out of the loop could help too.

PS: just you know it's bad habit to uses TAB when indenting your code; try using double spaces it' the best, imo.
« Last Edit: August 29, 2016, 01:37:34 am by shobits1 »

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: How to fix awesome lags?
« Reply #9 on: August 29, 2016, 05:30:37 am »
PS: just you know it's bad habit to uses TAB when indenting your code; try using double spaces it' the best, imo.
100% agree. ;D

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: How to fix awesome lags?
« Reply #10 on: August 29, 2016, 03:01:55 pm »
PS: just you know it's bad habit to uses TAB when indenting your code; try using double spaces it' the best, imo.
100% agree. ;D
Really? Thanks
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

 

TinyPortal © 2005-2018