Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
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  
  Game loop help  (Read 557 times)
0 Members and 1 Guest are viewing this topic.
Offline HoWseR

Junior Newbie





« Posted 2013-05-15 19:48:23 »

Hey everyone, I've just gotten started with java game development and i though that I'd create a simple space invaders clone to start off with. I've created an update and render loop and managed to render a sprite to the screen.

While i was first creating the game loop it updated 60 times per second as i was expecting and i was getting a bunch of fps. But now when I've started adding stuff to the render function of my game it only updates at around 55-59 times a second.

Sure this isn't huge performance hit but i would like to know what causes this.

So if anyone feels like looking at the code here it is:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
       int frames = 0;
      int ticks = 0;
      double targetTickTime = 1000000000 / 60;
      double lastUpdateTime = System.nanoTime();
      double lastFPSPrintTime = System.nanoTime();

      while (running) {
         double nowTime = System.nanoTime();
         if (nowTime - lastUpdateTime >= targetTickTime) {
            ticks++;
            tick();
            lastUpdateTime = System.nanoTime();
         }
         if (nowTime - lastFPSPrintTime >= 1000000000) {
            System.out.println("Updates: " + ticks + ", Frames: " + frames);
            ticks = 0;
            frames = 0;
            lastFPSPrintTime = System.nanoTime();
         }
         render();
         frames++;
      }

<(  Azn  )> HUGGIES
Offline Z-Man
« Reply #1 - Posted 2013-05-15 20:20:59 »

Well, you're currently rendering as fast as you possibly can. I would recommended rendering 60 times a second as well, since rendering is usually pretty costly. I would also suggest using Thread.sleep() to eat up the time between updates instead of just letting the loop run and eat up CPU cycles. To see what I'm talking about, have a look at this article on game loops, especially the fixed timestep loop that Yields/Sleeps for the left over time.
Offline HoWseR

Junior Newbie





« Reply #2 - Posted 2013-05-15 20:32:29 »

Well, you're currently rendering as fast as you possibly can. I would recommended rendering 60 times a second as well, since rendering is usually pretty costly. I would also suggest using Thread.sleep() to eat up the time between updates instead of just letting the loop run and eat up CPU cycles. To see what I'm talking about, have a look at this article on game loops, especially the fixed timestep loop that Yields/Sleeps for the left over time.

When you say I should render 60 times per frame as well, do you mean I should just call tick() and then render()? or is there a better way of doing it?

<(  Azn  )> HUGGIES
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jhffvcc

Senior Newbie







« Reply #3 - Posted 2013-05-15 23:10:03 »

That's the way I do it
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  
               long lastTime = System.nanoTime();
          double nsPerTick = 1000000000D / 60D; //~60fps
     
      int ups = 0;
      int fps = 0;
     
      long lastTimer = System.currentTimeMillis();
      double delta = 0;
     
      while(game loop not quit blub) {
         long now = System.nanoTime();
         delta += (now - lastTime) / nsPerTick;
         lastTime = now;
         boolean renderOK = false;
         while(delta >= 1) {
            ups++;
            update();
            delta -= 1;
            renderOK = true;
         }
         if(shouldRender) {
            fps++;
            render();
         }
         if(System.currentTimeMillis() - lastTimer >= 1000) {
            lastTimer += 1000;
            System.out.println("frames: " + fps + " | updates: " + ups);
            fps = 0;
            ups = 0;
         }
      }

constantly 60 fps.
Offline Z-Man
« Reply #4 - Posted 2013-05-16 00:39:05 »

When you say I should render 60 times per frame as well, do you mean I should just call tick() and then render()? or is there a better way of doing it?
Yes. If you looked at the two best examples from the article I linked to you updating and rendering are both done before the time wasting (or waiting for the next cycle) is. The article gets a little more advanced and compensates for running slow by "dropping" frames. It does this by updating without rendering until it catches up, and then it renders.
Offline HoWseR

Junior Newbie





« Reply #5 - Posted 2013-05-16 06:43:45 »

Ok i got it working now, thanks for the helpful responses =)

<(  Azn  )> HUGGIES
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.

toopeicgaming1999 (16 views)
2014-11-26 15:22:04

toopeicgaming1999 (15 views)
2014-11-26 15:20:36

toopeicgaming1999 (7 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (24 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!