private void run() { int frames = 0; double nsPerFrame = 1000000000.0 / 60.0; double unProcessed = 0; long lastFrameTime = System.nanoTime(); long currentFrameTime = System.currentTimeMillis(); while (running) { long now = System.nanoTime(); unProcessed += (now - lastFrameTime) / nsPerFrame; lastFrameTime = now; if (Display.isCloseRequested()) running = false; while (unProcessed > 1) { unProcessed -= 1; tick(); display.render(); } frames++; if (System.currentTimeMillis() - currentFrameTime > 1000) { currentFrameTime += 1000; // Print.line(frames + " fps"); Display.setTitle(frames + " fps"); frames = 0; } Display.sync(60); Display.update(); } Display.destroy(); }
private void submitFrame() { if (!broadcastController.getIsBroadcasting() || broadcastController.getIsPaused()) { return; } long curTime = System.nanoTime(); long nanoPerFrame = 1000000000 / broadcastFramesPerSecond; // If you send frames too quickly to the SDK (based on the broadcast FPS you configured) it will // not be able // to make use of them all. In that case, it will simply release buffers without using them // which means the // game wasted time doing the capture. To mitigate this, the app should pace the captures to // the broadcast FPS. long captureDelta = curTime - lastCaptureTime; boolean isTimeForNextCapture = captureDelta >= nanoPerFrame; if (!isTimeForNextCapture) { return; } FrameBuffer buffer = broadcastController.getNextFreeBuffer(); broadcastController.captureFrameBuffer_ReadPixels(buffer); broadcastController.submitFrame(buffer); lastCaptureTime = curTime; }
private void loop() { long lastFrameTime = 0; while (!Display.isCloseRequested()) { long now = System.nanoTime(); long renderFps = 30; long nanoPerFrame = 1000000000 / renderFps; // update the animation if (now - lastFrameTime >= nanoPerFrame) { lastFrameTime = now; angle += 2.0f; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(view_rotx, 1.0f, 0.0f, 0.0f); glRotatef(view_roty, 0.0f, 1.0f, 0.0f); glRotatef(view_rotz, 0.0f, 0.0f, 1.0f); glPushMatrix(); glTranslatef(-3.0f, -2.0f, 0.0f); glRotatef(angle, 0.0f, 0.0f, 1.0f); glCallList(gear1); glPopMatrix(); glPushMatrix(); glTranslatef(3.1f, -2.0f, 0.0f); glRotatef(-2.0f * angle - 9.0f, 0.0f, 0.0f, 1.0f); glCallList(gear2); glPopMatrix(); glPushMatrix(); glTranslatef(-3.1f, 4.2f, 0.0f); glRotatef(-2.0f * angle - 25.0f, 0.0f, 0.0f, 1.0f); glCallList(gear3); glPopMatrix(); glPopMatrix(); Display.update(); } handleInput(); if (broadcastController != null) { submitFrame(); broadcastController.update(); } if (chatController != null) { chatController.update(); } } }
/** * The constructor that creates the font from the given file at the given height. * * @param filePath - The path to file including the file type * @param fontHeight - The height (size) of the font */ public TrueTypeFont(String filePath, int fontHeight) { this.fontHeight = fontHeight; long startTime = 0L; if (Debug.enabled) startTime = System.nanoTime(); textureID = glGenTextures(); cdata = STBTTBakedChar.mallocBuffer(96); try { ByteBuffer ttf = IOUtil.ioResourceToByteBuffer(filePath, 160 * 1024); ByteBuffer bitmap = BufferUtils.createByteBuffer(BITMAP_W * BITMAP_H); STBTruetype.stbtt_BakeFontBitmap(ttf, fontHeight, bitmap, BITMAP_W, BITMAP_H, 32, cdata); glBindTexture(GL_TEXTURE_2D, textureID); glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, BITMAP_W, BITMAP_H, 0, GL_ALPHA, GL_UNSIGNED_BYTE, bitmap); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } catch (IOException e) { throw new RuntimeException(e); } xbuf = BufferUtils.createFloatBuffer(1); ybuf = BufferUtils.createFloatBuffer(1); quad = STBTTAlignedQuad.malloc(); if (Debug.enabled) { long endTime = System.nanoTime(); Debug.println( " Loaded font: " + filePath + "\n\tFont height: " + fontHeight + "px" + "\n\tLoad time: " + Debug.noNotation.format((endTime - startTime) / 1000000000.0) + "s", Debug.ANSI_CYAN); } }
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(); }
public static long getTime() { return System.nanoTime() / 1000000; }