Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualBruce Sutherland

Posted 08 April 2012 - 09:26 PM

That's one way to do it.  Don't fall into the trap of thinking that there's only one way to do something.  Do whatever fits your own design and goals.  It's easy when starting out to look for that silver bullet resource which tells you exactly how to do something.

I'll tell you for free, it doesn't exist Posted Image.

Another way to use a Thread would be to use a SurfaceHolder inside the class which extends SurfaceView and Runnable.  This now lets you entirely contain the game loop inside the RenderingView class, whilst also being a bit easier to understand than the tutorial you posted.

The next step is to decide if Canvas is good enough for your needs or if you should move to OpenGL.  The framework I'm current'y writing for myself is OpenGL 2.0 based and has the Android UI thread, a game logic thread and a rendering thread.  Once I'm happy enough with the basics I can throw it on BitBucket and you can see how I am doing that.

So,

public class Game extends Activity {
	RenderingView	 renderingView;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		renderingView = new RenderingView(this);
		setContentView(renderingView);
	}

	public void onResume() {
		super.onResume();
		renderingView.resume();
	}

	public void onPause() {
		super.onPause();
		renderingView().pause();
	}
}

public class RenderingView extends SurfaceView implements Runnable {
	Thread thread = null;
	SurfaceHolder holder;
	volatile boolean running = false;

	public RenderingView(Context context) {
		super(context);
		holder = getHolder();
	}

	public void resume() {
		running = true;
		thread = new Thread(this);
		thread.start();
	}

	public void run() {
		while (running) {
			if (!holder.getSurface().isValid())
				continue;

			Canvas canvas = holder.lockCanvas();
			// Use canvas to draw things here
			holder.unlockCanvasAndPost(canvas);
		}
	}

	public void pause() {
		running = false;
		while (true) {
			try {
				thread.join();
			} catch (InterruptedException e) {
				// wait
			}
		}
	}
}

#1Bruce Sutherland

Posted 08 April 2012 - 09:20 PM

That's one way to do it.  Don't fall into the trap of thinking that there's only one way to do something.  Do whatever fits your own design and goals.  It's easy when starting out to look for that silver bullet resource which tells you exactly how to do something.

I'll tell you for free, it doesn't exist :).

Another way to use a Thread would be to use a SurfaceHolder inside the class which extends SurfaceView and Runnable.  This now lets you entirely contain the game loop inside the RenderingView class, whilst also being a bit easier to understand than the tutorial you posted.

The next step is to decide if Canvas is good enough for your needs or if you should move to OpenGL.  The framework I'm current'y writing for myself is OpenGL 2.0 based and has the Android UI thread, a game logic thread and a rendering thread.  Once I'm happy enough with the basics I can throw it on BitBucket and you can see how I am doing that.

So,

public class Game extends Activity {
    RenderingView     renderingView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        renderingView = new RenderingView(this);
        setContentView(renderingView);
    }

    public void onResume() {
        super.onResume();
        renderingView.resume();
    }

    public void onPause() {
        super.onPause();
        renderingView().pause();
    }
}

public class RenderingView extends SurfaceView implements Runnable {
    Thread thread = null;
    SurfaceHolder holder;
    volatile boolean running = false;

    public RenderingView(Context context) {
        super(context);
        holder = getHolder();
    }

    public void resume() {
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        while (running) {
            if (!holder.getSurface().isValid())
                continue;

            Canvas canvas = holder.lockCanvas();
            // Use canvas to draw things here
            holder.unlockCanvasAndPost(canvas);
        }
    }

    public void pause() {
        running = false;
        while (true) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                // wait
            }
        }
    }
}

PARTNERS