0

I believe the problem I have is that gameThread won't work unless I have use GL10 gl in gameview.drawFrame(). I've tried creating a Object gl passing that in but still wont work. Also is it wrong to use game.onResume to update the game? I've created an instance of the gameThread inside of Main.class and classed the

public class GameThread extends Thread {
Object  gl;
private static int MAX_FRAME_SKIPS;
private static int FRAME_PERIOD;
private OpenGLActivity game;
private MyGLRenderer gameView;
private boolean gameRunning = true;
private boolean running = false;
Handler handler = new Handler();

public GameThread( int maxFPS, int maxFrameSkips) {
    game = new OpenGLActivity();
    gameView = new MyGLRenderer();
    MAX_FRAME_SKIPS = maxFrameSkips;
    FRAME_PERIOD = 1000 / maxFPS;
    gl = new Object();
}

@Override
public void run() {
    long beginTime;
    long timeDiff;
    int sleepTime;
    int framesSkipped;
    beginTime = System.currentTimeMillis();
    framesSkipped = 0;
    this.gameRunning = this.game.isRunning();
    this.game.onResume(); // Update game
    this.gameView.onDrawFrame((GL10) gl);    // Render the game
    timeDiff = System.currentTimeMillis() - beginTime;  // Calculate cycle length
    sleepTime = (int) (FRAME_PERIOD - timeDiff);         // Calculate time available to sleep

    // Checks if got time to sleep, either sleeps thread or catches up
    if (sleepTime > 0) {
        if (this.gameRunning && running) {
            handler.postDelayed(this, sleepTime);
        }
        while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
            this.game.onResume();                         // Update without rendering
            sleepTime += FRAME_PERIOD;                  // Add frame period to check if in next frame
            framesSkipped++;
            if (this.gameRunning && running) {
                this.run();                             // No time to wait! RUN! CATCH UP!
            }
        }
    }
}

public void setRunning(boolean running) {
    this.running = running;
}

}

public class Main extends Activity{

    GameThread gt = new GameThread(48, 100);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
    }

    public void PlayGame(View v) {
        Intent intent = new Intent(Main.this, OpenGLActivity.class);
        startActivity(intent);
        gt.setRunning(true);
    }
genpfault
  • 51,148
  • 11
  • 85
  • 139
will
  • 75
  • 1
  • 7

1 Answers1

2

The fact that you don't have a GL10 object is only a symptom here. In fact, I don't think that's ever used on Android. It's part of the javax.microedition.khronos.opengles package, which I figure is some kind of standard Java OpenGL ES interface. I'm not sure if it's functional, but I certainly have never seen anybody use it.

The OpenGL interfaces that are normally used in Android are in the android.opengl package, and are named GLES10, GLES11, GLES20, GLES30, GLES31.

On those classes, all the methods are static. So you never really need an object instance to make OpenGL calls.

But, and this is your actual problem: To make OpenGL calls, you need to create an OpenGL context, and make it current. You will also need a rendering surface. There are two main options for this:

  • The (moderately) hard way by using the EGL14 interface (or EGL10 if you need to support API levels lower than 17).
  • You can go the more convenient and widely used route, and use a GLSurfaceView, which creates the context and rendering surface for you.

I recommend that you start with the official tutorial in the Android documentation. All of the above will become much clearer once you go through this: http://developer.android.com/training/graphics/opengl/index.html.

You should also be aware that OpenGL ES 1.x is mostly considered obsolete. ES 2.0 and higher versions are much more widely used these days.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • I would like to add the link to another answer of yours https://stackoverflow.com/a/27092070/502144 , which greatly explains how to use `EGL14` interface. – fdermishin Jan 04 '21 at 10:28