thoughts on Texture.java

Anything libgdx related goes here!

thoughts on Texture.java

Postby wangjunyong » Fri Apr 13, 2012 9:05 am

I think the method create(TextureData data) should not be called in the construct method, the reasons is that if I set an assetmanager for texture, then i hope the texture to be loaded in assetmanager in another thread, not right now, also I newed a Texture but not use it , then we don't need create it actually.

I also got a question:
I have lots of textures or texture-atlas, and i want to load them using assetmanager, but I can't get the reference of the texture or texture-atlas until it is loaded, whether there is another way to achieve this ?

I am developing an android game, and using libgdx, it is a nice game engine, thank you all
wangjunyong
 
Posts: 30
Joined: Tue Feb 07, 2012 4:29 am

Re: thoughts on Texture.java

Postby wangjunyong » Fri Apr 13, 2012 9:38 am

I looked the Texture2d in Cocos2d-Android, and I think the code is doing things in right order:

first: new Texture2d(String fileName), and we can get the width, height and other things not related to opengl

second: loadTexture(GL10 gl) do the actual opengl operations, after this we can see it on the screen.

see: http://code.google.com/p/cocos2d-androi ... ure2D.java

other engine like andengine usually has two threads: logic and render , and they load texture like cocos2d-android do things in two steps, in libgdx we only has one thread: render thread, seems there is no need to seperate those mentioned above, but when loading is show, things changed ?
wangjunyong
 
Posts: 30
Joined: Tue Feb 07, 2012 4:29 am

Re: thoughts on Texture.java

Postby wangjunyong » Fri Apr 13, 2012 12:15 pm

AssetManager can do loading things in seperate thread, but as for texture in fact also devide the texture into parts ,
In TextureLoader.java, following is found:

Code: Select all
@Override
   public void loadAsync(AssetManager manager, String fileName, TextureParameter parameter) {
      if (parameter == null || (parameter != null && parameter.textureData == null)) {
         Pixmap pixmap = null;
         Format format = null;
         boolean genMipMaps = false;
         texture = null;

         if (parameter != null) {
            format = parameter.format;
            genMipMaps = parameter.genMipMaps;
            texture = parameter.texture;
         }

         FileHandle handle = resolve(fileName);
         if (!fileName.contains(".etc1")) {
            if (fileName.contains(".cim"))
               pixmap = PixmapIO.readCIM(handle);
            else
               pixmap = new Pixmap(handle);
            data = new FileTextureData(handle, pixmap, format, genMipMaps);
         } else {
            data = new ETC1TextureData(handle, genMipMaps);
         }
      } else {
         data = parameter.textureData;
         if (!data.isPrepared())
            data.prepare();
         texture = parameter.texture;
      }
   }

   @Override
   public Texture loadSync(AssetManager manager, String fileName, TextureParameter parameter) {
      Texture texture = this.texture;
      if (texture != null) {
         texture.load(data);
      } else {
         texture = new Texture(data);
      }
      if (parameter != null) {
         texture.setFilter(parameter.minFilter, parameter.magFilter);
         texture.setWrap(parameter.wrapU, parameter.wrapV);
      }
      return texture;
   }


so I think we can new a texture and don't load it's data, but load it's data in a new thread or load it's data later, and then we call create(TextureData data) for Texture;
wangjunyong
 
Posts: 30
Joined: Tue Feb 07, 2012 4:29 am

Re: thoughts on Texture.java

Postby mzechner » Fri Apr 13, 2012 10:58 pm

I'm not quite sure i can follow. If you use AssetManager, the main portion is loaded in the loading thread (the image data), and the corresponding texture object is created on the rendering thread afterwards. You can get a reference to the Texture as soon as AssetManager and TextureLoader are done with that.

If you do not use AssetManager, you can create your TextureData on a separate thread, and call new Texture(data) on the rendering thread afterwards.

Not quite sure i understand the problem i'm afraid :/
mzechner
Site Admin
 
Posts: 4713
Joined: Sat Jul 10, 2010 3:50 pm

Re: thoughts on Texture.java

Postby wangjunyong » Sat Apr 14, 2012 10:45 am

