Android Tutorial [B4X] [BitmapCreator] Cross platform Falling Sand game

SS-2018-06-19_11.20.27.png


The main challenge here is the performance. All particles are updated if needed and redrawn every cycle.

The Particles array stores the state of all "game pixels". A value of 0 means that the pixel is empty. Other values mean that there is a particle on that pixel.
Sand particles move down or diagonally if they can.

The RowsState array tracks the rows that are considered dirty. Non-dirty rows are skipped.

Single dimension arrays of bytes can be accessed in an optimized way in B4i with Bit.FastArrayGet / Set methods (https://www.b4x.com/android/forum/threads/92951/#content). I used these methods in some of the cases.

All the important code is implemented in the Game class, which is of course shared between the three projects. No sprites are used in this game. The drawings are done in the MoveParticles sub.
As in the Tetris game the layout is managed with the designer script.

You need to change the GameUtils module that is included in the zip files with the one attached separately.

Dependencies:

- Recent version of XUI.
- BitmapCreator v3.6 (B4J, B4i)

The three projects are attached. I've also uploaded an executable jar. Download and double click.
www.b4x.com/b4j/files/games/FallingSand_ExecutableJar.jar
 

Attachments

  • B4A_FallingSand.zip
    25.6 KB · Views: 879
  • B4i_FallingSand.zip
    177.6 KB · Views: 673
  • B4J_FallingSand.zip
    24.6 KB · Views: 712
  • GameUtils.bas
    6.5 KB · Views: 767
Last edited:

Roger Taylor

Member
Licensed User
Longtime User
I have another question concerning the main game loop. It calls mGame.Tick(gs) after gs.GameTime is incremented. What does .GameTime actually control? I need another loop that runs as fast as the app will allow with no concerns about the FPS. This is where I need to do my CPU stepping. So far I've only been able to get a 60 Hz CPU rate because the MainLoop seems to be locked to this and that's where I temporarily placed my test CPU stepping call.

Also, does the GameStep class do all of the FPS maintenance? I see it locking to 30, 60, 90, 120 FPS... the nearest it can achieve for the device the app is running on.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
What does .GameTime actually control?
It allows the consumer to have a time measurement that is separate from the actual FPS.
I wouldn't spend too much time with this code as a much more powerful implementation is available in X2 framework.
In X2 it tries to maintain 60 FPS and reduces it to 30 FPS if needed. It tries to avoid fluctuations which are more noticeable than a lower frame rate.
 
Top