Jump to content
  • Advertisement
Sign in to follow this  
tom_mai78101

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

This topic is 2304 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

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.

Share this post


Link to post
Share on other sites
Advertisement
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 smile.png.

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!