Keyboard input added In Engine.java

This commit is contained in:
Léo 2021-05-20 15:54:54 +02:00
parent 7a79c19c98
commit 59d822015a

View File

@ -2,6 +2,8 @@ package engine;
import engine.math.*;
import engine.object.*;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
@ -15,7 +17,7 @@ import static org.lwjgl.system.MemoryUtil.NULL;
public class Engine {
private long window;
private static long window;
private final List<ObjectGl> objectsGl;
@ -49,25 +51,28 @@ public class Engine {
int width = 1280;
int height = 720;
this.window = glfwCreateWindow(width, height, "Boulevard Combattant", NULL, NULL);
assert this.window != NULL;
this.setWindow(glfwCreateWindow(width, height, "Boulevard Combattant", NULL, NULL));
assert this.getWindow() != NULL;
boolean present = glfwJoystickPresent(GLFW_JOYSTICK_1);
System.out.println(present);
// On récupère les informations du moniteur principal
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
assert vidmode != null;
// On met la fenêtre au centre de l'écran principale
glfwSetWindowPos(this.window, (vidmode.width() - width)/2, (vidmode.height() - height)/2);
glfwSetWindowPos(this.getWindow(), (vidmode.width() - width)/2, (vidmode.height() - height)/2);
glfwSetKeyCallback(window, new Input());
glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_TRUE);
glfwSetKeyCallback(getWindow(), new Input());
glfwSetInputMode(getWindow(), GLFW_STICKY_KEYS, GLFW_TRUE);
// Contexte = zone cible des rendus
glfwMakeContextCurrent(this.window);
glfwShowWindow(this.window);
glfwMakeContextCurrent(this.getWindow());
glfwShowWindow(this.getWindow());
GL.createCapabilities();
glfwSetFramebufferSizeCallback(window, resizeWindow);
glfwSetFramebufferSizeCallback(getWindow(), resizeWindow);
glEnable(GL_DEPTH_TEST); // Z-Buffer plus z est grand plus l'objet est proche de la camera limite à 100.0f au dela l'objet disparait
@ -96,10 +101,13 @@ public class Engine {
for (ObjectGl objectGl : objectsGl) {
objectGl.render();
}
int error = glGetError();
if (error != GL_NO_ERROR) System.out.println(error);
glfwSwapBuffers(window); //Envoie le buffer vers le moniteur
glfwSwapBuffers(getWindow()); //Envoie le buffer vers le moniteur
}
public void add_objectGl(ObjectGl obj){
@ -120,7 +128,7 @@ public class Engine {
}
public boolean shouldClose(){
return glfwWindowShouldClose(window);
return glfwWindowShouldClose(getWindow());
}
/**
@ -136,6 +144,7 @@ public class Engine {
public static void main(String[] args) {
Engine engine = new Engine();
int speed = 2 ; //vitesse déplacement Object
engine.init();
// Add objects to render
@ -163,6 +172,11 @@ public class Engine {
smiley.translate(new Vector3f( (float) Math.sin(time), (float) Math.cos(time), 0.0f));
smiley2.rotateZ(0.8f);
input(smiley, speed);
input(smiley2, speed);
//essential part v
engine.update();
@ -184,5 +198,20 @@ public class Engine {
if(engine.shouldClose()) engine.setRunning(false);
}
}
public static void input(ObjectGl token, int speed) {
if (Input.isKeyDown(GLFW.GLFW_KEY_W))token.translate(new Vector3f ( 0.0f, speed * 5.0f, 0.0f));
if (Input.isKeyDown(GLFW.GLFW_KEY_A))token.translate(new Vector3f (speed *-5.0f, 0.0f, 0.0f));
if (Input.isKeyDown(GLFW.GLFW_KEY_S))token.translate(new Vector3f ( 0.0f,speed * -5.0f, 0.0f));
if (Input.isKeyDown(GLFW.GLFW_KEY_D))token.translate(new Vector3f (speed * 5.0f, 0.0f, 0.0f));
}
public static long getWindow() {
return window;
}
public void setWindow(long window) {
this.window = window;
}
}