diff options
Diffstat (limited to 'demos/GLLandScape')
-rw-r--r-- | demos/GLLandScape/GLLandScape1.java | 7 | ||||
-rw-r--r-- | demos/GLLandScape/GLLandScape1w1.java | 56 | ||||
-rwxr-xr-x | demos/GLLandScape/GLLandScape1w1Applet.java | 177 |
3 files changed, 186 insertions, 54 deletions
diff --git a/demos/GLLandScape/GLLandScape1.java b/demos/GLLandScape/GLLandScape1.java index 209f6db..25ed9cf 100644 --- a/demos/GLLandScape/GLLandScape1.java +++ b/demos/GLLandScape/GLLandScape1.java @@ -577,6 +577,7 @@ class GLLandScape1 extends GLAnimCanvas gl.glEnable(GL_TEXTURE_2D); texture_mapping = TRUE; glj.gljCheckGL(); + glj.gljFree(); repaint(); } } @@ -595,6 +596,7 @@ class GLLandScape1 extends GLAnimCanvas gl.glDisable(GL_TEXTURE_2D); texture_mapping = FALSE; glj.gljCheckGL(); + glj.gljFree(); repaint(); } } @@ -623,6 +625,7 @@ class GLLandScape1 extends GLAnimCanvas gl.glEnable(GL_FOG); land_fogging = TRUE; glj.gljCheckGL(); + glj.gljFree(); repaint(); } } @@ -641,6 +644,7 @@ class GLLandScape1 extends GLAnimCanvas gl.glDisable(GL_FOG); land_fogging = FALSE; glj.gljCheckGL(); + glj.gljFree(); repaint(); } } @@ -669,6 +673,7 @@ class GLLandScape1 extends GLAnimCanvas gl.glShadeModel(GL_FLAT); flat_shading = TRUE; glj.gljCheckGL(); + glj.gljFree(); repaint(); } } @@ -687,6 +692,7 @@ class GLLandScape1 extends GLAnimCanvas gl.glShadeModel(GL_SMOOTH); flat_shading = FALSE; glj.gljCheckGL(); + glj.gljFree(); repaint(); } } @@ -706,6 +712,7 @@ class GLLandScape1 extends GLAnimCanvas gl.glDeleteLists(theLand, 1); genLand(); check_height(); + glj.gljFree(); repaint(); setSuspended(false); } diff --git a/demos/GLLandScape/GLLandScape1w1.java b/demos/GLLandScape/GLLandScape1w1.java index 9f6f1a3..cef739a 100644 --- a/demos/GLLandScape/GLLandScape1w1.java +++ b/demos/GLLandScape/GLLandScape1w1.java @@ -187,7 +187,7 @@ class GLLandScape1w1 extends GLAnimCanvas Dimension dim = getSize(); reshape(dim.width,dim.height); - myMove(FORWARD,mySpeed,true,false); + myMove(FORWARD,mySpeed,true); check_height(); repaint(); /* and start our working thread ... */ @@ -216,7 +216,7 @@ class GLLandScape1w1 extends GLAnimCanvas if(flat_shading == FALSE) gl.glShadeModel(GL_SMOOTH); - myMove(FORWARD,mySpeed,true,false); + myMove(FORWARD,mySpeed,true); check_height(); @@ -247,7 +247,6 @@ class GLLandScape1w1 extends GLAnimCanvas planes[i].theta = ((float)(random.nextInt() % 257))*0.1111f; planes[i].tick(); - if (isSuspended()) repaint(); System.out.println("plane added"); return; } @@ -261,7 +260,6 @@ class GLLandScape1w1 extends GLAnimCanvas if (planes[i].speed != 0.0f) { planes[i].speed = 0.0f; - if (isSuspended()) repaint(); System.out.println("plane removed"); return; } @@ -432,7 +430,7 @@ class GLLandScape1w1 extends GLAnimCanvas /* Function that moves the eye / turns the angle of sight. */ /* Updates scene if update. */ - void myMove(int dir, float amount, boolean update, boolean fetchGLContext) + void myMove(int dir, float amount, boolean update) { float diff = (float)Math.sqrt(lookx * lookx + lookz * lookz); /* @@ -448,7 +446,6 @@ class GLLandScape1w1 extends GLAnimCanvas alpha+=(float)((3.0*Math.PI)/2.0); float _alpha = (float) (alpha*180f/Math.PI ); */ - boolean do_repaint = false; lookx /= diff; lookz /= diff; //myDir=dir; @@ -464,7 +461,6 @@ class GLLandScape1w1 extends GLAnimCanvas // lookx^2 + lookz^2 != 1 for some reason. Fix. lookx /= diff; lookz /= diff; - do_repaint = true; break; case STRAFELEFT: posx += lookz * amount; @@ -472,15 +468,12 @@ class GLLandScape1w1 extends GLAnimCanvas break; case UP: posy += amount; - do_repaint = true; break; case LOOKUP: looky += amount; - do_repaint = true; break; case LOOKSET: looky = amount; - do_repaint = true; break; } int x = (int)(posx / (comp * 1.0f)); @@ -495,21 +488,10 @@ class GLLandScape1w1 extends GLAnimCanvas posz = MAP * comp; if (update) { - if(fetchGLContext && glj.gljMakeCurrent() == false) - { - System.out.println("problem in use() method"); - return; - } gl.glMatrixMode(GL_MODELVIEW); gl.glLoadIdentity(); glu.gluLookAt(posx,posy,posz, posx+lookx, posy+looky, posz+lookz, 0.0f,1.0f,0.0f); - - if(fetchGLContext) - glj.gljFree(); - - if( do_repaint ) - repaint(); } } @@ -603,7 +585,6 @@ class GLLandScape1w1 extends GLAnimCanvas public void reshape(int w, int h) { - glj.gljResize(w,h); gl.glViewport(0,0,w,h); gl.glMatrixMode(GL_PROJECTION); gl.glLoadIdentity(); @@ -644,7 +625,7 @@ class GLLandScape1w1 extends GLAnimCanvas gl.glEnable(GL_TEXTURE_2D); texture_mapping = TRUE; glj.gljCheckGL(); - repaint(); + glj.gljFree(); } } } @@ -662,7 +643,7 @@ class GLLandScape1w1 extends GLAnimCanvas gl.glDisable(GL_TEXTURE_2D); texture_mapping = FALSE; glj.gljCheckGL(); - repaint(); + glj.gljFree(); } } } @@ -690,7 +671,7 @@ class GLLandScape1w1 extends GLAnimCanvas gl.glEnable(GL_FOG); land_fogging = TRUE; glj.gljCheckGL(); - repaint(); + glj.gljFree(); } } } @@ -708,7 +689,7 @@ class GLLandScape1w1 extends GLAnimCanvas gl.glDisable(GL_FOG); land_fogging = FALSE; glj.gljCheckGL(); - repaint(); + glj.gljFree(); } } } @@ -736,7 +717,7 @@ class GLLandScape1w1 extends GLAnimCanvas gl.glShadeModel(GL_FLAT); flat_shading = TRUE; glj.gljCheckGL(); - repaint(); + glj.gljFree(); } } } @@ -754,7 +735,7 @@ class GLLandScape1w1 extends GLAnimCanvas gl.glShadeModel(GL_SMOOTH); flat_shading = FALSE; glj.gljCheckGL(); - repaint(); + glj.gljFree(); } } } @@ -773,9 +754,26 @@ class GLLandScape1w1 extends GLAnimCanvas gl.glDeleteLists(theLand, 1); genLand(); check_height(); - repaint(); setSuspended(false); + glj.gljFree(); } + public String getGLString(int id) + { + String result = "null"; + if(glj!=null) + { + if(glj.gljMakeCurrent() == false) + { + System.out.println("problem in use() method"); + return result; + } + result = gl.glGetString(id); + glj.gljCheckGL(); + glj.gljFree(); + } + + return result; + } } diff --git a/demos/GLLandScape/GLLandScape1w1Applet.java b/demos/GLLandScape/GLLandScape1w1Applet.java index 90da5f3..ea55f7f 100755 --- a/demos/GLLandScape/GLLandScape1w1Applet.java +++ b/demos/GLLandScape/GLLandScape1w1Applet.java @@ -1,13 +1,14 @@ import gl4java.GLContext; +import gl4java.GLFunc; import java.awt.*; import java.applet.*; import java.awt.event.*; public class GLLandScape1w1Applet extends Applet - implements MouseListener, KeyListener, ActionListener + implements MouseListener, KeyListener, ActionListener, ItemListener { - GLLandScape1w1 landScape = null; + public GLLandScape1w1 landScape = null; Button bAddPlane = null; Button bRemPlane = null; @@ -19,10 +20,16 @@ public class GLLandScape1w1Applet extends Applet Button bFps; TextField tFps; + Checkbox bUseRepaint = null; + Checkbox bUseFpsSleep = null; + boolean gameOn = true; SpeedTimer tMySpeed = null; + GLThread gt1 = null; + GLThread gt2 = null; + public void init() { init(0,0); @@ -51,13 +58,21 @@ public class GLLandScape1w1Applet extends Applet add("East", glFeatures); Panel pFps = new Panel(); - pFps.setLayout(new GridLayout(1,3)); + pFps.setLayout(new GridLayout(1,5)); pFps.add(bInfo =new Button("info")); pFps.add(bFps =new Button("get fps")); pFps.add(tFps =new TextField()); bInfo.addMouseListener(this); bFps.addMouseListener(this); tFps.addActionListener(this); + + bUseRepaint = new Checkbox("repaint", true); + bUseRepaint.addItemListener(this); + pFps.add(bUseRepaint); + bUseFpsSleep = new Checkbox("fps-sleep", true); + bUseFpsSleep.addItemListener(this); + pFps.add(bUseFpsSleep); + add("South", pFps); if(w==0 && h==0) @@ -79,9 +94,16 @@ public class GLLandScape1w1Applet extends Applet add("Center", landScape); } + public void startTestThreads() + { + gt1 = new GLThread(landScape, GLFunc.GL_VENDOR, "gt1"); + gt2 = new GLThread(landScape, GLFunc.GL_VERSION, "gt2"); + gt1.start(); + gt2.start(); + } + public void destroy() { - landScape.stop(); landScape.cvsDispose(); landScape.removeMouseListener(this); landScape.removeKeyListener(this); @@ -114,6 +136,7 @@ public class GLLandScape1w1Applet extends Applet gljLib + ", " + glLib + ", " + gluLib); GLContext.gljNativeDebug = false; + GLContext.gljThreadDebug = false; GLContext.gljClassDebug = false; GLLandScape1w1Applet window = @@ -141,12 +164,39 @@ public class GLLandScape1w1Applet extends Applet Dimension ps = window.getPreferredSize(); f.setBounds(-100,-100,99,99); f.setVisible(true); - f.setVisible(false); + //f.setVisible(false); Insets i = f.getInsets(); f.setBounds(0,0, ps.width+i.left+i.right, ps.height+i.top+i.bottom); f.setVisible(true); + + // ThreadTest + // window.startTestThreads(); + } + + public void itemStateChanged( ItemEvent evt ) + { + ItemSelectable comp = evt.getItemSelectable(); + + if( comp.equals(bUseRepaint) ) + { + if(landScape!=null) + { + landScape.setUseRepaint(bUseRepaint.getState()); + System.out.println("LandScape uses repaint: "+ + bUseRepaint.getState()); + } + } + else if( comp.equals(bUseFpsSleep ) ) + { + if(landScape!=null) + { + landScape.setUseFpsSleep(bUseFpsSleep.getState()); + System.out.println("LandScape uses fps-sleep: "+ + bUseFpsSleep.getState()); + } + } } // Methods required for the implementation of MouseListener @@ -215,12 +265,12 @@ public class GLLandScape1w1Applet extends Applet { if ( key==KeyEvent.VK_LEFT ) { - landScape.myMove(landScape.TURNLEFT, 0.1f, true, true); + landScape.myMove(landScape.TURNLEFT, 0.1f, false); tMySpeed.start(); } else if ( key==KeyEvent.VK_RIGHT ) { - landScape.myMove(landScape.TURNLEFT, -0.1f, true, true); + landScape.myMove(landScape.TURNLEFT, -0.1f, false); tMySpeed.start(); } else if ( key==KeyEvent.VK_UP ) @@ -241,24 +291,24 @@ public class GLLandScape1w1Applet extends Applet float v = tMySpeed.getSpeed(); if ( e.isShiftDown() ) - tMySpeed.setSpeed(v/2.0f); + tMySpeed.setSpeed(v-2.0f); else tMySpeed.setSpeed(v-1.0f); tMySpeed.start(); } else if ( key==KeyEvent.VK_PAGE_UP ) { - landScape.myMove(landScape.LOOKUP, 0.1f, true,true); + landScape.myMove(landScape.LOOKUP, 0.1f, false); tMySpeed.start(); } else if ( key==KeyEvent.VK_PAGE_DOWN ) { - landScape.myMove(landScape.LOOKUP, -0.1f, true,true); + landScape.myMove(landScape.LOOKUP, -0.1f, false); tMySpeed.start(); } else if ( key==KeyEvent.VK_END ) { - landScape.myMove(landScape.LOOKSET, 0f, true,true); + landScape.myMove(landScape.LOOKSET, 0f, false); tMySpeed.start(); } } @@ -306,7 +356,7 @@ public class GLLandScape1w1Applet extends Applet class SpeedTimer implements Runnable { protected Thread me = null; - protected boolean to=false, started=false; + protected boolean to=false; protected int _t; protected float speed = 0.0f; @@ -315,53 +365,130 @@ public class GLLandScape1w1Applet extends Applet public SpeedTimer(GLLandScape1w1 ls, int t) { _ls=ls; _t=t; } - public void start() + public synchronized void start() { - if(me!=null) stop(); - to=false; - started=true; + if(me!=null && to) return; + + stop(); me = new Thread(this); me.start(); } - public void stop() { + public synchronized void stop() { if(me!=null) { - me.stop(); me=null; - started=false; - to=false; + + notifyAll(); + + while(to) { + try { + wait(); + } catch (Exception e) {} + } } } public void run() { if(me!=null) { - try {me.sleep(_t,0);} catch (InterruptedException e){} - to=true; - while(speed>1.0f) + synchronized (this) { + to=true; + notifyAll(); + } + while(me!=null && (-0.2f>=speed || speed>=0.2f) ) { + try {me.sleep(_t,0);} catch (InterruptedException e){} speed/=2.0f; _ls.setMySpeed(speed); - try {me.sleep(_t,0);} catch (InterruptedException e){} + // System.out.println("timer: lower speed: "+speed); } speed = 0.0f; _ls.setMySpeed(0f); + + synchronized (this) { + to=false; + notifyAll(); + } } } public boolean isActive() { return to; } - public boolean isStarted() { return started; } public float getSpeed() { return speed; } public void setSpeed(float v) { speed=v; _ls.setMySpeed(speed); + //System.out.println("timer: set speed: "+speed); } } + class GLThread implements Runnable { + protected Thread me = null; + protected boolean to=false; + protected int _id; + protected String _name; + + protected GLLandScape1w1 _ls; + + public GLThread(GLLandScape1w1 ls, int id, String name) + { _ls=ls; _id=id; _name=name;} + + public synchronized void start() + { + if(me!=null && to) return; + + stop(); + + me = new Thread(this, _name); + me.start(); + } + + public synchronized void stop() { + if(me!=null) + { + me=null; + + notifyAll(); + + while(to) { + try { + wait(); + } catch (Exception e) {} + } + } + } + + public void run() { + if(me!=null) + { + synchronized (this) { + to=true; + notifyAll(); + } + while(me!=null) + { + if(_ls.cvsIsInit()) + { + if(GLContext.gljThreadDebug) + _ls.getGLString(_id); + else + System.out.println("gt: "+_ls.getGLString(_id)); + } + } + + synchronized (this) { + to=false; + notifyAll(); + } + } + } + + public boolean isActive() { return to; } + } + class InfoWin extends Frame implements ActionListener { |