Jump to content
  • Advertisement
Sign in to follow this  
Noxir

OpenGL Any delay?

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any delay function in OpenGL that works 100%? If I press "i" the backpack opens and closes very very fast on my game.
Quote:
if(keys[73]) { if(backpackOpen) { /////DELAY? backpackOpen = false; } else { /////DELAY? backpackOpen = true; } }
thnks

Share this post


Link to post
Share on other sites
Advertisement
OpenGL is just for graphics, and doesn't handle delaying. So this isn't really an OpenGL problem. The issue you have is that your input is constant and triggers every frame. You probably don't really want a delay because that will slow down your rendering and so on. One way of fixing this problem is to move to event-based input. Another way might be to remember the key state from the previous frame and to only do something when it has changed. (Ie. if keys[73] was false last frame and is true this frame, then change the backpack state.)

Share this post


Link to post
Share on other sites
I personally stray away from event based input. I'd do something more like this:


int keys[] = new int[256];
int delays[] = new int[256];
int lastPresed[] = new int[256];

public void setDelay(int key, int delay){
delays[key] = delay;
}

public boolean keyDown(int key){
if(KEY_IS_DOWN_ACCORDING_TO_YOUR_INPUT_METHOD){
if(Engine.getCurrentTick() - lastPressed[key] >= delays[key]){
lastPressed[key] = Engine.getCurrentTick();
return true;
}
else
return false;
}
return false;
}







Where Engine.getCurrentTick() returns the number of frames since the start of the engine. I.E., if your game runs at 60fps and it has run for 1 minute, then Engine.getCurrentTick() would return 3600 (60fps * 60sec)

Then your backpack code would look like this:


int backpackButton = 73;
int backpackOpenButtonDelay = 60; // Delay of 1 second.

setDelay(backpackButton, backpackOpenButton);

if(keyDown(backpackButton))
backpackOpen = !backpackOpen;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!