LWJGL: how to delay between states

Hi,

I am trying to do an intro similar to those like any other game, and I have it working but I want a delay on the image so that it stays on screen for longer.

This is my state code:

	private void render() {
		switch (state) {
		case INTRO:
			IntroState.draw();
			state = State.MAIN_MENU;
			break;
		case MAIN_MENU:
			glColor3f(0.0f, 0.0f, 1.0f);
			glRectf(0, 0, width, height);
			break;
		case GAME:
			gameState.draw();
			break;
		case RAND:
			new RandomTerrain();
			break;
		}
	}

I am trying to use this code:

			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

but if I put it in the INTRO state it just delays the intro being shown, and if I put it in the MAIN_MENU state, it sort of works, but I need a better way of doing it.

Thanks,

  • Dan

I generally use some kind of timer to control transitions etc. Sleeping to handle transitions is a ‘not so good’ idea. Leave your update & render loop running at the desired FPS/UPS, and add the delta time to your timer in the update loop. If the time is greater than the duration, then transition to the next state.

So as a dirty little example, it usually goes like this:



long duration = 10000;
long timer = 0;

public void update(long delta) {
     timer += delta;
     if(timer >= duration) {
         //Handle transition here. Reset timers. Do whatever you have to do.
     }
}


Ok this is how I’ve tried to set it up

	long duration = 1000;
	long timer;
	public void	update(long delta) {
	     timer += delta;
	     if(timer >= duration) {
			state = State.MAIN_MENU;
	     }
	}
	
	// Render Game States
	private void render() {
		switch (state) {
		case INTRO:
			IntroState.draw();
			update(timer);
			break;
		case MAIN_MENU:
			glColor3f(0.0f, 0.0f, 1.0f);
			glRectf(0, 0, width, height);
			break;
		case GAME:
			gameState.draw();
			break;
		case RAND:
			new RandomTerrain();
			break;
		}
	}

but it doesnt seem to work, I am assuming that update(timer); is not how I should be doing it, ideas?
Thanks,

  • Dan

Pass the delta time as the argument, but the timer. Also rest the timer to 0 afterwards so you can reuse it.

Sorry could you try and reword that please? I didnt quite understand that :frowning:

Thanks,

  • Dan

Dan, I’ve got a sneaking suspicion that you might need to go and review program structure a little bit, particularly in terms of Java & OOP (as evidenced by the code snippet you have provided, and by the way that you just lumped the update() method there). Just to give you some real quick pointers:

  • A game is generally a repeated loop, in which you 1) update logic, then 2) render it. I.e.:

public void go() {
	while(running) {
		//Do some timing calculations to determine delta.
		update(delta);
		render();
		try {
			Thread.sleep(sleepTime); //sleepTime being calculated from your timing too.
		} catch(InterruptedException ex) {

		}
	}
}

  • In the case of a state based game, States could be considered objects that have their own update() and render() methods. I.e. they handle all their updating and rendering themselves. Then your main game loop just becomes a case of calling the update() and render() methods on the current state, passing them the required parameters.

  • Instead of controlling program flow by having switches in your render or update methods, hold a reference to the current state somewhere (i.e. State currentState). Then instead of switches, you simply call currentState.update(delta);, currentState.render();.

  • In your particular case, the introState could hold its own timer variables, and handle the timing within it’s own update(delta) method (hence this is where the timer stuff I provided would be ideally located). Once the timing criteria are met, the introState then passes on control to the next state. (In a simple case it could do this itself by setting currentState to the next desired state. In a more complex case, it might interact with a form of state manager which handles current, reserved and transitioning states).

Right, that’s it. Very, very sorry if I have taught you to suck eggs with any of this, or made any silly assumptions.

Cheers.

EDIT: I should add that this is “program structure according to nerb” which may not be the right, best, or only way of doing things. You’ve obviously got some grasp on the concepts, but it just looks very messy to me. You have a mixture of state-handled rendering, non-state handled rendering, and no evidence of a solid update() method either in or outside of your states. Structure like this might suffice for a very small program, but I foresee you running in to trouble soon as things grow in size &/or complexity.