Thank you for your answering. You are right. using AssetManager, I can get a reference of Texture or TextureAtlas as soon as it is loaded, as the examples AssetManagerTest shows.

but the more often things happens like this, I newed a lots of textures, I wan't use their references, but I don't want the textures load at the same time.
in simple words , I think Texture.java should be like this: if I new a texture, then I can get the texture's width, height, I can use it's reference, but it is not loaded, meaning don't do the create(TextureData data) operations(createGLHandle() is not needed right now, because I may don't want it to show on screen);
and cocos2d-android do things like this, if you new a texture( can be newed in any thread, not only in gl thread) , then you can use it reference, you can get it's width and height, but the texture is not loaded( I mean it can't be drawed on screen right now, for it's doesn't do the [glGenTextures] [GLUtils.texImage2D] gl operations

oh, my poor English :cry: ,hope you get my point now..
wangjunyong
 
Posts: 30
Joined: Tue Feb 07, 2012 4:29 am

Re: thoughts on Texture.java

Postby mzechner » Sat Apr 14, 2012 7:30 pm

I get your point, that's what TextureData is for. It is a non-OpenGL resource, mostly holding a Pixmap inside. You can instantiate that anywhere you want. If you need a texture call new Texture(textureData), done.
mzechner
Site Admin
 
Posts: 4713
Joined: Sat Jul 10, 2010 3:50 pm

Re: thoughts on Texture.java

Postby wangjunyong » Sun Apr 15, 2012 3:15 am

yeap,TextureData is non-opengl resource, and I can use it to init a Texture, so why when I new a texture, the method create(TextureData data) is called in the construct method(this is done acquiescently)? I think not call create method.

normally, TextureData mean nothing to me, for I don't use it directly, I use texture directly. I need the reference of a texture, not other's. but if I new a texture, a texturedata is created inside texture, and then consume the texturedata( this is what create(TextureData data) do, and this step(I mean consuming Texturedata in texture's construct method) is not neccessary).

TextureData is a good thing, and I think its useage equal to the bitmap in texture2d in cocos2d-android, or andengine, and thoes engines don't "consume" bitmaps in Texture's construct method. consuming a texturedata is a heavy operation, ( :?: here, I am not sure i'm right). so I think we should'n not consume texturedata in texture's construct method.

:) ,thank you for your guys, you are awesome! :D
wangjunyong
 
Posts: 30
Joined: Tue Feb 07, 2012 4:29 am

Re: thoughts on Texture.java

Postby wangjunyong » Mon Apr 16, 2012 2:55 am

sorry, but can the TextureData gets its width and height before the method prepare() is called ?

the bitmap in android can do this , just decode its width and height

Code: Select all
BitmapFactory.Options opts = new BitmapFactory.Options();
      opts.inJustDecodeBounds = true;// not load the whole bitmap, just get it's bounds: width and height
      InputStream in = null;
      try {
         if (_resid != -1) {
            in = activity.getResources().openRawResource(_resid);
         } else {
            in = activity.getAssets().open(_assetName);
         }
      } catch (Exception e) {
         e.printStackTrace();
         in = null;
      }
      if (in == null) {
         Log.w("tag", "Texture initSize failed");
         _width = _height = -1;
         new RuntimeException().printStackTrace();
         return;
      }
      BitmapFactory.decodeStream(in, null, opts);
      try {
         in.close();
      } catch (Exception e) {
      }
      _imageWidth = opts.outWidth;
      _imageHeight = opts.outHeight;


the reference of bitmapoptions says like this:
Code: Select all
/**
         * If set to true, the decoder will return null (no bitmap), but
         * the out... fields will still be set, allowing the caller to query
         * the bitmap without having to allocate the memory for its pixels.
         */
        public boolean inJustDecodeBounds;
wangjunyong
 
Posts: 30
Joined: Tue Feb 07, 2012 4:29 am

Re: thoughts on Texture.java

Postby mzechner » Mon Apr 16, 2012 1:44 pm

No, it can't. One has to load the data before one can know the details. We do not provide means to only read headers of images.
mzechner
Site Admin
 
Posts: 4713
Joined: Sat Jul 10, 2010 3:50 pm


Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 10 guests