Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  Gameloop experiment  (Read 12026 times)
0 Members and 1 Guest are viewing this topic.
Offline Wiki Duke

?





« Posted 2012-07-20 04:18:35 »

Note: you are watching revision 17 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!

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  
37  
38  
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 i=0; i<entities.size(); i++) {
             entities[i].processTick(deltaTime);
        }
    }

    public void render() {
        for(int i=0; i<entities.size(); i++) {
             entities[i].draw();
        }
    }

    public boolean isRunning() {
        return running;
    }
 
    public void exit() {
        running = false;
    }

    public void run() {
        long lastFrameNanos;
       
        while ( this.isRunning() ) {
           long now = System.nanoTime(); // TODO wrap in a method that fixes negative nanoTimes
          long deltaTime = now - lastFrameNanos;
           lastFrameNanos = now;
           logic(deltaTime);
           render();
        }
    }
}
This wiki entry has had 18 revisions with contributions from 10 members. (more info)
Offline dishmoth
« Reply #1 - Posted 2012-07-20 10:05:29 »

Quote
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?

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2012-07-20 10: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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dishmoth
« Reply #3 - Posted 2012-07-20 10:33:40 »

In that case I'm surprised you're so unambitious.  We should be using this thread to build a MMORPG!

Online matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2012-07-20 12:02:51 »

In that case I'm surprised you're so unambitious.  We should be using this thread to build a MMORPG!

 persecutioncomplex ... [size=8pt]the new github...[/size]  persecutioncomplex  Roll Eyes  Pointing  Tongue

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline sproingie

JGO Kernel


Medals: 202



« Reply #5 - Posted 2012-07-20 15: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).

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-07-20 19:13:14 »

Why call it more than once? Minimalism ftw.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #7 - Posted 2012-07-20 19:16:07 »

I can't change anything. It just gives me "Loading..." all the time Sad
EDIT: only when using quick edit.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2012-07-20 19:18:04 »

I can't change anything. It just gives me "Loading..." all the time Sad
Hm... maybe it's [quick edit] that's broken for wiki entries. Just click [modify] instead, it should work.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #9 - Posted 2012-07-20 19: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'  Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #10 - Posted 2012-07-20 19:21:54 »

I can't change anything. It just gives me "Loading..." all the time Sad
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 Kiss
don't get me wrong... joke Cheesy

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2012-07-20 22:47:30 »

Just one line?
It's a wiki, I just changed the rules! Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #12 - Posted 2012-07-21 07: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.

Online matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #13 - Posted 2012-07-21 09:03:31 »

Why don't say only logical 1-lines okey?
So you only are allowed to write 1 ";" per edit/person?

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline sproingie

JGO Kernel


Medals: 202



« Reply #14 - Posted 2012-07-21 15: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. 

Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #15 - Posted 2012-07-21 16: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.

Offline sproingie

JGO Kernel


Medals: 202



« Reply #16 - Posted 2012-07-21 16: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).
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #17 - Posted 2012-07-21 16: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.

Offline Gudradain
« Reply #18 - Posted 2012-07-21 16:50:39 »

I love the ball idea Smiley

btw we are still missing the thread.sleep() to follow the framerate
Offline Icecore

Senior Member


Medals: 5



« Reply #19 - Posted 2012-07-31 20:51:06 »

I love the ball idea Smiley

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 Wink
Also you can change “Sleep number” and see result Wink
And don’t set to low sleep time for thread, it may produce lags and eat performance ^^
http://pastebin.java-gaming.org/82699379718

Also I have this mini class for debug purposes(and now only Wink), sometimes tired write System…
http://pastebin.java-gaming.org/e0826199917
Offline Ultroman

JGO Knight


Medals: 24
Projects: 1


Snappin' at snizzes since '83


« Reply #20 - Posted 2012-07-31 21:02:04 »

Here's a Ball  Tongue
http://pastebin.java-gaming.org/ee082069919

- Jonas
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #21 - Posted 2012-08-14 18: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)

Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

atombrot (25 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!