Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to understand RENDERMODE_WHEN_DIRTY so that I could save battery life wherever possible.. I made this sample application so that GlSurfaceView is updated with random color only when the random number temp is greater than 50... If the value of temp is less than 50, I want it to retain the last random color.. But in this code sample, it is not retaining it but making the screen go black forever!

Basically I want to know if my implementation of RENDERMODE_WHEN_DIRTY correct...

package com.blob.blob;

import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.util.Log;

public class OpenGLActivity extends Activity implements Renderer
{
    GLSurfaceView glView;
    Random rand ;
    @Override
    public void onCreate(Bundle s)
    {
        super.onCreate(s);
        glView = new GLSurfaceView(this);
        glView.setRenderer(this);
        glView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        rand = new Random();
        setContentView(glView);

    }

    public void onDrawFrame(GL10 gl) 
    {           
        int temp = rand.nextInt(100);

        if(temp > 50)
        {
        glView.requestRender();

        gl.glClearColor(rand.nextFloat(), rand.nextFloat(), rand.nextFloat(), 1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        }       

    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {

        Log.d("OpenglTest", "Surface Changed");
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        Log.d("OpenglTest", "Surface Created");
    }

    public void onPause()
    {

        super.onPause();
        glView.onPause();


    }


    public void onResume()
    {       
        super.onResume();
        glView.onResume();
    }
}
share|improve this question
    
What do you mean by going black forever? can you explain a bit more please? –  José Ignacio Jan 18 '13 at 18:18
    
@JoséIgnacio Sorry for super late reply.. Black means the screen is quite literally black and the onDrawFrame() method is not called again! –  BLOB Jan 22 '13 at 17:07
add comment

1 Answer

up vote 1 down vote accepted

RENDERMODE_WHEN_DIRTY means "do not call onDrawFrame() unless something explicitly requests rendering with requestRender()". Your code snippet is calling requestRender() from within onDrawFrame(), so nothing will ever happen.

The idea behind requestRender() is that nothing is drawn until some other thread has decided that rendering needs to happen. The SurfaceView docs note that it "may be called from any thread", which is necessary for this method to have any value, because the renderer thread is just sitting there waiting for somebody to wake it up.

share|improve this answer
    
Haha.. makes sense.. –  BLOB Jan 24 '13 at 4:12
    
In addition, I've noticed that there are occasionally also "uninvited" onDrawFrame() calls that it's good to prepare for. I now think that doing a requestRender() ignites at least one render pass. –  akauppi yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.