Advertisement
Giuseppe499

Main.java

Oct 27th, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. package com.main;
  2.  
  3.  
  4. import org.lwjgl.glfw.GLFWKeyCallback;
  5. import static org.lwjgl.glfw.GLFW.*;
  6.  
  7. import static org.lwjgl.opengl.GL11.*;
  8. import static org.lwjgl.system.MemoryUtil.*;
  9. import org.lwjgl.opengl.GLContext;
  10.  
  11. public class Main {
  12.    
  13.     private boolean running;
  14.    
  15.     private static long windowID;
  16.    
  17.     GLFWKeyCallback keyCallback;
  18.    
  19.     public Main() {
  20.        
  21.         initWindow();
  22.        
  23.     }
  24.    
  25.     private static void initWindow() {
  26.        
  27.         // Inizializza GLFW    
  28.         if (glfwInit() != GL_TRUE)
  29.         {
  30.             System.err.println("Error initializing GLFW");
  31.             System.exit(1);
  32.         }
  33.        
  34.         // Crea la Finestra    
  35.         windowID = glfwCreateWindow(1366, 768, "Window", NULL, NULL);
  36.  
  37.         if (windowID == NULL)
  38.         {
  39.             System.err.println("Error creating a window");
  40.             System.exit(1);
  41.         }
  42.        
  43.         glfwWindowHint(GLFW_SAMPLES, 4);
  44.         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  45.         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  46.         glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  47.         glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  48.         glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  49.        
  50.         glfwMakeContextCurrent(windowID);
  51.         GLContext.createFromCurrent();
  52.         glfwSwapInterval(1);
  53.        
  54.     }
  55.    
  56.    
  57.     public void initGame() {
  58.        
  59.     }
  60.    
  61.     public void update(float delta) {
  62.        
  63.         if (glfwGetKey(windowID, GLFW_KEY_UP) == GLFW_PRESS)
  64.         {
  65.             running = false;
  66.         }
  67.        
  68.     }
  69.    
  70.     public void render(float delta) {
  71.        
  72.     }
  73.    
  74.     public void dispose() {
  75.        
  76.     }
  77.    
  78.     public void end() {
  79.         running = false;
  80.     }
  81.    
  82.    
  83.     public void start() {
  84.         float now, last, delta;
  85.  
  86.         last = 0;
  87.        
  88.         glfwSetKeyCallback(windowID, keyCallback = GLFWKeyCallback(this::glfwKeyCallback));
  89.  
  90.         // Inizializza il Gioco
  91.         initGame();
  92.        
  93.         running = true;
  94.  
  95.         // Loop continuously and render and update
  96.         while (running && glfwWindowShouldClose(windowID) != GL_TRUE)
  97.         {
  98.             // Get the time
  99.             now = (float) glfwGetTime();
  100.             delta = now - last;
  101.             last = now;
  102.  
  103.             // Update e Render
  104.             update(delta);
  105.             render(delta);
  106.  
  107.             // Poll the events and swap the buffers
  108.             glfwPollEvents();
  109.             glfwSwapBuffers(windowID);
  110.         }
  111.  
  112.         // Dispose the game
  113.         dispose();
  114.        
  115.         // Release the callbacks
  116.         keyCallback.release();
  117.  
  118.         // Distrugge la Finestra al Termine del Programma
  119.         glfwDestroyWindow(windowID);
  120.         glfwTerminate();
  121.  
  122.         System.exit(0);
  123.     }
  124.    
  125.     public void glfwKeyCallback(long window, int key, int scancode, int action, int mods)
  126.     {
  127.         // End on escape
  128.         if (key == GLFW_KEY_ESCAPE && action != GLFW_RELEASE)
  129.             end();
  130.     }
  131.    
  132.     public static long getWindowID() {
  133.         return windowID;
  134.     }
  135.    
  136.     public static void main(String[] args) {
  137.        
  138.         new Main().start();
  139.  
  140.        
  141.     }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement