void dispatch() { synchronized (events) { if (mousemv != null) { mousepos = new Coord(mousemv.getX(), mousemv.getY()); ui.mousemove(mousemv, mousepos); mousemv = null; } InputEvent e = null; while ((e = events.poll()) != null) { if (e instanceof MouseEvent) { MouseEvent me = (MouseEvent) e; if (me.getID() == MouseEvent.MOUSE_PRESSED) { ui.mousedown(me, new Coord(me.getX(), me.getY()), me.getButton()); } else if (me.getID() == MouseEvent.MOUSE_RELEASED) { ui.mouseup(me, new Coord(me.getX(), me.getY()), me.getButton()); } else if (me instanceof MouseWheelEvent) { ui.mousewheel( me, new Coord(me.getX(), me.getY()), ((MouseWheelEvent) me).getWheelRotation()); } } else if (e instanceof KeyEvent) { KeyEvent ke = (KeyEvent) e; if (ke.getID() == KeyEvent.KEY_PRESSED) { ui.keydown(ke); } else if (ke.getID() == KeyEvent.KEY_RELEASED) { ui.keyup(ke); } else if (ke.getID() == KeyEvent.KEY_TYPED) { ui.type(ke); } } ui.lastevent = System.currentTimeMillis(); } } }
UI newui(Session sess) { if (ui != null) ui.destroy(); ui = new UI(new Coord(w, h), sess); ui.root.guprof = uprof; ui.root.grprof = rprof; ui.root.ggprof = gprof; if (getParent() instanceof Console.Directory) ui.cons.add((Console.Directory) getParent()); ui.cons.add(this); if (glconf != null) ui.cons.add(glconf); return (ui); }
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(); } }
void rootdraw(GLState.Applier state, UI ui, BGL gl) { GLState.Buffer ibuf = new GLState.Buffer(state.cfg); gstate.prep(ibuf); ostate.prep(ibuf); GOut g = new GOut(gl, state.cgl, state.cfg, state, ibuf, new Coord(w, h)); state.set(ibuf); g.state(ostate); g.apply(); gl.glClearColor(0, 0, 0, 1); gl.glClear(GL.GL_COLOR_BUFFER_BIT); synchronized (ui) { ui.draw(g); } if (Config.dbtext) { int y = h - 150; FastText.aprintf( g, new Coord(10, y -= 15), 0, 1, "FPS: %d (%d%%, %d%% idle)", fps, (int) (uidle * 100.0), (int) (ridle * 100.0)); Runtime rt = Runtime.getRuntime(); long free = rt.freeMemory(), total = rt.totalMemory(); FastText.aprintf( g, new Coord(10, y -= 15), 0, 1, "Mem: %,011d/%,011d/%,011d/%,011d", free, total - free, total, rt.maxMemory()); FastText.aprintf(g, new Coord(10, y -= 15), 0, 1, "Tex-current: %d", TexGL.num()); FastText.aprintf(g, new Coord(10, y -= 15), 0, 1, "GL progs: %d", g.st.numprogs()); GameUI gi = ui.root.findchild(GameUI.class); if ((gi != null) && (gi.map != null)) { try { FastText.aprintf( g, new Coord(10, y -= 15), 0, 1, "MV pos: %s (%s)", gi.map.getcc(), gi.map.camera); } catch (Loading e) { } if (gi.map.rls != null) FastText.aprintf( g, new Coord(10, y -= 15), 0, 1, "Rendered: %,d+%,d(%,d)", gi.map.rls.drawn, gi.map.rls.instanced, gi.map.rls.instancified); } if (Resource.remote().qdepth() > 0) FastText.aprintf( g, new Coord(10, y -= 15), 0, 1, "RQ depth: %d (%d)", Resource.remote().qdepth(), Resource.remote().numloaded()); } Object tooltip; try { synchronized (ui) { tooltip = ui.root.tooltip(mousepos, ui.root); } } catch (Loading e) { tooltip = "..."; } Tex tt = null; if (tooltip != null) { if (tooltip instanceof Text) { tt = ((Text) tooltip).tex(); } else if (tooltip instanceof Tex) { tt = (Tex) tooltip; } else if (tooltip instanceof Indir<?>) { Indir<?> t = (Indir<?>) tooltip; Object o = t.get(); if (o instanceof Tex) tt = (Tex) o; } else if (tooltip instanceof String) { if (((String) tooltip).length() > 0) tt = (Text.render((String) tooltip)).tex(); } } if (tt != null) { Coord sz = tt.sz(); Coord pos = mousepos.add(sz.inv()); if (pos.x < 0) pos.x = 0; if (pos.y < 0) pos.y = 0; g.chcolor(244, 247, 21, 192); g.rect(pos.add(-3, -3), sz.add(6, 6)); g.chcolor(35, 35, 35, 192); g.frect(pos.add(-2, -2), sz.add(4, 4)); g.chcolor(); g.image(tt, pos); } ui.lasttip = tooltip; Resource curs = ui.root.getcurs(mousepos); if (curs != null) { if (cursmode == "awt") { if (curs != lastcursor) { try { setCursor(makeawtcurs(curs.layer(Resource.imgc).img, curs.layer(Resource.negc).cc)); lastcursor = curs; } catch (Exception e) { cursmode = "tex"; } } } else if (cursmode == "tex") { Coord dc = mousepos.add(curs.layer(Resource.negc).cc.inv()); g.image(curs.layer(Resource.imgc), dc); } } state.clean(); GLObject.disposeall(state.cgl, gl); }