Input """cleanup"""

This commit is contained in:
Antoine 2021-05-28 18:34:49 +02:00
parent 68580ce40b
commit 0185a14b0b
2 changed files with 119 additions and 117 deletions

View File

@ -119,6 +119,7 @@ public class Engine {
/** /**
* Add obj to the render queue * Add obj to the render queue
*
* @param obj ObjectGl to render * @param obj ObjectGl to render
*/ */
public void add_objectGl(ObjectGl obj) { public void add_objectGl(ObjectGl obj) {
@ -192,10 +193,10 @@ public class Engine {
// Game logic should fit here // Game logic should fit here
if (present) { if (present) {
gamepadInput(zangief, speed); Input.gamepadInput(zangief, speed);
} }
keyboardInput(zangief, speed); Input.keyboardInput(zangief, speed);
// input(smiley2, speed); // input(smiley2, speed);
/* /*
@ -222,88 +223,4 @@ public class Engine {
if (engine.shouldClose()) engine.setRunning(false); if (engine.shouldClose()) engine.setRunning(false);
} }
} }
private static void gamepadInput(ObjectGl token, int speed) {
ByteBuffer gamepadButton = glfwGetJoystickButtons(GLFW_JOYSTICK_1);
FloatBuffer gamepadAxes = glfwGetJoystickAxes(GLFW_JOYSTICK_1);
assert gamepadAxes != null;
assert gamepadButton != null;
String name = GLFW.glfwGetJoystickName(GLFW_JOYSTICK_1);
System.out.println("GamePad Name :" + name);
// for (int i =0 ; i < gamepadAxes.capacity(); i++) {
// System.out.println(i + " :" + gamepadAxes.get(i));
// }
if (gamepadButton.get(0) ==1 ) { // appuie sur croix(PlayStation) A (Xbox)
token.translate(new Vector3f ( 0.0f, speed * 5.0f, 0.0f));
}
if ( (gamepadAxes.get(2) < -0.1 || gamepadAxes.get(2) > 0.1) ) { // de droite à gauche //joystick gauche
token.translate(new Vector3f (5*speed * gamepadAxes.get(2), 0.0f, 0.0f));
if ( gamepadAxes.get(2) < -0.1 ){
token.setTextureWrap(121,0,57,80, ObjectGl.DEFAULT);
}else if (gamepadAxes.get(2) > 0.1) {
token.setTextureWrap(178,0,62,82, ObjectGl.DEFAULT);
}
}
if ( (gamepadAxes.get(3) < -0.1 || gamepadAxes.get(3) > 0.1) ) { // de haut en bas //joystick gauche
token.translate(new Vector3f (0.0f, -5* speed * gamepadAxes.get(3), 0.0f));
}
/* Buttons
0 : Croix / A
1: rond /B
2: carré / X
3: triangle / Y
4: L1 / LB
5: R1 / RB
6:select
7:start
8:L3
9:R3
10: haut
11: droite
12: bas
13: gauche
*/
/* Axes
0 : left X axe ( right : 1 left -1)
1: left Y axe ( down : 1 , Up -1)
2: right X axe ( right : 1 left -1)
3: right Y axe ( down : 1 , Up -1)
4:L2 / LT : 1 active, -1 unactive
5: R2 /RT : 1 active, -1 unactive
*/
}
public static void keyboardInput(ObjectGl token, int speed) {
boolean keyPressed = false;
if (Input.isKeyDown(GLFW.GLFW_KEY_S)) {
token.setTextureWrap(161,260,56,59, ObjectGl.STICK_BOTTOM);
keyPressed = true;
} else if (Input.isKeyDown(GLFW.GLFW_KEY_W)) {
// token.translate(new Vector3f (0.0f, speed * 5.0f, 0.0f));
keyPressed = true;
}
if (Input.isKeyDown(GLFW.GLFW_KEY_A)) {
token.translate(new Vector3f (speed *-5.0f, 0.0f, 0.0f));
// token.setTextureWrap(121,0,57,80, ObjectGl.STICK_TOP);
token.setTextureWrap(121,0,57,82, ObjectGl.STICK_TOP);
keyPressed = true;
}
else if (Input.isKeyDown(GLFW.GLFW_KEY_D)) {
token.translate(new Vector3f (speed * 5.0f, 0.0f, 0.0f));
// token.setTextureWrap(178,0,62,82, ObjectGl.STICK_TOP);
token.setTextureWrap(178,0,62,82, ObjectGl.STICK_TOP);
keyPressed = true;
}
// if (!keyPressed) token.setTextureWrap(58,0,62,84, ObjectGl.STICK_TOP);
if (!keyPressed) token.setTextureWrap(58,0,62,82, ObjectGl.STICK_TOP);
}
} }

View File

@ -1,8 +1,13 @@
package engine; package engine;
import engine.math.Vector3f;
import engine.object.ObjectGl;
import org.lwjgl.glfw.GLFW; import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWKeyCallback; import org.lwjgl.glfw.GLFWKeyCallback;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL11.*;
@ -19,8 +24,88 @@ public class Input extends GLFWKeyCallback {
if (glGetInteger(GL_POLYGON_MODE) == GL_FILL) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); if (glGetInteger(GL_POLYGON_MODE) == GL_FILL) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
} }
public static boolean isKeyDown(int keyCode) { public static boolean isKeyDown(int keyCode) {
return glfwGetKey(Engine.getWindow(), keyCode) == 1; return glfwGetKey(Engine.getWindow(), keyCode) == 1;
} }
public static void gamepadInput(ObjectGl token, int speed) {
ByteBuffer gamepadButton = glfwGetJoystickButtons(GLFW_JOYSTICK_1);
FloatBuffer gamepadAxes = glfwGetJoystickAxes(GLFW_JOYSTICK_1);
assert gamepadAxes != null;
assert gamepadButton != null;
String name = GLFW.glfwGetJoystickName(GLFW_JOYSTICK_1);
System.out.println("GamePad Name :" + name);
// for (int i =0 ; i < gamepadAxes.capacity(); i++) {
// System.out.println(i + " :" + gamepadAxes.get(i));
// }
if (gamepadButton.get(0) ==1 ) { // appuie sur croix(PlayStation) A (Xbox)
token.translate(new Vector3f( 0.0f, speed * 5.0f, 0.0f));
}
if ( (gamepadAxes.get(2) < -0.1 || gamepadAxes.get(2) > 0.1) ) { // de droite à gauche //joystick gauche
token.translate(new Vector3f (5 * speed * gamepadAxes.get(2), 0.0f, 0.0f));
if ( gamepadAxes.get(2) < -0.1 ){
token.setTextureWrap(121,0,57,80, ObjectGl.DEFAULT);
}else if (gamepadAxes.get(2) > 0.1) {
token.setTextureWrap(178,0,62,82, ObjectGl.DEFAULT);
}
}
if ( (gamepadAxes.get(3) < -0.1 || gamepadAxes.get(3) > 0.1) ) { // de haut en bas //joystick gauche
token.translate(new Vector3f (0.0f, -5 * speed * gamepadAxes.get(3), 0.0f));
}
/* Buttons
0 : Croix / A
1: rond /B
2: carré / X
3: triangle / Y
4: L1 / LB
5: R1 / RB
6:select
7:start
8:L3
9:R3
10: haut
11: droite
12: bas
13: gauche
*/
/* Axes
0 : left X axe ( right : 1 left -1)
1: left Y axe ( down : 1 , Up -1)
2: right X axe ( right : 1 left -1)
3: right Y axe ( down : 1 , Up -1)
4:L2 / LT : 1 active, -1 unactive
5: R2 /RT : 1 active, -1 unactive
*/
}
public static void keyboardInput(ObjectGl token, int speed) {
boolean keyPressed = false;
if (Input.isKeyDown(GLFW.GLFW_KEY_S)) {
token.setTextureWrap(161,260,56,59, ObjectGl.STICK_BOTTOM);
keyPressed = true;
} else if (Input.isKeyDown(GLFW.GLFW_KEY_W)) {
keyPressed = true;
}
if (Input.isKeyDown(GLFW.GLFW_KEY_A)) {
token.translate(new Vector3f (speed * -5.0f, 0.0f, 0.0f));
token.setTextureWrap(121,0,57,82, ObjectGl.STICK_TOP);
keyPressed = true;
}
else if (Input.isKeyDown(GLFW.GLFW_KEY_D)) {
token.translate(new Vector3f (speed * 5.0f, 0.0f, 0.0f));
token.setTextureWrap(178,0,62,82, ObjectGl.STICK_TOP);
keyPressed = true;
}
if (!keyPressed) token.setTextureWrap(58,0,62,82, ObjectGl.STICK_TOP);
}
} }