private static void killAll() { // Kill any waiting threads for (Thread t : Thread.getAllStackTraces().keySet()) { if (t.getState() == Thread.State.WAITING && !t.getName().contains("Disposer")) { t.interrupt(); } } System.exit(0); // Exit }
/** 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(); }
/** Runs the game (the "main loop") */ private static void run() { while (!finished) { // Always call Window.update(), all the time Display.update(); if (Display.isCloseRequested()) { // Check for O/S close requests finished = true; } else if (Display.isActive()) { // The window is in the foreground, so we should play the game logic(); render(); Display.sync(FRAMERATE); } else { // The window is not in the foreground, so we can allow other stuff to run and // infrequently update try { Thread.sleep(100); } catch (InterruptedException e) { } logic(); if (Display.isVisible() || Display.isDirty()) { // Only bother rendering if the window is visible or dirty render(); } } } }
public void addWindow(final String id, final Lwjgl3Application app) { if (app.autoloop) // cannot have a running loop return; Thread thisThread = Thread.currentThread(); if (thisThread == mainThread) { if (getWindow(id) == null) { app.id = id; queueWindows.add(app); } } else { Runnable run = new Runnable() { @Override public void run() { if (getWindow(id) == null) { app.id = id; queueWindows.add(app); } } }; postMainRunnable(run); } }
public static void main(String[] args) throws InterruptedException { try { Display.setTitle("Simple Square"); Display.create(); } catch (LWJGLException e) { e.printStackTrace(); } // draw the square glBegin(GL_LINE_LOOP); glVertex3f(-0.5f, -0.5f, 0f); glVertex3f(-0.5f, 0.5f, 0f); glVertex3f(0.5f, 0.5f, 0f); glVertex3f(0.5f, -0.5f, 0f); glEnd(); // flush any commands that are still hanging about. glFlush(); // update window. Display.update(); while (!Display.isCloseRequested()) { Thread.sleep(100); } Display.destroy(); }
@Override protected void logic() { try { Thread.sleep(16, 666); } catch (InterruptedException ex) { Logger.getLogger(Kostka.class.getName()).log(Level.SEVERE, null, ex); } }
public void postWindowRunnableAndWait(Runnable runnable) { synchronized (SYNC) { windowRunnablesWait = runnable; } while (windowRunnablesWait != null) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public static boolean isInRenderThread() { return Thread.currentThread().getId() == RENDER_THREAD_ID; }
@Override public void onUpdate() { cur = now; now = getTimeSinceStartMillis(); TIME_DELTA = (now - cur) / 100.0f; if (current_world != null && !isPaused()) { if (!dontUpdate) { Iterator<UpdatableDrawable> updates = current_world.getUpdatables(); while (updates.hasNext()) { UpdatableDrawable s = updates.next(); if (s == null) continue; try { s.update(); } catch (Throwable t) { t.printStackTrace(); } } } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearColor(0f, 0f, 0f, 1f); // ROBO //todo get this crap changed into proper ogl glMatrixMode(GL_MODELVIEW); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glLoadIdentity(); glScalef(ZOOM_SCALE, ZOOM_SCALE, 1f); // ROBO //todo change this crap into proper postprocess and per material shaders ShaderFactory.executePreRender(); if (isFading) { long time = getTime() - startTime; curAlpha = Camera.ease(startAlpha, newAlpha, speed, time); if (curAlpha == newAlpha) { isFading = false; } } if (isFadingBox) { long time = getTime() - startTime; float alpha = Camera.ease(0f, 1f, speed, time); if (alpha == 1f) { isFadingBox = false; } glColor4f(1f, 1f, 1f, alpha); renderBox(); } glTranslatef(-Camera.getX(), -Camera.getY(), 0f); glColor4f(1f, 1f, 1f, curAlpha); // ROBO //todo get all these into proper batches if (curAlpha > 0f) { Iterator<Drawable> usprites = current_world.getUnsortedDrawables(); while (usprites.hasNext()) { Drawable s = usprites.next(); if (s == null) continue; try { if (!s.neverClip() && (!(s instanceof TileObject) || !((TileObject) s).isParallaxLayer()) && Camera.isOffScreen(s.getX(), s.getY(), s.getWidth() / 2, s.getHeight() / 2)) continue; s.render(); } catch (Throwable t) { t.printStackTrace(); } } Iterator<Drawable> sprites = current_world.getSortedDrawables(); while (sprites.hasNext()) { Drawable s = sprites.next(); if (s == null) continue; try { if (!s.neverClip() && (!(s instanceof TileObject) || !((TileObject) s).isParallaxLayer()) && Camera.isOffScreen(s.getX(), s.getY(), s.getWidth() / 2, s.getHeight() / 2)) continue; s.render(); } catch (Throwable t) { t.printStackTrace(); } } } ShaderFactory.executePostRender(); glLoadIdentity(); glScalef(ZOOM_SCALE, ZOOM_SCALE, 1f); if (show_ui) { for (UI e : current_world.getElements()) { if (e == null) return; try { e.render(); } catch (Throwable t) { t.printStackTrace(); } } } if (next_world != null) { long time = getTime() - fadeStartTime; float percent; if (time > fadeDuration) { percent = 1; } else { percent = time / fadeDuration; } float emu = curAlpha; curAlpha = percent; // Some drawables require the curAlpha to be set, so we save what it use to be // and set it glColor4f(1f, 1f, 1f, curAlpha); Iterator<Drawable> next_sprites = next_world.getSortedDrawables(); while (next_sprites.hasNext()) { Drawable s = next_sprites.next(); if (s == null) continue; try { if (!s.neverClip() && (!(s instanceof TileObject) || !((TileObject) s).isParallaxLayer()) && Camera.isOffScreen(s.getX(), s.getY(), s.getWidth() / 2, s.getHeight() / 2)) continue; s.render(); } catch (Throwable t) { t.printStackTrace(); } } glLoadIdentity(); glScalef(ZOOM_SCALE, ZOOM_SCALE, 1f); for (UI e : next_world.getElements()) { if (e == null) return; try { e.render(); } catch (Throwable t) { t.printStackTrace(); } } curAlpha = emu; if (percent == 1) { current_world.onUnload(); current_world = next_world; current_world.onDisplay(); next_world = null; } } try { AnimationFactory.executeTick(); // Execute any animation } catch (IllegalAccessException e) { e.printStackTrace(); } Camera.executeAnimation(); // Execute any interlop // exitOnGLError("RenderService.renderSprites"); Display.update(); fpsTime += TIME_DELTA; fpsCount++; if (fpsCount == 100) { fpsCount = 0; FPS = (1000f / fpsTime); Display.setTitle("FPS: " + FPS); fpsTime = 0; } if (Display.isCloseRequested()) { kill(); } if (GameSettings.Graphics.FPSLimit != -1) { Display.sync(GameSettings.Graphics.FPSLimit); } } else { try { Thread.sleep(15); // Keep the thread busy } catch (InterruptedException e) { e.printStackTrace(); } } }
@Override protected void onStart() { INSTANCE = this; RENDER_THREAD_ID = Thread.currentThread().getId(); try { loadIcons(); Display.setIcon(icons); Display.setVSyncEnabled(GameSettings.Graphics.vsync); } catch (IOException e) { e.printStackTrace(); } try { setDisplayMode( GameSettings.Display.window_width, GameSettings.Display.window_height, GameSettings.Display.fullscreen); Display.create(); // ROBO //todo get all this changed to proper OGL glClearColor(0f, 0f, 0f, 1f); glClearDepth(1f); glViewport(0, 0, GameSettings.Display.window_width, GameSettings.Display.window_height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho( 0.0f, GameSettings.Display.resolution.getWidth(), GameSettings.Display.resolution.getHeight(), 0.0f, 0.1f, -1f); glMatrixMode(GL_MODELVIEW); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glLoadIdentity(); curAlpha = 1f; capabilities = GLContext.getCapabilities(); System.out.println("OpenGL version: " + glGetString(GL_VERSION)); System.out.println("Building shaders.."); long ms = getTime(); ShaderFactory.buildAllShaders(); System.out.println("Done! Took " + (getTime() - ms) + "ms."); } catch (LWJGLException e) { e.printStackTrace(); System.exit(0); } catch (Exception e) { e.printStackTrace(); System.exit(0); } finally { if (looping) { looping = false; Display.destroy(); } } started = getTime(); next_tick = getTime(); getDelta(); }
public void waitForFade() throws InterruptedException { if (current_world == null) return; while (isFading) { Thread.sleep((long) speed); } }
private void init() throws LWJGLException { // create Window of size 800x600 Display.setDisplayMode(new DisplayMode(1024, 768)); Display.setLocation( (Display.getDisplayMode().getWidth() - 300) / 2, (Display.getDisplayMode().getHeight() - 300) / 2); Display.setTitle("Gears"); try { Display.create(); } catch (LWJGLException e) { // This COULD be because of a bug! A delay followed by a new attempt is supposed to fix it. e.printStackTrace(); try { Thread.sleep(1000); } catch (InterruptedException ignored) { } Display.create(); } // setup ogl FloatBuffer pos = BufferUtils.createFloatBuffer(4).put(new float[] {5.0f, 5.0f, 10.0f, 0.0f}); FloatBuffer red = BufferUtils.createFloatBuffer(4).put(new float[] {0.8f, 0.1f, 0.0f, 1.0f}); FloatBuffer green = BufferUtils.createFloatBuffer(4).put(new float[] {0.0f, 0.8f, 0.2f, 1.0f}); FloatBuffer blue = BufferUtils.createFloatBuffer(4).put(new float[] {0.2f, 0.2f, 1.0f, 1.0f}); pos.flip(); red.flip(); green.flip(); blue.flip(); glLight(GL_LIGHT0, GL_POSITION, pos); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); /* make the gears */ gear1 = glGenLists(1); glNewList(gear1, GL_COMPILE); glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red); gear(1.0f, 4.0f, 1.0f, 20, 0.7f); glEndList(); gear2 = glGenLists(1); glNewList(gear2, GL_COMPILE); glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green); gear(0.5f, 2.0f, 2.0f, 10, 0.7f); glEndList(); gear3 = glGenLists(1); glNewList(gear3, GL_COMPILE); glMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue); gear(1.3f, 2.0f, 0.5f, 10, 0.7f); glEndList(); glEnable(GL_NORMALIZE); glMatrixMode(GL_PROJECTION); System.err.println("LWJGL: " + Sys.getVersion() + " / " + LWJGLUtil.getPlatformName()); System.err.println("GL_VENDOR: " + glGetString(GL_VENDOR)); System.err.println("GL_RENDERER: " + glGetString(GL_RENDERER)); System.err.println("GL_VERSION: " + glGetString(GL_VERSION)); System.err.println(); System.err.println( "glLoadTransposeMatrixfARB() supported: " + GLContext.getCapabilities().GL_ARB_transpose_matrix); if (!GLContext.getCapabilities().GL_ARB_transpose_matrix) { // --- not using extensions glLoadIdentity(); } else { // --- using extensions final FloatBuffer identityTranspose = BufferUtils.createFloatBuffer(16) .put(new float[] {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}); identityTranspose.flip(); glLoadTransposeMatrixARB(identityTranspose); } float h = (float) 300 / (float) 300; glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -40.0f); }
private void start() { running = true; thread = new Thread(this, "ContinuousWindow"); thread.start(); }