B4A Library GameView - Create 2D Android games

Jim Brown

Active Member
Licensed User
Hi Erel,
Just a couple of suggestions:

Add: A .Visible flag to the BitmapData structure.
If False, then the game object would not be rendered when the GameView is redrawn.

Add: .Position(x,y) as a better way to re-position objects. Just a helper command really, instead of having to manipulate the objects .DestRect values.

Since this is geared toward gaming it would also be good to have:

.CollidesWith(anotherObject) - Returns true if the object is colliding (or overlapping) the refrerenced BitmapData object. This could be a simple box-box check​
.DistanceFrom(anotherObject) - Returns the distance between the object and the referenced BitmapData object​
 

Erel

Administrator
Staff member
Licensed User
Good points about the Visible and Position methods.

Tomorrow I will post the second tutorial which is a working Asteroids game. The code is made of several classes that are intended for reuse in other projects.
One of these classes (SpriteAnimator) moves and animates sprites and another one (CollisionDetector) detects collisions.

As I see it, all the higher level features should be implemented in Basic4android (usually as classes). This will allow developers to customize them as needed. Different games have different requirements.
 

terrencejohns

New Member
Licensed User
Nothing in zip file

Hi there
I've just recently purchased this B4A, and it works well except I find a lot of zip files downloaded contain no files or are corrupt. I'm trying to download the Gameview Library, but when trying to unzip, I find that there's nothing in it. I experienced this with a few of the tutorials as well. Please help

Terrence (terrencejohns:BangHead:)
 

klaus

Expert
Licensed User
Could you precise which downloads.
I had rarely problems downloading zip files.
Every time, downloading a second time solved the problem.
I unzip with 7zip, it seems that other programs have or had some trouble.
But I haven't downloaded everything.

Best regards.
 

worm

Member
Licensed User
This library allows you to use the hardware acceleration feature of Android 3.0 or above devices.
Hi Erel,
I tried the attached program and it works on Android < 3.0.
How the GameView behaves in such case?

Thanks
 

Attachments

Erel

Administrator
Staff member
Licensed User
I will work fine with Android < 3.0, however the graphics will not use the hardware acceleration feature, which is critical for many games.
 

Jim Brown

Active Member
Licensed User
Hi Erel,
Is is possible for you to add support for alpha level rendering per game object? That is, 0..255 (transparent to opaque)
Also, please consider the .Visible option which will make this library much more useful.
Thanks, Jim
 

Erel

Administrator
Staff member
Licensed User
Currently there are no plans to further improve GameView as it was replaced by libGDX library which is much more powerful.
 

Jim Brown

Active Member
Licensed User
LibGDX, whilst powerful, is also (what I consider) massive in size. Gameview weighs in at 7K compared to the ~2MB LibGDX library.
So for a light-weight basic game GameView is good enough. Would you consider releasing its source code Erel?
 

Erel

Administrator
Staff member
Licensed User
Here is the code:
B4X:
package anywheresoftware.b4a.objects;

import java.util.Iterator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BALayout;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.DontInheritEvents;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.keywords.Common;
import anywheresoftware.b4a.keywords.constants.Colors;
import anywheresoftware.b4a.objects.collections.List;
import anywheresoftware.b4a.objects.drawable.CanvasWrapper;
import anywheresoftware.b4a.objects.drawable.CanvasWrapper.BitmapWrapper;
import anywheresoftware.b4a.objects.drawable.CanvasWrapper.RectWrapper;

/**
 * A view that draws itself with hardware accelerated graphics. Suitable for 2d games.
 *See this <link>tutorial|http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/20038-gameview-create-2d-android-games-part-i.html</link>.
 *The hardware acceleration method used is only available in Android 3.0 and above (API level 11 and above).
 */
@ActivityObject
@Version(0.9f)
@ShortName("GameView")
@DontInheritEvents
@Events(values={"Touch (Action As Int, X As Float, Y As Float)"})
public class GameViewWrapper extends ViewWrapper<GameViewWrapper.MyPanel>{

