Guest User

Window.java JGO

a guest
Nov 30th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package net.mrpizzacake.futura.core;
  2.  
  3. import org.lwjgl.opengl.GLContext;
  4. import org.lwjgl.system.glfw.ErrorCallback;
  5. import org.lwjgl.system.glfw.GLFWvidmode;
  6. import java.nio.ByteBuffer;
  7.  
  8. import static org.lwjgl.system.MemoryUtil.*;
  9. import static org.lwjgl.system.glfw.GLFW.*;
  10.  
  11. import static org.lwjgl.opengl.GL11.*;
  12.  
  13. public class Window
  14. {
  15.     private long id;
  16.     private final Game game;
  17.     public final int width;
  18.     public final int height;
  19.     private final CharSequence title;
  20.     private final double aspect;
  21.  
  22.     public Window(Game g, int w, int h, CharSequence t)
  23.     {
  24.         game = g;
  25.         width = w;
  26.         height = h;
  27.         title = t;
  28.         aspect = (double)width / (double)height;
  29.  
  30.         game.parent = this;
  31.  
  32.         if ((id = glfwCreateWindow(width, height, title, NULL, NULL)) == NULL)
  33.             throw new RuntimeException("Window couldn't be created");
  34.            
  35.         ByteBuffer vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  36.        
  37.         glfwSetWindowPos(
  38.                                 id,
  39.                                 (GLFWvidmode.width(vidMode) - width) / 2,
  40.                                 (GLFWvidmode.height(vidMode) - height) / 2
  41.         );
  42.  
  43.         glfwMakeContextCurrent(id);
  44.         GLContext.createFromCurrent();
  45.  
  46.         glViewport(0, 0, width, height);
  47.         glMatrixMode(GL_PROJECTION);
  48.         glOrtho(0, width, 0, height, -1, 1);
  49.         glMatrixMode(GL_MODELVIEW);
  50.         glLoadIdentity();
  51.  
  52.         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  53.  
  54.         glfwShowWindow(id);
  55.  
  56.         game.init();
  57.  
  58.         while ( glfwWindowShouldClose(id) == GL_FALSE ) {
  59.             update();
  60.             render();
  61.         }
  62.  
  63.         glfwDestroyWindow(id);
  64.     }
  65.  
  66.     private void update()
  67.     {
  68.         game.update(this);
  69.     }
  70.  
  71.     private void render()
  72.     {
  73.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  74.         glLoadIdentity();
  75.  
  76.         game.render(this);
  77.         System.out.println(glGetError());
  78.  
  79.         glfwSwapBuffers(id);
  80.         glfwPollEvents();
  81.     }
  82.  
  83.     public static void initGLFW()
  84.     {
  85.         glfwSetErrorCallback(ErrorCallback.Util.getDefault());
  86.  
  87.         if (glfwInit() != GL_TRUE)
  88.             throw new RuntimeException("GLFW couldn't be initialized");
  89.  
  90.         glfwDefaultWindowHints();
  91.         glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
  92.         glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment