Exemple #1
0
  private void initgl() {
    final Thread caller = Thread.currentThread();
    final haven.error.ErrorHandler h = haven.error.ErrorHandler.find();
    addGLEventListener(
        new GLEventListener() {
          Debug.DumpGL dump = null;

          public void display(GLAutoDrawable d) {
            GL2 gl = d.getGL().getGL2();
            /*
               if((dump == null) || (dump.getDownstreamGL() != gl))
            dump = new Debug.DumpGL((GL4bc)gl);
               if(Debug.kf2 && !Debug.pk2)
            dump.dump("/tmp/gldump");
               dump.reset();
               gl = dump;
               */
            if (inited) redraw(gl);
          }

          public void init(GLAutoDrawable d) {
            GL gl = d.getGL();
            glconf = GLConfig.fromgl(gl, d.getContext(), getChosenGLCapabilities());
            glconf.pref = GLSettings.load(glconf, true);
            ui.cons.add(glconf);
            if (h != null) {
              h.lsetprop("gl.vendor", gl.glGetString(gl.GL_VENDOR));
              h.lsetprop("gl.version", gl.glGetString(gl.GL_VERSION));
              h.lsetprop("gl.renderer", gl.glGetString(gl.GL_RENDERER));
              h.lsetprop("gl.exts", Arrays.asList(gl.glGetString(gl.GL_EXTENSIONS).split(" ")));
              h.lsetprop("gl.caps", d.getChosenGLCapabilities().toString());
              h.lsetprop("gl.conf", glconf);
            }
            gstate =
                new GLState() {
                  public void apply(GOut g) {
                    BGL gl = g.gl;
                    gl.glColor3f(1, 1, 1);
                    gl.glPointSize(4);
                    gl.joglSetSwapInterval(1);
                    gl.glEnable(GL.GL_BLEND);
                    // gl.glEnable(GL.GL_LINE_SMOOTH);
                    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
                    if (g.gc.glmajver >= 2) gl.glBlendEquationSeparate(GL.GL_FUNC_ADD, GL2.GL_MAX);
                    if (g.gc.havefsaa()) {
                      /* Apparently, having sample
                       * buffers in the config enables
                       * multisampling by default on
                       * some systems. */
                      g.gl.glDisable(GL.GL_MULTISAMPLE);
                    }
                    GOut.checkerr(gl);
                  }

                  public void unapply(GOut g) {}

                  public void prep(Buffer buf) {
                    buf.put(global, this);
                  }
                };
          }

          public void reshape(
              GLAutoDrawable d, final int x, final int y, final int w, final int h) {
            ostate = OrthoState.fixed(new Coord(w, h));
            rtstate =
                new GLState() {
                  public void apply(GOut g) {
                    g.st.proj = Projection.makeortho(new Matrix4f(), 0, w, 0, h, -1, 1);
                  }

                  public void unapply(GOut g) {}

                  public void prep(Buffer buf) {
                    buf.put(proj2d, this);
                  }
                };
            HavenPanel.this.w = w;
            HavenPanel.this.h = h;
          }

          public void displayChanged(GLAutoDrawable d, boolean cp1, boolean cp2) {}

          public void dispose(GLAutoDrawable d) {}
        });
  }
Exemple #2
0
  public void run() {
    try {
      Thread drawthread = new HackThread(drawfun, "Render thread");
      drawthread.start();
      synchronized (drawfun) {
        while (state == null) drawfun.wait();
      }
      try {
        long now, then;
        long frames[] = new long[128];
        int framep = 0, waited[] = new int[128];
        while (true) {
          int fwaited = 0;
          Debug.cycle();
          UI ui = this.ui;
          then = System.currentTimeMillis();
          CPUProfile.Frame curf = null;
          if (Config.profile) curf = uprof.new Frame();
          synchronized (ui) {
            if (ui.sess != null) ui.sess.glob.ctick();
            dispatch();
            ui.tick();
            if ((ui.root.sz.x != w) || (ui.root.sz.y != h)) ui.root.resize(new Coord(w, h));
          }
          if (curf != null) curf.tick("dsp");

          BGL buf = new BGL();
          GLState.Applier state = this.state;
          rootdraw(state, ui, buf);
          if (curf != null) curf.tick("draw");
          synchronized (drawfun) {
            now = System.currentTimeMillis();
            while (bufdraw != null) drawfun.wait();
            bufdraw = new Frame(buf, state.cgl);
            drawfun.notifyAll();
            fwaited += System.currentTimeMillis() - now;
          }

          ui.audio.cycle();
          if (curf != null) curf.tick("aux");

          now = System.currentTimeMillis();
          long fd = bgmode ? this.bgfd : this.fd;
          if (now - then < fd) {
            synchronized (events) {
              events.wait(fd - (now - then));
            }
            fwaited += System.currentTimeMillis() - now;
          }

          frames[framep] = now;
          waited[framep] = fwaited;
          for (int i = 0, ckf = framep, twait = 0; i < frames.length; i++) {
            ckf = (ckf - 1 + frames.length) % frames.length;
            twait += waited[ckf];
            if (now - frames[ckf] > 1000) {
              fps = i;
              uidle = ((double) twait) / ((double) (now - frames[ckf]));
              break;
            }
          }
          framep = (framep + 1) % frames.length;

          if (curf != null) curf.tick("wait");
          if (curf != null) curf.fin();
          if (Thread.interrupted()) throw (new InterruptedException());
        }
      } finally {
        drawthread.interrupt();
        drawthread.join();
      }
    } catch (InterruptedException e) {
    } finally {
      ui.destroy();
    }
  }