Android Question Spritesheets

Spright

Active Member
How would you draw tiles from a spritesheet efficiently inside B4A? I just installed libGDX thinking that must be the better choice.
Can you batch a bunch of graphics from a spritesheet or how would you go about it?

Also Is there a way to stretch and rotate the graphics while drawing using matrix while drawing?
 

Spright

Active Member
Thanks, It's an actiongame so I'm just trying out B3A to see if it fits the bill I will be doing around 2500 objects so it needs to be put together carefully. I tried to add the libGDX library and it works on my phone quiet well up to 8000 objects, actually too well so I need to get an old phone now to see if it's really okay on an average phone. There will be more things to consider that slow down everything eventually.

One thing for instance is that I need a way using alpha, and i need to draw to a texture using a matrix (e.g. in the form of SetMatrix 1,0,0,1,0,0). I think libGDX might be able to do that I'm not sure. I also need to consider what would the best way to be to stretch the screen in B4A, as most phones are very high-resolution and sometime it's nice to work on a known resolution that stretches in this project.
 
Upvote 0

Spright

Active Member
I post some code that I hope the reads okay because I'm posting away from the compiler.

The basis for this code is the example Animation and I djusted it to use a Framebuffer for now because I want to experiment before i use views.

The next step now is to set the matrix but I'm used to SetMatrix command that comes with 6 parameters but it seems be missing in libgdx is there a way around this? I am not sure what the format is called, but the identity matrix in this format will be SetMatrix 1,0,0,1,0,0.

B4X:
Build1=Default,flm.b4a.libgdxtest
Group=Default Group
Library1=core
Library2=libgdx
Library3=reflection
ManifestCode='This code will be applied to the manifest file during compilation.~\n~'You do not need to modify it in most cases.~\n~'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136~\n~AddManifestText(~\n~<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="29"/>~\n~<supports-screens android:largeScreens="true" ~\n~    android:normalScreens="true" ~\n~    android:smallScreens="true" ~\n~    android:anyDensity="true"/>)~\n~SetApplicationAttribute(android:icon, "@drawable/icon")~\n~SetApplicationAttribute(android:label, "$LABEL$")~\n~'End of default text.~\n~AddManifestText(<uses-feature android:glEsVersion="0x00020000" android:required="true"/>)~\n~
NumberOfFiles=0
NumberOfLibraries=3
NumberOfModules=0
Version=12
@EndOfDesignText@

#Region Module Attributes
    #FullScreen: True
    #IncludeTitle: False
    #ApplicationLabel: Sprites
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
    Type typCaveman(Pos As lgMathVector2, HeadLeft As Boolean, StateTime As Float)
End Sub

Sub Globals
    Dim lGdx As LibGDX
    Dim GL As lgGL
    Dim Camera As lgOrthographicCamera
    Dim Batch As lgSpriteBatch
    Dim Texture As lgTexture
    Dim Cavemen(1024) As typCaveman
    Dim LeftWalk As lgAnimation
    Dim RightWalk As lgAnimation
    Dim CavemanSize As Int =     64
    Dim Velocity As Int = 1'20
    Dim FBO As lgFrameBuffer
    Dim FBO2 As lgFrameBuffer
    Dim texSmiley As lgTexture
    Dim IP As lgInputProcessor
    Dim texRegionFBO As lgTextureRegion
    Dim texRegionFBO2 As lgTextureRegion
End Sub

Sub Activity_Create(FirstTime As Boolean)
    lGdx.Initialize("LG")
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 GetRegionArray(MultiDimArray As Object, Index As Int) As lgTextureRegion()
    Dim r As Reflector
    r.Target = MultiDimArray
    Return r.GetArray(Array As Int(Index))
End Sub
 
Sub LG_Create
    
    ' Initializes  renderer
    Batch.Initialize
    Texture.Initialize("walkanim.png")
    Dim Region As lgTextureRegion
    Region.InitializeWithTexture(Texture)
    Dim WalkFrames(,) As lgTextureRegion = Region.Split(CavemanSize, CavemanSize)
    Dim LeftWalkFrames() As lgTextureRegion = GetRegionArray(WalkFrames, 0)
    Dim RightWalkFrames(LeftWalkFrames.Length) As lgTextureRegion
    For i = 0 To RightWalkFrames.Length - 1
        Dim Frame As lgTextureRegion
        Frame.InitializeWithRegion(LeftWalkFrames(i))
        Frame.Flip(True, False)
        RightWalkFrames(i) = Frame
    Next
    LeftWalk.Initialize(0.25, LeftWalkFrames)
    RightWalk.Initialize(0.25, RightWalkFrames)

    'Initializes the position and direction of cavemen
    Dim MU As lgMathUtils
    For i = 0 To Cavemen.Length - 1
        Cavemen(i).Initialize
        Cavemen(i).Pos.set(Rnd(-CavemanSize, lGdx.Graphics.Width), Rnd(0, lGdx.Graphics.Height - CavemanSize))
        Cavemen(i).HeadLeft = (Rnd(0, 2) = 0)
        Cavemen(i).StateTime = MU.RandomFloat
    Next
    texSmiley.Initialize("smiley.png")
    FBO2.Initialize(FBO2.FORMAT_RGBA8888, lGdx.Graphics.Width, lGdx.Graphics.Height) ' Later use 800 x 600
End Sub

Sub LG_Resize(Width As Int, Height As Int)
    Camera.Initialize
    Camera.SetToOrtho(False)
End Sub

Sub LG_Render
    GL.glClear(GL.GL20_COLOR_BUFFER_BIT)
    Camera.Update
    Batch.ProjectionMatrix = Camera.Combined

    RenderToFBO2

    For i = 0 To Cavemen.Length - 1
        Cavemen(i).StateTime = Cavemen(i).StateTime + 1
        If Cavemen(i).HeadLeft Then
            Cavemen(i).pos.x = Cavemen(i).pos.x - 1
        Else
            Cavemen(i).pos.x = Cavemen(i).pos.x + 1
        End If
        If Cavemen(i).pos.x < -CavemanSize Then Cavemen(i).pos.x = lGdx.Graphics.Width
        If Cavemen(i).pos.x > lGdx.Graphics.Width Then Cavemen(i).pos.x = -CavemanSize
    Next

    Dim Frame As lgTextureRegion
    Batch.Begin
    


    ' Set Matrix 1,0,0,1,0,0

    ' Draw FrameBuffer with sprites
    Batch.DrawRegion(texRegionFBO2, 0, 0)



    Batch.End
End Sub

Sub LG_Pause
End Sub

Sub LG_Resume
End Sub

Sub LG_Dispose
    Texture.dispose
    Batch.dispose
End Sub

 

Sub RenderToFBO2

    FBO2.Begin

    GL.glClear(GL.GL20_COLOR_BUFFER_BIT)
    Batch.Begin
    Dim X, Y As Int
    ' Draw sprites (animated)
    For i = 0 To Cavemen.Length - 1
        If Cavemen(i).HeadLeft Then
            Frame = LeftWalk.GetKeyFrame2(Cavemen(i).StateTime, True)
        Else
            Frame = RightWalk.GetKeyFrame2(Cavemen(i).StateTime, True)
        End If
        Batch.DrawTex2(Texture, Cavemen(i).pos.x, Cavemen(i).pos.y,  32dip, 32dip)
        
    Next
    
    Batch.End
    FBO2.End
    
    texRegionFBO2.InitializeWithTexture(FBO2.ColorBufferTexture)
    texRegionFBO2.Flip(False, True)
End Sub
 
Upvote 0
Top