Jump to content

Loading textures


nmiguelmoura
 Share

Recommended Posts

Hi all!

I'm building an edu app that will be packed with cordova. In one of the scenes, there is a bunch of small objects in stage. When an object is pressed, a hi-res texture of that object appears in the screen. This hi-res image is loaded only when the object is pressed with the following code:

var self=this; //this object inherits from Sprite
//loader is a PIXI.loader
loader.add('bigTexture',imgPath);
loader.load(function(){
     self._currentTexture=PIXI.Texture.fromImage(imgPath);
     self.texture=self._currentTexture;
    });

As soon as the user dismisses the image, it disappears from screen and the texture must be destroyed. The user can than click on the same object or on another one and the process restarts. To destroy the texture and remove it from loader i use this code.

this.parent.removeChild(this); //removes sprite from stage
delete (loader.resources['bigTexture']); 
this._currentTexture.destroy(true);
this._currentTexture=null;

I have two questions, and would be great if someone could help:

Is this the correct way to do this? I mean, load texture, destroy texture, remove from loader to allow reloading again?

Thanks for your help,

Nuno

Link to comment
Share on other sites

Your solution is acceptable, however best solution depends on your game type. Dont forget that sometimes PIXI puts stuff in PIXI.utils.TextureCache, I suggest you to clear it after loader completes. The other thing is that Pixiv4 has GC that takes care of webgl textures that werent used for a long time, so its not a tragedy if you forget to call texture.destroy(true);

I'm solving this kind of problem for RPGMaker MV and its not easy:

For each scene you have to create a loader with specific textures for that scene. When user exits it, you just free all the specific textures. If new scene has some of old resources, just move them instead.

I also have special cache with TTL for streamed and big resources.

 

Link to comment
Share on other sites

21 minutes ago, nmiguelmoura said:

Thanks Ivan,

Like always, you were a big help. Good to know the code is fine.

I didn't knew i could create more than one loader, i keep using the same one.

Thanks,

Nuno

Yes, you can just "forget" about previous scene and its loader, and it will be collected by javascript GC, while pixi gc will handle videomemory part. You can also manually run that garbage collector between scenes or when the game is paused: renderer.textureGC.run(). It will find textures that werent used for 60 seconds or so.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...