Skip to content

Commit

Permalink
Merge pull request #23 from nickd3000/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
nickd3000 authored May 26, 2024
2 parents 1d2b965 + 0bd12e4 commit 3a826ae
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 118 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.nickd3000</groupId>
<artifactId>garnet</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<url>https://github.com/nickd3000/garnet</url>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/physmo/garnet/DebugDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

Expand Down
20 changes: 16 additions & 4 deletions src/main/java/com/physmo/garnet/Garnet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

Expand Down
11 changes: 3 additions & 8 deletions src/main/java/com/physmo/garnet/drawablebatch/Sprite2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
121 changes: 22 additions & 99 deletions src/main/java/com/physmo/garnet/input/Input.java
Original file line number Diff line number Diff line change
@@ -1,110 +1,56 @@
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<ButtonConfig> 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() {
tick();
}

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);
Expand All @@ -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;
}

}
1 change: 0 additions & 1 deletion src/main/java/com/physmo/garnet/input/InputAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ public class InputAction {
public static int FIRE1 = 5;
public static int FIRE2 = 6;
public static int MENU = 7;

}
47 changes: 47 additions & 0 deletions src/main/java/com/physmo/garnet/input/Keyboard.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 3a826ae

Please sign in to comment.