Android Question [SOLVED] LibGDX with Downloaded Textures

wonder

Expert
Licensed User
Longtime User
SOLVED:
B4X:
extra_life_source.dispose
extra_life_source.InitializeWithFile(lGdx.Files.Absolute(File.DirDefaultExternal & "/item01buy_texture.png"))
extra_life_downloaded_buy_texture  = False    
File.Delete(File.DirDefaultExternal, "item01buy_texture.png")
Thanks walterf25!! :D
_______________________________________________________________


Hello GDX gurus!

My game has a small shop where you can buy an "Extra Life".
The default texture for this item is bundled with the apk and initialized in LG_Create.

If I want, however, to update the in-game shop graphical assets, I don't want to force the user to download and updated version of the game.
Instead, the game itself checks the server for new textures and downloads them accordingly.
So far this part is done and working properly.

The code below saves the new texture in the external assets folder:

B4X:
Sub JobDone (Job As HttpJob)
            ...
            Case "GetItem01BUY_Texture"
                If extra_life_downloaded_buy_texture = False Then
                    If File.Exists(File.DirDefaultExternal, "item01buy_texture.png") Then File.Delete(File.DirDefaultExternal, "item01buy_texture.png")
                    Dim input As Bitmap
                    Dim output As OutputStream
                    input  = Job.GetBitmap              
                   output = File.OpenOutput(File.DirDefaultExternal, "item01buy_texture.png", False)
                   input.WriteToStream(output, 100, "PNG")
                    output.Close              
                    If File.Exists(File.DirDefaultExternal, "item01buy_texture.png") Then
                        extra_life_downloaded_buy_texture = True
                    End If
                End If
                ...
End Sub


Now... how can I load this new PNG file into the 'extra_life_source' variable (lgTexture)?
I can't do it in LG_Create since the download happens during LG_Render.

B4X:
Sub Globals
    ...
    Dim extra_life_source As lgTexture
    ...
End Sub

Sub LG_Create
    ...
    extra_life_source.Initialize("default_texture.png")
    ...
End Sub

Sub LG_Render
    ...
    If Downloaded_Texture = True Then
        'Replace the default texture with new downloaded texture
            extra_life_source = "item01buy_texture.png"
        'and do this only one time
            Downloaded_Texture = False
    End If
    ...
End Sub

Sub LG_Dispose
    ...
    extra_life_source.dispose
    ...
End Sub

Please help me with this IF statement in LG_Render, since the above it's only pseudo-code.

Thanks in advance.
 
Last edited:

walterf25

Expert
Licensed User
Longtime User
Hello GDX gurus!

My game has a small shop where you can buy an "Extra Life".
The default texture for this item is bundled with the apk and initialized in LG_Create.

If I want, however, to update the in-game shop graphical assets, I don't want to force the user to download and updated version of the game.
Instead, the game itself checks the server for new textures and downloads them accordingly.
So far this part is done and working properly.

The code below saves the new texture in the internal assets folder:

B4X:
Sub JobDone
                Case "GetItem01BUY_Texture"
                    If File.Exists(File.DirInternal, "item01buy_texture.png") Then File.Delete(File.DirInternal, "item01buy_texture.png")
                    Dim Item01BUY_Texture_file As OutputStream
                    Item01BUY_Texture_file = File.OpenOutput(File.DirInternal, "item01buy_texture.png", False)
                    File.Copy2(Job.GetInputStream, Item01BUY_Texture_file)
                    Item01BUY_Texture_file.Close           
                    If File.Exists(File.DirInternal, "item01buy_texture.png") Then
                        Downloaded_Texture = True
                    End If
End Sub


Now... how can I load this new PNG file into the 'extra_life_source' variable (lgTexture)?
I can't do it in LG_Create since the download happens during LG_Render.

B4X:
Sub Globals
    ...
    Dim extra_life_source As lgTexture
    ...
End Sub

Sub LG_Create
    ...
    extra_life_source.Initialize("default_texture.png")
    ...
End Sub

Sub LG_Render
    ...
    If Downloaded_Texture = True Then
        'Replace the default texture with new downloaded texture
            extra_life_source = "item01buy_texture.png"
        'and do this only one time
            Downloaded_Texture = False
    End If
    ...
End Sub

Sub LG_Dispose
    ...
    extra_life_source.dispose
    ...
End Sub

Please help me with this IF statement in LG_Render, since the above it's only pseudo-code.

Thanks in advance.
You should be able to initialize the texture with

B4X:
Texture.InitializeWithFile(lGdx.Files.Internal("image.png"))

the files method can point to where ever directory you have your file saved.

I think.

Walter
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
You should be able to initialize the texture with

B4X:
Texture.InitializeWithFile(lGdx.Files.Internal("image.png"))

the files method can point to where ever directory you have your file saved.

I think.

Walter

Thanks Walter,

I've tried your suggestion. Unfortunately the game crashes.
I've tried something like this before. To my understanding initializing textures in LG_Render crashes the app.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Thanks Walter,

I've tried your suggestion. Unfortunately the game crashes.
I've tried something like this before. To my understanding initializing textures in LG_Render crashes the app.
The way i did this on my Whack a Bieber game is by creating a class.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Thanks Walter,

I've tried your suggestion. Unfortunately the game crashes.

What's the error you get when it crashes?
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Yep, that's the regular "you did something wrong in LibGDX. Go figure it out." crash error message.
why don't you call a sub from the Lg_Render to make the texture change, use the lgdx.CallSubUI method. see if that works.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
why don't you call a sub from the Lg_Render to make the texture change, use the lgdx.CallSubUI method. see if that works.
Using this method:

Error occured
_____________________________________________

An error has occured in sub:
java.lang.RuntimeException: Texture already initialized.
Continue?
_____________________________________________
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Using this method:

Error occured
_____________________________________________

An error has occured in sub:
java.lang.RuntimeException: Texture already initialized.
Continue?
_____________________________________________
You gotta dispose of the texture before initializing it again
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
You gotta dispose of the texture before initializing it again

This solved the issue, thank you so much for your help!!
I'll update the first post with the correct code for future reference.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
This solved the issue, thank you so much for your help!!
I'll update the first post with the correct code for future reference.
Glad it worked!

:cool:
 
Upvote 0
Top