B4J Library jLibGDXemulator - The easiest way to port your B4A LibGDX games

jLibGDXemulator
Work In Progress [Open Source]


dual.jpg

B4J vs B4A

Have you created a game in B4A using LibGDX? Wouldn't it be awesome if you could just copy-paste your code from B4A into B4J and have it work?
Well, look no further! The future is here! :D

Instructions:
1. Download both jLibGDXemulator and jGameViewHelper libraries and extract the files into your B4J libraries folder.
2. Include the jLibGDXemulator library in your project.
3. Add the LibGDX.bas module to your project.

Remember: jGameViewHelper is required to be in your libs folder.

In order to have B4J understanding the [B]Informatix[B]'s[/B][/B] B4A LibGDX syntax, I've created a sort of emulator, using [B]Erel's[/B] excellent B4J GameView lib.
Below, a B4J (left) and B4A (right) sprite animation example running side-by-side using exactly the same LibGDX code. Ok, 99% the same code...



So far, only a few basic LibGDX functions have been "ported" so don't expect any complex operations such as collision detection. This project is mainly focused in emulating the rendering part.

Remember: jGameViewHelper is required to be in your libs folder.

B4A Example:
B4X:
'B4A Code
'=================================================
    #Region  Project Attributes
        #ApplicationLabel: LibGDX Test
        #VersionCode: 1
        #VersionName: 1
        'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: Landscape
        #CanInstallToExternalStorage: True
    #End Region

    #Region  Activity Attributes
        #FullScreen: True
        #IncludeTitle: False
    #End Region
'=================================================

'B4J Code
'=================================================
'    #Region  Project Attributes
'        #MainFormWidth: 600
'        #MainFormHeight: 400
'    #End Region
'=================================================

Sub Process_Globals
    'Common
        Type Sprite(Texture As lgTexture, TextureRegion As lgTextureRegion, ActiveFrame As Int, ActiveFrameX As Int, ActiveFrameY As Int, Rows As Int, FPS As Int, TimeStamp As Long)

    'B4J Code
    '=================================================
'        'Standard B4J stuff
'            Dim fx              As JFX
'            Dim MainForm        As Form
'
'        'LibGDX
'            Dim GLView          As View
'            Dim lGdx            As LibGDX
'            Dim Gl              As lgGL
'            Dim Camera          As lgOrthographicCamera
'            Dim Batch           As lgSpriteBatch
'            Dim Ryu             As Sprite
    '=================================================
End Sub

'B4J Code
'==========================================================
'Sub AppStart (Form1 As Form, Args() As String)
'    'MainForm Settings
'        MainForm = Form1
'        MainForm.SetFormStyle("UNIFIED")
'        MainForm.RootPane.LoadLayout("test") 'Load the layout file.
'        MainForm.Show
'
'    'Initialize the LibGDX layer
'        lGdx.Initialize
'End Sub
'==========================================================

'B4A Code
'==========================================================
    Sub Globals
        Dim GLView          As View
        Dim lGdx            As LibGDX
        Dim Gl              As lgGL
        Dim Camera          As lgOrthographicCamera
        Dim Batch           As lgSpriteBatch
        Dim Ryu             As Sprite
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        'Do not forget to load the layout file created with the visual designer. For example:
            Activity.LoadLayout("Layout1")

        'Initialize the LibGDX layer
            GLView = lGdx.InitializeView("LG")
            Activity.AddView(GLView, 0%x, 0%y, 100%x, 100%y)
    End Sub

    Sub Activity_Resume
        If lGdx.IsInitialized Then lGdx.Resume
    End Sub

    Sub Activity_Pause(UserClosed As Boolean)
        If lGdx.IsInitialized Then lGdx.Pause
    End Sub
'==========================================================

Sub LG_Create
    'B4J Code:
'        Batch.Initialize(lGdx.lgCanvas)
    'B4A Code:
        Batch.Initialize
    'Common
        Dim Texture As lgTexture
        Dim TextureRegion As lgTextureRegion
        Texture.Initialize("ryu.png")
        TextureRegion.InitializeWithTexture(Texture)
        Ryu.Texture       = Texture
        Ryu.TextureRegion = TextureRegion
End Sub

Sub LG_Resize(Width As Int, Height As Int)
    'Sets the camera viewport
        Camera.Initialize
        Camera.SetToOrtho2(True, lGdx.Graphics.Width, lGdx.Graphics.Height)
End Sub

Sub LG_Render
    'Clears the screen
        Gl.glClear(Gl.GL10_COLOR_BUFFER_BIT)
                 
    'Updates the matrices of the camera
        Camera.Update

    'Uses the coordinate system specified by the camera
        Batch.ProjectionMatrix = Camera.Combined

    'Sprite Animator
        Animation

    'Renderer
        Batch.Begin
        Batch.DrawRegion2(Ryu.TextureRegion, 0, 0, 300, 300)
        Batch.End
End Sub

Sub LG_Dispose
    Batch.dispose
    Ryu.Texture.dispose
End Sub

Sub LG_Pause
    LG_Resume
End Sub

Sub LG_Resume
End Sub

Sub Animation
    Ryu.FPS = 24
    If DateTime.Now - Ryu.TimeStamp > (1 / Ryu.FPS * 1000) Then
        Ryu.TimeStamp = DateTime.Now

        Ryu.Rows = 8
        Ryu.ActiveFrameX = Ryu.ActiveFrame Mod Ryu.Rows
        Ryu.ActiveFrameY = Floor(Ryu.ActiveFrame / Ryu.Rows)

        Ryu.TextureRegion.SetRegion(Ryu.ActiveFrameX * 178, _
                                    Ryu.ActiveFrameY * 178, _
                                    178, 178)
          
        Ryu.TextureRegion.Flip(False, True)
          
        Ryu.ActiveFrame = Ryu.ActiveFrame + 1
        If Ryu.ActiveFrame > 9 Then Ryu.ActiveFrame = 0
    End If
End Sub

B4J Example:
B4X:
'B4A Code
'=================================================
'    #Region  Project Attributes
'        #ApplicationLabel: LibGDX Test
'        #VersionCode: 1
'        #VersionName: 1
'        'SupportedOrientations possible values: unspecified, landscape or portrait.
'        #SupportedOrientations: Landscape
'        #CanInstallToExternalStorage: True
'    #End Region
'
'    #Region  Activity Attributes
'        #FullScreen: True
'        #IncludeTitle: False
'    #End Region
'=================================================

'B4J Code
'=================================================
    #Region  Project Attributes
        #MainFormWidth: 600
        #MainFormHeight: 400
    #End Region
'=================================================

Sub Process_Globals
    'Common
        Type Sprite(Texture As lgTexture, TextureRegion As lgTextureRegion, ActiveFrame As Int, ActiveFrameX As Int, ActiveFrameY As Int, Rows As Int, FPS As Int, TimeStamp As Long)

    'B4J Code
    '=================================================
        'Standard B4J stuff
            Dim fx              As JFX
            Dim MainForm        As Form

        'LibGDX
            Dim GLView          As View
            Dim lGdx            As LibGDX
            Dim Gl              As lgGL
            Dim Camera          As lgOrthographicCamera
            Dim Batch           As lgSpriteBatch
            Dim Ryu             As Sprite
    '=================================================
End Sub

'B4J Code
'==========================================================
Sub AppStart (Form1 As Form, Args() As String)
    'MainForm Settings
        MainForm = Form1
        MainForm.SetFormStyle("UNIFIED")
        MainForm.RootPane.LoadLayout("test") 'Load the layout file.
        MainForm.Show

    'Initialize the LibGDX layer
        lGdx.Initialize
End Sub
'==========================================================

'B4A Code
'==========================================================
'    Sub Globals
'        Dim GLView          As View
'        Dim lGdx            As LibGDX
'        Dim Gl              As lgGL
'        Dim Camera          As lgOrthographicCamera
'        Dim Batch           As lgSpriteBatch
'        Dim Ryu             As Sprite
'    End Sub
'
'    Sub Activity_Create(FirstTime As Boolean)
'        'Do not forget to load the layout file created with the visual designer. For example:
'            Activity.LoadLayout("Layout1")
'
'        'Initialize the LibGDX layer
'            GLView = lGdx.InitializeView("LG")
'            Activity.AddView(GLView, 0%x, 0%y, 100%x, 100%y)
'    End Sub
'
'    Sub Activity_Resume
'        If lGdx.IsInitialized Then lGdx.Resume
'    End Sub
'
'    Sub Activity_Pause(UserClosed As Boolean)
'        If lGdx.IsInitialized Then lGdx.Pause
'    End Sub
'==========================================================

Sub LG_Create
    'B4J Code:
        Batch.Initialize(lGdx.lgCanvas)
    'B4A Code:
'        Batch.Initialize
    'Common
        Dim Texture As lgTexture
        Dim TextureRegion As lgTextureRegion
        Texture.Initialize("ryu.png")
        TextureRegion.InitializeWithTexture(Texture)
        Ryu.Texture       = Texture
        Ryu.TextureRegion = TextureRegion
End Sub

Sub LG_Resize(Width As Int, Height As Int)
    'Sets the camera viewport
        Camera.Initialize
        Camera.SetToOrtho2(True, lGdx.Graphics.Width, lGdx.Graphics.Height)
End Sub

Sub LG_Render
    'Clears the screen
        Gl.glClear(Gl.GL10_COLOR_BUFFER_BIT)
                 
    'Updates the matrices of the camera
        Camera.Update

    'Uses the coordinate system specified by the camera
        Batch.ProjectionMatrix = Camera.Combined

    'Sprite Animator
        Animation

    'Renderer
        Batch.Begin
        Batch.DrawRegion2(Ryu.TextureRegion, 0, 0, 300, 300)
        Batch.End
End Sub

Sub LG_Dispose
    Batch.dispose
    Ryu.Texture.dispose
End Sub

Sub LG_Pause
    LG_Resume
End Sub

Sub LG_Resume
End Sub

Sub Animation
    Ryu.FPS = 24
    If DateTime.Now - Ryu.TimeStamp > (1 / Ryu.FPS * 1000) Then
        Ryu.TimeStamp = DateTime.Now

        Ryu.Rows = 8
        Ryu.ActiveFrameX = Ryu.ActiveFrame Mod Ryu.Rows
        Ryu.ActiveFrameY = Floor(Ryu.ActiveFrame / Ryu.Rows)

        Ryu.TextureRegion.SetRegion(Ryu.ActiveFrameX * 178, _
                                    Ryu.ActiveFrameY * 178, _
                                    178, 178)
          
        Ryu.TextureRegion.Flip(False, True)
          
        Ryu.ActiveFrame = Ryu.ActiveFrame + 1
        If Ryu.ActiveFrame > 9 Then Ryu.ActiveFrame = 0
    End If
End Sub

Feel free to fork or contribute to this project.
I'm looking forward to know your opinions. :)
 

Attachments

  • B4A - GDXemu Example.zip
    145.8 KB · Views: 655
  • B4J - GDXemu Example.zip
    232.7 KB · Views: 682
  • jLibGDXemulator.zip
    13.3 KB · Views: 665
  • LibGDX.bas
    1.7 KB · Views: 647
Last edited:

gpe

Member
Licensed User
Longtime User
looks great, but I cannot get it work !
java.lang.RuntimeException: java.lang.UnsupportedClassVersionError: anywheresoftware/b4j/object/GameViewHelper : Unsupported major.minor version 52.0

What can I do?
txs
 

wonder

Expert
Licensed User
Longtime User
looks great, but I cannot get it work !
java.lang.RuntimeException: java.lang.UnsupportedClassVersionError: anywheresoftware/b4j/object/GameViewHelper : Unsupported major.minor version 52.0

What can I do?
txs
Thanks! :)

I just realized that my lib is actually dependent on jGameViewHelper. You don't need to add it to your project, but you still need to have it on your libs folder. It should solve the problem.
 
Last edited:

gpe

Member
Licensed User
Longtime User
Thanks! :) Are you referring to the provided example?
yes, but probably it's because my ignorance :(
I believe to have done everything correctly, but many of b4j examples don't work. As a matter of fact I0m having lot of problems with programs made in windows7 32bit or before, now used by my customers on 64bit PC, wondows 8 and even worse windows10. I'm close to give up!
 

wonder

Expert
Licensed User
Longtime User
I've have edited the post, @gpe:

I just realized that my lib is actually dependent on jGameViewHelper. You don't need to add it to your project, but you still need to have it on your libs folder. It should solve the problem.

Try downloading it and give it another try.
 

walterf25

Expert
Licensed User
Longtime User
jLibGDXemulator
Work In Progress [Open Source]


Have you created a game in B4A using LibGDX? Wouldn't it be awesome if you could just copy-paste your code from B4A into B4J and have it work?
Well, look no further! The future is here! :D

Instructions:
1. Download both jLibGDXemulator and jGameViewHelper libraries and extract the files into your B4J libraries folder.
2. Include the jLibGDXemulator library in your project.
3. Add the LibGDX.bas module to your project.

Remember: jGameViewHelper is required to be in your libs folder.

In order to have B4J understanding the [B]Informatix[B]'s[/B][/B] B4A LibGDX syntax, I've created a sort of emulator, using [B]Erel's[/B] excellent B4J GameView lib.
Below, a B4J (left) and B4A (right) sprite animation example running side-by-side using exactly the same LibGDX code. Ok, 99% the same code...

dual.png


So far, only a few basic LibGDX functions have been "ported" so don't expect any complex operations such as collision detection. This project is mainly focused in emulating the rendering part.

Remember: jGameViewHelper is required to be in your libs folder.

B4A Example:
B4X:
'B4A Code
'=================================================
    #Region  Project Attributes
        #ApplicationLabel: LibGDX Test
        #VersionCode: 1
        #VersionName: 1
        'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: Landscape
        #CanInstallToExternalStorage: True
    #End Region

    #Region  Activity Attributes
        #FullScreen: True
        #IncludeTitle: False
    #End Region
'=================================================

'B4J Code
'=================================================
'    #Region  Project Attributes
'        #MainFormWidth: 600
'        #MainFormHeight: 400
'    #End Region
'=================================================

Sub Process_Globals
    'Common
        Type Sprite(Texture As lgTexture, TextureRegion As lgTextureRegion, ActiveFrame As Int, ActiveFrameX As Int, ActiveFrameY As Int, Rows As Int, FPS As Int, TimeStamp As Long)

    'B4J Code
    '=================================================
'        'Standard B4J stuff
'            Dim fx              As JFX
'            Dim MainForm        As Form
'
'        'LibGDX
'            Dim GLView          As View
'            Dim lGdx            As LibGDX
'            Dim Gl              As lgGL
'            Dim Camera          As lgOrthographicCamera
'            Dim Batch           As lgSpriteBatch
'            Dim Ryu             As Sprite
    '=================================================
End Sub

'B4J Code
'==========================================================
'Sub AppStart (Form1 As Form, Args() As String)
'    'MainForm Settings
'        MainForm = Form1
'        MainForm.SetFormStyle("UNIFIED")
'        MainForm.RootPane.LoadLayout("test") 'Load the layout file.
'        MainForm.Show
'
'    'Initialize the LibGDX layer 
'        lGdx.Initialize
'End Sub
'==========================================================

'B4A Code
'==========================================================
    Sub Globals
        Dim GLView          As View
        Dim lGdx            As LibGDX
        Dim Gl              As lgGL
        Dim Camera          As lgOrthographicCamera
        Dim Batch           As lgSpriteBatch
        Dim Ryu             As Sprite
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        'Do not forget to load the layout file created with the visual designer. For example:
            Activity.LoadLayout("Layout1")

        'Initialize the LibGDX layer
            GLView = lGdx.InitializeView("LG")
            Activity.AddView(GLView, 0%x, 0%y, 100%x, 100%y)
    End Sub

    Sub Activity_Resume
        If lGdx.IsInitialized Then lGdx.Resume
    End Sub

    Sub Activity_Pause(UserClosed As Boolean)
        If lGdx.IsInitialized Then lGdx.Pause
    End Sub
'==========================================================

Sub LG_Create
    'B4J Code:
'        Batch.Initialize(lGdx.lgCanvas)
    'B4A Code:
        Batch.Initialize
    'Common
        Dim Texture As lgTexture
        Dim TextureRegion As lgTextureRegion
        Texture.Initialize("ryu.png")
        TextureRegion.InitializeWithTexture(Texture)
        Ryu.Texture       = Texture
        Ryu.TextureRegion = TextureRegion
End Sub

Sub LG_Resize(Width As Int, Height As Int)
    'Sets the camera viewport
        Camera.Initialize
        Camera.SetToOrtho2(True, lGdx.Graphics.Width, lGdx.Graphics.Height)
End Sub

Sub LG_Render
    'Clears the screen
        Gl.glClear(Gl.GL10_COLOR_BUFFER_BIT)
                     
    'Updates the matrices of the camera
        Camera.Update

    'Uses the coordinate system specified by the camera
        Batch.ProjectionMatrix = Camera.Combined

    'Sprite Animator
        Animation

    'Renderer
        Batch.Begin
        Batch.DrawRegion2(Ryu.TextureRegion, 0, 0, 300, 300)
        Batch.End
End Sub

Sub LG_Dispose
    Batch.dispose
    Ryu.Texture.dispose
End Sub

Sub LG_Pause
    LG_Resume
End Sub

Sub LG_Resume
End Sub

Sub Animation
    Ryu.FPS = 24
    If DateTime.Now - Ryu.TimeStamp > (1 / Ryu.FPS * 1000) Then
        Ryu.TimeStamp = DateTime.Now

        Ryu.Rows = 8
        Ryu.ActiveFrameX = Ryu.ActiveFrame Mod Ryu.Rows
        Ryu.ActiveFrameY = Floor(Ryu.ActiveFrame / Ryu.Rows)

        Ryu.TextureRegion.SetRegion(Ryu.ActiveFrameX * 178, _
                                     Ryu.ActiveFrameY * 178, _
                                     178, 178)
              
        Ryu.TextureRegion.Flip(False, True)
              
        Ryu.ActiveFrame = Ryu.ActiveFrame + 1
        If Ryu.ActiveFrame > 9 Then Ryu.ActiveFrame = 0
    End If
End Sub

B4J Example:
B4X:
'B4A Code
'=================================================
'    #Region  Project Attributes
'        #ApplicationLabel: LibGDX Test
'        #VersionCode: 1
'        #VersionName: 1
'        'SupportedOrientations possible values: unspecified, landscape or portrait.
'        #SupportedOrientations: Landscape
'        #CanInstallToExternalStorage: True
'    #End Region
'
'    #Region  Activity Attributes
'        #FullScreen: True
'        #IncludeTitle: False
'    #End Region
'=================================================

'B4J Code
'=================================================
    #Region  Project Attributes
        #MainFormWidth: 600
        #MainFormHeight: 400
    #End Region
'=================================================

Sub Process_Globals
    'Common
        Type Sprite(Texture As lgTexture, TextureRegion As lgTextureRegion, ActiveFrame As Int, ActiveFrameX As Int, ActiveFrameY As Int, Rows As Int, FPS As Int, TimeStamp As Long)

    'B4J Code
    '=================================================
        'Standard B4J stuff
            Dim fx              As JFX
            Dim MainForm        As Form

        'LibGDX
            Dim GLView          As View
            Dim lGdx            As LibGDX
            Dim Gl              As lgGL
            Dim Camera          As lgOrthographicCamera
            Dim Batch           As lgSpriteBatch
            Dim Ryu             As Sprite
    '=================================================
End Sub

'B4J Code
'==========================================================
Sub AppStart (Form1 As Form, Args() As String)
    'MainForm Settings
        MainForm = Form1
        MainForm.SetFormStyle("UNIFIED")
        MainForm.RootPane.LoadLayout("test") 'Load the layout file.
        MainForm.Show

    'Initialize the LibGDX layer 
        lGdx.Initialize
End Sub
'==========================================================

