More Threading Problems

Problems with Canvas, OpenGL, etc...

More Threading Problems

Postby tttcowan » Fri Nov 12, 2010 9:01 pm

Hi, i've got the GL Renderer class below.

I'm having problems where I call the setDrawQueue method from the gamephysics thread which populates an array of drawables.... The problem is the screen is flickering... It's like the physics thread is jumping and preventing the gl thread from running... but i've synchronised it properly i think so i cant see why it would.... Does anyone have any ideas why it might be occuring? I'm still learning threading so i might of missed something.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.sickgames.opengl;
  2.  
  3. import javax.microedition.khronos.egl.EGLConfig;
  4. import javax.microedition.khronos.opengles.GL10;
  5.  
  6. import org.sickgames.DrawableQueueObject;
  7. import org.sickgames.GameRegistry;
  8.  
  9. import android.content.Context;
  10. import android.graphics.Bitmap;
  11. import android.graphics.BitmapFactory;
  12. import android.graphics.Matrix;
  13. import android.opengl.GLSurfaceView.Renderer;
  14. import android.opengl.GLUtils;
  15. import android.util.Log;
  16.  
  17. public class GLRenderer implements Renderer {
  18.        
  19.         public float mXScale = 1;
  20.         public float mYScale = 1;
  21.         private int mWidth;
  22.         private int mHeight;
  23.         private DrawableQueueObject[] drawQueue;
  24.         private boolean drawQueueChanged = true;
  25.         private DrawableBitmap[] drawableBitmapArray;
  26.         private double mFrameRate; // Proper frame rate.
  27.         private int mFrameRateFramesSinceLastSecond; // Used for frame rate.
  28.         private double mFrameRateLastSecond;
  29.  
  30.         private Context context;
  31.        
  32.         public GLRenderer(Context context) {
  33.                 // Initialize our square.
  34.                 GameRegistry.setGLRendererReady(false);
  35.                 drawQueue = new DrawableQueueObject[512];
  36.                 drawableBitmapArray = new DrawableBitmap[512];
  37.                 this.context = context;
  38.         }
  39.  
  40.         public void onDrawFrame(GL10 gl) {
  41.                 double now = System.currentTimeMillis() /1000;
  42.                
  43.                 if (now < mFrameRateLastSecond + 1) {
  44.                         mFrameRateFramesSinceLastSecond++;
  45.                 } else {
  46.                         mFrameRateLastSecond = now;
  47.                         mFrameRate = mFrameRateFramesSinceLastSecond;
  48.                         mFrameRateFramesSinceLastSecond = 0;
  49.                         Log.d("GLThread", "Frames/s : " + mFrameRate);
  50.                 }
  51.                        
  52.                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  53.  
  54.                        
  55.                 DrawableBitmap.beginDrawing(gl, mWidth, mHeight, mXScale, mYScale);
  56.                 synchronized (this) {
  57.                         if (drawQueueChanged == true) {
  58.                         populateBitmapArray();
  59.                         drawQueueChanged = false;
  60.  
  61.  
  62.                         drawBitmapArray(gl);                   
  63.                 }
  64.                 }
  65.  
  66.                
  67.                 DrawableBitmap.endDrawing(gl);
  68.  
  69.         }
  70.  
  71.         public void onSurfaceChanged(GL10 gl, int width, int height) {
  72.                 GameRegistry.setGLRendererReady(false);        
  73.                 mWidth = width;
  74.                 mHeight = height;
  75.                 mXScale = ((float) width) / GameRegistry.GAME_WIDTH;
  76.                 mYScale = ((float) height) / GameRegistry.GAME_HEIGHT;
  77.                 gl.glViewport(0, 0, width, height);
  78.                 float ratio = (float) width / height;
  79.                 gl.glMatrixMode(GL10.GL_PROJECTION);
  80.         gl.glLoadIdentity();
  81.                 gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
  82.                 GameRegistry.setGLRendererReady(true);
  83.  
  84.         }
  85.  
  86.         public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  87.                 GameRegistry.setGLRendererReady(false);        
  88.                 gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
  89.                 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  90.                 gl.glShadeModel(GL10.GL_FLAT);
  91.         gl.glDisable(GL10.GL_DEPTH_TEST);
  92.         gl.glEnable(GL10.GL_TEXTURE_2D);
  93.         gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
  94.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  95.        
  96.         GameRegistry.textureFactory.loadAllTextures(gl, context);
  97.      
  98.                
  99.                 populateBitmapArray();
  100.                 mFrameRate = 0;
  101.                 mFrameRateFramesSinceLastSecond = 0;
  102.                 mFrameRateLastSecond = 0;
  103.  
  104.        
  105.         }
  106.        
  107.        
  108.         public synchronized void setDrawQueue(DrawableQueueObject[] newDrawQueue) {
  109.                 drawQueue = newDrawQueue;
  110.                 drawQueueChanged = true;
  111.         }
  112.        
  113.         private synchronized void populateBitmapArray() {
  114.  
  115.                 //Clear Array
  116.                 for (int x = 0; x < drawableBitmapArray.length; x++) {
  117.                         if (drawableBitmapArray[x] != null)
  118.                                 drawableBitmapArray[x] = null;
  119.                         else break;
  120.                 }
  121.                
  122.                 //Populate array
  123.                 for (int x = 0; x < drawQueue.length; x++) {
  124.                         if (drawQueue[x] != null) {
  125.                                 DrawableBitmap bitmap = new DrawableBitmap();
  126.                                 bitmap.mXPos = drawQueue[x].xPos;
  127.                                 bitmap.mYPos = drawQueue[x].yPos;
  128.                                 bitmap.mXScale = drawQueue[x].xScale;
  129.                                 bitmap.mYScale = drawQueue[x].yScale;
  130.                                 bitmap.setTexture(drawQueue[x].resourceId);
  131.                                 drawableBitmapArray[x] = bitmap;
  132.                                
  133.                         }
  134.                         else break;
  135.                 }
  136.         }
  137.        
  138.         private synchronized void drawBitmapArray(GL10 gl) {
  139.                 for (int x = 0; x < drawableBitmapArray.length; x++) {
  140.                         if (drawableBitmapArray[x] != null) {
  141.                                 drawableBitmapArray[x].draw(gl);
  142.                         }
  143.                         else break;
  144.                 }
  145.         }
  146.        
  147.     /**
  148.      * This function blocks while drawFrame() is in progress, and may be used by other threads to
  149.      * determine when drawing is occurring.
  150.      */
  151.    
  152.     public synchronized void waitDrawingComplete() {
  153.     }
  154.  
  155.  
  156. }
  157.  
Parsed in 0.048 seconds, using GeSHi 1.0.8.4


Thanks a lot
tttcowan
Developer
Developer
 
Posts: 46
Joined: Mon Jul 19, 2010 11:08 pm

Top

Re: More Threading Problems

Postby tttcowan » Sat Nov 13, 2010 2:07 pm

Sorted this now if anybody is interested... Problem was the line
drawQueue = newDrawQueue;

Standard java stuff, both the glRender drawq and the game physics thread drawq was pointing at the same object.
tttcowan
Developer
Developer
 
Posts: 46
Joined: Mon Jul 19, 2010 11:08 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: Google [Bot] and 2 guests