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: 306
Last edited:

Informatix

Expert
Licensed User
You've said so yourself:

You misunderstood what I wrote. I never suggested to hide anything, just to avoid repeating the same static drawings to save FPS.
What I suggest is to draw your background, render it into a bitmap and reuse this bitmap instead of redrawing all the complex stuff composing your background. The drawing is done in the Render event, the copy to a bitmap is done in the Update event and a flag is set to say "hey, you can now render this bitmap instead" that will be read by the next Render event. No need for a second Accel. Surf.
It's a bit strange that drawing only 50 bitmaps take so much time that you need to hide the process (unless you load these bitmaps in the Render event which is a terrible idea).
 

ilan

Expert
Licensed User
hi

i am working on a new simple puzzle game and i tried first to use only the core lib (draw everything via canvas)
the code works but the performance was not good enough so i decided to try acclerated surface since libgdx would be to much for such a simple game
and it is working really great.

just wanted to thank you for this lib :)
 

Informatix

Expert
Licensed User
hi

is it possible to draw a glowing line with Accelerated Surface?
A "glowing line" is a composite object made usually of two things (a line and a halo), so the answer is YES (you can display anything with this lib) and NO (such object does not exist as is).
 

ilan

Expert
Licensed User

Informatix

Expert
Licensed User
i wanted to use this: https://www.b4x.com/android/forum/t...with-a-gradient-or-texture.57246/#post-361056

but AC ist not a canvas that can use that option.
This code does what I said: it draws a halo (a radiant light around an object) and a line, the halo being just a blurred line. You can reuse the same idea with AC but I don't have the time for now to create an example. In brief, you create the mask and the paint objects and you set the paint property of AC to this paint object before drawing. It's mainly a copy/paste operation.
 

ilan

Expert
Licensed User
This code does what I said: it draws a halo (a radiant light around an object) and a line, the halo being just a blurred line. You can reuse the same idea with AC but I don't have the time for now to create an example. In brief, you create the mask and the paint objects and you set the paint property of AC to this paint object before drawing. It's mainly a copy/paste operation.

i could not figure out how to do it with AS (tried for 2 days), currently i am using a transparent panel and i am drawing via canvas and the code i posted above on that panel but i would like to use AS for that since it is using Hardware Acceleration. if you will find time to make a simple short example code (only how to draw 1 line) i will really appreciate it. thanx in advance :)
 

Informatix

Expert
Licensed User
i could not figure out how to do it with AS (tried for 2 days), currently i am using a transparent panel and i am drawing via canvas and the code i posted above on that panel but i would like to use AS for that since it is using Hardware Acceleration. if you will find time to make a simple short example code (only how to draw 1 line) i will really appreciate it. thanx in advance :)
As I said, it's just a copy/paste and you replace the canvasJO part to draw the line by:
B4X:
Line1.CreateLine(50%x, 50%y, False)
Line1.Paint = paintJO
AC.DrawObjectAt(Line1, 10dip, 10dip)
BUT this effect requires to disable the hardware acceleration because in accelerated mode, the canvas draws a solid line for the halo.
 

Attachments

  • Glowing line.zip
    1.3 KB · Views: 90

ilan

Expert
Licensed User
Thank you @Informatix , i didnot understood you well that i should use javaobject for that.

I thought i could do it by creating a gradientline with as_texture (color to transparent) and like this get a glowing effect.

So i guess i have to use box2d lights for that. Drawing with canvas get me 9-12 fps.

Thank you :)
 

Informatix

Expert
Licensed User
Thank you @Informatix , i didnot understood you well that i should use javaobject for that.

I thought i could do it by creating a gradientline with as_texture (color to transparent) and like this get a glowing effect.

So i guess i have to use box2d lights for that. Drawing with canvas get me 9-12 fps.

Thank you :)
Did you try to measure the performance with my lib?
Anyway, the simplest and fastest method to draw things like a laser beam is to draw a texture containing the effect already rendered on a transparent background. You have a complete explanation by following the link given in my guide "How to make games".
 

ilan

Expert
Licensed User
Did you try to measure the performance with my lib?

i am using your FPS code from one of your AS examples.

maybe i will use your advice and create (about 30 small pngs) that got only the glow effect and draw the image behind the line.
i will need to draw each line instead of drawing the whole path like i do know so i hope that the performance wont go down.

btw this is the game i am working on it now and i would like to draw instead of the filled shapes only glowing frames without any filled color in it.


EDIT: this game is using Only Accelerated Surface and Core lib + lots of math :)

https://www.b4x.com/android/forum/threads/power-blocks-puzzle-game.67933/#post-430681
 
Last edited:

sorex

Expert
Licensed User
@Informatix ,

I'm testing this lib again and notice that it's a lot smoother on my poor 2.3 Samsung Ace phone than it was on my 2.2 Samsung Apolo that didn't seem to have acceleration on board.
Even smoother than in genymotion for some reason.

So I got a wrong impression all these years about being faster etc :)

What I seem to mis as 'basic function' is a _longClick trigger?
 

sorex

Expert
Licensed User
does that count for _Click aswell?

I mean having both a click & longclick event.
 
Top