13 Replies - 729 Views - Last Post: 23 October 2011 - 10:59 AM Rate Topic: -----

#1 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

input lag in tetris

Posted 15 October 2011 - 04:46 PM

Hi All,

i am developing a tetris clone, using SDL. i am done with most of the things but i am getting input lag
problem. When i try to move each of the tetrominoes left or right it does not take the input instantly,but
after some seconds . What may be the cause of this? earlier i thought my collision detection is taking up
time but when i excluded collision detection , then also its taking time. Earlier i thought the code which runs from the time user presses a key to the time the tetromino is rendered, should
be less, in order to deal with this lag! to check this i even removed collision detection function, but still
lag was there,so there must be some thing else going on!Please suggest what might have caused it??

following is the loop which runs for each tetrominoes from the time it appears on screen to the
time it rest on other tetromino:-

while(each tetrominoes doesnot rest)
{
descend the current tetromino;
check for input event;
handle this input event ,move the tetrominoes; -> it calls the collision detection function.
update the screen;
}

Is This A Good Question/Topic? 0
  • +

Replies To: input lag in tetris

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: input lag in tetris

Posted 15 October 2011 - 07:23 PM

If you are timing all activity bound to screen refresh then you will get lag. Have your game loop tick over at a maximum rate and have your subsystems tick over at their own rates within the game loop.
Was This Post Helpful? 0
  • +
  • -

#3 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: input lag in tetris

Posted 15 October 2011 - 07:55 PM

Agreed with ButchDean. I'm doing a tetris clone with my game updating around 45 fps, and have the actions moveLeft, moveRight, moveDown, drop, and rotate call when a key is pressed, and if it's being held a counter will increase with each game update. If the key is released, the counter is reset, or if the counter reaches a threshold(like 10 updates) redo the action and reset the counter. I've worked "resting"/setting the current piece the same way, so the player has the ability to rotate/float the piece indefinitely before it locks and a new piece begins dropping.

This post has been edited by Greltam: 15 October 2011 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: input lag in tetris

Posted 16 October 2011 - 02:57 PM

View PostButchDean, on 15 October 2011 - 07:23 PM, said:

If you are timing all activity bound to screen refresh then you will get lag. Have your game loop tick over at a maximum rate and have your subsystems tick over at their own rates within the game loop.

Hi,
Thanks for your attention :) , actually i don't have any idea about ticks you are talking about , can you please suggest
me some tutorial for this?

Thanks&Regards
Was This Post Helpful? 0
  • +
  • -

#5 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: input lag in tetris

Posted 21 October 2011 - 04:39 AM

Actually i want to make my question more accurate, sorry i could not explain it clearly before..
In real, i don't get lag..but when i press multiple keystrokes then only it applies these keystrokes
one by one,which takes up time, i just played tetris online , and in that game these multiple keystrokes
are applied instantly,i mean it looks like they don't have any queue for input, they just get input and
apply it to the tetrominoes instantly....Does anybody here have idea ,how it can be done ?..please suggest..
and i don't think this particular problem is associated with ticks..

Thanks & Regards.

This post has been edited by gamazone: 21 October 2011 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: input lag in tetris

Posted 21 October 2011 - 05:23 AM

View Postgamazone, on 21 October 2011 - 12:39 PM, said:

...
and i don't think this particular problem is associated with ticks..

...

What makes you think it isn't?
Was This Post Helpful? 0
  • +
  • -

#7 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: input lag in tetris

Posted 21 October 2011 - 05:28 AM

View PostButchDean, on 21 October 2011 - 05:23 AM, said:

View Postgamazone, on 21 October 2011 - 12:39 PM, said:

...
and i don't think this particular problem is associated with ticks..

...

What makes you think it isn't?


because if it is..then why its not there for single keystroke? please tell if i am missing
something..
Was This Post Helpful? 0
  • +
  • -

#8 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: input lag in tetris

Posted 21 October 2011 - 05:31 AM

Handle ALL the input events in the queue on your "tick"?
while(each tetrominoes doesnot rest)
{
  descend the current tetromino;
  for all input events
  {
  check for input event;
  handle this input event ,move the tetrominoes; -> it calls the collision detection function.
  }
  update the screen;
}


And a "Tick" is when you're program goes through all the code to handle input/update/draw a frame, then it will do another tick where it handles input/updates/draws, forever until your program stops.

A tick for you might be:
while(each tetrominoes doesnot rest)
{
  //From here
  descend the current tetromino;
  check for input event;
  handle this input event ,move the tetrominoes; -> it calls the collision detection function.
  update the screen;
  //To Here would be considered a "Tick"
  //And everytime your while loop executes another "Tick" happens.
} 


Was This Post Helpful? 1
  • +
  • -

#9 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: input lag in tetris

Posted 21 October 2011 - 07:55 PM

Hi Greltam,

Thanks for explaining tick so well and simple :), also after reading your post i have following concerns

1]Are you saying i should update the screen after handling all inputs? In this case only the result of
last input event will be rendered.

2]don't you think the update screen function should be inside for loop you mentioned? as after getting the input
event , i will be moving it (update the game state), then this change should be rendered.

