Skip to main content
gamedev.net
🔒 Locked

Weird Java Stuff

Started by angry Jul 11, 2006 at 7:28 PM 5 replies 1.2k views
Original Post
angry
angry
I get some weird results in my java code, basically what happens is that I set a flag in one of my classes to true outside a try catch statement and then when I read the flag within the try catch statement it returns both true and false :/ Basically the code looks something like this:

// in VertexBuffer.java
public void lock() {
  locked = true;
  // do more stuff here...
}

public void isLocked() {
  return locked;
}

// in Main.java
public void someMethod() {

  terrain.patch[0][0].getVertexBuffer().lock();
  try {
    if( terrain.getPatch(0, 0).getVertexBuffer().isLocked() == true ) { // omg! JVM knows it's true!
      System.out.println("Hello World!");
    }
    if( terrain.getPatch(0, 0).getVertexBuffer().isLocked() == false ) { // omg! WTH? JVM thinks this is also true
      System.out.println("Hello World, Again!");
    }
    // more stuff here...
  } catch(Exception e) {
    // do appropiate stuff
  }
}




Output:

Hello World!
Hello World, Again!
What happens is that both if statements succeed!!! Now how in the hell is that possible??
Ranger50000
Ranger50000
I'm not sure is there any way you could post the exact section of code that is malfunctioning?
I wish my lawn was emo, so it would cut itself.
angry
angry
I don't see how posting all code could be any use. Anyways here's a light version of the code:

in here, put the getVertex() method in mind, it's the one the exception comes from.
// VertexBuffer.javapublic class VertexBuffer { // the size of the type of the vertex components private int type; // the number of vertex components each vertex has, i.e x, y, z  private int size; // the number of vertices this buffer contains private int count; // the mode this buffer is in private int mode;  // the buffer that contains the vertices  private ByteBuffer buffer;  private boolean locked = false; public VertexBuffer(int type, int size, int count, int mode) {   // init stuff... } public void getVertex(int i, Vector3f v) throws IllegalAccessException {   if( (mode&VIDEO) == VIDEO && !locked ) {     throw new IllegalAccessException("Can't read from a vertex buffer that isn't locked!");   }   v.x = buffer.getFloat(i*type*size);   v.y = buffer.getFloat(i*type*size+type);   v.z = buffer.getFloat(i*type*size+type*2); } public void setVertex(int i, Vector3f v) throws IllegalAccessException {   if( (mode&VIDEO) == VIDEO && !locked ) {     throw new IllegalAccessException("Can't write to a vertex buffer that isn't locked!");   }    buffer.putFloat(i*type*size, v.x);    buffer.putFloat(i*type*size+type, v.y);    buffer.putFloat(i*type*size+type*2, v.z);  } public void lock() {   locked = true;   // more stuff happens here, but they're not needed } public void unlock() {   locked = false;   // some more stuff... } public boolean isLocked() {  return locked; }}


// TerrainPatch.javapublic class TerrainPatch {  protected VertexBuffer vertexBuffer;  public TerrainPatch(Heightmap heightmap, ...some more parameters) {    generateVertices(heightmap);  }  public void generateVertices(Heightmap heightmap) {    vertexBuffer = new VertexBuffer(VertexBuffer.FLOAT, 3, heightmap.getWidth()*heightmap.getHeight(), VertexBuffer.VIDEO |                           VertexBuffer.VERTICES | VertexBuffer.STATIC);    // fill vertices with two simple for loops then returns...  }  public VertexBuffer getVertexBuffer() {    return vertexBuffer;  }}


// Terrain.javapublic class Terrain {  public TerrainPatch patches;  public Terrain(Heightmap heightmap) {    createPatches(heightmap);    // initialize and some other stuff...  }  public void createPatches(Heightmap hm) {    for( int j=0; j<patchCount; j++ ) {      for( int i=0; i<patchCount; i++ ) {        patches[j] = new TerrainPatch(hm, ...other stuff);      }     }   }   public TerrainPatch getPatch(int x, int z) {     return patches[z][x];   }}


And here's where the interesting stuff happens, scroll down to raiseTerrain method.
// TerrainRenderer.javapublic class TerrainRenderer {   public TerrainRenderer() {    init();  }  public void init() {    // initialize stufff..  }  public void run() {    while( running ) {      handleInput();      render();    }  }  public void handleInput() {    if( Mouse.isButtonDown(0) ) {      raiseTerrain();    }  }  public void raiseTerrain() {    // retrieve all visible patches and lock them    LinkedList<TerrainPatch> visiblePatches = terrain.getVisiblePatches(camera);    ListIterator<TerrainPatch> patchIterator = visiblePatches.listIterator(0);    TerrainPatch patch;    while( patchIterator.hasNext() ) {      patch = patchIterator.next();      patch.getVertexBuffer().lock();    }    try {      Vector3f v = camera.getMousePos3D();      terrainEditor.selectVertices(v.x, v.y, v.z, 10.0f);      java.util.Vector<TerrainEditor.SelectionInfo> sel = terrainEditor.getSelection();            if( sel.size() > 0 ) {        // raise/lower the heights        for( int i=0; i<sel.size(); i++ ) {          patch = terrain.getPatch(sel.get(i).patchIndexX, sel.get(i).patchIndexY);	          // HERE!!! is where the exception is thrown.. And I know for           // 100% sure that the patch at sel.get(i).patchIndexX and          // patchIndexY is locked!          patch.getVertexBuffer().getVertex(sel.get(i).getVertexIndex(), v);          patch.getVertexBuffer().setVertex(sel.get(i).getVertexIndex(), v.x, v.y+0.1f, v.z);          patch.calcErrors(8.0f, 480.0f, 45.0f, 1.0f);        }      }    } catch(Exception e) { // the above causes an exception because patch                            // is not locked! even tho it is locked, and                            // I'm 10000% sure of it!      e.printStackTrace();      System.exit(0);    }    // unlock all visible patches    patchIterator = visiblePatches.listIterator(0);    while( patchIterator.hasNext() ) {      patch = patchIterator.next();      patch.getVertexBuffer().unlock();    }  }  public static void main(String[] args) {    new TerrainRenderer();  }}

Ok, that's basically how it really looks like. And the try catch statement throws an IllegalAccessException from the VertexBuffer.getVertex() method. Even tho I locked that patch earlier, in the while loop before the try and catch statement. And the sel variable contains the right patch indices, and i've compared them to the one's that are retrieved earlier by the getVisiblePatches() method, and they're the same and should be since, the camera hasn't moved or anything between the calls. And the render method only renders patches returned from the getVisiblePatches() method, so those patches are the only ones that can be raised. Err.. enough confusing explaining. All I know is my code is 1000% accurate, tho maybe not that optimized hehe..


edit:
Any why I am confused, is because if I change some code in TerrainRender.java:
// TerrainRenderer.java  public void raiseTerrain() {    // .. removed some stuff here... for reference look the source tag above    try {      Vector3f v = camera.getMousePos3D();      terrainEditor.selectVertices(v.x, v.y, v.z, 10.0f);      java.util.Vector<TerrainEditor.SelectionInfo> sel = terrainEditor.getSelection();            if( sel.size() > 0 ) {        // raise/lower the heights        for( int i=0; i<sel.size(); i++ ) {          patch = terrain.getPatch(sel.get(i).patchIndexX, sel.get(i).patchIndexY);	          // EDIT!! Let's check if this patch really is locked or not          if( patch.isLocked() == true ) {            System.out.println("Patch is locked!");          }          if( patch.isLocked() == false ) {            System.out.println("Patch is NOT locked!!");          }          // EDIT!!!!!!          // HERE!!! is where the exception is thrown.. And I know for           // 100% sure that the patch at sel.get(i).patchIndexX and          // patchIndexY is locked!          patch.getVertexBuffer().getVertex(sel.get(i).getVertexIndex(), v);          patch.getVertexBuffer().setVertex(sel.get(i).getVertexIndex(), v.x, v.y+0.1f, v.z);          patch.calcErrors(8.0f, 480.0f, 45.0f, 1.0f);        }      }    } catch(Exception e) { // the above causes an exception because patch                            // is not locked! even tho it is locked, and                            // I'm 10000% sure of it!      e.printStackTrace();      System.exit(0);    }    // unlock all visible patches    patchIterator = visiblePatches.listIterator(0);    while( patchIterator.hasNext() ) {      patch = patchIterator.next();      patch.getVertexBuffer().unlock();    }  }


I get the output:
Patch is locked!Patch is NOT locked!!

See why I'm confused?

[Edited by - angry on July 11, 2006 8:20:54 PM]
angry
angry
Woooops! :P

Forgot that the method calcErrors() locks and unlocks the vertex buffer when it calculates the errors, hehe... Sry for wasting ur time and ur brain cells =) Gotta fix so that you have to call unlock for every lock you've made :)
Raghar
Raghar
public void isLocked() ?
How did you force it to compile?
angry
angry
Quote:
Original post by Raghar
public void isLocked() ?
How did you force it to compile?


Oh.. that's just a typo :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.