Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help on understanding the game loop  (Read 690 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-04-14 01:54:33 »

Hi everyone,
I've been learning java for more than 2 months now, and i have managed to do some "advanced" things (simple text editor, simple calculator, simple paint application) but when it turns out to games i can almost do everything by my own except the game loop i always needs to check out some other people codes to "manage" the solution, any one can help please ?
thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline quew8

JGO Coder


Medals: 29



« Reply #1 - Posted 2013-04-14 02:10:50 »

The game loop is remarkably simple once you "get" it.
1  
2  
3  
4  
5  
6  
7  
8  
9  
initialize();
int delta = 0;
while(true) {
    delta = calculateDelta();
    update(delta);
    render();
    sync();
}
destroy();


So this is the basic structure of a simple game. So now I'll go through it step by step.
Before you enter into the game loop, you want to initialize everything. This means create your window, load your graphics, sounds, levels etc.
Then we enter into the loop which will continue to run until the user presses exit. NB, one "frame," is one iteration of this loop. The first thing you do is calculate the delta.
Delta in sciences means "change in" and in this context we mean change in time, as in the difference between the time at the start of the previous frame and the start of this frame. The reason we use this is to make sure the game runs at the same rate no matter how long individual frames take to "do." This will become clearer later on if you don't understand.
Next we "update." Call this what you want all it means is "do game logic." In this part, you calculate new positions of objects that are moving, you calculate what has hit what (aka collisions detection), you calculate who's dead and who killed them etc. This is where delta comes in. If you have a ball moving at 5 pixels per second, you need to know how many seconds have passed by to work out how many pixels it has actually moved. That's basics right? Yet it always takes a while to sink in. The last thing you have to do here is see if the player wants to exit and break the loop if so.
Then we "render". In this bit you take everything you calculated in update (who is where and how much blood is around) and draw it in your window so that the player(s) can actually see whats going on.This bit is really as complex as you need it to be and can be anything under the sun. (Sun as in Sun Microsystems)
The last bit of the loop is syncing. It is really necessary but makes sure your game doesn't consume all the cpu time and battery power. Say you want your game to run at 60 frames per second. What happens if an iteration of the loop takes less time than 1 / 60 seconds? You need to wait a little which is all the sync does. If your using LWJGL, then there is a special little function that does this for you.
And after all this you tear down your program and release any resources you may have had.

P.S. There's nothing more "advanced" than "simple" programs. (I kid, I know what you mean).
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #2 - Posted 2013-04-14 02:22:08 »

The game loop is remarkably simple once you "get" it.
1  
2  
3  
4  
5  
int delta = 0;
while(true) {
    delta = calculateDelta();

}


That is a terrible loop.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-04-14 02:33:45 »

The game loop is remarkably simple once you "get" it.
1  
2  
3  
4  
5  
int delta = 0;
while(true) {
    delta = calculateDelta();

}

That is a terrible loop.
Indeed. How is your example going to help if it uses magical methods that don't exist? Not to mention that you don't even show how to use delta.
Offline quew8

JGO Coder


Medals: 29



« Reply #4 - Posted 2013-04-14 02:37:16 »

I'm sorry, I pressed enter by mistake (actually tab and enter?) and was editing it when you posted.
Offline Z-Man
« Reply #5 - Posted 2013-04-14 02:44:37 »

quew8 was more helpful than either of you, he gave the basic structure of a game loop and described it.

I'd suggest taking a look at Eli's article on game loops to see the different kinds and some ways to implement them. I also wouldn't use while(true), it's probably best to have a boolean that you can change in order to exit the loop and then exit the game.
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #6 - Posted 2013-04-14 02:44:48 »

Fixed timestep loop (mostly taken from Eli's article):

http://pastebin.java-gaming.org/bf3455c3455

EDIT: Accidentally left Display.isCloseRequested() in there. Replace it with a running boolean if you aren't using LWJGL.

fps
is the frames per second for the last frame, in case you want an fps counter.
capped
if set to true, caps the framerate TARGET_FPS. Otherwise it maxes the framerate. It always updates at the rate of GAME_HERTZ per second (except when experiencing lag).

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #7 - Posted 2013-04-14 02:45:37 »

quew8 was more helpful than either of you, he gave the basic structure of a game loop and described it.

I'd suggest taking a look at Eli's article on game loops to see the different kinds and some ways to implement them. I also wouldn't use while(true), it's probably best to have a boolean that you can change in order to exit the loop and then exit the game.
If you read the post before you, we were responding to the unedited version of quew8's first post, which was incomplete.
Offline Z-Man
« Reply #8 - Posted 2013-04-14 02:48:12 »

So I was. My mistake.
Offline alaslipknot
« Reply #9 - Posted 2013-04-14 14:29:18 »

i'll take a look to all of them and keep you in touch in case i had a problem
thank you very much

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline alaslipknot
« Reply #10 - Posted 2013-04-14 15:41:57 »

The game loop is remarkably simple once you "get" it.
...


thanx dude
that seems very clear and funny too   Roll Eyes
thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (7 views)
2014-07-29 12:53:52

Dwinin (7 views)
2014-07-29 10:59:34

E.R. Fleming (23 views)
2014-07-29 03:07:13

E.R. Fleming (9 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!