Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  20 FPS Rendering/Gameloop  (Read 2308 times)
0 Members and 1 Guest are viewing this topic.
Offline Chandler_01

Junior Newbie





« Posted 2012-09-10 02:52:28 »

I am really lost as to why my code runs at only 20FPS on an Ivy Bridge Core i7...

If anyone could help, it'd be nice. Huh

I have to create a game for my class. It's not necessarily a homework question. It'll work. Just VERY slow.
Offline Buzzyboy
« Reply #1 - Posted 2012-09-10 02:56:23 »

If you gave us more information, we could help you.

Offline Vladiedoo
« Reply #2 - Posted 2012-09-10 02:58:04 »

Buzzyboy beat me to it, if you could post some of your code it'd be much easier for us to help, currently you've informed us that you're using Java2D and that your code runs slowly on a fast CPU, that's not too much to work off of. To post some code type [code.][./code] and insert your code in between, but without those two periods in there.

Edit: If you're new to JGO, welcome  Grin!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Chandler_01

Junior Newbie





« Reply #3 - Posted 2012-09-10 03:32:23 »

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  
package me.astrodev.hoodoo;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.*;
import java.util.*;

import javax.swing.*;


import me.astrodev.hoodoo.gui.Button;
import me.astrodev.hoodoo.gui.ButtonListener;

public class HoodooComponent extends Canvas implements Runnable, MouseMotionListener, MouseListener, KeyListener, ButtonListener{

   private static final long serialVersionUID = 1L;
   public static final int GAME_WIDTH = 512;
   public static final int GAME_HEIGHT = GAME_WIDTH * 3 / 4;
   public static final int SCALE = 2;

   public int fps = 0;;

   private boolean mouseMoved = false;
   private boolean mouseHidden = false;
   private int mouseHideTime = 0;
   public int tickNumber = 0;

   public Keys keys = new Keys();

   public static boolean running = false;
   private Cursor emptyCursor;

   public HoodooComponent() {
      this.setPreferredSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
      this.setMinimumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
      this.setMaximumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
      this.addKeyListener(new InputHandler(keys));
   }

   public void start() {
      running = true;
      Thread thread = new Thread(this);
      thread.setPriority(Thread.MAX_PRIORITY);
      thread.start();
   }

   public static void main(String[] args) {
      HoodooComponent hc = new HoodooComponent();
      JFrame frame = new JFrame();
      JPanel panel = new JPanel(new BorderLayout());
      panel.add(hc);
      frame.setContentPane(panel);
      frame.pack();
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      hc.start();
   }

   public void run() {
      long lastTime = System.nanoTime();
      double unprocessed = 0;
      int frames = 0;
      long lastTimer1 = System.currentTimeMillis();

      int toTick = 0;

      long lastRenderTime = System.nanoTime();
      int min = 999999999;
      int max = 0;

      while (running) {
         if (!this.hasFocus()) {
            keys.release();
         }

         double nsPerTick = 1000000000.0 / 60;
         boolean shouldRender = false;
         while (unprocessed >= 1) {
            toTick++;
            unprocessed -= 1;
         }

         int tickCount = toTick;
         if (toTick > 0 && toTick < 3) {
            tickCount = 1;
         }
         if (toTick > 20) {
            toTick = 20;
         }

         for (int i = 0; i < tickCount; i++) {
            toTick--;
            tick();
            shouldRender = true;
         }
         //            shouldRender = true;

         BufferStrategy bs = getBufferStrategy();
         if (bs == null) {
            createBufferStrategy(3);
            continue;
         }
         if (shouldRender) {
            frames++;
            Graphics g = bs.getDrawGraphics();

            render(g);

            long renderTime = System.nanoTime();
            int timePassed = (int) (renderTime - lastRenderTime);
            if (timePassed < min) {
               min = timePassed;
            }
            if (timePassed > max) {
               max = timePassed;
            }
            lastRenderTime = renderTime;
         }

         long now = System.nanoTime();
         unprocessed += (now - lastTime) / nsPerTick;
         lastTime = now;

         try {
            Thread.sleep(1);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }

         if (shouldRender) {
            if (bs != null) {
               bs.show();
            }
         }

         if (System.currentTimeMillis() - lastTimer1 > 1000) {
            lastTimer1 += 1000;
            fps = frames;
            frames = 0;
         }
      }
   }


   private void render(Graphics g) {
      g.setColor(Color.BLACK);
      g.fillRect(0, 0, getWidth(), getHeight());
   }

   private void tick() {

   }

   private void init() {
      try {
         emptyCursor = Toolkit.getDefaultToolkit().createCustomCursor(new BufferedImage(16,16, BufferedImage.TYPE_INT_ARGB), new Point(0,0), "empty");
      } catch (RuntimeException e) {
         e.printStackTrace();
      }
      setFocusTraversalKeysEnabled(false);
      requestFocus();
   }

   @Override
   public void keyPressed(KeyEvent e) {
      // TODO Auto-generated method stub

   }

   @Override
   public void keyReleased(KeyEvent e) {
      // TODO Auto-generated method stub

   }

   @Override
   public void keyTyped(KeyEvent e) {
      // TODO Auto-generated method stub

   }

   @Override
   public void mouseClicked(MouseEvent e) {
   }

   @Override
   public void mouseEntered(MouseEvent e) {
      // TODO Auto-generated method stub

   }

   @Override
   public void mouseExited(MouseEvent e) {
      // TODO Auto-generated method stub

   }

   @Override
   public void mousePressed(MouseEvent e) {
      // TODO Auto-generated method stub

   }

   @Override
   public void mouseReleased(MouseEvent e) {
      // TODO Auto-generated method stub
   }

   @Override
   public void mouseDragged(MouseEvent e) {
      mouseMoved = true;
   }

   @Override
   public void mouseMoved(MouseEvent e) {
      mouseMoved = true;
   }

   @Override
   public void buttonPressed(Button button) {

   }

}


Here it is.. I hope you can help
Offline Vladiedoo
« Reply #4 - Posted 2012-09-10 04:09:02 »

Unfortunately I have to focus on finishing my homework and then going to sleep right now. If I get back home tomorrow and the problem isn't solved I'll make sure to give your code a good look over. Until then you can read about game loops here: http://www.java-gaming.org/topics/game-loops/24220/view.html

Your code runs at 60fps for me (I use a i5), I had to remove a few lines that were giving me errors though, here is everything I deleted
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
// lines 13 and 14
import me.astrodev.hoodoo.gui.Button;
import me.astrodev.hoodoo.gui.ButtonListener;

// line 16 I got rid of the ButtonListener

// line 30
public Keys keys = new Keys();

// line 39
this.addKeyListener(new InputHandler(keys));

// line 77
keys.release();

// lines 223-226
@Override
public void buttonPressed(Button button) {

}


You can add the following lines of code to your render method to see the fps (I may be wrong, I added them and saw 60 (I also ran a different program, Fraps, that confirmed it running at 60, so I assume the following lines are accurate)
1  
2  
3  
g.setColor(Color.YELLOW);
g.drawString(Integer.toString(fps), 100, 100);
g.setColor(Color.BLACK);


EDIT: I'm also still learning Java so I feel I won't be able to accurately state your problem, the two top things things would be to check out the link I provided and check out those lines I deleted since I was able to run your code smoothly.
Offline aazimon
« Reply #5 - Posted 2012-09-13 19:33:15 »

I took your code and ran it. I got 60 - 70 frames per second. How are you calculating your frames per second?
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.

rwatson462 (38 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

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

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

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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