• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
tom_mai78101

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

4 posts in this topic

[url="http://obviam.net/index.php/a-very-basic-the-game-loop-for-android/"]Currently, I'm looking at a tutorial for SurfaceView and multithreading implementation here.[/url] 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 ([url="http://shopping.pchome.com.tw/DJAA19-A58195266"]source[/url]) 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: [b]By implementing Runnable to a class extending Canvas, and using that class in SurfaceView.onDraw().[/b]

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

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img].

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,

[code]
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
}
}
}
}
[/code]
0

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?
0

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

Share this post


Link to post
Share on other sites
[quote name='Strategy' timestamp='1340787696' post='4953249']
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.
[/quote]

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

Share this post


Link to post
Share on other sites

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  
Followers 0