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. ???
I have to create a game for my class. It’s not necessarily a homework question. It’ll work. Just VERY slow.
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. ???
I have to create a game for my class. It’s not necessarily a homework question. It’ll work. Just VERY slow.
If you gave us more information, we could help you.
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 ;D!
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
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
// 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)
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.
I took your code and ran it. I got 60 - 70 frames per second. How are you calculating your frames per second?