3]i think i am handling all input events on the tick : -following is the original code
while(rest==false)
{ 
rest=sp.move(SDLK_DOWN); // this function , keeps the tetromino moving down..."tick start here"
SDL_Delay(250);
if(SDL_PollEvent(&event)) // this function checks the event queue,if any event returns 1 else 0
{
if(event.type==SDL_QUIT)
exit(0);
else if(event.type==SDL_KEYDOWN) //if event is keydown, then inside move function it is determined which key 
rest=sp.move(event.key.keysym.sym); //is pressed ..screen is updated in the move function itself.."one tick ends here"
}
sp.check_completion();// checks if any of the row filled with tetrominoes 
}


Was This Post Helpful? 0
  • +
  • -

#10 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: input lag in tetris

Posted 22 October 2011 - 05:52 AM

You should try rendering once per gameloop, after doing all the input events. If you have problems with the render we can help you on that.

while(rest==false)
{ 
  rest=sp.move(SDLK_DOWN); // this function , keeps the tetromino moving down..."tick start here"
  SDL_Delay(250);
  
  //Here, you only call pollEvent once. You should have this in a while loop like this.
  //if(SDL_PollEvent(&event))
  while(SDL_PollEvent(&event))
  {
    if(event.type==SDL_QUIT)
      exit(0);
    else if(event.type==SDL_KEYDOWN)
      rest=sp.move(event.key.keysym.sym); 
  }
  sp.check_completion();// checks if any of the row filled with tetrominoes 
}


Was This Post Helpful? 1
  • +
  • -

#11 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: input lag in tetris

Posted 22 October 2011 - 07:18 AM

View PostGreltam, on 21 October 2011 - 01:31 PM, said:

Handle ALL the input events in the queue on your "tick"?
while(each tetrominoes doesnot rest)
{
  descend the current tetromino;
  for all input events
  {
  check for input event;
  handle this input event ,move the tetrominoes; -> it calls the collision detection function.
  }
  update the screen;
}


And a "Tick" is when you're program goes through all the code to handle input/update/draw a frame, then it will do another tick where it handles input/updates/draws, forever until your program stops.

A tick for you might be:
while(each tetrominoes doesnot rest)
{
  //From here
  descend the current tetromino;
  check for input event;
  handle this input event ,move the tetrominoes; -> it calls the collision detection function.
  update the screen;
  //To Here would be considered a "Tick"
  //And everytime your while loop executes another "Tick" happens.
} 



some notes on what has been said here:

1. Collision should be the first thing you check for, since if a collision has occurred and an animation had already taken place in the previous loop, user input will have no effect and the animation will have already taken place. All you are doing is wasting extra processing on redundant function calls.

2. You need to be certain what exactly a 'tick' is! I am assuming a 'tick' yo be a single step of a block moving down one position. If this is the case then it should be occurring at regular intervals depending on the timing of your game loop. so if you have something like:

while(GameIsRunning()) // GameIsRunning() returns a bool.
{
    if(!TenMillisecondElapsed())  // True if 10 milliseconds elapsed, the game ticks over this often.
        continue;                 // Re-evaluate game loop.

   // Game execution logic here.
}



And for the game execution logic I would be inclined to code it very much like

    // Update screen at 60fps irrespective of anything else.
    if(Update60FPSRender())
       UpdateScreen();

    if(CollisionDetectedChecked60TimesPerSecond())
       continue; // No need to take action if collision has taken place.

    // Check for user input at a high frequency.
    // This has the effect of compensating for multiple key presses.
    if(UpdateUserInputAt100timePerSecond())
    {
       // Update block positions internally, but DO NOT render here.
    }

    // All other processing here.



As you can see the loop is tight and you only perform the necessary processing as required at independent frequencies and only when circumstances change. This is how it should be done.
Was This Post Helpful? 1
  • +
  • -

#12 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: input lag in tetris

Posted 23 October 2011 - 10:25 AM

Quote

while(SDL_PollEvent(&event))



Hi Greltam,

the above change has solved my problem :) :) :). Thanks dude, i have been modifying the code to solve the problem , adopted different ways of getting input....but could not catch this little but significant change... thanks again.
Was This Post Helpful? 0
  • +
  • -

#13 gamazone  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 44
  • Joined: 05-April 11

Re: input lag in tetris

Posted 23 October 2011 - 10:52 AM

Quote

while(GameIsRunning()) // GameIsRunning() returns a bool.
{
    if(!TenMillisecondElapsed())  // True if 10 milliseconds elapsed, the game ticks over this often.
        continue;                 // Re-evaluate game loop.

   // Game execution logic here.
}



And for the game execution logic I would be inclined to code it very much like

    // Update screen at 60fps irrespective of anything else.
    if(Update60FPSRender())
       UpdateScreen();

    if(CollisionDetectedChecked60TimesPerSecond())
       continue; // No need to take action if collision has taken place.

    // Check for user input at a high frequency.
    // This has the effect of compensating for multiple key presses.
    if(UpdateUserInputAt100timePerSecond())
    {
       // Update block positions internally, but DO NOT render here.
    }

    // All other processing here.



As you can see the loop is tight and you only perform the necessary processing as required at independent frequencies and only when circumstances change. This is how it should be done.


thanks ButchDean for explaining "ticks" more precisely..:)

This post has been edited by ButchDean: 23 October 2011 - 11:01 AM
Reason for edit:: FTFY

Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: input lag in tetris

Posted 23 October 2011 - 10:59 AM

No problem, gamazone. Good luck with your project. :)

This post has been edited by ButchDean: 23 October 2011 - 11:00 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1