Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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 taking around 30% of CPU  (Read 2070 times)
0 Members and 1 Guest are viewing this topic.
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Posted 2013-07-17 23:57:37 »

Hi, I was looking at the game loop tutorial and put it in a simple tile map based demo. I noticed that it consumes around 30% of the CPU. Is this value expected? Minecraft consumes exactly the same in my machine, but the game is much heavier. Just wondering if I could optimize my cpu consuption. Here is the code if anyone want to try, can run in window mode or applet.

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  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;

public class TileMap1 extends Applet implements Runnable {
   private static final int SCREEN_WIDTH = 8 * 32;
   private static final int SCREEN_HEIGHT = 7 * 32;
   private boolean isApplet;
   private Thread gameThread;
   private int fps;
   private BufferedImage tiles;
   private Image offImage;
   private int[][] myMap = { { 1, 1, 1, 1, 1, 1, 1, 1 }, //
        { 1, 0, 0, 0, 0, 0, 0, 1 }, //
        { 1, 0, 1, 0, 0, 0, 0, 1 }, //
        { 1, 0, 0, 0, 0, 1, 0, 1 }, //
        { 1, 0, 0, 0, 0, 0, 0, 2 }, //
        { 1, 1, 1, 1, 1, 1, 1, 1 } };

   private int[][] myMap2 = { { 1, 1, 1, 1, 1, 1, 1, 1 }, //
        { 1, 0, 0, 0, 0, 0, 0, 1 }, //
        { 1, 0, 1, 0, 0, 0, 0, 1 }, //
        { 1, 0, 0, 0, 0, 1, 0, 1 }, //
        { 3, 0, 0, 0, 0, 0, 0, 1 }, //
        { 1, 1, 1, 1, 1, 1, 1, 1 } };

   private int[][] currentMap;
   private Sprite hero;
   private boolean[] controls = new boolean[5];

   public TileMap1() {
      this.hero = new Sprite(2, 1);
      this.isApplet = true;
      this.currentMap = this.myMap;
      super.enableEvents(KeyEvent.KEY_EVENT_MASK);
   }

   public void init() {
      this.offImage = super.createImage(SCREEN_WIDTH, SCREEN_HEIGHT);
      try {
         URL url = this.getClass().getClassLoader().getResource("tiles.png");
         this.tiles = ImageIO.read(url);
      } catch (Exception e) {
         e.printStackTrace();
         System.exit(0);
      }
   }

   public void start() {
      if (this.gameThread == null) {
         this.gameThread = new Thread(this);
      }
      this.gameThread.start();
   }

   public void stop() {
      this.gameThread = null;
   }

   public void paint(Graphics g) {
      this.render(this.offImage.getGraphics());
      g.drawImage(this.offImage, 0, 0, 640, 480, null);

   }

   private void render(Graphics g) {
      g.setColor(Color.black);
      g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

      // Draws our map.
     int mapWidth = this.currentMap[0].length;
      int mapHeight = this.currentMap.length;
      for (int i = 0; i < mapWidth; i++) {
         for (int j = 0; j < mapHeight; j++) {
            int frame = this.currentMap[j][i];
            g.drawImage(this.tiles.getSubimage(frame * 32, 0, 32, 32),
                  i * 32, j * 32, null);
         }
      }

      hero.render(g);

      g.setColor(Color.red);
      g.drawString("FPS:" + this.fps, 10, 10);

   }

   public void run() {
      long lastLoopTime = System.nanoTime();
      final int TARGET_FPS = 60;
      final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
      long lastFpsTime = 0;
      int fps = 0;

      while (true) {
         long now = System.nanoTime();
         long updateLength = now - lastLoopTime;
         lastLoopTime = now;
         double delta = updateLength / ((double) OPTIMAL_TIME);

         // update the frame counter
        lastFpsTime += updateLength;
         fps++;
         // System.out.println(lastFpsTime);
        // update our FPS counter if a second has passed since
        // we last recorded
        if (lastFpsTime >= 1000000000) {
            // System.out.println("(FPS: " + fps + ")");
           this.fps = fps;
            lastFpsTime = 0;
            fps = 0;
         }

         this.logic();
         this.paint(this.getGraphics());
         Toolkit.getDefaultToolkit().sync();

         try {
            Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
         } catch (Exception e) {
         }
      }
   }

