Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (739)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (820)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: ClaasJG (posted 2016-11-10 17:36:32, viewed 2318 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236  
import static org.lwjgl.glfw.GLFW.GLFW_FALSE;
import static org.lwjgl.glfw.GLFW.GLFW_RESIZABLE;
import static org.lwjgl.glfw.GLFW.GLFW_TRUE;
import static org.lwjgl.glfw.GLFW.GLFW_VISIBLE;
import static org.lwjgl.glfw.GLFW.glfwCreateWindow;
import static org.lwjgl.glfw.GLFW.glfwDefaultWindowHints;
import static org.lwjgl.glfw.GLFW.glfwDestroyWindow;
import static org.lwjgl.glfw.GLFW.glfwHideWindow;
import static org.lwjgl.glfw.GLFW.glfwInit;
import static org.lwjgl.glfw.GLFW.glfwMakeContextCurrent;
import static org.lwjgl.glfw.GLFW.glfwPostEmptyEvent;
import static org.lwjgl.glfw.GLFW.glfwSetErrorCallback;
import static org.lwjgl.glfw.GLFW.glfwShowWindow;
import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
import static org.lwjgl.glfw.GLFW.glfwTerminate;
import static org.lwjgl.glfw.GLFW.glfwWaitEvents;
import static org.lwjgl.glfw.GLFW.glfwWindowHint;
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLCapabilities;

/**
 * @license <a href="http://www.wtfpl.net/">WTFPL</a>
 * @author ClaasJG
 */
public class Window {

   private static final Thread GL_THREAD;
   private static final Queue<Runnable> GLFW_THREAD_EVENTS;
   private static Thread glfwThread;
   private static final List<Window> WINDOWS;
   private static boolean windowSystemRunning;

   static {
      GLFW_THREAD_EVENTS = new ConcurrentLinkedQueue<>();

      WINDOWS = new CopyOnWriteArrayList<>();
      GL_THREAD = new Thread() {
         @Override
         public void run() {
            while (true) {
               while (!WINDOWS.isEmpty()) {
                  for (Window window : WINDOWS) {
                     if (window.lock.tryLock()) {
                        try {
                           glfwMakeContextCurrent(window.id);
                           GL.setCapabilities(window.capabilities);
                           window.render();
--                        } catch (java.lang.IllegalStateException ise) {
--                           throw ise;
                        } finally {
                           GLFW.glfwMakeContextCurrent(0);
                           window.lock.unlock();
                        }
                     }
                  }
               }

               try {
                  synchronized (GL_THREAD) {
                     while (WINDOWS.isEmpty())
                        GL_THREAD.wait();
                  }
               } catch (InterruptedException e) {
                  throw new RuntimeException("Error waiting!", e);
               }
            }
         }
      };
      GL_THREAD.setDaemon(true);
      GL_THREAD.setName("GL THREAD");
      GL_THREAD.start();
   }

   public static void onGlfwThread(Runnable r, boolean wait) {
      if (Thread.currentThread() == glfwThread)
         r.run();
      else {
         if (wait) {
            Throwable[] error = { null };
            CountDownLatch cbLatch = new CountDownLatch(1);
            onGlfwThread(() -> {
               try {
                  r.run();
               } catch (Throwable t) {
                  error[0] = t;
               } finally {
                  cbLatch.countDown();
               }
            }, false);
            try {
               cbLatch.await();
            } catch (InterruptedException e) {
               throw new RuntimeException("Error waiting for callback latch", e);
            }

            if (null != error[0])
               throw new RuntimeException("Error in callback '" + r + "'", error[0]);
         } else {
            GLFW_THREAD_EVENTS.add(r);
            glfwPostEmptyEvent();
         }
      }
   }

   public static void shutDown() {
      windowSystemRunning = false;
   }

   public static void startUp(Runnable init) {
      glfwThread = Thread.currentThread();

      GLFWErrorCallback.createThrow().set();

      if (!glfwInit())
         throw new RuntimeException("Error initializing glfw!");

      glfwDefaultWindowHints();
      glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
      glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

      if (null != init)
         init.run();

      windowSystemRunning = true;
      while (windowSystemRunning) {
         glfwWaitEvents();
         for (Runnable r; null != (r = GLFW_THREAD_EVENTS.poll()); r.run())
            ;
      }

      glfwTerminate();
      glfwSetErrorCallback(null).free();

      WINDOWS.clear();
      glfwThread = null;
   }

   private final GLCapabilities capabilities;

   private final long id;

   private final Lock lock;

   private final List<Consumer<Window>> renderers;

   public Window(int width, int height, String title, Window shared) {
      lock = new ReentrantLock();
      renderers = new CopyOnWriteArrayList<>();

      long[] idH = { 0 };
      GLCapabilities[] capsH = { null };
      onGlfwThread(() -> {
         if (null == shared)
            idH[0] = glfwCreateWindow(width, height, title, 0, 0);
         else {
            shared.lock.lock();// Not current on any other thread now!
            try {
               idH[0] = glfwCreateWindow(128, 128, title, 0, shared.id);
            } finally {
               shared.lock.unlock();
            }
         }

         if (0 == idH[0])
            throw new RuntimeException("Error creating window!");

         glfwMakeContextCurrent(idH[0]);
         capsH[0] = GL.createCapabilities();
         glfwMakeContextCurrent(0);
      }, true);
      capabilities = capsH[0];
      id = idH[0];

      WINDOWS.add(this);
      synchronized (GL_THREAD) {
         GL_THREAD.notifyAll();
      }
   }

   public Window addRenderer(Consumer<Window> renderer) {
      renderers.add(renderer);
      return this;
   }

   public void destroy() {
      WINDOWS.remove(this);
      onGlfwThread(() -> {
         try {
            this.lock.lock();
            glfwDestroyWindow(id);
         } finally {
            this.lock.unlock();
         }
      }, false);
   }

   public Window removeRenderer(Consumer<Window> renderer) {
      renderers.remove(renderer);
      return this;
   }

   private void render() {
      for (Consumer<Window> renderer : renderers)
         renderer.accept(this);
   }

   public void setVisible(boolean visible) {
      onGlfwThread(() -> {
         if (visible)
            glfwShowWindow(id);
         else
            glfwHideWindow(id);
      }, false);
   }

   public boolean shouldClose() {
      return glfwWindowShouldClose(id);
   }

   public void swapBuffer() {
      glfwSwapBuffers(id);
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 
Ecumene (57 views)
2017-09-30 02:57:34

theagentd (80 views)
2017-09-26 18:23:31

cybrmynd (187 views)
2017-08-02 12:28:51

cybrmynd (185 views)
2017-08-02 12:19:43

cybrmynd (192 views)
2017-08-02 12:18:09

Sralse (203 views)
2017-07-25 17:13:48

Archive (767 views)
2017-04-27 17:45:51

buddyBro (902 views)
2017-04-05 03:38:00

CopyableCougar4 (1452 views)
2017-03-24 15:39:42

theagentd (1327 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!