By the end of this episode you will have learned how to implement LWJGL game states.
Why and when are the LWJGL game states used?
To accomplish things like the Skyrim launcher, the main menu in Counter-Strike, or the cinematic when launching Diablo III, the developers use different game states.
Initializing the LWJGL game states
To represent the current game state, an enumerator is used. There are three states: ‘INTRO’, ‘MAIN_MENU’, and ‘GAME’. The code for the enumerator looks like this.
public enum State { INTRO, GAME, MAIN_MENU; } |
The program has one static instance of the type State, set by default to the ‘INTRO’ state.
private static State state = State.INTRO; |
Rendering the LWJGL game states
To switch over all the possible states, a switch statement is used. Symbolically a colour is assigned to every state. A new method called ‘glRectf(…)’ is also used. This method draws a rectangle with the first and last two parameters being the corners.
switch (state) { case INTRO: glColor3f(1.0f, 0.0f, 0.0f); glRectf(0, 0, 640, 480); break; case GAME: glColor3f(0.0f, 1.0f, 0.0f); glRectf(0, 0, 640, 480); break; case MAIN_MENU: glColor3f(0.0f, 0.0f, 1.0f); glRectf(0, 0, 640, 480); break; } |
The coloured rectangles (quadrilaterals) are just placeholders for things like a Game Menu, an Intro, and a Game Over screen. Post creative and original ideas in the comments.
Input handling
To switch between the different game states, the input is retrieved from the event queue. Insert this code in the game loop.
while (Keyboard.next()) { if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) { if(state == State.INTRO) state = State.MAIN_MENU; else if(state == State.MAIN_MENU) state = State.GAME; else if(state == State.GAME) state = State.INTRO; } } |
Conclusion
This concludes the tutorial for game states. As always, should you have any questions or remarks, put them in the comments section below.
Credits
This article was written with the help of Jakob Stein Sturlunson.
http://thecodinguniverse.com/lwjgl-frame-rate-independent-movement/
I did not search your whole website; had i done so i would have seen the answer was just multiplying by delta time.
Kind Regards,
JV.
How did you resolve the issue below;
while updating my display i am also polling for input. This means if i am running at 100FPS i will poll input 100 times; in turn this results in movement x+=1 100times, or turns my light on and off 100 times.
loop:
while(!Display.isCloseRequested()){
//Delta Time
int delta = new GameTime().getDelta(lastFrame);
//Input Handling
handler.Handler();
}
and method to folow:
public void increaseRotationSpeed(){
rotationSpeed += 0.02f;
}
This is most likely the wrong way to go about it, i was thinking about using a thread and polling myself.
How would you resolve this? Can you provide some pseudocode ?
The amount of movement should depend on the delta. If the frame-rate is high, then the movement per update should be less, and vice versa. Pseudo-code:
int delta = ...;
float movement = unprocessedMovement * delta;
I remember when I was doing 2d graphic programming for a cute little RPG engine and found out about this on my own as a way to stop the character from being able to move while an in-game dialogue box was up. Now that I see a similar method to the one I used has been posted here I don’t feel like such a failure.
private void loop() {
while (!Display.isCloseRequested()) {
clearGL();
input();
logic(state);
render();
Display.sync(fps);
Display.update();
}
}
Thanks for you’re tutorials!
You’re welcome, Peter!
Great tutorial ! But with one mistake :
————————————————————————————
while (Keyboard.next()) {
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
if(state == State.INTRO)
state = State.MAIN_MENU;
if(state == State.MAIN_MENU)
state = State.GAME;
if(state == State.GAME)
state = State.INTRO;
}
}
—————————— should be —————————————-
while (Keyboard.next()) {
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
if(state == State.INTRO)
state = State.MAIN_MENU;
else if(state == State.MAIN_MENU)
state = State.GAME;
else if(state == State.GAME)
state = State.INTRO;
}
}
————————————————————————————
Thanks again for those tutorials !
Thanks. I have edited it!
You put the lime in the cooucnt and drink the article up.