2021-05-03 02:29:11 +02:00
|
|
|
/**
|
|
|
|
* CLASS ENGINE
|
2021-05-13 21:51:40 +02:00
|
|
|
*
|
2021-05-03 02:29:11 +02:00
|
|
|
* Classe principale du moteur de jeu
|
2021-05-13 21:51:40 +02:00
|
|
|
*
|
2021-05-03 02:29:11 +02:00
|
|
|
* @author François Autin
|
2021-05-13 21:51:40 +02:00
|
|
|
*
|
2021-05-03 02:29:11 +02:00
|
|
|
*/
|
|
|
|
package engine;
|
|
|
|
|
2021-05-13 20:55:04 +02:00
|
|
|
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
|
2021-05-13 20:40:21 +02:00
|
|
|
import org.lwjgl.glfw.GLFWVidMode;
|
|
|
|
import org.lwjgl.opengl.GL;
|
|
|
|
|
|
|
|
import static org.lwjgl.glfw.GLFW.*;
|
|
|
|
import static org.lwjgl.opengl.GL11.*;
|
|
|
|
import static org.lwjgl.system.MemoryUtil.*;
|
|
|
|
|
2021-05-03 02:29:11 +02:00
|
|
|
public class Engine {
|
|
|
|
|
2021-05-13 20:40:21 +02:00
|
|
|
private long window;
|
|
|
|
|
|
|
|
private Scene scene;
|
|
|
|
|
|
|
|
private boolean running;
|
|
|
|
|
|
|
|
public Engine(){
|
|
|
|
this.running = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void init() {
|
|
|
|
if(!glfwInit()){
|
|
|
|
// TODO Erreur d'initialisation
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2021-05-13 21:05:11 +02:00
|
|
|
int width = 800;
|
|
|
|
int height = 600;
|
2021-05-13 20:40:21 +02:00
|
|
|
this.window = glfwCreateWindow(width, height, "Boulevard Combattant", NULL, NULL);
|
|
|
|
assert this.window != NULL;
|
|
|
|
|
2021-05-13 20:55:04 +02:00
|
|
|
// On récupère les informations du moniteur principal
|
2021-05-13 20:40:21 +02:00
|
|
|
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
|
|
|
|
assert vidmode != null;
|
|
|
|
|
|
|
|
glfwSetWindowPos(this.window, (vidmode.width() - width)/2, (vidmode.height() - height)/2);
|
|
|
|
|
|
|
|
glfwSetKeyCallback(window, new Input());
|
2021-05-13 21:51:40 +02:00
|
|
|
glfwSetInputMode(window, GLFW_STICKY_KEYS, GLFW_TRUE);
|
2021-05-13 20:40:21 +02:00
|
|
|
|
|
|
|
// Contexte = zone cible des rendus
|
|
|
|
glfwMakeContextCurrent(this.window);
|
|
|
|
glfwShowWindow(this.window);
|
|
|
|
GL.createCapabilities();
|
|
|
|
|
2021-05-13 20:55:04 +02:00
|
|
|
glfwSetFramebufferSizeCallback(window, resizeWindow);
|
|
|
|
|
2021-05-13 20:40:21 +02:00
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
System.out.println("OpenGL: " + glGetString(GL_VERSION));
|
|
|
|
|
2021-05-14 18:47:05 +02:00
|
|
|
this.scene = new Scene("shaders/vert.vert", "shaders/frag.frag", Primitive.triangle, Primitive.triangle_indices, Primitive.triangle_couleur);
|
2021-05-13 20:40:21 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void update(){
|
|
|
|
glfwPollEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void render(){
|
|
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT); //SWAP avec le precedent
|
|
|
|
scene.render();
|
|
|
|
int error = glGetError();
|
|
|
|
if (error != GL_NO_ERROR) System.out.println(error);
|
|
|
|
glfwSwapBuffers(window); //Envoie le buffer vers le moniteur
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run(){
|
|
|
|
init();
|
|
|
|
while(running){
|
|
|
|
update();
|
|
|
|
render();
|
|
|
|
if(glfwWindowShouldClose(window)) running = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:55:04 +02:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
2021-05-13 21:51:40 +02:00
|
|
|
private static final GLFWFramebufferSizeCallback resizeWindow = new GLFWFramebufferSizeCallback(){
|
2021-05-13 20:55:04 +02:00
|
|
|
@Override
|
|
|
|
public void invoke(long window, int width, int height){
|
|
|
|
glViewport(0,0,width,height);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-13 20:40:21 +02:00
|
|
|
public static void main(String[] args) {
|
|
|
|
Engine engine = new Engine();
|
|
|
|
engine.run();
|
|
|
|
}
|
|
|
|
|
2021-05-03 02:29:11 +02:00
|
|
|
}
|