B4A Library [Lib] Accelerated surface

This library provides a dedicated drawing surface embedded inside a view, which benefits from the hardware acceleration. With it, you get the speed of OpenGL for 2D without the complexity.
It includes many Canvas methods (with anti-aliasing, matrix and camera) and a few useful methods for Bitmaps and Drawables (AlterColors, Crop, LoadNinePatch, ReduceColors, SetDensity, etc.). You can import the Matrix, Camera, Paint and Path objects from another library (if they are not wrapped).

surface.png

imageviews.jpg


You can use it to make games:

princess_tiles.png

space_enemies.png


It includes a tool (TextFactory) to do nice titles (that you can export to a bitmap):

textfactory.jpg


It supports Porter-Duff modes, color filters and texture blending (the processing time is very fast):

pdmodes.jpg


The archive includes four benchmarks:

Perf.png


Because of my lack of (free) time, don't expect answers from me about this library if you're not one of my donors.

Download the latest version (1.12)
To convert a project from v0.9x to v1.x, read this.

Hints & Tips

This library does not work with Android versions < 2 (Eclair and Froyo may exhibit performance problems, so I recommend only Gingerbread or a newer version for animations with a high FPS).
The hardware acceleration is not enabled with Android versions < 3.
 

Attachments

  • Java source - AcceleratedSurface.zip
    21.3 KB · Views: 653
Last edited:

kostas3001

Member
Licensed User
Longtime User
I tested with Soundpool, but the effect of "frozen image" is the same ... are small cuts in the sequence of images ...

Can you put Shooting sound on the SpaceEnemies example?
 

Informatix

Expert
Licensed User
Longtime User
I tested with Soundpool, but the effect of "frozen image" is the same ... are small cuts in the sequence of images ...

Can you put Shooting sound on the SpaceEnemies example?

Example:
In Activity_Create:
SP.Initialize(8)
mySoundID = SP.Load(File.DirAssets, SoundFile)

When you want to play the sound:
SP.Play(mySoundID, 1, 1, 1, 0, 1)

When you exit the game:
SP.Release
 

kostas3001

Member
Licensed User
Longtime User
Yes, but when y put it in AcSf_Touch, then I have the problem...

Sub AcSf_Touch(Action As Int, X As Int, Y As Int, Event As Object)
If Action = Activity.ACTION_DOWN Then
If FireWait <= 0 Then
Dim LaserShot As typLaser

SP.Stop(PlayId1)
PlayId1 = SP.Play(LoadId1, 1, 1, 1, 0, 1)

LaserShot.Y = PositionY + Bit.ShiftRight(Nave.Height, 1) - 0.25%y
LaserShot.X = PositionX + Nave.Width ' - 1.5%x - LaserShotSize
LaserShots.Add(LaserShot)
FireWait = WaitBeforeNextShot

End If
End If
End Sub
 

Informatix

Expert
Licensed User
Longtime User
Yes, but when y put it in AcSf_Touch, then I have the problem...

Sub AcSf_Touch(Action As Int, X As Int, Y As Int, Event As Object)
If Action = Activity.ACTION_DOWN Then
If FireWait <= 0 Then
Dim LaserShot As typLaser

SP.Stop(PlayId1)
PlayId1 = SP.Play(LoadId1, 1, 1, 1, 0, 1)

LaserShot.Y = PositionY + Bit.ShiftRight(Nave.Height, 1) - 0.25%y
LaserShot.X = PositionX + Nave.Width ' - 1.5%x - LaserShotSize
LaserShots.Add(LaserShot)
FireWait = WaitBeforeNextShot

End If
End If
End Sub

I don't understand why you call SP.Stop.
Here's a Space Enemies version with sounds. I grabbed these sounds on internet and I don't know their license, so don't use them in your project.
 

Attachments

  • SEsound.zip
    65.1 KB · Views: 262

kostas3001

Member
Licensed User
Longtime User
I don’t understand what happens ...
With your program I have the same effect.

On my tablet everything works perfectly (ARCHOS 101 G9, Android 4.0.4) but on my phone (Sony Ericsson Neo V, Android 4.0.4) experiment sometimes small 'lag', picture freezes, and it's annoying.

I dont have any problem if I don’t activate the sound.

I think that the sound interruptions have influence on drawing processes.

Lets see if I can explain my self… I have some objects with diferent trajectories on the screen at constant speed, but when I activate the sound unexpected “accelerations” occurs while running it on the tablet and the screen freezes (small ‘lag’) while running it on the phone… The tablet has better hardware.

Sorry for the inconvenience.
 

Informatix

Expert
Licensed User
Longtime User
but on my phone (Sony Ericsson Neo V, Android 4.0.4) experiment sometimes small 'lag', picture freezes, and it's annoying.

I cannot say what happens on your tablet, I don't have this model. But I have a Neo V with Android 4.0.4 and everything works well on it. Even with the emulator, there's no lag or noticeable issue. So your problem is a complete mystery to me.
What is your B4A version? Do you run in Debug mode?
 

kostas3001

Member
Licensed User
Longtime User
My B4A versión: 2.52, and yes, I can run in Debug mode.

And I have the problem when I use Sub AcSf_Touch(Action As Int, X As Int, Y As Int, Event As Object) quickly
 
Last edited:

kostas3001

Member
Licensed User
Longtime User
I've tried what you say and the result is the same.
I uninstalled Antivirus.

The problem appears when I press the screen to fire.

This afternoon I will try external hardware, Bluetooth communication, to shoot with an external switch, no need to press the screen, and I will comment on the result.
 

kostas3001

Member
Licensed User
Longtime User
Yes, and the result is the same...

and on my software, if I put a background continuous sound, the effect is worse.

The commercial games with sound works well.
 

Informatix

Expert
Licensed User
Longtime User
Yes, and the result is the same...

and on my software, if I put a background continuous sound, the effect is worse.

The commercial games with sound works well.

You have a problem with your device because I have the same device and no problem on mine. Did you "tweak" some settings on it? Did you install a custom ROM? I could say the problem come from SoundPool but I see no problem at all on my four devices and on the emulator.
Java developpers may use other libraries to play sounds so trying their apps doesn't help to understand.
 

kostas3001

Member
Licensed User
Longtime User
No, my device is original, without changes.

OK, thanks for your patience and your help. I will continue investigating.
 

kostas3001

Member
Licensed User
Longtime User
It's the same effect.

I have deleted several programs and services, but is the same effect.
Maybe a little better...
 

Informatix

Expert
Licensed User
Longtime User
It's the same effect.

I have deleted several programs and services, but is the same effect.
Maybe a little better...

That really looks like a problem with your device. Here are two videos (bad quality, sorry) showing the two versions of SE (SoundPool and OpenSL) on the Xperia Neo V:
http://les2fred.servehttp.com:8080/fbsharing/siEuRCE4
The antivirus creates a slight lag a few seconds after the beginning on one video, but it's almost unnoticeable.
 

sterlingy

Active Member
Licensed User
Longtime User
Is there a way to use AcceleratedSurface with sprite sheets, similar to the way GameView parses a sprite sheet into individual images for animating?
 

Informatix

Expert
Licensed User
Longtime User
Is there a way to use AcceleratedSurface with sprite sheets, similar to the way GameView parses a sprite sheet into individual images for animating?

Copying the sprite from the sheet is a bit slow, whatever method you use. It's better to separate the individual images before animating them. As you can see in my benchmark, one of the slowest method to draw a bitmap is to use the DrawBitmap function, which is the one used to retrieve a sprite from a sheet (and the one used by GameView).
 
Top