Games [XUI2D] Cross platform tips

Erel

B4X founder
Staff member
Licensed User
Longtime User
XUI2D allows us to develop games that run on B4A, B4J and B4i.
This tutorial will explain the recommended way to organize your projects.
I recommend following the suggested structure even if you target a single platform. It is very simple and will help you add more platforms in the future.

I guess that most developers will want to create a mobile game. It is still very useful to start with the B4J implementation. The development cycle in B4J is much faster.

Code modules

We want to share all the game modules between the different platforms.
There are two types of modules:

- The game modules. These are the most important modules and these modules must be shared between the three platforms.
- Platform specific modules including the Main module.

The folders structure should be like this:

SS-2018-09-03_12.55.20.png


Inside the root game folder we have a folder for each platform. The shared modules are stored here.
There is also a shared files folder that will soon be discussed.

As you probably guessed, the platform specific modules are stored in the platform specific folders:

SS-2018-09-03_12.56.52.png


In all the examples included in the examples pack, this folder only contains the main project file and the unused starter service in B4A.

The modules should be added as relative links and it should look like this:

SS-2018-12-19_18.04.57.png


Note the light grey relative path. This means that the game modules are one folder up.

Files

With the exception of the layout files, all other files should be shared between the projects.
The files are saved in the Shared Files folder and are copied with a custom build action that is defined in the Game module:
B4X:
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
Robocopy only copies the files if they were modified so it will not slow down the compilation.
"folders ready" is a new compilation step that happens after the project generated folders were created.

Now we can edit the files in the Shared Files folder and they will be updated automatically when we run the project.

Tips

- B4A and B4J XUI2D library is based on jBox2D. B4i iXUI2D is based on the C++ box2d. In most cases the behavior will be the same. However there are some edge cases.
- Be careful with modifying returned B2Vec2 vectors unless the documentation says that the vector can be modified. When in doubt use B2Vec2.CreateCopy and modify the new vector. The behavior of such cases can be different between the platforms.
- You should of course test on all platforms. Especially when you test performance.
- B4A is the only platform where XUI.Scale is not 1 (on phones). It can take some time to understand how to handle the device scale.
- The simplest way to create a new project is to make a copy of one of the examples inside the X2 folder. As the references are relative all the references will be correct and the projects will be ready.
- There are currently 18 different examples in the examples pack. This means that there are 54 projects. It would not have been possible to create and maintain these projects without this organized structure.
- The files in the platform specific Files folder are empty (size = 0). They are copied from the Shared Files folder on the first run.
- You can use similar structure for other projects as well. Not just XUI2D.
 
Last edited:

tsteward

Well-Known Member
Licensed User
Longtime User
So in a non game project where I have a B4A and B4J versions of my app I can create a shared directory for image files etc. by adding #CustomBuildAction:
Do I have to add the files to the file tab in the IDE still?
 
Top