Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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 eating 100% cpu  (Read 766 times)
0 Members and 1 Guest are viewing this topic.
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Posted 2013-01-04 02:21:31 »

This is the base game loop I'm using for a game for J4K 2013. But it is eating 100% of my CPU. I'm running on Ubuntu 32 bit and Java 6. Any tips how can I avoid eating 100% of CPU? Thanks!


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  
      int FPS = 60;
      int MILI_FRAME_LENGTH = 1000 / FPS;
      int frameCount = 0;

      while (true) {
         long startTime = System.currentTimeMillis();

         long lastLoopTime = System.currentTimeMillis();
         long lastFpsTime = 0;
         long fps = 0;
         while (true) {
            long delta = System.currentTimeMillis() - lastLoopTime;
            lastLoopTime = System.currentTimeMillis();
            lastFpsTime += delta;
            fps++;

            // If more than 1000ms (1s) has passed reset the fps count.
            if (lastFpsTime >= 1000) {
               lastFpsTime = 0;
               fps = 0;
            }

            logic(delta);
            render(g);

            frameCount++;
            while ((System.currentTimeMillis() - startTime)
                  / MILI_FRAME_LENGTH < frameCount) {
               Thread.yield();
            }

            // Ensures that the display is up-to-date. This keeps the
            // animation smoother for the Linux OS.
            Toolkit.getDefaultToolkit().sync();
         }
      }
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #1 - Posted 2013-01-04 02:29:49 »

Thread.yield only temporarily pauses the the thread and allows others to run. Since you don't have another thread, it has no effect.
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html

Smiley
Online HeroesGraveDev

JGO Kernel


Medals: 269
Projects: 11
Exp: 2 years


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


« Reply #2 - Posted 2013-01-04 02:56:57 »

Looks like you need to learn about game loops...

Here you go!

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

JGO Coder


Projects: 6


Java games rock!


« Reply #3 - Posted 2013-01-04 11:56:12 »

Thanks, I took this game loop from an older entry of the 4k competition but maybe I have modified it and removed the sleep statement. Thanks for pointing it out. Now my cpu rate is ok and my game still smooth.
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.

theagentd (20 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (46 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (69 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
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!