Android Open Source - aeon_java_android Texture






From Project

Back to project page aeon_java_android.

License

The source code is released under:

GNU General Public License

If you think the Android project aeon_java_android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
* Aeon Android Game Engine/* ww  w . jav a 2s  .c  o  m*/
* 
* This file is part of Aeon Android Game Engine (AGE).
*
* AGE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AGE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AGE.  If not, see <http://www.gnu.org/licenses/>.
*
* @author Robin Degen
* @version 0.1
*/

package aeon.engine.resources;

import aeon.utility.Vector2f;
import android.graphics.Bitmap;
import android.opengl.GLES20;
import android.opengl.GLUtils;

public class Texture extends Resource
{
  public Texture(Bitmap bitmap)
  {
    super(ResourceType.Texture);
    
    m_size.set(bitmap.getWidth(), bitmap.getHeight());
    
    __finalize_texture(bitmap);
  }

  @Override
  public boolean unload()
  {
      if (m_texture == 0)
        return false;

      //glDeleteTextures wants an array,... so we make one...
      int[] handle = new int[1];
      handle[0] = m_texture;
      GLES20.glDeleteTextures(1, handle, 0);

    return true;
  }
  
  public void bind()
  {
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, m_texture);

    //TODO: query the texture from the active shader
    GLES20.glUniform1i(0, 0);
  }
  
  public Vector2f get_size()
  {
    return m_size;
  }
  
  private void __finalize_texture(Bitmap bitmap)
  {
    int texture = __generate_gltexture_handle();

    //Bind it
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture);
    
    //Set texture parameters
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
    
    //Upload the bitmap to texture memory
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    
    m_texture = texture;
  }
  
  private int __generate_gltexture_handle()
  {
    int[] handle = new int[1];
    GLES20.glGenTextures(1, handle, 0);
    
    return handle[0];
  }

  int       m_texture = 0;
  Vector2f     m_size = new Vector2f(0, 0);
}




Java Source Code List

aeon.console.Logger.java
aeon.engine.AeonActivity.java
aeon.engine.AeonGLSurface.java
aeon.engine.AeonRenderer.java
aeon.engine.GameObject.java
aeon.engine.Sprite.java
aeon.engine.resources.ResourceManager.java
aeon.engine.resources.ResourceType.java
aeon.engine.resources.Resource.java
aeon.engine.resources.ShaderType.java
aeon.engine.resources.Shader.java
aeon.engine.resources.Texture.java
aeon.examplegame.ExampleGame.java
aeon.utility.Color.java
aeon.utility.Vector2f.java