Sign in to follow this  

Problem with very simple game loop thread

This topic is 2017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys,

I'm learning android game development with Opengl ES and trying to implement a very simple game loop using a separated thread, that is basically just an infinite loop. When I try running the application on the emulator, it enters the infinite loop and doesn't do anything else.. the rendering thread does nothing..

I'm new to programming with threads but, even if the game loop thread is stuck in an infinite loop, isn't the render thread suppose to draw things on screen?

Here is the code of my surfaceview and game thread class (the render class is the triangle drawing example and works fine when I don't start the game thread..)

[CODE]
public class MainThread extends Thread {

private boolean running;
public void setRunning(boolean running) {
this.running = running;
}
@Override
public void run() {
while (running) {

}
}
}
[/CODE]

[CODE]
class GameSurfaceView extends GLSurfaceView {
private GameRenderer mRenderer;
private MainThread mThread;

public GameSurfaceView(Context context){
super(context);
// Create an OpenGL ES 2.0 context.
setEGLContextClientVersion(2);

// set the mRenderer member
mRenderer = new GameRenderer();
setRenderer(mRenderer);
mThread = new MainThread ();
// Render the view only when there is a change
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
super.surfaceCreated(holder);

mThread.setRunning(true);
mThread.run();
}


public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
mThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
retry = false;
}
}

}
[/CODE]

This is a very basic example, the game loop is not supposed to do anything, I'm just trying to learn how to make it run while the rendering thread draws the triangle.

Thanks!

Share this post


Link to post
Share on other sites
I never did any Android stuff. Did you check which lines of code get executed. Insert print/log commands. I'm not sure but shouldn't mThread.run() be mThread.start() ?

Share this post


Link to post
Share on other sites
[quote name='JoJoSim1' timestamp='1336036026' post='4937028']
I never did any Android stuff. Did you check which lines of code get executed. Insert print/log commands. I'm not sure but shouldn't mThread.run() be mThread.start() ?
[/quote]

He nailed it. The run() call simply executes the implementation inline. You have to call start() to launch the run() implementation on its own thread.

Share this post


Link to post
Share on other sites

This topic is 2017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this