Wiki Duke
« League of Dukes » JGO Visitor
|
 |
«
Posted
2012-07-20 06:18:35 » |
|
Note: you are watching revision 15 of this wiki entry.
(
view plain diff
)
Hello,
To celebrate the start of the new wiki we will try to create a gameloop together as a community. Since this is a bit of an easy task and there are many gameloop around I will ask to not copy paste any existing gameloop. Instead, to get a wiki feel to that experiment, each person contributing can only add, remove or modify a few lines.
Good luck community!
[code]
public class GameLoopExperiment implements Runnable {
private boolean running;
private final int framerate = 60;
private final long frameIntervalNanos = 1_000_000_000L / framerate;
public void logic(int deltaTime) {
for(int index = size; --index >= 0;)
{
gameObjectArray[index].processTick(deltaTime);
}
}
public void render() {
for(int index = size; --index >= 0;)
{
gameObjectArray[index].draw();
}
}
public boolean isRunning() {
return running;
}
public void run() {
long lastFrameNanos;
while ( this.isRunning() ) {
long now = System.nanoTime();
long deltaTime = now - lastFrameNanos;
lastFrameNanos = now;
logic(deltaTime);
render();
lastFrameNanos = getNanoTime(); // TODO: Implement.\n }
}
}
[/code]
Hello, To celebrate the start of the new wiki we will try to create a gameloop together as a community. Since this is a bit of an easy task and there are many gameloop around I will ask to not copy paste any existing gameloop. Instead, to get a wiki feel to that experiment, each person contributing can only add, remove or modify a few lines. Good luck community! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class GameLoopExperiment implements Runnable {
private boolean running; private final int framerate = 60; private final long frameIntervalNanos = 1_000_000_000L / framerate;
public void logic(int deltaTime) { for(int index = size; --index >= 0;) { gameObjectArray[index].processTick(deltaTime); } }
public void render() { for(int index = size; --index >= 0;) { gameObjectArray[index].draw(); } }
public boolean isRunning() { return running; }
public void run() { long lastFrameNanos; while ( this.isRunning() ) { long now = System.nanoTime(); long deltaTime = now - lastFrameNanos; lastFrameNanos = now; logic(deltaTime); render(); } } } |
This wiki entry has had 18 revisions with contributions from 10 members.
( more info)
|
|
|
|
|
dishmoth
|
 |
«
Reply #1 - Posted
2012-07-20 12:05:29 » |
|
To celebrate the start of the new wiki we will try to create a gameloop together as a community. Just to be pedantic, I take it we're talking about a fixed frame-rate loop (no 'deltaTime' passed to the logic() function) in plain Java (Java2D). For ultimate newb-friendliness, how about a demo applet/application that uses the loop so everyone can see how lovely and smooth the final result is?
|
|
|
|
Riven
|
 |
«
Reply #2 - Posted
2012-07-20 12:09:22 » |
|
That's way beyond the scope of this wiki entry. It's pretty much doomed, hence the 'experiment' !
I'm very interested to see what cruft we can create, as a community.
Serious attempts at making a game loop probably should have their own articles.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
dishmoth
|
 |
«
Reply #3 - Posted
2012-07-20 12:33:40 » |
|
In that case I'm surprised you're so unambitious. We should be using this thread to build a MMORPG!
|
|
|
|
matheus23
|
 |
«
Reply #4 - Posted
2012-07-20 14:02:51 » |
|
|
|
|
|
sproingie
|
 |
«
Reply #5 - Posted
2012-07-20 17:46:25 » |
|
Moving lastFrameNanos into a local? That's going to make it interesting to calculate the delta between run() calls. It's already gotten schizophrenic.
(EDIT: oh i'm a dip. Runnable. As in run doesn't return. The loop made that much obvious).
|
|
|
|
|
Riven
|
 |
«
Reply #6 - Posted
2012-07-20 21:13:14 » |
|
Why call it more than once? Minimalism ftw.
|
|
|
|
matheus23
|
 |
«
Reply #7 - Posted
2012-07-20 21:16:07 » |
|
I can't change anything. It just gives me "Loading..." all the time  EDIT: only when using quick edit.
|
|
|
|
Riven
|
 |
«
Reply #8 - Posted
2012-07-20 21:18:04 » |
|
I can't change anything. It just gives me "Loading..." all the time  Hm... maybe it's [quick edit] that's broken for wiki entries. Just click [modify] instead, it should work.
|
|
|
|
erikd
|
 |
«
Reply #9 - Posted
2012-07-20 21:19:29 » |
|
Just one line? I'd like to add a constructor with the framerate as parameter. EDIT: And what if you want to rename something? That usually requires more than one line to edit. Because obviously 'framerate' should be called 'targetFrameRate' 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
matheus23
|
 |
«
Reply #10 - Posted
2012-07-20 21:21:54 » |
|
I can't change anything. It just gives me "Loading..." all the time  Hm... maybe it's [quick edit] that's broken for wiki entries. Just click [modify] instead, it should work. Full-quoting the previous message typically pisses me off (people, this is not email!) so your chances are pretty much ruined  don't get me wrong... joke 
|
|
|
|
Riven
|
 |
«
Reply #11 - Posted
2012-07-21 00:47:30 » |
|
Just one line?
It's a wiki, I just changed the rules! 
|
|
|
|
Mads
|
 |
«
Reply #12 - Posted
2012-07-21 09:52:48 » |
|
We're going back to those entity systems now? The for-loops are odd, too. I'm on a tablet - wish I could participate.
|
|
|
|
matheus23
|
 |
«
Reply #13 - Posted
2012-07-21 11:03:31 » |
|
Why don't say only logical 1-lines okey? So you only are allowed to write 1 ";" per edit/person?
|
|
|
|
sproingie
|
 |
«
Reply #14 - Posted
2012-07-21 17:21:20 » |
|
We're going back to those entity systems now? The for-loops are odd, too. I'm on a tablet - wish I could participate.
An array named "entities" doesn't automatically mean "entity system". Ultimately you have a list of things to draw and loop over it. Maybe when I feel like my "turn" comes around again, I'll abstract it, god knows I'll replace the C-style loop. Unless of course someone beats me to it ... Why don't say only logical 1-lines okey? So you only are allowed to write 1 ";" per edit/person?
If you notice the current rules, it's a "few" lines now.
|
|
|
|
|
Mads
|
 |
«
Reply #15 - Posted
2012-07-21 18:12:51 » |
|
An array named "entities" doesn't automatically mean "entity system". Ultimately you have a list of things to draw and loop over it. Isn't that going into game logic, and beyond the scope of the gameloop? I agree the C-style loop is weird for iterations. Last time I looked it was invalid code though, which is... worse.
|
|
|
|
sproingie
|
 |
«
Reply #16 - Posted
2012-07-21 18:16:28 » |
|
It's just to give you the idea. I wouldn't take it seriously. Maybe we can write an actual runnable program for an implementation (bunch of bouncing balls maybe).
|
|
|
|
|
Mads
|
 |
«
Reply #17 - Posted
2012-07-21 18:27:18 » |
|
It's just to give you the idea. I wouldn't take it seriously. Maybe we can write an actual runnable program for an implementation (bunch of bouncing balls maybe).
I wouldn't be against it. We can make a smaller nested class to hold position, radius, color, and update and render methods.
|
|
|
|
Gudradain
|
 |
«
Reply #18 - Posted
2012-07-21 18:50:39 » |
|
I love the ball idea  btw we are still missing the thread.sleep() to follow the framerate
|
|
|
|
|
Icecore
|
 |
«
Reply #19 - Posted
2012-07-31 22:51:06 » |
|
I love the ball idea  btw we are still missing the thread.sleep() to follow the framerate I call it Syncronize =) Hi work not perfect(sleep not right on big numbers like fps 230), but I use him  Also you can change “Sleep number” and see result  And don’t set to low sleep time for thread, it may produce lags and eat performance ^^ http://pastebin.java-gaming.org/82699379718Also I have this mini class for debug purposes(and now only  ), sometimes tired write System… http://pastebin.java-gaming.org/e0826199917
|
|
|
|
|
Ultroman
|
 |
«
Reply #20 - Posted
2012-07-31 23:02:04 » |
|
|
- Jonas
|
|
|
erikd
|
 |
«
Reply #21 - Posted
2012-08-14 20:14:34 » |
|
btw we are still missing the thread.sleep() to follow the framerate
In my game loop code, the throttling mechanic is simply an 'entity' and not part of the main loop. To me it makes sense to implement that outside of the main loop as that gives you the opportunity to swap out different throttling strategies, even in runtime. For example, cut-scenes may require a hard-vsync'ed 30FPS, while during gameplay you might prefer to target 60fps and allow some screen tearing if things get hectic. (sorry for necro-ing an old thread)
|
|
|
|
|