/** Main Loop for GLFW. This call will block <br> */
  public void start() {
    mainThread = Thread.currentThread();
    windowThread.start();
    running = true;
    while (running) {
      executeMainRunnables();

      for (int i = 0; i < queueWindows.size; ) {
        final Lwjgl3Application app = queueWindows.removeIndex(i);
        i--;

        Runnable run =
            new Runnable() {
              @Override
              public void run() {
                glfwMakeContextCurrent(0);
                jumpLoop = true;
              }
            };
        postWindowRunnableAndWait(run);

        initWindow(app);
        glfwShowWindow(app.graphics.window);

        Runnable run2 =
            new Runnable() {
              @Override
              public void run() {
                initContext(app);
                windows.add(app);
                tmpfirst = true;
              }
            };
        postWindowRunnable(run2);
        jumpLoop = false;
        break;
      }

      if (tmpfirst && windows.size == 0) {
        running = false;
      }

      glfwWaitEvents();
    }
    try {
      windowThread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    glfwTerminate();
    if (errorCallback != null) errorCallback.release();
  }
示例#2
0
  public void init() {
    // if not true after being executed then it will fail. - THIS MUST RETURN TRUE IN ORDER TO
    // CONTINUE
    if (glfwInit() != GL_TRUE) {
      // Once GLFW is initialised - window can be created
      System.err.println("GLFW initialisation has failed");
      throw new IllegalStateException("Unable to initialize GLFW");
    }

    // indicate that the window we wish to create is to be made resizable
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

    //           glfwCreateWindow(width, height, title, monitor, share)
    window = glfwCreateWindow(800, 600, "Mikes Tester Window", NULL, NULL);

    // if window is not populated with necessary bytes - then it fails
    if (window == NULL) {
      System.err.println("Failed to create window");
      glfwTerminate();
      throw new RuntimeException("Failed to create the GLFW window");
    }

    glfwSetKeyCallback(window, keyCallback = new Input());

    // Returns the video resolution of primary monitor
    GLFWVidMode vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    //

    int WIDTH = 300;
    int HEIGHT = 300;

    // Centre the window
    glfwSetWindowPos(window, (vidMode.width() - WIDTH) / 2, (vidMode.height() - HEIGHT) / 2);

    // Make the OpenGL context current
    glfwMakeContextCurrent(window);

    // library can detect the context and make the OpenGL bindings available for use.
    GL.createCapabilities();

    // Enable v-sync
    glfwSwapInterval(1);
    // show the window
    glfwShowWindow(window);

    // glClearColor(056f, 0.258f, 0.425f, 1.0f);

    glEnable(GL_DEPTH_TEST);

    System.out.println("OPenGL: " + glGetString(GL_VERSION));
  }
示例#3
0
  public void run() {
    System.out.println("Hello LWJGL " + Sys.getVersion() + "!");

    try {
      init();
      loop();

      // Release window and window callbacks
      glfwDestroyWindow(window);
      keyCallback.release();
    } finally {
      // Terminate GLFW and release the GLFWerrorfun
      glfwTerminate();
      errorCallback.release();
    }
  }
示例#4
0
文件: Main.java 项目: Osoldier/Vox
  public void run() {
    init();
    engine = new Engine();

    long lastTime = System.nanoTime();
    double delta = 0.0;
    // Amout of nanoseconds in 1/60th of a second
    double ns = 1000000000.0 / 60.0;
    long timer = System.currentTimeMillis();
    int updates = 0;
    int frames = 0;
    while (running) {
      long now = System.nanoTime();
      delta += (now - lastTime) / ns;
      lastTime = now;
      // if delta >= than one then the amount of time >= 1/60th of a second
      if (delta >= 1.0) {
        update();
        updates++;
        delta--;
      }
      render();
      frames++;
      // If a second has passed, we print the stats
      if (System.currentTimeMillis() - timer > 1000) {
        timer += 1000;
        System.out.println(updates + " ups, " + frames + " fps");
        updates = 0;
        frames = 0;
      }
      if (glfwWindowShouldClose(window) == GL_TRUE) running = false;
    }
    engine.CleanUp();
    keyCallback.release();
    sizeCallback.release();
    mouseCallback.release();
    focusCallback.release();
    glfwDestroyWindow(window);
    glfwTerminate();
  }