   private void checkDoor(int j, int i) {
      if (this.currentMap[j][i] == 2) {
         this.currentMap = myMap2;
         hero.x = 1 * 32 + 6;
      } else if (this.currentMap[j][i] == 3) {
         this.currentMap = myMap;
         hero.x = 6 * 32 + 6;
      }
   }

   private void logic() {
      int dx = 0;
      int dy = 0;
      if (this.controls[0]) {
         dx = -1;
      } else if (this.controls[1]) {
         dx = 1;
      } else if (this.controls[2]) {
         dy = -1;
      } else if (this.controls[3]) {
         dy = 1;
      }
      hero.move(dx, dy);
   }

   protected void processKeyEvent(KeyEvent e) {
      int[] keys = new int[] { KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,
            KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_SPACE };
      for (int i = 0; i < keys.length; i++) {
         if (e.getKeyCode() == keys[i]) {
            this.controls[i] = e.getID() == KeyEvent.KEY_PRESSED;
         }
      }
      if (e.getKeyCode() == KeyEvent.VK_ESCAPE && !this.isApplet) {
         System.exit(0);
      }
   }

   public void buildFrame() {
      Frame f = new Frame("Tile Based Maps Tutorial");
      f.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
      f.setSize(640, 480);
      f.setResizable(false);
      Dimension dimension = getToolkit().getScreenSize();
      Rectangle rectangle = f.getBounds();
      f.setLocation((dimension.width - rectangle.width) / 2,
            (dimension.height - rectangle.height) / 2);
      f.add(this);
      f.setVisible(true);
      this.isApplet = false;
      this.init();
      this.start();
   }

   public static void main(String[] args) {
      TileMap1 g = new TileMap1();
      g.buildFrame();
   }

   class Sprite {
      int x;
      int y;

      int dx;
      int dy;

      Sprite(int px, int py) {
         x = px * 32 + 6;
         y = py * 32 + 6;
      }

      void move(int pDx, int pDy) {
         dx = pDx;
         dy = pDy;

         if (pDy != 0) {
            if (canMove(x, y + dy)) {
               checkDoor((int) Math.floor(y / 32),
                     (int) Math.floor(x / 32));
               y += dy;
            }
         }

         if (pDx != 0) {
            if (canMove(x + dx, y)) {
               checkDoor((int) Math.floor(y / 32),
                     (int) Math.floor(x / 32));
               x += dx;
            }
         }
      }

      boolean canMove(int x, int y) {
         int downY = (int) Math.floor((y + 20) / 32);
         int upY = (int) Math.floor(y / 32);
         int leftX = (int) Math.floor(x / 32);
         int rightX = (int) Math.floor((x + 20) / 32);

         if (dy == -1) {
            return (!isSolid(upY, leftX) && !isSolid(upY, rightX));
         } else if (dy == 1) {
            return (!isSolid(downY, leftX) && !isSolid(downY, rightX));
         }

         if (dx == -1) {
            return (!isSolid(downY, leftX) && !isSolid(upY, leftX));
         } else if (dx == 1) {
            return (!isSolid(upY, rightX) && !isSolid(downY, rightX));
         }

         return false;
      }

      private boolean isSolid(int j, int i) {
         return (currentMap[j][i] == 1);
      }

      void render(Graphics g) {
         // Body
        g.setColor(Color.YELLOW);
         g.fillOval(x, y, 20, 20);
         g.setColor(Color.BLACK);
         g.drawOval(x, y, 20, 20);

         // Eyes
        g.setColor(Color.WHITE);
         g.fillOval(x + 2, y + 5, 8, 10);
         g.fillOval(x + 10, y + 5, 8, 10);
         g.setColor(Color.BLACK);
         g.drawOval(x + 2, y + 5, 8, 10);
         g.drawOval(x + 10, y + 5, 8, 10);
         g.fillOval(x + 4 + (dx * 2), y + 8 + (dy * 2), 4, 4);
         g.fillOval(x + 12 + (dx * 2), y + 8 + (dy * 2), 4, 4);
      }
   }
}
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #1 - Posted 2013-07-19 00:59:03 »

Sleep time is relative, but the key to not absorbing a lot of CPU time is to increase the time that it sleeps. It totally depends on the computer, but the more time in general you allow your program to sleep, the lower the CPU usage is going to be. (Besides, you have to leave time to allow your computer to do other processes.)

Another, slightly less problem for CPU time, is graphics. Usually, you want to make as little graphic calls as possible. (This is why in OpenGL they are so insistent over VBO's and VAO's.) Reducing the calls made to draw things onto the screen will also improve CPU time.

Lastly, try to stay away from excessive looping whenever possible. Recursive loops can creep up on CPU time pretty quickly. These are usually minor and only become a problem when used in excess.

Those are the 3 major culprits when it comes to gaming that I see really take a lot out of the CPU. As long as you aren't designing your classes badly, there shouldn't be a problem.

As for your code, look for the examples within this site that lay out some pretty good techniques. Optimizing code is a trap that is possibly best avoided. Much better to try and get your game working and tweaking it if it is not performing to the standard you want it to.

Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #2 - Posted 2013-07-19 02:53:16 »

Thanks for the tips. The code for the loop I got from this site under the articles sections. But still not sure if this is the best option and also if this simple could that I posted could be optimized?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Z-Man
« Reply #3 - Posted 2013-07-19 05:07:30 »

When I was still using Java2D I slept for 1 millisecond at a time until the extra time was used up (I believe it increased the accuracy), then I moved to sleeping for 1 millisecond for 80% of the extra time and then yielding for the rest of the time. I believe I also used aspects from other game loops I've seen here on JGO (sorry if my example includes someones code, I don't have a source) Something like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
 // Disclaimer: I haven't looked at this code in a while and it likely isn't the best solution, just an example of what seemed to work for me

 while (running) {
   long start, timePassed;
   start = System.nanoTime();
   // Update until we're caught up (hopefully just once)
  for (int updates = 0; (timePassed = start - lastUpdate) > waitTime && updates < MAX_UPDATES_PER_TICK; updates++) {
      // Update here
     lastUpdate += waitTime;
   }

   // Render

   while ((timePassed = System.nanoTime() - lastUpdate) < waitTime) {
      if (timePassed <= waitTime * 0.8)
         Util.sleep(1);
      else
         Thread.yield();
   }
}


I also used changed a few lines in the Sync class that Riven wrote for LWJGL so that it worked without LWJGL and used that for a while. It seemed to work pretty well. With that you just have to do something like this:
1  
2  
3  
4  
5  
6  
// Really simple example
while (running) {
   // Update
  // Render
  Sync.sync(60) // Or whatever FPS you want
}


EDIT: I forgot to mention that I have since moved to LWJGL and now that I've adjusted to the differences, I love it. I would highly recommend LWJGL if you want to mess around with more low level stuff and do more things yourself, or LibGDx if you want some higher level functionality already implemented for you and access to the low level bits as well.
Offline trollwarrior1
« Reply #4 - Posted 2013-07-25 15:09:17 »

Minecraft uses the most performance wise rendering methods available. You're using shit java2d. You need to go learn libGDX (Probably better options for starters) or LWJGL(More advanced options)
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.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (25 views)
2014-09-07 01:12:14

Longarmx (32 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
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!