'B4A Code
'==========================================================
'    Sub Globals
'        Dim GLView          As View
'        Dim lGdx            As LibGDX
'        Dim Gl              As lgGL
'        Dim Camera          As lgOrthographicCamera
'        Dim Batch           As lgSpriteBatch
'        Dim Ryu             As Sprite
'    End Sub
'
'    Sub Activity_Create(FirstTime As Boolean)
'        'Do not forget to load the layout file created with the visual designer. For example:
'            Activity.LoadLayout("Layout1")
'
'        'Initialize the LibGDX layer
'            GLView = lGdx.InitializeView("LG")
'            Activity.AddView(GLView, 0%x, 0%y, 100%x, 100%y)
'    End Sub
'
'    Sub Activity_Resume
'        If lGdx.IsInitialized Then lGdx.Resume
'    End Sub
'
'    Sub Activity_Pause(UserClosed As Boolean)
'        If lGdx.IsInitialized Then lGdx.Pause
'    End Sub
'==========================================================

Sub LG_Create
    'B4J Code:
        Batch.Initialize(lGdx.lgCanvas)
    'B4A Code:
'        Batch.Initialize
    'Common
        Dim Texture As lgTexture
        Dim TextureRegion As lgTextureRegion
        Texture.Initialize("ryu.png")
        TextureRegion.InitializeWithTexture(Texture)
        Ryu.Texture       = Texture
        Ryu.TextureRegion = TextureRegion
End Sub

Sub LG_Resize(Width As Int, Height As Int)
    'Sets the camera viewport
        Camera.Initialize
        Camera.SetToOrtho2(True, lGdx.Graphics.Width, lGdx.Graphics.Height)
End Sub

Sub LG_Render
    'Clears the screen
        Gl.glClear(Gl.GL10_COLOR_BUFFER_BIT)
                     
    'Updates the matrices of the camera
        Camera.Update

    'Uses the coordinate system specified by the camera
        Batch.ProjectionMatrix = Camera.Combined

    'Sprite Animator
        Animation

    'Renderer
        Batch.Begin
        Batch.DrawRegion2(Ryu.TextureRegion, 0, 0, 300, 300)
        Batch.End
End Sub

Sub LG_Dispose
    Batch.dispose
    Ryu.Texture.dispose
End Sub

Sub LG_Pause
    LG_Resume
End Sub

Sub LG_Resume
End Sub

Sub Animation
    Ryu.FPS = 24
    If DateTime.Now - Ryu.TimeStamp > (1 / Ryu.FPS * 1000) Then
        Ryu.TimeStamp = DateTime.Now

        Ryu.Rows = 8
        Ryu.ActiveFrameX = Ryu.ActiveFrame Mod Ryu.Rows
        Ryu.ActiveFrameY = Floor(Ryu.ActiveFrame / Ryu.Rows)

        Ryu.TextureRegion.SetRegion(Ryu.ActiveFrameX * 178, _
                                     Ryu.ActiveFrameY * 178, _
                                     178, 178)
              
        Ryu.TextureRegion.Flip(False, True)
              
        Ryu.ActiveFrame = Ryu.ActiveFrame + 1
        If Ryu.ActiveFrame > 9 Then Ryu.ActiveFrame = 0
    End If
End Sub

Feel free to fork or contribute to this project.
I'm looking forward to know your opinions. :)
Whaaaaaaaaaaaaaaaaaaaaaattttttttttttttttttttttttttt?
WOW, really cool stuff man, you Rock!!!

Walter
 

ilan

Expert
Licensed User
Longtime User
Hi wonder

So is it possible to use libgdx with b4j?

I would like to start a new game in b4j and use libgdx for it. Can we also use box2d in b4j?

No need to port an android game i would like to make a new one but use the libgdx engine and get the performance like i get in b4a.
 

wonder

Expert
Licensed User
Longtime User
Hi Ilan! :)

No, all this lib does is "translate" some of the LibGDX syntax into GameView lib syntax.
Have a look at the source code and you'll understand what I am talking about... :)

The alternative would be having someone (*cof* *cof* @Informatix *cof* *cof*) writing a wrapper for the LibGDX PC version... :rolleyes:
 

ilan

Expert
Licensed User
Longtime User
another option would be to run an android app (that includes libgdx) on an emulator on my desktop but i dont want to be depending on 3rd party apps.
 

ilan

Expert
Licensed User
Longtime User
I can't afford that much, I think we should crowdfund it using a fixed amount, in order to reach Fred's target price.

No problem but first fred needs to tell us if it is possible and if he is ready to do it.

You are a close friend to him maybe ask him (but do it nicely) :D
 
Top