1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

B4A Library GameView - Create 2D Android games

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jul 31, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Attached Files:

  2. Jim Brown

    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​
  3. Erel

    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.
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. terrencejohns

    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:)
  6. klaus

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

    terrencejohns New Member Licensed User

    Hi there
    I got a response from Erel on another thread - Using Google Chrome solves the problem - Thank you
  8. worm

    worm Member Licensed User

    Hi Erel,
    I tried the attached program and it works on Android < 3.0.
    How the GameView behaves in such case?


    Attached Files:

  9. Erel

    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.
  10. Armoured

    Armoured Member Licensed User

    Erel this lib is more new of the 0.90 version included in the basic4androids setup?
  11. Erel

    Erel Administrator Staff Member Licensed User

    It is the same version.
  12. Jim Brown

    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
  13. Erel

    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.
  14. Jim Brown

    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?
  15. Erel

    Erel Administrator Staff Member Licensed User

    Here is the code:
    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/
     *The hardware acceleration method used 
    is only available in Android 3.0 and above (API level 11 and above).
    "Touch (Action As Int, X As Float, Y As Float)"})
    public class GameViewWrapper extends ViewWrapper<GameViewWrapper.MyPanel>{

       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() {

             public boolean onTouch(View v, MotionEvent event) {
               ba.raiseEventFromUI(getObject(), eventName + "_touch", event.getAction(), event.getX(),
               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;
       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;
         public String toString() {
           return "Src: " + String.valueOf(SrcRect) + ", Dest: " + String.valueOf(DestRect) + ", Bitmap: " +
           String.valueOf(Bitmap) + ", Delete: " + String.valueOf(Delete);
       public static class MyPanel extends View {
         public List sprites = new List();
         public MyPanel(Context context) {

         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) {
             else {
               Rect destRect = br.DestRect.getObject();
               if (br.Rotate != 0 || br.Flip != 0) {
                 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);
               else {
                 c.drawBitmap(br.Bitmap.getObject(), br.SrcRect.getObjectOrNull(), destRect, null);
    If possible please upload your improvements as a new library...
    GabrielM, Caffeind and Jim Brown like this.
  16. Informatix

    Informatix Expert Licensed User

    You may also consider my Accelerated Surface library.
  17. ivanomonti

    ivanomonti Well-Known Member Licensed User

    Erel, this library for B4I ... possible!
  18. ThRuST

    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.
  19. Informatix

    Informatix Expert Licensed User

    Use the Gesture Detector library.
    For "some openGL needs", the Accelerated Surface library is probably a better choice.
    ThRuST likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice