Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Canvas or Open GL ES for 2d game? [closed]

I want to create a 2d game and I would like to know which way to go given my knowledge background and some details about the game itself.

My background (the relevant stuff):

  • I started to learn android programming 10 days ago, I have done the game menu.

  • I have a fair amount of experience programming in java (for desktop).

  • I have some experience in the past with openGL in C++, but only doing 3d stuff (Also I don't remember many things I learned). I have never programmed any open GL shaders. I learned things like basic transformations like rotations / scaling / translating , applying textures (texture coordinates), and some very primitive lights stuff.

The game is supposed to have:

  • Sprites. +-50 average visible per frame.

  • Pinch to zoom/panning gestures.

  • This feature: Being able to move for example one sprite to another place, using the finger to draw the path to follow. This should be possible in any zoom level.

  • Sounds. Gunshots, explosions, simple short sounds.

  • As a bonus it would be nice to have some effects like lines with glow , warp effects, etc, but this is really not required.

  • There should be simple ways to draw geometric shapes like circles / squares / triangles / etc (I mean not in the game it self, but in the API's used to develop it)

I would like to finish the game in 1/2 months.

Now what would you think the best solution would be given the characteristics of the game, my background and the time avaiable? OpenGL ES 2.0? Canvas? or APIs/ graphics/game engines /libraries (which ones, and is there documentation/tutorials?)?

The game itself is simple. The things i mentioned are really the big stuff here, at least for me.

Examples of games sharing characteristics with mine would be: Osmos , Spirit

like image 716
jmacedo Avatar asked Dec 26 '22 19:12

jmacedo


2 Answers

First off, Canvas is not incapable of doing a game. However if you look at the game I did entirely with canvas "Squirrel Wars" (on the market), that is about the limitations of a game using canvas on an average device.

Problems with using Canvas is limitations on the memory you have to work with, & limitations of the FPS since it is not hardware accelerated.

The largest limitation and one of my reasons for switching to OpenGL ES 1.1 is that OpenGL will make it so much more easy to port to iOS if you need to.

Also using OpenGL ES you will have much more power over what you do, much more control, and it should be sufficient to hold a many sprites as you could fit in the screen with out cluttering it.

In my current Open GL game I am designing, I am able to render 300-400 sprites a frame at 25-32 FPS.

However by limiting the drawing of sprites to whats on the screen, more like 20-50, I hold out at about 60-70 FPS.

There is a learning curve with OpenGL though, but it is worth learning. I have a 2D FrameWork I designed in 2Days of being introduced and researching the topic, and it meets all my 2D needs perfectly.

I now can reuse my FrameWork for any 2D game I could ever want to design.

without giving to much of it away here is some code to start you off

    import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

public class RunGameActivity extends Activity{
private GLSurfaceView glSurfaceView;
private GLRenderer GLRENDERER;

@Override
public void onCreate(Bundle savedInstanceState) {
    /***************************************************************************************** 
     * This is called when this activity is first creates.
     * First we remove the title bar, make our app full screen
     * It is set to landscape in the manifest file.
     * We than create our OPENGL ES surfaceview and its renderer for us to use
     * and finally setContentView() view so we see the OPENGL ES surfaceview
     * ***************************************************************************************/
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);

    Log.d("RunGameActivity", "onCreate");

    glSurfaceView = new GLSurfaceView(this);
    GLRENDERER = new GLRenderer(this);
    glSurfaceView.setRenderer(GLRENDERER);
    setContentView(glSurfaceView);

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    /***************************************************************************************** 
     * Here we get the incoming on touch event and send it down towards our game
     * via the GLRenderer class. The game does all the actual work with it.
     * ***************************************************************************************/
    Log.d("RunGameActivity", "OnTouchEvent");
synchronized(this) {
        GLRENDERER.HandleMotionEvent(event);
}
    return true;
}



/* Remember to resume the glSurface  */
@Override
protected void onResume() {
    /***************************************************************************************** 
     * Called when app resumes
     * ***************************************************************************************/
    super.onResume();
    glSurfaceView.onResume();
    Log.d("RunGameActivity", "onResume");
}

/* Also pause the glSurface  */
@Override
protected void onPause() {
    /***************************************************************************************** 
     * Called when app pauses
     * ***************************************************************************************/
    super.onPause();
    glSurfaceView.onPause();
    Log.d("RunGameActivity", "onPause");
}

}

I really dont want to just give away everything, its worth LEARNING

But I have created a way for me to handle everything in a very organized manner, from creating vertices, textures, dynamic text, to handling input, faking a "Camera" managing frame rate independent movement, handling gravity When I need it, detecting collision, and animating sprites. Not to mention, handling AdMob ads, inAppBilling, and all the sound Ill ever need.

Just start reading. Its all over the web, and in books. Worth the investment of $40 if you can find a good book.

A lot of the code you will have to custom create yourself. But it is worth setting up your own Framework.

Again I would recommend OpenGL 1.* Compatible on all devices you could want as well.

to get started with the code I gave you, copy paste it into your activity, right click the objects, and create their classes. Eclipse will auto tell/fill alot of the basic stuff for you.

like image 50
WIllJBD Avatar answered Jan 03 '23 14:01

WIllJBD


If you want to include pre Android 3.0 devices, I would not use the canvas. It's not hardware accelerated, so you will get really terrible frame rates.

I did a little experimenting with canvas on my android 2.3 device, and even with a high end processor I couldn't get a textured square to slide across the screen without severe stuttering.

Android 3 & 4 have hardware accelerated views, but for 2.3 I think you really need GPU utilization to make anything remotely playable. You can probably forget about special effects (particles, glows, etc) as well if you don't have GPU acceleration.

OpenGL I believe is the way to go here, or you can use an OpenGL wrapper library (AndEngine seems popular) if you don't want to deal with the API yourself.

like image 27
Tim Avatar answered Jan 03 '23 14:01

Tim