Input """cleanup"""
This commit is contained in:
parent
68580ce40b
commit
0185a14b0b
@ -22,7 +22,7 @@ public class Engine {
|
|||||||
private static long window;
|
private static long window;
|
||||||
|
|
||||||
private final List<ObjectGl> objectsGl;
|
private final List<ObjectGl> objectsGl;
|
||||||
|
|
||||||
private static boolean present = glfwJoystickPresent(GLFW_JOYSTICK_1);
|
private static boolean present = glfwJoystickPresent(GLFW_JOYSTICK_1);
|
||||||
|
|
||||||
private boolean running;
|
private boolean running;
|
||||||
@ -39,7 +39,7 @@ public class Engine {
|
|||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
ObjectGl.projection = Matrix4f.orthographic(-1000, 1000, -1000 * aspectRatio, 1000 * aspectRatio, 0.1f, 100.0f);
|
ObjectGl.projection = Matrix4f.orthographic(-1000, 1000, -1000 * aspectRatio, 1000 * aspectRatio, 0.1f, 100.0f);
|
||||||
ObjectGl.view = Matrix4f.translate(new Vector3f(0.0f,0.0f,1.0f));
|
ObjectGl.view = Matrix4f.translate(new Vector3f(0.0f, 0.0f, 1.0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -62,8 +62,8 @@ public class Engine {
|
|||||||
int height = this.height;
|
int height = this.height;
|
||||||
this.setWindow(glfwCreateWindow(width, height, "Boulevard Combattant", NULL, NULL));
|
this.setWindow(glfwCreateWindow(width, height, "Boulevard Combattant", NULL, NULL));
|
||||||
assert getWindow() != NULL;
|
assert getWindow() != NULL;
|
||||||
|
|
||||||
|
|
||||||
System.out.println(present);
|
System.out.println(present);
|
||||||
|
|
||||||
// On récupère les informations du moniteur principal
|
// On récupère les informations du moniteur principal
|
||||||
@ -71,7 +71,7 @@ public class Engine {
|
|||||||
assert vidmode != null;
|
assert vidmode != null;
|
||||||
|
|
||||||
// On met la fenêtre au centre de l'écran principale
|
// On met la fenêtre au centre de l'écran principale
|
||||||
glfwSetWindowPos(getWindow(), (vidmode.width() - width)/2, (vidmode.height() - height)/2);
|
glfwSetWindowPos(getWindow(), (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
|
||||||
|
|
||||||
glfwSetKeyCallback(getWindow(), new Input());
|
glfwSetKeyCallback(getWindow(), new Input());
|
||||||
glfwSetInputMode(getWindow(), GLFW_STICKY_KEYS, GLFW_TRUE);
|
glfwSetInputMode(getWindow(), GLFW_STICKY_KEYS, GLFW_TRUE);
|
||||||
@ -95,14 +95,14 @@ public class Engine {
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void update(){
|
public void update() {
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public void render(){
|
public void render() {
|
||||||
|
|
||||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
@ -119,25 +119,26 @@ 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) {
|
||||||
objectsGl.add(obj);
|
objectsGl.add(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove_objectGl(ObjectGl obj){
|
public void remove_objectGl(ObjectGl obj) {
|
||||||
objectsGl.remove(obj);
|
objectsGl.remove(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isRunning(){
|
public boolean isRunning() {
|
||||||
return running;
|
return running;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRunning(boolean b){
|
public void setRunning(boolean b) {
|
||||||
running = b;
|
running = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean shouldClose(){
|
public boolean shouldClose() {
|
||||||
return glfwWindowShouldClose(getWindow());
|
return glfwWindowShouldClose(getWindow());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,16 +154,16 @@ public class Engine {
|
|||||||
* Est appelé àa chaque modification de la taille de la fenêtre, et modifie la taille de la zone de rendu
|
* Est appelé àa chaque modification de la taille de la fenêtre, et modifie la taille de la zone de rendu
|
||||||
* pour quelle corresponde à la taille de la fenêtre
|
* pour quelle corresponde à la taille de la fenêtre
|
||||||
*/
|
*/
|
||||||
private static final GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback(){
|
private static final GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void invoke(long window, int width, int height){
|
public void invoke(long window, int width, int height) {
|
||||||
glViewport(0,0,width,height);
|
glViewport(0, 0, width, height);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Engine engine = new Engine(1280, 720, 9.0f/16.0f);
|
Engine engine = new Engine(1280, 720, 9.0f / 16.0f);
|
||||||
int speed = 10 ; //vitesse déplacement Object
|
int speed = 10; //vitesse déplacement Object
|
||||||
engine.init();
|
engine.init();
|
||||||
|
|
||||||
// Add objects to render
|
// Add objects to render
|
||||||
@ -170,32 +171,32 @@ public class Engine {
|
|||||||
|
|
||||||
String path2 = "textures/awesomeface.png";
|
String path2 = "textures/awesomeface.png";
|
||||||
|
|
||||||
ObjectGl zangief = new ObjectGl(0f,60f,80f,10f, path, null);
|
ObjectGl zangief = new ObjectGl(0f, 60f, 80f, 10f, path, null);
|
||||||
zangief.setTextureWrap(58,0,62,84, ObjectGl.STICK_TOP);
|
zangief.setTextureWrap(58, 0, 62, 84, ObjectGl.STICK_TOP);
|
||||||
engine.add_objectGl(zangief);
|
engine.add_objectGl(zangief);
|
||||||
zangief.translate(new Vector3f(-5000.0f,500.0f,10.0f));
|
zangief.translate(new Vector3f(-5000.0f, 500.0f, 10.0f));
|
||||||
zangief.setColor(new Vector3f(1.0f, 1.0f, 1.0f));
|
zangief.setColor(new Vector3f(1.0f, 1.0f, 1.0f));
|
||||||
zangief.useTime = true;
|
zangief.useTime = true;
|
||||||
zangief.setShader("shaders/StylishShaders/BasicVert.glsl","shaders/StylishShaders/PerlinNoise.glsl");
|
zangief.setShader("shaders/StylishShaders/BasicVert.glsl", "shaders/StylishShaders/PerlinNoise.glsl");
|
||||||
|
|
||||||
ObjectGl smiley2 = new ObjectGl(0.0f,500.0f,500.0f, 1f, path2, null);
|
ObjectGl smiley2 = new ObjectGl(0.0f, 500.0f, 500.0f, 1f, path2, null);
|
||||||
engine.add_objectGl(smiley2);
|
engine.add_objectGl(smiley2);
|
||||||
smiley2.translate(new Vector3f(0.0f,0.0f,5.0f));
|
smiley2.translate(new Vector3f(0.0f, 0.0f, 5.0f));
|
||||||
|
|
||||||
long timer = System.currentTimeMillis();
|
long timer = System.currentTimeMillis();
|
||||||
long lastFrame;
|
long lastFrame;
|
||||||
int frame = 0;
|
int frame = 0;
|
||||||
boolean nextFrame = false;
|
boolean nextFrame = false;
|
||||||
|
|
||||||
while(engine.isRunning()){
|
while (engine.isRunning()) {
|
||||||
lastFrame = System.currentTimeMillis();
|
lastFrame = System.currentTimeMillis();
|
||||||
// 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);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -214,96 +215,12 @@ public class Engine {
|
|||||||
frame = 0;
|
frame = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while(!nextFrame){
|
while (!nextFrame) {
|
||||||
nextFrame = System.currentTimeMillis() - lastFrame >= 16.66f;
|
nextFrame = System.currentTimeMillis() - lastFrame >= 16.66f;
|
||||||
}
|
}
|
||||||
|
|
||||||
nextFrame = false;
|
nextFrame = false;
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user