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,305
Last edited:

Carcas

Member
Licensed User
Longtime User
Initially I thought it was my device

I did a lot of tests on my device (debug/release)

if I do not put in the timer loop this:

B4X:
lblFPS.Text = NumberFormat(fps, 0, 0)

the movement is not fluid. it was very strange.

if i put this

B4X:
lblFPS.Text = "test"

is nice

if i put this

B4X:
lblFPS.Text = ""

is bad

On samsung S2 some problem
 
Last edited:

Carcas

Member
Licensed User
Longtime User
without label does not work

Also the speed changes.

ultimately if I remove the label FPS from the exampleSmile does not work
 

robots4life

New Member
I just had a question before I purchase this software. If I were to use gameview instead of imageviews, would it increase the speed of the game if the pictures loaded changed every tick rather then having them rotate/move the same picture each time as you showed in these examples?
 

sterlingy

Active Member
Licensed User
Longtime User
You should look at the lib libGDX. If you are new to b4a, you might find the learning curve a bit high, but once you get into it, you'll find it quite easy, and speed is not an issue.
 
Top