Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


SurfaceView, Canvas, and Threading: Would my Java-esque concept work out well?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 tom_mai78101   Members   -  Reputation: 577

Like
0Likes
Like

Posted 14 March 2012 - 05:14 AM

Currently, I'm looking at a tutorial for SurfaceView and multithreading implementation here. I do know that SurfaceView requires a Canvas in its onDraw().

Whenever I think of Canvas, I tend to think of my experiences with programming with Java AWT Canvas, albeit beginner's level. My concept is that I put my experiences to good use from Java to Android, by re-implementing my methods into the Android Canvas.

Some books of reference (source) said that Android Canvas and Java AWT Canvas can be seen as the same, and continued mentioning how all Canvas concepts can be used along with Android Canvas, one way or another.

Now, the keys of the concept: By implementing Runnable to a class extending Canvas, and using that class in SurfaceView.onDraw().

Is this preferred? Or I may be seeing things in hindsight? Thanks in advance.

Sponsor:

#2 Bruce Sutherland   Members   -  Reputation: 126

Like
0Likes
Like

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 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
			}
		}
	}
}


#3 tom_mai78101   Members   -  Reputation: 577

Like
0Likes
Like

Posted 22 June 2012 - 11:00 PM

OMG! I have been cranking up on midterms and finals, and completely forgot about this.

There is one problem I'm stuck on, and it's about the implementation of the Thread and Runnable interface themselves.

Completely looping through with my Java AWT method of game loop threading, I obtain a stable frame rate of 15 to 16 FPS. So far, I have not tried out the SurfaceHolder and run Thread inside method.

Then I went ahead and tried implementing OpenGL ES 1.0's drawFrame(), and that somehow gives a stable 30 to 60 FPS, depending on how powerful the Android smartphone is. (I have two smartphones, HTC Wildfire S, and HTC Evo 3D, the latter is more powerful and much larger than the former.)

Simply put, I see that Canvas uses the CPU part of Android, while OpenGL uses the GPU part of Android, which makes a lot different than what I used to think about.

I'll have another go and see which implementation best suits my needs. And to my understanding, it's either Canvas drawing (Easy, 2D, slow, 100% pixel control) vs. OpenGL (Hard, 2D/3D, fast, 0% pixel control), one of the two I have to choose for the game loop.

Am I correct?

#4 Strategy   Members   -  Reputation: 187

Like
0Likes
Like

Posted 27 June 2012 - 03:01 AM

Look at the LunarLander sample that follows with the Android SDK - it contains pretty much a complete SurfaceView/Canvas implementation. There is little reason not to

Canvas is good for most simple graphical requirements. If you require animation/action in your game, OpenGL is probably the way to go, though. There exist several libraries out there - LibGDX and AndDevEngine being the most popular.
Michael A.Designer/Developer
A Brief History of Rome
Pirates and traders

#5 tom_mai78101   Members   -  Reputation: 577

Like
0Likes
Like

Posted 02 July 2012 - 08:04 AM

Look at the LunarLander sample that follows with the Android SDK - it contains pretty much a complete SurfaceView/Canvas implementation. There is little reason not to

Canvas is good for most simple graphical requirements. If you require animation/action in your game, OpenGL is probably the way to go, though. There exist several libraries out there - LibGDX and AndDevEngine being the most popular.


I'm not worried about the use of Canvas or OpenGL ES. I'm more about how the game loop is to be designed around Canvas and SurfaceView, in order to maximize the amount of FPS you can acculumate on an Android phone.

In other words, I'm trying to lower the O(n^2) of my game loop (as I see it, I have a while loop nested inside a while loop, and 1 arbitary function that unlocks and locks the Canvas for the SurfaceView.)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS