diff --git a/pom.xml b/pom.xml
index 302238b..f8417ae 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
io.github.nickd3000
garnet
- 0.3.0
+ 0.4.0
jar
${project.groupId}:${project.artifactId}
https://github.com/nickd3000/garnet
diff --git a/src/main/java/com/physmo/garnet/DebugDrawer.java b/src/main/java/com/physmo/garnet/DebugDrawer.java
index 845a5e7..4962843 100644
--- a/src/main/java/com/physmo/garnet/DebugDrawer.java
+++ b/src/main/java/com/physmo/garnet/DebugDrawer.java
@@ -93,7 +93,7 @@ public int drawString(Graphics g, String str, int y) {
}
public String getMouseCoordsString() {
- int[] mousePosition = input.getMousePosition();
+ int[] mousePosition = input.getMouse().getPosition();
return String.format("Mouse X:%d Y:%d", mousePosition[0], mousePosition[1]);
}
diff --git a/src/main/java/com/physmo/garnet/Garnet.java b/src/main/java/com/physmo/garnet/Garnet.java
index 4898e44..19923d9 100644
--- a/src/main/java/com/physmo/garnet/Garnet.java
+++ b/src/main/java/com/physmo/garnet/Garnet.java
@@ -9,8 +9,22 @@
import java.util.ArrayList;
import java.util.List;
-import static org.lwjgl.glfw.GLFW.*;
-import static org.lwjgl.opengl.GL11.*;
+import static org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE;
+import static org.lwjgl.glfw.GLFW.GLFW_RELEASE;
+import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
+import static org.lwjgl.glfw.GLFW.glfwPollEvents;
+import static org.lwjgl.glfw.GLFW.glfwSetKeyCallback;
+import static org.lwjgl.glfw.GLFW.glfwSetWindowShouldClose;
+import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
+import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
+import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
+import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
+import static org.lwjgl.opengl.GL11.GL_SCISSOR_TEST;
+import static org.lwjgl.opengl.GL11.glClear;
+import static org.lwjgl.opengl.GL11.glClearColor;
+import static org.lwjgl.opengl.GL11.glDisable;
+import static org.lwjgl.opengl.GL11.glEnable;
+import static org.lwjgl.opengl.GL11.glIsEnabled;
// NOTE: on MacOS we need to add a vm argument: -XstartOnFirstThread
public class Garnet {
@@ -48,11 +62,9 @@ public Graphics getGraphics() {
public void init() {
-
display.init();
sound.init();
input.init();
- input.setWindowHandle(display.getWindowHandle());
garnetApp.init(this);
debugDrawer.init();
diff --git a/src/main/java/com/physmo/garnet/drawablebatch/Sprite2D.java b/src/main/java/com/physmo/garnet/drawablebatch/Sprite2D.java
index 0f03aef..ecd9f41 100644
--- a/src/main/java/com/physmo/garnet/drawablebatch/Sprite2D.java
+++ b/src/main/java/com/physmo/garnet/drawablebatch/Sprite2D.java
@@ -166,19 +166,14 @@ public int getType() {
private void renderRotated(float textureScale) {
glPushMatrix();
- glTranslatef(x + _w, y + _h, 0);
-
- glRotatef(angle, 0f, 0f, 1.0f);
-
double z = camera.getZoom();
- // new
- float xo = (float) (camera.getWindowX() - (camera.getX() * z));
- float yo = (float) (camera.getWindowY() - (camera.getY() * z));
+ float xo = (float) (camera.getWindowX() - ((camera.getX() - x) * z));
+ float yo = (float) (camera.getWindowY() - ((camera.getY() - y) * z));
glTranslatef(xo, yo, 0);
glScalef((float) z, (float) z, 1);
- // new
+ glRotatef(angle, 0f, 0f, 1.0f);
float txs = tx * textureScaleX;
float tys = ty * textureScaleY;
diff --git a/src/main/java/com/physmo/garnet/input/Input.java b/src/main/java/com/physmo/garnet/input/Input.java
index c969d95..04e7e71 100644
--- a/src/main/java/com/physmo/garnet/input/Input.java
+++ b/src/main/java/com/physmo/garnet/input/Input.java
@@ -1,32 +1,25 @@
package com.physmo.garnet.input;
import com.physmo.garnet.Garnet;
-import com.physmo.garnet.Utils;
import java.util.ArrayList;
import java.util.List;
-import static org.lwjgl.glfw.GLFW.*;
-
+/**
+ * Manages input subsystems and action system
+ */
public class Input {
- public static final int MOUSE_BUTTON_LEFT = GLFW_MOUSE_BUTTON_LEFT;
- public static final int MOUSE_BUTTON_RIGHT = GLFW_MOUSE_BUTTON_RIGHT;
- public static final int MOUSE_BUTTON_MIDDLE = GLFW_MOUSE_BUTTON_MIDDLE;
- private final int[] mousePosition = new int[2];
- private final int[] mousePositionPrev = new int[2];
- private final boolean[] mouseButtonState = new boolean[3];
- private final boolean[] mouseButtonStatePrev = new boolean[3];
- public boolean printKeyCodes = false;
Garnet garnet;
+ Mouse mouse;
+ Keyboard keyboard;
+ ;
List actionConfigList;
- long windowHandle;
- int maxKeys = 512;
- private final boolean[] buttonState = new boolean[maxKeys];
- private final boolean[] buttonStatePrev = new boolean[maxKeys];
public Input(Garnet garnet) {
this.garnet = garnet;
+ mouse = new Mouse(garnet);
+ keyboard = new Keyboard(garnet);
}
public void postStateChangeTask() {
@@ -34,77 +27,30 @@ public void postStateChangeTask() {
}
public void tick() {
- System.arraycopy(buttonState, 0, buttonStatePrev, 0, buttonState.length);
- updateMouse();
+ mouse.update();
+ keyboard.update();
}
- private void updateMouse() {
- mousePositionPrev[0] = mousePosition[0];
- mousePositionPrev[1] = mousePosition[1];
-
- double[] x = new double[1];
- double[] y = new double[1];
-
- glfwGetCursorPos(windowHandle, x, y);
- double[] windowToPixelsScale = garnet.getDisplay().getWindowToPixelsScale();
-
- x[0] /= windowToPixelsScale[0];
- y[0] /= windowToPixelsScale[1];
-
- mousePosition[0] = (int) x[0];
- mousePosition[1] = (int) y[0];
-
- System.arraycopy(mouseButtonState, 0, mouseButtonStatePrev, 0, mouseButtonState.length);
- mouseButtonState[MOUSE_BUTTON_LEFT] = glfwGetMouseButton(windowHandle, MOUSE_BUTTON_LEFT) > 0;
- mouseButtonState[MOUSE_BUTTON_MIDDLE] = glfwGetMouseButton(windowHandle, MOUSE_BUTTON_MIDDLE) > 0;
- mouseButtonState[MOUSE_BUTTON_RIGHT] = glfwGetMouseButton(windowHandle, MOUSE_BUTTON_RIGHT) > 0;
+ public Mouse getMouse() {
+ return mouse;
}
- public int[] getMousePosition() {
- return mousePosition;
- }
-
- public int[] getMousePositionScaled(double scale) {
- return new int[]{(int) (mousePosition[0] / scale), (int) (mousePosition[1] / scale)};
- }
-
- /**
- * Returns the mouse position normalised to 0..1 double values.
- *
- * @return
- */
- public double[] getMousePositionNormalised() {
- int windowWidth = garnet.getDisplay().getWindowWidth();
- int windowHeight = garnet.getDisplay().getWindowHeight();
- double x = (double) mousePosition[0] / (double) windowWidth;
- double y = (double) mousePosition[1] / (double) windowHeight;
-
- return new double[]{Utils.clampUnit(x), Utils.clampUnit(y)};
+ public Keyboard getKeyboard() {
+ return keyboard;
}
public void init() {
+ mouse.init();
+ keyboard.init();
+
actionConfigList = new ArrayList<>();
setDefaults();
- garnet.addKeyboardCallback((key, scancode, action, mods) -> {
-
- if (printKeyCodes) {
- System.out.println("keyboard handler - key:" + key + " scancode:" + scancode + " action:" + action);
- }
-
- if (action == 1) {
- buttonState[key] = true;
- } else if (action == 0) {
- buttonState[key] = false;
- }
- });
-
}
public void setDefaults() {
-
addKeyboardAction(InputKeys.KEY_LEFT, InputAction.LEFT);
addKeyboardAction(InputKeys.KEY_RIGHT, InputAction.RIGHT);
addKeyboardAction(InputKeys.KEY_UP, InputAction.UP);
@@ -117,49 +63,26 @@ public void addKeyboardAction(int keyCode, int actionId) {
actionConfigList.add(new ButtonConfig(keyCode, actionId));
}
- public boolean isMouseButtonPressed(int mouseButtonId) {
- return mouseButtonState[mouseButtonId];
- }
-
- /**
- * True if mouse button first pressed this frame.
- *
- * @param mouseButtonId
- * @return
- */
- public boolean isMouseButtonFirstPress(int mouseButtonId) {
- return (mouseButtonState[mouseButtonId] && !mouseButtonStatePrev[mouseButtonId]);
- }
- public boolean isPressed(int actionId) {
+ public boolean isActionKeyPressed(int actionId) {
boolean pressed = false;
for (ButtonConfig buttonConfig : actionConfigList) {
if (buttonConfig.actionId == actionId) {
- if (buttonState[buttonConfig.keyCode]) pressed = true;
+ if (keyboard.getKeyboardState()[buttonConfig.keyCode]) pressed = true;
}
}
return pressed;
}
- public boolean isFirstPress(int actionId) {
+ public boolean isActionKeyFirstPress(int actionId) {
boolean firstPress = false;
for (ButtonConfig buttonConfig : actionConfigList) {
if (buttonConfig.actionId == actionId) {
- if (buttonState[buttonConfig.keyCode] &&
- !buttonStatePrev[buttonConfig.keyCode]) firstPress = true;
+ if (keyboard.getKeyboardState()[buttonConfig.keyCode] &&
+ !keyboard.getKeyboardStatePrev()[buttonConfig.keyCode]) firstPress = true;
}
}
return firstPress;
}
-
- public boolean isPressedThisFrame(InputAction button) {
-
- return false;
- }
-
- public void setWindowHandle(long windowHandle) {
- this.windowHandle = windowHandle;
- }
-
}
diff --git a/src/main/java/com/physmo/garnet/input/InputAction.java b/src/main/java/com/physmo/garnet/input/InputAction.java
index fdf71e7..cf4215b 100644
--- a/src/main/java/com/physmo/garnet/input/InputAction.java
+++ b/src/main/java/com/physmo/garnet/input/InputAction.java
@@ -13,5 +13,4 @@ public class InputAction {
public static int FIRE1 = 5;
public static int FIRE2 = 6;
public static int MENU = 7;
-
}
diff --git a/src/main/java/com/physmo/garnet/input/Keyboard.java b/src/main/java/com/physmo/garnet/input/Keyboard.java
new file mode 100644
index 0000000..2f5a407
--- /dev/null
+++ b/src/main/java/com/physmo/garnet/input/Keyboard.java
@@ -0,0 +1,47 @@
+package com.physmo.garnet.input;
+
+import com.physmo.garnet.Garnet;
+
+public class Keyboard {
+
+ public boolean printKeyCodes = false;
+ Garnet garnet;
+ int maxKeys = 512;
+ private final boolean[] keyboardState = new boolean[maxKeys];
+ private final boolean[] keyboardStatePrev = new boolean[maxKeys];
+
+ public Keyboard(Garnet garnet) {
+ this.garnet = garnet;
+ }
+
+ public void init() {
+ garnet.addKeyboardCallback((key, scancode, action, mods) -> {
+
+ if (printKeyCodes) {
+ System.out.println("keyboard handler - key:" + key + " scancode:" + scancode + " action:" + action);
+ }
+
+ if (action == 1) {
+ keyboardState[key] = true;
+ } else if (action == 0) {
+ keyboardState[key] = false;
+ }
+ });
+ }
+
+ public void update() {
+ System.arraycopy(keyboardState, 0, keyboardStatePrev, 0, keyboardState.length);
+ }
+
+ public boolean[] getKeyboardState() {
+ return keyboardState;
+ }
+
+ public boolean[] getKeyboardStatePrev() {
+ return keyboardStatePrev;
+ }
+
+ public void setPrintKeyCodes(boolean printKeyCodes) {
+ this.printKeyCodes = printKeyCodes;
+ }
+}
diff --git a/src/main/java/com/physmo/garnet/input/Mouse.java b/src/main/java/com/physmo/garnet/input/Mouse.java
new file mode 100644
index 0000000..b816fbf
--- /dev/null
+++ b/src/main/java/com/physmo/garnet/input/Mouse.java
@@ -0,0 +1,93 @@
+package com.physmo.garnet.input;
+
+import com.physmo.garnet.Garnet;
+import com.physmo.garnet.Utils;
+
+import static org.lwjgl.glfw.GLFW.GLFW_MOUSE_BUTTON_LEFT;
+import static org.lwjgl.glfw.GLFW.GLFW_MOUSE_BUTTON_MIDDLE;
+import static org.lwjgl.glfw.GLFW.GLFW_MOUSE_BUTTON_RIGHT;
+import static org.lwjgl.glfw.GLFW.glfwGetCursorPos;
+import static org.lwjgl.glfw.GLFW.glfwGetMouseButton;
+
+public class Mouse {
+
+ public static final int BUTTON_LEFT = GLFW_MOUSE_BUTTON_LEFT;
+ public static final int BUTTON_RIGHT = GLFW_MOUSE_BUTTON_RIGHT;
+ public static final int BUTTON_MIDDLE = GLFW_MOUSE_BUTTON_MIDDLE;
+ private final int[] position = new int[2];
+ private final int[] positionPrev = new int[2];
+ private final boolean[] buttonState = new boolean[3];
+ private final boolean[] buttonStatePrev = new boolean[3];
+ Garnet garnet;
+ long windowHandle;
+
+ public Mouse(Garnet garnet) {
+ this.garnet = garnet;
+ }
+
+ public void init() {
+ windowHandle = garnet.getDisplay().getWindowHandle();
+ }
+
+ void update() {
+ positionPrev[0] = position[0];
+ positionPrev[1] = position[1];
+
+ double[] x = new double[1];
+ double[] y = new double[1];
+
+ glfwGetCursorPos(windowHandle, x, y);
+ double[] windowToPixelsScale = garnet.getDisplay().getWindowToPixelsScale();
+
+ x[0] /= windowToPixelsScale[0];
+ y[0] /= windowToPixelsScale[1];
+
+ position[0] = (int) x[0];
+ position[1] = (int) y[0];
+
+ System.arraycopy(buttonState, 0, buttonStatePrev, 0, buttonState.length);
+ buttonState[BUTTON_LEFT] = glfwGetMouseButton(windowHandle, BUTTON_LEFT) > 0;
+ buttonState[BUTTON_MIDDLE] = glfwGetMouseButton(windowHandle, BUTTON_MIDDLE) > 0;
+ buttonState[BUTTON_RIGHT] = glfwGetMouseButton(windowHandle, BUTTON_RIGHT) > 0;
+ }
+
+
+ public int[] getPosition() {
+ return position;
+ }
+
+ public int[] getPositionScaled(double scale) {
+ return new int[]{(int) (position[0] / scale), (int) (position[1] / scale)};
+ }
+
+ /**
+ * Returns the mouse position normalised to 0..1 double values.
+ *
+ * @return
+ */
+ public double[] getPositionNormalised() {
+ int windowWidth = garnet.getDisplay().getWindowWidth();
+ int windowHeight = garnet.getDisplay().getWindowHeight();
+ double x = (double) position[0] / (double) windowWidth;
+ double y = (double) position[1] / (double) windowHeight;
+
+ return new double[]{Utils.clampUnit(x), Utils.clampUnit(y)};
+ }
+
+
+ public boolean isButtonPressed(int mouseButtonId) {
+ return buttonState[mouseButtonId];
+ }
+
+ /**
+ * True if mouse button first pressed this frame.
+ *
+ * @param mouseButtonId
+ * @return
+ */
+ public boolean isButtonFirstPress(int mouseButtonId) {
+ return (buttonState[mouseButtonId] && !buttonStatePrev[mouseButtonId]);
+ }
+
+
+}
diff --git a/src/main/java/com/physmo/garnet/tilegrid/TileGridDrawer.java b/src/main/java/com/physmo/garnet/tilegrid/TileGridDrawer.java
index a9c46c7..1591f26 100644
--- a/src/main/java/com/physmo/garnet/tilegrid/TileGridDrawer.java
+++ b/src/main/java/com/physmo/garnet/tilegrid/TileGridDrawer.java
@@ -45,10 +45,10 @@ public void draw(Graphics g, int drawPosX, int drawPosY) {
double[] visibleRect = camera.getVisibleRect();
- int xStart = (int) (visibleRect[0] / tileWidth) - 1;
- int yStart = (int) (visibleRect[1] / tileHeight) - 1;
- int xSize = (int) ((visibleRect[2]) / tileWidth) + 2;
- int ySize = (int) ((visibleRect[3]) / tileHeight) + 2;
+ int xStart = (int) (visibleRect[0] / tileWidth) - 2;
+ int yStart = (int) (visibleRect[1] / tileHeight) - 2;
+ int xSize = (int) ((visibleRect[2]) / tileWidth) + 3;
+ int ySize = (int) ((visibleRect[3]) / tileHeight) + 3;
for (int y = yStart; y <= yStart + ySize; y++) {
for (int x = xStart; x <= xStart + xSize; x++) {