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

Discussion in 'B4J Libraries & Classes' started by wonder, Sep 5, 2015.

  1. wonder

    wonder Expert Licensed User

    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 Informatix's B4A LibGDX syntax, I've created a sort of emulator, using Erel's 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:
    Code:
    '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%x0%y100%x100%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, 
    00300300)
            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 * 1000Then
            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, _
                                        
    178178)
              
            Ryu.TextureRegion.Flip(
    FalseTrue)
              
            Ryu.ActiveFrame = Ryu.ActiveFrame + 
    1
            
    If Ryu.ActiveFrame > 9 Then Ryu.ActiveFrame = 0
        
    End If
    End Sub
    B4J Example:
    Code:
    '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, 
    00300300)
            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 * 1000Then
            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, _
                                        
    178178)
              
            Ryu.TextureRegion.Flip(
    FalseTrue)
              
            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. :)
     

    Attached Files:

    Last edited: Oct 13, 2016
    Toley, KMatle, JakeBullet70 and 11 others like this.
  2. gpe

    gpe Member Licensed 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
     
  3. wonder

    wonder Expert Licensed User

    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: Sep 18, 2015
  4. gpe

    gpe Member Licensed User

    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!
     
  5. wonder

    wonder Expert Licensed 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.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You need to upgrade to Java 8 (under Tools - Configure Paths).
     
    wonder likes this.
  7. wonder

    wonder Expert Licensed User

  8. walterf25

    walterf25 Well-Known Member Licensed User

    Whaaaaaaaaaaaaaaaaaaaaaattttttttttttttttttttttttttt?
    WOW, really cool stuff man, you Rock!!!

    Walter
     
    wonder likes this.
  9. wonder

    wonder Expert Licensed User

    Thanks Walter!! :)
    Have you tried it? Which LibGDX functions would you like to see emulated?
     
  10. ilan

    ilan Expert Licensed 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.
     
  11. wonder

    wonder Expert Licensed 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:
     
    inakigarm and ilan like this.
  12. ilan

    ilan Expert Licensed User

    :D

    Ok i will try

    Mr @Informatix how hard would it be to port libgdx (incl box2d) to b4j?
     
  13. wonder

    wonder Expert Licensed User

    I would crowdfund it 30 euro! :)
     
    Last edited: Oct 13, 2016
    inakigarm likes this.
  14. ilan

    ilan Expert Licensed 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.
     
  15. ilan

    ilan Expert Licensed User

    i put 300 euro for this :)

    edit: maybe fred could tell us his price and we will start a donation campaign for it
     
  16. inakigarm

    inakigarm Well-Known Member Licensed User

    ;)
     
  17. wonder

    wonder Expert Licensed 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.
     
  18. ilan

    ilan Expert Licensed User

    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
     
  19. Informatix

    Informatix Expert Licensed User

    Sorry but don't count on it. There are hundreds of classes in libGDX.
     
  20. ilan

    ilan Expert Licensed User

    thanx for replying @Informatix.

    i understand that it is a lot of work but we are ready to pay you for this work. is it something that could be interesting for you?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice