Android Question [SOLVED] LibGDX with Downloaded Textures

Discussion in 'Android Questions' started by wonder, Feb 12, 2015.

  1. wonder

    wonder Expert Licensed User

    SOLVED:
    Code:
    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:

    Code:
    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.

    Code:
    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: Feb 13, 2015
    ellpopeb4a and inakigarm like this.
  2. walterf25

    walterf25 Well-Known Member Licensed User

    You should be able to initialize the texture with

    Code:
    Texture.InitializeWithFile(lGdx.Files.Internal("image.png"))
    the files method can point to where ever directory you have your file saved.

    I think.

    Walter
     
    wonder likes this.
  3. wonder

    wonder Expert Licensed 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.
     
  4. walterf25

    walterf25 Well-Known Member Licensed User

    The way i did this on my Whack a Bieber game is by creating a class.
     
  5. walterf25

    walterf25 Well-Known Member Licensed User

    What's the error you get when it crashes?
     
  6. wonder

    wonder Expert Licensed User

    "Unfortunately HyperBloX has stopped. (OK)"
     
  7. walterf25

    walterf25 Well-Known Member Licensed User

    that's all? LOL
     
    Peter Simpson likes this.
  8. wonder

    wonder Expert Licensed User

    Yep, that's the regular "you did something wrong in LibGDX. Go figure it out." crash error message.
     
    Peter Simpson likes this.
  9. walterf25

    walterf25 Well-Known Member Licensed 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.
     
  10. wonder

    wonder Expert Licensed User

    Using this method:

    Error occured
    _____________________________________________

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

    walterf25 Well-Known Member Licensed User

    You gotta dispose of the texture before initializing it again
     
    wonder likes this.
  12. wonder

    wonder Expert Licensed 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.
     
    walterf25 likes this.
  13. walterf25

    walterf25 Well-Known Member Licensed User

    Glad it worked!

    :cool:
     
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