Android Open Source - Phylane Sprite






From Project

Back to project page Phylane.

License

The source code is released under:

COPYRIGHT 2014 TRISTON JONES

If you think the Android project Phylane 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

package com.lvadt.phylane.graphics;
/*ww w. j av a2  s.  c  om*/
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;
import android.util.Log;

public class Sprite {

    private static final float defWidth = 1280;
    private static final float defHeight = 720;
    private static double wRatio, hRatio;
  private static final String TAG = "Sprite";
    private Bitmap tempbmp; //Unscaled bitmap
  private Context context;

    //Make the following variables public for the physics engine
    public Bitmap bmp;
  public float width, height;
  public float x, y;
  public float prevX;
  public float rot = 0;
  public Boolean bRot = false;
  public int texId;

    /**
     * Create a new sprite object
     * @param c the context
     * @param gl the GL10 object
     * @param fileName the fie to be loaded
     * @param posx its starting position x
     * @param posy its starting position y
     */
  public Sprite(Context c, GL10 gl, String fileName, float posx, float posy){
        context = c;
      wRatio = c.getResources().getDisplayMetrics().widthPixels/defWidth;
        hRatio = c.getResources().getDisplayMetrics().heightPixels/defHeight;
    loadTexture(gl, fileName);
    x = posx;
    y = posy;
  }

    /**
     * Sets the rotation of an object
     * @param rotation the rotation
     */
  public void setRotation(float rotation){
    rot = rotation;
    bRot = true;
  }

    /**
     * Assigns an object a new texture id
     * @param gl the GL10 object
     * @return its new id
     */
  private int newTextureId(GL10 gl){
    int[] temp = new int[1];
    gl.glGenTextures(1, temp, 0);
    return temp[0];
  }

    /**
     * Loads the sprites texture
     * @param gl the GL10 object
     * @param fileName the fie to be loaded
     */
  private void loadTexture(GL10 gl, String fileName){
    texId = newTextureId(gl);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, texId);
    
    //Use assets and input stream to load bitmap
    AssetManager assets = context.getAssets();
    InputStream is = null;
    try{
      is = assets.open(fileName);
    }
    catch(IOException e){
      e.printStackTrace();
    }
    
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = true;
    
    if(is != null){
      try{
        tempbmp = BitmapFactory.decodeStream(is, null, options);

                bmp = Bitmap.createScaledBitmap(tempbmp, (int) (tempbmp.getWidth()*wRatio), (int) (tempbmp.getHeight()*hRatio), false);
        is.close();
      }
      catch(IOException e){
        e.printStackTrace();
      }
            finally{
                GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
                width = bmp.getWidth();
                height = bmp.getHeight();
            }
    }
    else{
      Log.i(TAG, "Error Loading " + fileName);
    }
    
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
    gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_MODULATE);
    
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
  }

    /**
     * Sets the sprites position
     * @param posx the new position x
     * @param posy the new position y
     */
  public void setPos(float posx, float posy){
    prevX = x;
    x = posx;
    y = posy;
  }

    /**
     * Renders the sprite using a square
     * @param gl the GL10 object
     */
  void render(GL10 gl){

        float ver[] = new float[12];
    float tex[] = new float[12];
    
    ver[0] = x;        ver[1] = y;
    ver[2] = x;        ver[3] = y + height;
    ver[4] = x + width;    ver[5] = y + height;
    ver[6] = x + width;    ver[7] = y;
    ver[8] = x;        ver[9] = y;
    ver[10] = x + width;  ver[11] = y + height;
    
    float u1 = 0.0f;
    float u2 = 1.0f;
    float v1 = 0.0f;
    float v2 = 1.0f;
    
    tex[0] = u1;      tex[1] = v1;
    tex[2] = u1;      tex[3] = v2;
    tex[4] = u2;      tex[5] = v2;
    tex[6] = u2;      tex[7] = v1;
    tex[8] = u1;      tex[9] = v1;
    tex[10] = u2;      tex[11] = v2;
    
    //Vertex Buffer
    ByteBuffer vbb = ByteBuffer.allocateDirect(ver.length * 6);
    vbb.order(ByteOrder.nativeOrder());
    FloatBuffer vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(ver);
    vertexBuffer.position(0);
    
    //Texture Buffer
    ByteBuffer tbb = ByteBuffer.allocateDirect(tex.length * 6);
    tbb.order(ByteOrder.nativeOrder());
    FloatBuffer textureBuffer = tbb.asFloatBuffer();
    textureBuffer.put(tex);
    textureBuffer.position(0);

    //Draw
    gl.glPushMatrix();
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glActiveTexture(GL10.GL_TEXTURE0);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, texId);
    
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);
    gl.glPopMatrix();
  }

    /**
     * Destroys the sprite object to make sure bitmaps get recycled properly
     */
  void destroy(){
    //Recycle the bitmap
        tempbmp.recycle();
    bmp.recycle();
  }
}




Java Source Code List

com.lvadt.phylane.BuildConfig.java
com.lvadt.phylane.activity.Fly.java
com.lvadt.phylane.activity.HomeScreen.java
com.lvadt.phylane.activity.LevelComplete.java
com.lvadt.phylane.activity.LoadScreen.java
com.lvadt.phylane.activity.MainMenu.java
com.lvadt.phylane.activity.MessagePopup.java
com.lvadt.phylane.activity.Prefs.java
com.lvadt.phylane.activity.Splash.java
com.lvadt.phylane.activity.Store.java
com.lvadt.phylane.activity.Tutorial.java
com.lvadt.phylane.graphics.GLRenderer.java
com.lvadt.phylane.graphics.Sprite.java
com.lvadt.phylane.model.GameObject.java
com.lvadt.phylane.model.Level.java
com.lvadt.phylane.model.LoadingScreens.java
com.lvadt.phylane.model.Objects.java
com.lvadt.phylane.model.Plane.java
com.lvadt.phylane.model.Player.java
com.lvadt.phylane.model.WorldObject.java
com.lvadt.phylane.physics.Physics.java
com.lvadt.phylane.utils.Data.java
com.lvadt.phylane.utils.OnSwipeTouchListener.java
com.lvadt.phylane.utils.Sound.java