jeu-de-combat/src/engine/Engine.java
2021-05-19 02:52:15 +02:00

148 lines
4.0 KiB
Java

/**
* CLASS ENGINE
*
* Classe principale du moteur de jeu
*
* @author François Autin
*
*/
package engine;
import engine.object.ObjectGl;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
public class Engine {
private long window;
private List<ObjectGl> objectsGl;
private boolean running;
/**
* Create the engine and initial attributes use .init() to start the engine
*/
public Engine() {
this.running = false;
this.objectsGl = new ArrayList<ObjectGl>();
}
/**
* Start the engine
* Create the window
*/
private void init() {
glfwInit();
this.running = true;
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //On utilise la version 3.3 d'openGL
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //Compatible MAC
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Le core profile est l'interface 'avancé' d'openGL
int width = 1280;
int height = 720;
this.window = glfwCreateWindow(width, height, "Boulevard Combattant", NULL, NULL);
assert this.window != NULL;
// 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);
glfwSetKeyCallback(window, new Input());
glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_TRUE);
// Contexte = zone cible des rendus
glfwMakeContextCurrent(this.window);
glfwShowWindow(this.window);
GL.createCapabilities();
glfwSetFramebufferSizeCallback(window, resizeWindow);
glEnable(GL_DEPTH_TEST); // Z-Buffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
System.out.println("OpenGL: " + glGetString(GL_VERSION));
}
/**
*
*/
private void update(){
glfwPollEvents();
}
/**
*
*/
private void render(){
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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
}
/**
* TODO la boucle de rendu doit être inscrite dans la boucle de gameplay: cette methode doit disparaitre
*/
public void run(){
while(running){
update();
render();
if(glfwWindowShouldClose(window)) running = false;
}
}
public void add_objectGl(ObjectGl obj){
objectsGl.add(obj);
}
public void remove_objectGl(ObjectGl obj){
objectsGl.remove(obj);
}
/**
* Est appelé à 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
*/
private static final GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback(){
@Override
public void invoke(long window, int width, int height){
glViewport(0,0,width,height);
}
};
public static void main(String[] args) {
Engine engine = new Engine();
engine.init();
// Add objects to render
ObjectGl cube = new ObjectGl(-0.2f,-0.2f,0.0f,1.0f,1.0f);
engine.add_objectGl(cube);
// engine.remove_objectGl(cube);
engine.run();
}
}