Implemented input handling
This commit is contained in:
parent
8ce2320da1
commit
ffd9ed775c
9 changed files with 148 additions and 7 deletions
|
@ -1,2 +1,3 @@
|
|||
# pixels
|
||||
|
||||
x, y - 2D Game engine in the works...
|
Binary file not shown.
BIN
pixels/bin/bz/bronze/pixels/Input.class
Normal file
BIN
pixels/bin/bz/bronze/pixels/Input.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,15 +1,20 @@
|
|||
package bz.bronze.pixels;
|
||||
|
||||
public class Gameloop implements Runnable {
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
public class Gameloop implements Runnable {
|
||||
private Thread thread;
|
||||
private Renderer renderer;
|
||||
private Input input;
|
||||
|
||||
private boolean running = false;
|
||||
private final double TPS = 1.0/60.0;
|
||||
|
||||
private String metrics;
|
||||
|
||||
public void start() {
|
||||
renderer = new Renderer();
|
||||
input = new Input();
|
||||
|
||||
thread = new Thread(this);
|
||||
thread.run();
|
||||
|
@ -45,20 +50,24 @@ public class Gameloop implements Runnable {
|
|||
|
||||
while (unprocessedTime >= TPS) {
|
||||
unprocessedTime -= TPS;
|
||||
|
||||
render = true;
|
||||
|
||||
//TODO: Gameloop
|
||||
metrics = "TPS: " + tps + " | " + "Mouse X: " + input.mouseX + " | " + "Mouse Y: " + input.mouseY + " | " + "Scroll: " + input.scroll;
|
||||
Window.window.setTitle(Window.title + " | " + metrics);
|
||||
|
||||
input.update();
|
||||
|
||||
if (tickTime >= 1.0) {
|
||||
tickTime = 0;
|
||||
tps = ticks;
|
||||
ticks = 0;
|
||||
}
|
||||
|
||||
System.out.println("TPS: " + tps);
|
||||
}
|
||||
|
||||
if (render) {
|
||||
renderer.clear();
|
||||
renderer.test();
|
||||
//renderer.clear();
|
||||
|
||||
Window.update();
|
||||
ticks++;
|
||||
|
|
125
pixels/src/bz/bronze/pixels/Input.java
Normal file
125
pixels/src/bz/bronze/pixels/Input.java
Normal file
|
@ -0,0 +1,125 @@
|
|||
package bz.bronze.pixels;
|
||||
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.awt.event.MouseMotionListener;
|
||||
import java.awt.event.MouseWheelEvent;
|
||||
import java.awt.event.MouseWheelListener;
|
||||
|
||||
public class Input implements KeyListener, MouseListener, MouseMotionListener, MouseWheelListener {
|
||||
private final int NUM_KEYS = 256;
|
||||
private boolean[] keys = new boolean[NUM_KEYS];
|
||||
private boolean[] keysLast = new boolean[NUM_KEYS]; // Keys on last frame
|
||||
|
||||
private final int NUM_BUTTONS = 5;
|
||||
private boolean[] buttons = new boolean[NUM_BUTTONS];
|
||||
private boolean[] buttonsLast = new boolean[NUM_BUTTONS]; // Buttons on last frame
|
||||
|
||||
public int mouseX, mouseY, scroll;
|
||||
|
||||
public Input() {
|
||||
mouseX = 0;
|
||||
mouseY = 0;
|
||||
scroll = 0;
|
||||
|
||||
Window.viewport.addKeyListener(this);
|
||||
Window.viewport.addMouseListener(this);
|
||||
Window.viewport.addMouseMotionListener(this);
|
||||
Window.viewport.addMouseWheelListener(this);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
scroll = 0;
|
||||
|
||||
for (int i = 0; i < NUM_KEYS; i++) {
|
||||
keysLast[i] = keys[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_BUTTONS; i++) {
|
||||
buttonsLast[i] = buttons[i];
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isKey(int keyCode) {
|
||||
return keys[keyCode];
|
||||
}
|
||||
|
||||
public boolean isKeyUp(int keyCode) {
|
||||
return !keys[keyCode] && keysLast[keyCode];
|
||||
}
|
||||
|
||||
public boolean isKeyDown(int keyCode) {
|
||||
return keys[keyCode] && !keysLast[keyCode];
|
||||
}
|
||||
|
||||
public boolean isButton(int button) {
|
||||
return buttons[button];
|
||||
}
|
||||
|
||||
public boolean isButtonUp(int button) {
|
||||
return !buttons[button] && buttonsLast[button];
|
||||
}
|
||||
|
||||
public boolean isButtonDown(int button) {
|
||||
return buttons[button] && !buttonsLast[button];
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||
scroll = e.getWheelRotation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
mouseX = e.getX() / Window.scale;
|
||||
mouseY = e.getY() / Window.scale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
mouseX = e.getX() / Window.scale;
|
||||
mouseY = e.getY() / Window.scale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
buttons[e.getButton()] = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
buttons[e.getButton()] = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
keys[e.getKeyCode()] = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
keys[e.getKeyCode()] = false;
|
||||
}
|
||||
}
|
|
@ -12,9 +12,15 @@ public class Renderer {
|
|||
p = ((DataBufferInt)Window.pixels.getRaster().getDataBuffer()).getData();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
public void test() {
|
||||
for (int i = 0; i < p.length; i++) {
|
||||
p[i] += i;
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
for (int i = 0; i < p.length; i++) {
|
||||
p[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -15,7 +15,7 @@ public class Window {
|
|||
static int width = 800;
|
||||
static int height = 600;
|
||||
static int scale = 1;
|
||||
static String title = "pixels | v0.01";
|
||||
static String title = "pixels | v0.02";
|
||||
|
||||
static BufferedImage pixels = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
static Canvas viewport = new Canvas();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue