Games Use Atlas in Libgdx

ilan

Expert
Licensed User
Longtime User
hi

i would like to use atlas in my new libgdx game and not put all images in the file folder so i create the png + .pack file with libgdx texture packer

the problem is that i am not getting the right result.

i want to use lgtextures in my game so i can draw them how i want.

my code is:

B4X:
    Atlas.InitializeWithFile("atlas/myatlas.pack")

    background = Atlas.CreateSprite("back4").Texture
    holz1 = Atlas.CreateSprite("tree7").Texture
    holz2 = Atlas.CreateSprite("tree8").Texture
    holz1l = Atlas.CreateSprite("tree7l").Texture
    holz1r = Atlas.CreateSprite("tree7r").Texture
    redindi = Atlas.CreateSprite("redindi").Texture
    ground = Atlas.CreateSprite("ground3").Texture
    blueindi = Atlas.CreateSprite("blueindi").Texture
    bigtree = Atlas.CreateSprite("tree").Texture
    arrowtx = Atlas.CreateSprite("arrow").Texture

before i have loaded each texture like this and i had no problems

B4X:
    'textures
'    background.InitializeWithFile(lGdx.Files.internal("back4.png"))
'    holz1.InitializeWithFile(lGdx.Files.internal("tree7.png"))
'    holz2.InitializeWithFile(lGdx.Files.internal("tree8.png"))
'    holz1l.InitializeWithFile(lGdx.Files.internal("tree7l.png"))
'    holz1r.InitializeWithFile(lGdx.Files.internal("tree7r.png"))
'    redindi.InitializeWithFile(lGdx.Files.internal("redindi.png"))
'    ground.InitializeWithFile(lGdx.Files.internal("ground3.png"))
'    blueindi.InitializeWithFile(lGdx.Files.internal("blueindi.png"))
'    bigtree.InitializeWithFile(lGdx.Files.internal("tree.png"))
'    arrowtx.InitializeWithFile(lGdx.Files.internal("arrow.png"))

what i am getting now is this:
scr1.png


instead of this:

scr2.png


any advices, please? :)
 
Last edited:

ilan

Expert
Licensed User
Longtime User
i could use lgTextureRegion

B4X:
    Atlas.InitializeWithFile("atlas/myatlas.pack")
    background = Atlas.CreateSprite("back4").Texture
    region.InitializeWithTexture3(background, 2, 163, 800, 600)

and

B4X:
Batch.DrawRegion2(region,-vpW*0.15,0,vpW*maxscalefactor+(vpW*0.3),vpH*maxscalefactor)

but it doesnot make any sense to use an atlas and then define for each texture his region o_O
 

ilan

Expert
Licensed User
Longtime User
using regions instead of textures solve the problem but why does lgsprite.Texture return the WHOLE Atlas instead of the defined texture with the region in the atlas??

code that works:

B4X:
    Atlas.InitializeWithFile("atlas/myatlas.pack")
    background = Atlas.FindRegion("back4") '.CreateSprite("back4").Texture
    holz1 = Atlas.FindRegion("tree7")
    holz2 = Atlas.FindRegion("tree8")
    holz1l = Atlas.FindRegion("tree7l")
    holz1r = Atlas.FindRegion("tree7r")
    redindi = Atlas.FindRegion("redindi")
    ground = Atlas.FindRegion("ground3")
    blueindi = Atlas.FindRegion("blueindi")
    bigtree = Atlas.FindRegion("tree")
    arrowtx = Atlas.FindRegion("arrow")

DrawRegion2 (like DrawTex2)

B4X:
    Batch.Begin 'draw everything here!!
        Batch.DrawRegion2(background,-vpW*0.15,0,vpW*maxscalefactor+(vpW*0.3),vpH*maxscalefactor)
    Batch.End

DrawRegion3 (almost! like DrawTex5, NO flip option or draw defines src rect)

B4X:
            Batch.DrawRegion3(holz1,vec2.x,vec2.y,mycor.width/2,mycor.height/2,mycor.width,mycor.height,1,1,PosCon.Rad2Angle(wood.Angle))',0,0,16,96,False,False)

at least the project looks much cleaner now :D

cleanProject.png
 
Last edited:

melonZgz

Active Member
Licensed User
Longtime User
using regions instead of textures solve the problem but why does lgsprite.Texture return the WHOLE Atlas instead of the defined texture with the region in the atlas??
Yep, I have the same problem right now, when you reference the texture, it means the whole atlas texture.
I'm using now in my hill climb racing game batch.drawtex3 (because of uv mapping), wich expects a texture, and I can't put the image into an atlas, because the texture means the whole atlas texture.
 

ilan

Expert
Licensed User
Longtime User
Yep, I have the same problem right now, when you reference the texture, it means the whole atlas texture.
I'm using now in my hill climb racing game batch.drawtex3 (because of uv mapping), wich expects a texture, and I can't put the image into an atlas, because the texture means the whole atlas texture.

you could use Drawregion3 instead. it works for me.

and if i need to flip a texture i will create a separate one for it and use drawtex5 but for simple obstacles drawregion2 & drawregion3 are enough

no need for that :)

B4X:
background.Flip(True,False)

lgtextureregion has a flip function so you can flip it and still use drawregion3 like using drawtexture5 (COOL!!)
 
Last edited:
Top