Game Loop and Keyboard inut

Hey,

I have a problem with my keyboard event dispatchment. I thought Thread.yield() would be enough to give the keyboard time to react but I thought wrong. Is it possible to give the eventhread/ eventlistener ( not sure what it’s called) more time?
Without my loop the keyboard work great.


package net.tangledcode.mtm_game.game;

import java.awt.AWTEvent;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import net.tangledcode.mtm_game.entities.Player;

public class Game {
	private static boolean[] keys = new boolean[1024];
	
	private JFrame frame;
	private Canvas canvas;
	private BufferStrategy buffer;
	private BufferedImage bi;
	
	private GraphicsEnvironment ge;
	private GraphicsDevice gd;
	private GraphicsConfiguration gc;
	
	public Game(JFrame frame) {
		this.frame = frame;
		this.frame.getContentPane().removeAll();
		this.frame.update(this.frame.getGraphics());
		this.frame.setIgnoreRepaint(true);
		
		this.canvas = new Canvas();
		this.canvas.setIgnoreRepaint(true);
		this.canvas.setBounds(0, 0, 1200, 800);

		this.frame.add(this.canvas);
		this.frame.pack();
		this.frame.setVisible(true);
		
		this.canvas.createBufferStrategy(2);
		this.buffer = this.canvas.getBufferStrategy();
		
		this.ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		this.gd = this.ge.getDefaultScreenDevice();
		this.gc = this.gd.getDefaultConfiguration();
		
		this.bi = this.gc.createCompatibleImage(1200, 800);
		
		this.canvas.requestFocus();
		Keyboard.init();
	}
	
	public void start() {
		Graphics graphics = null;
	    Graphics2D g2d = null;
	    Color background = Color.BLACK;
			
	    int fps = 0;
	    int frames = 0;
	    long totalTime = 0;
	    long timeDiff = 0;
	    long currentTime = System.currentTimeMillis();
	    long lastTime = currentTime;
	    
    	Player player = new Player("../data/images/ships/player_01.png", 600, 400);
	    
		while(true) {
			try {
				lastTime = currentTime;
		        currentTime = System.currentTimeMillis();
		        timeDiff = currentTime - lastTime;
		        totalTime += currentTime - lastTime;
				if(totalTime > 1000) {
					totalTime -= 1000;
					fps = frames;
					frames = 0;
				} 
		        ++frames;
		        
		        g2d = this.bi.createGraphics();
		        g2d.setColor(background);
		        g2d.fillRect(0, 0, 1200, 800);
		        
		        // draw Stuff here
		        player.draw(g2d);
			        
		        player.doLogic(timeDiff);
		        player.doMovement(timeDiff); // checks if keys are pressed
		        
		        g2d.setFont( new Font( "Helvetica", Font.PLAIN, 14 ) );
		        g2d.setColor( Color.GREEN );
		        g2d.drawString( String.format( "FPS: %s", fps ), 20, 20 );
				
		        graphics = this.buffer.getDrawGraphics();
		        graphics.drawImage(this.bi, 0, 0, null );
		        if(!this.buffer.contentsLost()) {
		        	this.buffer.show();
		        }
		        
			} finally {
				if(graphics != null) {
					graphics.dispose();
				}
				if(g2d != null) {
					g2d.dispose();
				}
			}
		}
	}
}



package net.tangledcode.mtm_game.game;

import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Keyboard {
	private static boolean[] keys = new boolean[1024];

	public static void init() {
		Toolkit.getDefaultToolkit().addAWTEventListener(new KeyHandler(), AWTEvent.KEY_EVENT_MASK);         
	}

	public static void init(Component comp) {
		comp.addKeyListener(new KeyHandler());         
	}

	public static boolean isPressed(int key) {
		return Keyboard.keys[key];
	}

	public static void setPressed(int key,boolean pressed) {
		Keyboard.keys[key] = pressed;
	}

	private static class KeyHandler extends KeyAdapter implements AWTEventListener {
		public void keyPressed(KeyEvent e) {
 			if (e.isConsumed()) {
				return;
			}
			Keyboard.keys[e.getKeyCode()] = true;
		}

		public void keyReleased(KeyEvent e) {
			if (e.isConsumed()) {
				return;
			}

			KeyEvent nextPress = (KeyEvent) Toolkit.getDefaultToolkit().getSystemEventQueue().peekEvent(KeyEvent.KEY_PRESSED);

			if ((nextPress == null) || (nextPress.getWhen() != e.getWhen())) {
				Keyboard.keys[e.getKeyCode()] = false;
			}

		}

		public void eventDispatched(AWTEvent e) {
			System.out.println("event dispatched");
			if (e.getID() == KeyEvent.KEY_PRESSED) {
				keyPressed((KeyEvent) e);
			}
			if (e.getID() == KeyEvent.KEY_RELEASED) {
				keyReleased((KeyEvent) e);
			}
		}
	}
}


Thread.yield() should be enough, but it doesn’t actually appear anywhere in your code.

I think all the painting should be on the Awt Event Thread, not on the main thread.

Are you sure that the “start” method is being called on the main thread, not the Awt Event Thread? It doesn’t get called in the code you posted. If the start method is called as the result of pressing a button somewhere, it’s probably on the Awt Event Thread. If it’s on the Awt Event Thread, it would prevent any other code on the Awt Event Thread from running because it runs forever once called.

Also, the keyboard handler itself sort of surprises me. Usually, keyboard handlers store whether the key WAS pressed (i.e. was released), as well as whether it IS pressed. But if this works for you, that’s fine.

I’m starting the game through a button click. So, I know now where to look now, thanks.

I haven’t thought of implementing the function to save if a key was pressed. I will have a look at that, thanks again.

your keyboard might be faster if it implemented the KeyEventDispatcher interface, thereby it can be used on some KeyBoardFocusManager. :wink: