Android Tutorial GameView - Create 2D Android games - Part I

For new games it is recommended to use libGDX. See these tutorials:
How to make games
[URL='http://www.b4x.com/android/forum/threads/32592']Introduction to the libGDX library
[/URL]


GameView is a view that allows you to draw hardware accelerated graphics. Compared to software accelerated graphics, hardware accelerated graphics are many times faster. Using hardware accelerated graphics it is possible to create smooth, real-time games.

Note that the acceleration method used by GameView is only available from Android 3.0 and above. This also means that you need to reference android.jar from platform 11 or above (under Tools -> Configure paths).

You should add this line to the manifest editor:
B4X:
SetApplicationAttribute(android:hardwareAccelerated, "true")

It is important to understand how GameView works.

GameView holds a list of BitmapData objects. Each BitmapData object holds a reference to a bitmap and some parameters that tell GameView how to draw it.

When GameView redraws itself, it goes over the list of BitmapData objects and draws each one of them. The important thing about GameView is that the drawings are hardware accelerated.

In order to make GameView redraw itself you should call GameView.Invalidate.

BitmapData

Each BitmapData object represents a bitmap (or sprite) that will be drawn when GameView redraws itself.
BitmapData properties are:
  • Bitmap - The bitmap that will be drawn.
  • DestRect - A rectangle that defines the location and size of the drawn bitmap. For example to move a sprite you change DestRect values.
  • SrcRect - A rectangle that defines the bitmap's region that will be drawn. You can pass an uninitialized rectangle if you want to draw the complete bitmap. SrcRect can be useful for drawing a sprite from a sprite sheet, or to create scrolling effects.
  • Delete - A boolean value. When set to True, GameView will remove this BitmapData from the list during the next drawing (the bitmap will not be drawn).
  • Rotate - Number of degrees to rotate the bitmap.
  • Flip - Flips the bitmap based on one of the FLIP constants.

Typical game structure

Usually your game should consist of a single main timer. All the movements and the logic should happen in this timer's tick event. Eventually you call GameView.Invalidate. This will cause GameView to redraw itself after your code execution completes.

Simple bouncing smiley with a scrolling background

SS-2012-07-31_15.58.32.png


The attached project is a simple example with two BitmapData objects. One is the background and the other is the moving smiley. The background is scrolling to the left each tick. This is done by playing with SrcRect values so each part of the wide background image is draw each time.

The smiley DestRect is modified every tick to make it move.

The second part of this tutorial with a working "Asteroids" game is available here: http://www.b4x.com/forum/basic4andr...gameview-create-2d-android-games-part-ii.html

SS-2012-07-31_16.12.12.png


Another example (jumping smiley): http://www.b4x.com/forum/basic4andr...ngsmiley-gameview-example-iii.html#post151177

SS-2013-02-07_18.17.28.png
 

Attachments

  • GameViewSmiley.zip
    9.4 KB · Views: 5,299
Last edited:

squaremation

Active Member
Licensed User
Longtime User
Parse Error

I am getting message on device:

> Parse error
There is a problem parsing the package

:BangHead:
 

jaraiza

Active Member
Licensed User
Longtime User
Do you get this error with other applications / examples or only this one?

Note that GameView requires Android 4+ in order to use hardware acceleration. Though it should also run on older devices. Try to remove the hardwareAcceleration line from the manifest editor.

Android 4+ or 3+?

Also... Is there anyway to implement a way to make an app to use HW acceleration depending on the OS where it is being run on?

Thanks!
 

squaremation

Active Member
Licensed User
Longtime User
Resolved example issues on older devices

Do you get this error with other applications / examples or only this one?

Note that GameView requires Android 4+ in order to use hardware acceleration. Though it should also run on older devices. Try to remove the hardwareAcceleration line from the manifest editor.

Yes only this example. Remove hardwareAcceleration from manifest editor, still same error.
Unfiltered logs:
B4X:
/mnt/sdcard/temp.apk (at Binary XML file line #9): Requires newer sdk version #11 (current version is #8)
Parse error when parsing manifest. Discontinuing installation

Changed Manifest minimum sdk to 8
B4X:
<uses-sdk android:minSdkVersion="8" />

Now it will install but, Force Closes when you attempt to open

Unfiltered logs:
B4X:
If gv.IsHardwareAccelerated = False Then
java.lang.NoSuchMethodError: anywheresoftware.b4a.objects.GameViewWrapper$MyPanel.isHardwareAccelerated

OK I got it to work by:

1. Remove from Manifest Editor
B4X:
SetApplicationAttribute(android:hardwareAccelerated, "true") r
2. Change minSdkVersion to 8 in Manifest Editor
B4X:
<uses-sdk android:minSdkVersion="8" />
3. Block Comment this code in Activity_Create
B4X:
'   If gv.IsHardwareAccelerated = False Then
'      Msgbox("Hardware acceleration is not supported!!!", "")
'   End If

Then it will run on older devices YAY!
 
Last edited:

sorex

Expert
Licensed User
Longtime User
if you disable the check does that mean it will work on let's say 2.2 devices but at snail speed? or still 3.0+ only?
 

squaremation

Active Member
Licensed User
Longtime User
It make you able to use example on 2.2.
The example at least does not lag at all and runs well.:)
 

tekh2999

Member
Licensed User
Longtime User
HardwareAccelerated error

I'm having the same problem SetApplicationAttribute(android:hardwareAccelerate d, "true") is there
but still getting this error:

Compiling code. 0.08
Compiling layouts code. 0.05
Generating R file. Error
AndroidManifest.xml:17: error: No resource identifier found for attribute 'hardwareAccelerated' in package 'android'

I'm just using the Emulator that I set with GPU emulation and audio added to it.

can anyone help?
 
Last edited:

sterlingy

Active Member
Licensed User
Longtime User
I noticed in the sample code for GameviewSmiley, that there was a comment stating that images larger than 2048x2048 would not be drawn. Is this a limit of Android? Is there a way around this?

-Sterling
 

mjtaryan

Active Member
Licensed User
Longtime User
Very nice. Since I'm just beginning playing with games, I'm wondering how I would put GameView and the Touch Event together so that I can move game pieces via touch? Any advice would be helpful. Thanks.
 

sterlingy

Active Member
Licensed User
Longtime User

nicholas.jj.taylor

Member
Licensed User
Longtime User
Really good, but beyond my capability.... i wish i could make games.... for me to learn this i would need every line commented on

If that's true then it sounds to me as if you would be the perfect candidate to sit down with and write a game programming tutorial with.
 

Carcas

Member
Licensed User
Longtime User
I'm sorry but I'm going crazy

I created a simple app bouncy ball but it isnt' flowing. I used example for it.

I modified example to compare.

Each application has the identical parameters (timer.interval=1ms)(size=5%x)(speed 5dip)

The structures are identical but my app is islower and not smooth

help me otherwise I have to go to a psychiatrist

ps: manifest is Ok

my device is HUAWEI 300Y
 

Attachments

  • MyApp.zip
    11.3 KB · Views: 489
  • SmileModified.zip
    11 KB · Views: 470

Carcas

Member
Licensed User
Longtime User
Ok i understand thet this:

B4X:
fps = (1000 / Max(10, (DateTime.Now - lastTime)) + 20 * fps) / 21
    lblFPS.Text = NumberFormat(fps, 0, 0)
    lastTime = DateTime.Now

is necessary but i dont understand why?
 
Top