B4A Library Game Sprite Library

Jim Brown

Active Member
Licensed User
Hi all,
This is my first attempt at creating a library so I hope it works.
It is a heavily modified version of the code Erel kindly posted for his GameView library.

03/02/2014 - v0.1
First Release
04/02/2014 - v0.2
Added FPS code to the GameSpriteView
12/02/2014 - v0.3
Removed the list-based system replacing it with a sub _Draw() call instead. Also added a convenient DrawText() command. Useful for displaying basic text messages (FPS monitoring).
16/04/2014 - v0.4
Changed: AnimDelayRate to AnimDelayTime. Now using milliseconds for timing.
Added: Filter to the Sprite class
Added: DeltaTime to the GameSpriteView class


GameSprite Library v0.4



GameSprite a very light-weight (10K) library designed with 2D games creation in mind. As the name suggests the library is geared towards sprite handling with a simple to understand command set.
The library features a powerful one-call animation command to set up automated animation.

Hardware acceleration is supported.

The key features are:
  • GameSpriteView - the main display supporting virtual screen sizing, global rotation, global anchor point , get FPS, get DeltaTime
  • Sprite - A bitmap-based object with positioning, anchor, size, flip, rotate, colour, alpha, animation, region support, basic collision (overlap) checking, and distance checking.

GameSpriteView methods and variables
IsHardwareAccelerated()
VirtualDisplaySize() , VirtualWidth() , VirtualHeight() , VirtualX() , VirtualY()
Anchor() , AnchorX , AnchorY
DrawText()
Rotate
FPS
Sprite methods and variables
Bitmap
Position() , X , Y , CenterX , CenterY
Size() , ScaleSize() , Width , Height
Anchor() , MidAnchor, AnchorX, AnchorY,
Rotate , FlipX, FlipY
AnimDefineCells() , AnimSequence() , AnimCellRange() , AnimCellNumber , AnimDelayTime , AnimNextCell , AnimPreviousCell , AnimPlay
Region() , ResetRegion , CopyRegion()
isOver() , Overlaps() , DistanceTo()
ARGB() , RGB() , Alpha, Filter
Visible , Copy() , Reset​


You can get the library here, which includes some basic examples you get you started.
Included in the zip is an updated Pocket Invaders, which is a fully playable game to show-case the library.

DOWNLOAD LINK

Also now available, a companion tool written in B4J called SpriteMotion for assisting in the process of defining sprites contained within a sprite sheet.
 
Last edited:

Informatix

Expert
Licensed User
Between libGDX that is powerful but too big for small games with a few sprites, GameView that is very limited and Accelerated Surface which is not specifically games-oriented, there's some room for sure and your library sounds promising for the place. I added it to my tutorial about making games.
Just a comment: your FPS counting is completely wrong (e.g. I get 66 fps on a hardware that cannot go over 60 fps) and comes directly from GameView where it is completely wrong too because it counts the number of times you enter the Tick event, which is unrelated with the number of times things are drawn on screen, especially since honeycomb. If you want to know this number with accuracy, you have to override onDraw in your Java code and place your FPS counting here. You will see that real numbers are probably much lower than what you get currently.
 

Erel

Administrator
Staff member
Licensed User
The FPS calculated in GameView should be ~99% correct. It is correct because of the way timer works. The timer sends a delayed message to the message queue which is later processed by the main thread. It send a single message each time. The same thing happens when you call gv.Invalidate. Both onDraw and the timer tick event run on the same thread.

I tested it now by adding a counter to onDraw and compared it to a counter in Timer_Tick event. After several minutes the values were almost identical (99.8%).

BTW, Jim I think that you should write in your documentation that your library is based on GameView.
 

Informatix

Expert
Licensed User
The FPS calculated in GameView should be ~99% correct. It is correct because of the way timer works. The timer sends a delayed message to the message queue which is later processed by the main thread. It send a single message each time. The same thing happens when you call gv.Invalidate. Both onDraw and the timer tick event run on the same thread.

I tested it now by adding a counter to onDraw and compared it to a counter in Timer_Tick event. After several minutes the values were almost identical (99.8%).
I'd like to know how you can get up to 69 fps on a hardware that is limited to 60 fps and whether you tested this accuracy in low fps situations (where you enter regularly in the tick event but not at all in the draw event because of the time needed to render).
 

Erel

Administrator
Staff member
Licensed User
I didn't test it in a low fps situation. If you like I can send you a modified version of GameView with this counter.
 

Informatix

Expert
Licensed User
I discovered a strange thing while evaluating the performance of the lib (rather good as far as I can see):
if I set 160 walkers in the attached example, I have a smooth animation. If I set 5 more, everything is horribly slow. The drop in FPS is huge. Any explanation?
 

Attachments

Informatix

Expert
Licensed User
I didn't test it in a low fps situation. If you like I can send you a modified version of GameView with this counter.
I already made these tests some months ago when I was working on my Accelerated Surface library. When you look at the sequence of events (by sending a timestamp to the log when you enter the events), you can see the following situation when the drawing routine needs more time to render than the interval set for the timer:
- tick
- tick
- tick
- draw
- tick
- tick
- tick
- draw
- etc.

I already reported these findings somewhere in the forum (if an archaeologist wants to dig them...) and why the FPS count cannot be trusted when it is based on the Tick event.
 

Erel

Administrator
Staff member
Licensed User
I understand and agree. The FPS should be calculated based on the onDraw method and not the tick event.

Though the high FPS (~60) should be correct.
 

Informatix

Expert
Licensed User
Though the high FPS (~60) should be correct.
Yes, but the formula used is a bit optimistic. The following should be closer to the reality:
B4X:
If DateTime.Now > oldtime Then
  oldtime = DateTime.now + 1000
  fps = framecount
  framecount = 0
Else
  framecount = framecount + 1
End If
Edit: with the above formula, my Nexus 7 now reports only 60 fps in the best case, which is correct.
 
Last edited:

Jim Brown

Active Member
Licensed User
Please take all FPS discussions to another thread. While they are fascinating (and educational) I feel my library thread is being swamped.
This is good to know though ....
If you want to know this number with accuracy, you have to override onDraw in your Java code and place your FPS counting here. You will see that real numbers are probably much lower than what you get currently
Thanks, Jim.
 

Jim Brown

Active Member
Licensed User
Indeed, but as you can see its turning into an ongoing debate. I don't want it to consume my library thread.
 

Informatix

Expert
Licensed User
Indeed, but as you can see its turning into an ongoing debate. I don't want it to consume my library thread.
???? I hope we're free in this thread to speak of your library as long as we want. If you want to control what we say and how many messages we're allowed to post, don't worry, that will be the last from me.
 

Erel

Administrator
Staff member
Licensed User
There is no overhead at all. It will increase your app size in 10kb (1/30 of a standard image).
 

sorex

Expert
Licensed User
that's what I meant, 10K filesize addition.

I have issues to compile to that genymotion emulator. it nags about SDK things and when I tweak the manifest
it crashes and gives

"The app Animation (process anywheresoftware.b4a.samples.gameviewsmiley) has stopped unexpectedly."

the same happends on my phone...

Installing file to device. Error
pkg: /data/local/tmp/Animation_DEBUG.apk
Failure [INSTALL_FAILED_OLDER_SDK]

Restarting ADB Server may solve this problem.
Tools - Restart ADB Server. Device serial: 644243a29adc
Which SDK's do I need to have installed to get this working?
 

Erel

Administrator
Staff member
Licensed User
The minSdkVersion is set to 11 (Android 3). The hardware acceleration will not work with older versions. You haven't posted the error message from the emulator. So it is hard to say why it didn't work.
 
Top