added Gamepad

This commit is contained in:
DaniTheSkunk 2022-12-12 02:06:44 +00:00
parent 9fa8eb11bb
commit db610763ac
5 changed files with 115 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import com.danitheskunk.skunkworks.audio.nodes.Mixer;
import com.danitheskunk.skunkworks.audio.nodes.Node;
import com.danitheskunk.skunkworks.audio.nodes.SamplePlayer;
import com.danitheskunk.skunkworks.audio.nodes.TTS;
import com.danitheskunk.skunkworks.backends.gl.Gamepad;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.font.IFont;
@ -34,6 +35,7 @@ public abstract class BaseGame {
protected final SamplePlayer samplePlayer;
protected final TTS tts;
protected final IWindow window;
protected final BaseGamepad gamepad;
/**
* Create with given window size and title
@ -52,6 +54,7 @@ public abstract class BaseGame {
engine = new Engine();
window = engine.openWindow(windowSize, windowTitle);
gamepad = window.getGamepad(0);
//todo: load from .jar
debugFont = window.loadFontTileset("fonts/ega-8x14.png");
rootNode = new NodeRoot();
@ -153,6 +156,7 @@ public abstract class BaseGame {
currentFrameTime += delta;
engine.tick();
window.tick();
gamepad.tick();
//todo: frame rate control
if(currentFrameTime >= 1.0 / 60.0) {
rootNode.tick();

View File

@ -0,0 +1,57 @@
package com.danitheskunk.skunkworks;
public abstract class BaseGamepad {
protected final float[] axes;
protected final boolean[] buttonDown;
protected final boolean[] buttonPressed;
protected final int axesCount;
protected final int buttonCount;
public BaseGamepad(int axesCount, int buttonCount) {
this.axesCount = axesCount;
this.buttonCount = buttonCount;
axes = new float[axesCount];
buttonDown = new boolean[buttonCount];
buttonPressed = new boolean[buttonCount];
}
public int getAxesCount() {
return axesCount;
}
public int getButtonCount() {
return buttonCount;
}
public float getAxis(int i) {
return axes[i];
}
public boolean getButtonDown(int i) {
return buttonDown[i];
}
public boolean getButtonPressed(int i) {
return buttonPressed[i];
}
public void setAxis(int i, float value) {
axes[i] = value;
}
public void setButtonDown(int i, boolean value) {
buttonDown[i] = value;
}
public void setButtonPressed(int i, boolean value) {
buttonPressed[i] = value;
}
public void clearButtonPressed() {
for(int i = 0; i < buttonCount; ++i) {
buttonPressed[i] = false;
}
}
public abstract void tick();
}

View File

@ -1,6 +1,10 @@
package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.gfx.*;
import com.danitheskunk.skunkworks.backends.gl.Gamepad;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image;
import com.danitheskunk.skunkworks.gfx.NineSlice;
import com.danitheskunk.skunkworks.gfx.font.IFont;
import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D;
@ -9,6 +13,8 @@ import java.util.List;
public interface IWindow {
Engine getEngine();
BaseGamepad getGamepad(int id);
Vec2i getMousePos();
boolean isMouseClicked(int button);

View File

@ -2,10 +2,12 @@ package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.audio.ISample;
import com.danitheskunk.skunkworks.nodes.NodeSprite;
import org.lwjgl.glfw.GLFW;
public class TestNode extends BaseGame {
private final ISample kick;
private final NodeSprite sprite;
float[] axes;
public TestNode() {
super(new Vec2i(1280, 720), "Skunkworks");
@ -15,13 +17,26 @@ public class TestNode extends BaseGame {
sprite.setTexture(loadTexture("demoassets/test.png"));
sprite.setPos(new Vec2f(100, 100));
rootNode.add(sprite);
//doThing();
}
void doThing() {
sprite.tweenPos(new Vec2f(800, 400), 120).delay(60).then(() -> {
System.out.println("yay! got there! now lets go home");
sprite.tweenPos(new Vec2f(100, 100), 120);
sprite.tweenPos(new Vec2f(100, 100), 120).then(this::doThing);
playSample(kick);
});
}
@Override
protected void update(double delta) {
sprite.setPos(new Vec2f(640 + 640 * gamepad.getAxis(0), 360 + 360 * gamepad.getAxis(1)));
if(gamepad.getButtonPressed(0)) {
playSample(kick);
}
}
public static void main(String[] args) {
new TestNode().run();
}

View File

@ -0,0 +1,31 @@
package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.BaseGamepad;
import org.lwjgl.glfw.GLFW;
public class Gamepad extends BaseGamepad {
private final int jid;
public Gamepad(int jid) {
super(GLFW.glfwGetJoystickAxes(jid).limit(), GLFW.glfwGetJoystickButtons(jid).limit());
if(!GLFW.glfwJoystickPresent(jid) || !GLFW.glfwJoystickIsGamepad(jid)) {
throw new RuntimeException("gamepad not found");
}
this.jid = jid;
}
@Override
public void tick() {
var axesBuf = GLFW.glfwGetJoystickAxes(jid);
var buttonBuf = GLFW.glfwGetJoystickButtons(jid);
for(int i = 0; i < axesCount; ++i) {
axes[i] = axesBuf.get(i);
}
for(int i = 0; i < buttonCount; ++i) {
var val = buttonBuf.get() != 0;
buttonPressed[i] = !buttonDown[i] && val;
buttonDown[i] = val;
}
}
}