   @Override
   @Hide
   public void innerInitialize(final BA ba, final String eventName, boolean keepOldObject) {
     if (!keepOldObject)
       setObject(new MyPanel(ba.context));
     super.innerInitialize(ba, eventName, true);
     if (ba.subExists(eventName + "_touch")) {
       getObject().setOnTouchListener(new View.OnTouchListener() {

         @Override
         public boolean onTouch(View v, MotionEvent event) {
           ba.raiseEventFromUI(getObject(), eventName + "_touch", event.getAction(), event.getX(),
               event.getY());
           return true;

         }
       });
     }
   }
   /**
    * Tests whether hardware acceleration is supported.
    */
   public boolean getIsHardwareAccelerated() {
     return getObject().isHardwareAccelerated();
   }
   /**
    * Returns the list of BitmapData objects.
    */
   public List getBitmapsData() {
     return getObject().sprites;
   }
   @ShortName("BitmapData")
   public static class BitmapData {
     /**
      * The bitmap that will be drawn.
      */
     public BitmapWrapper Bitmap = new BitmapWrapper();
     /**
      * The source rectangle. Determines the bitmap's region that will be drawn. The complete bitmap will
      *be drawn if the rectangle is uninitialized.
      */
     public RectWrapper SrcRect = new RectWrapper();
     /**
      * The target rectangle. Determines the location and size of the drawn bitmap.
      */
     public RectWrapper DestRect = new RectWrapper();
     /**
      * Number of degrees to rotate the bitmap.
      */
     public int Rotate = 0;
     /**
      * Flips the bitmap based on one of the FLIP constants.
      */
     public int Flip = 0;
     public static final int FLIP_NONE = 0, FLIP_VERTICALLY = 1, FLIP_HORIZONTALLY = 2, FLIP_BOTH = 3;
     /**
      * If Delete is True then the BitmapData will be removed from the list when GameView is redrawn.
      */
     public boolean Delete = false;
     @Hide
     @Override
     public String toString() {
       return "Src: " + String.valueOf(SrcRect) + ", Dest: " + String.valueOf(DestRect) + ", Bitmap: " +
       String.valueOf(Bitmap) + ", Delete: " + String.valueOf(Delete);
     }
     
   }
   @Hide
   public static class MyPanel extends View {
     public List sprites = new List();
     
     public MyPanel(Context context) {
       super(context);
       sprites.Initialize();
     }

     @Override
     protected void onDraw(Canvas c) {
       Iterator<Object> it = sprites.getObject().iterator();
       while (it.hasNext()) {
         Object o = it.next();
         BitmapData br = (BitmapData)o;
         if (br.Delete) {
           it.remove();
         }
         else {
           Rect destRect = br.DestRect.getObject();
           if (br.Rotate != 0 || br.Flip != 0) {
             c.save();
             if (br.Rotate != 0)
               c.rotate(br.Rotate, destRect.centerX(), destRect.centerY());
             if (br.Flip != 0) {
               c.scale((br.Flip & BitmapData.FLIP_HORIZONTALLY) == BitmapData.FLIP_HORIZONTALLY ? -1 : 1,
                   (br.Flip & BitmapData.FLIP_VERTICALLY) == BitmapData.FLIP_VERTICALLY ? -1 : 1,
                       destRect.centerX(), destRect.centerY());
             }
             c.drawBitmap(br.Bitmap.getObject(), br.SrcRect.getObjectOrNull(), destRect, null);
             c.restore();
           }
           else {
             c.drawBitmap(br.Bitmap.getObject(), br.SrcRect.getObjectOrNull(), destRect, null);
           }
         }
       }
     }
   }
}
If possible please upload your improvements as a new library...
 

ThRuST

Well-Known Member
Licensed User
Can someone please update the Gameview library to include longclick and doubleclick as in the GDX library? I know Erel does not want to spend much more time on it but since the gameview is available it should better be upgraded, maybe someone else can do this? It's a great lightweight lib that is useful for some openGL needs. A way to interact with the library is fundamental.
 

Informatix

Expert
Licensed User
Can someone please update the Gameview library to include longclick and doubleclick as in the GDX library? I know Erel does not want to spend much more time on it but since the gameview is available it should better be upgraded, maybe someone else can do this? It's a great lightweight lib that is useful for some openGL needs. A way to interact with the library is fundamental.
Use the Gesture Detector library.
For "some openGL needs", the Accelerated Surface library is probably a better choice.
 
Top