Android Open Source - DivisionByZero Picture Box






From Project

Back to project page DivisionByZero.

License

The source code is released under:

Apache License

If you think the Android project DivisionByZero 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.ggstudios.divisionbyzero;
//from  ww w  .  ja va 2 s.  co  m
import com.ggstudios.utils.BufferUtils;

import android.opengl.GLES20;

import static fix.android.opengl.GLES20.glVertexAttribPointer;

public class PictureBox extends Drawable {
  protected float x, y;
  protected float w, h;
  protected float drawingW, drawingH;
  protected boolean center = false;

  protected int textureHandle = -1, textureId = -1;

  protected int handle = 0;

  protected float transparency = 1.0f;

  protected boolean isVisible = true;
  protected boolean cull = false;

  protected VBO vbo;

  private float scale = 1f;
  private float angle = 0f;

  /**
   * Constructor used to produce a empty PictureBox. Additional calls 
   * are required before this object becomes usable.
   * @param x
   * @param y
   */
  protected PictureBox(float x, float y){
    this.x = x;
    this.y = y;
  }

  public PictureBox(float x, float y, float w, float h, int resId, boolean isCentered){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.textureId = resId;
    center = isCentered;

    refresh();
  }
  
  public PictureBox(float x, float y, float w, float h, int resId){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.textureId = resId;

    refresh();
  }

  public PictureBox(float x, float y, VBO vbo, int resId){
    this.x = x;
    this.y = y;
    this.vbo = vbo;
    this.textureId = resId;

    refresh();
  }

  public void setVBO(VBO vbo) {
    this.vbo = vbo;
    w = vbo.width;
    h = vbo.height;

    handle = vbo.handle;    
  }

  public void setTexture(int resId){
    this.textureId = resId;
    textureHandle = Core.tm.get(resId);
  }

  public void setTextureHandle(int textureHandle) {
    textureId = -1;
    this.textureHandle = textureHandle;
  }

  public void generateBuffer(){
    vbo = null;

    float w, h;
    if(drawingW <= 0) {
      w = this.w;
      h = this.h;
    } else {
      w = drawingW;
      h = drawingH;
    }
    
    if(center) {
      float hw = w/2f;
      float hh = h/2f;
      float vertices[] = {
          //Vertices according to faces
          -hw,   -hh,   //Vertex 0
          hw,   -hh,   //v1
          -hw,   hh,   //v2
          hw,   hh,   //v3
      };

      handle = BufferUtils.copyToBuffer(vertices);
    } else {
      float vertices[] = {
          //Vertices according to faces
          0,   0,   //Vertex 0
          w,   0,   //v1
          0,   h,   //v2
          w,   h,   //v3
      };

      handle = BufferUtils.copyToBuffer(vertices);
    }
  }

  public void refresh(){
    if(this.vbo != null) {
      this.w = vbo.width;
      this.h = vbo.height;

      handle = vbo.handle;
    } else {
      generateBuffer();
    }

    if(textureId >= 0) {
      textureHandle = Core.tm.get(textureId);
    }
  }

  public void reset(){
    transparency = 1.0f;
  }

  public void draw(float offX, float offY) {
    final float finalX = x + offX;
    final float finalY = y + offY;

    if ( (finalX + w < 0
        || finalX > Core.cullR 
        || finalY + h < 0
        || finalY > Core.cullB
        || textureHandle == -1 
        || !isVisible) 
        && !Core.forceVisible) {
      // cull this drawable...
      cull = true;
    } else {
      cull = false;
    }

    if(!cull) {
      Utils.resetMatrix();
      Utils.scale(scale);

      if(angle != 0f)
        Utils.rotate(angle);

      Utils.translateAndCommit(finalX, finalY);

      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle);

      GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, handle);
      glVertexAttribPointer(Core.A_POSITION_HANDLE, 2, GLES20.GL_FLOAT, false, 0, 0);

      if(transparency < 1.0f){
        GLES20.glUniform4f(Core.U_TEX_COLOR_HANDLE, 1f, 1f, 1f, transparency);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
        GLES20.glUniform4f(Core.U_TEX_COLOR_HANDLE, 1f, 1f, 1f, 1f);
      } else
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    }
  }

  public void setSize(float w, float h) {
    scale = w/this.w;
  }

  public void setScale(float scale) {
    this.scale = scale;
  }

  public void setAngle(float angle) {
    this.angle = angle;
  }
}




Java Source Code List

com.ggstudios.divisionbyzero.ActivityAbout.java
com.ggstudios.divisionbyzero.ActivityLevelSelection.java
com.ggstudios.divisionbyzero.ActivityMainMenu.java
com.ggstudios.divisionbyzero.ActivitySettings.java
com.ggstudios.divisionbyzero.ActivitySplashScreen.java
com.ggstudios.divisionbyzero.ApplicationMain.java
com.ggstudios.divisionbyzero.BaseActivity.java
com.ggstudios.divisionbyzero.BaseDialog.java
com.ggstudios.divisionbyzero.BulletManager.java
com.ggstudios.divisionbyzero.Bullet.java
com.ggstudios.divisionbyzero.Button.java
com.ggstudios.divisionbyzero.Circle.java
com.ggstudios.divisionbyzero.ClickableCollection.java
com.ggstudios.divisionbyzero.Clickable.java
com.ggstudios.divisionbyzero.ConfirmDialog.java
com.ggstudios.divisionbyzero.Core.java
com.ggstudios.divisionbyzero.CustomGLSurfaceView.java
com.ggstudios.divisionbyzero.DialogManager.java
com.ggstudios.divisionbyzero.DrawableCollection.java
com.ggstudios.divisionbyzero.DrawableString.java
com.ggstudios.divisionbyzero.Drawable.java
com.ggstudios.divisionbyzero.EndDialog.java
com.ggstudios.divisionbyzero.Event.java
com.ggstudios.divisionbyzero.ExplosionGenerator.java
com.ggstudios.divisionbyzero.ExtrasManager.java
com.ggstudios.divisionbyzero.FontManager.java
com.ggstudios.divisionbyzero.GameRenderer.java
com.ggstudios.divisionbyzero.GameUpdater.java
com.ggstudios.divisionbyzero.Game.java
com.ggstudios.divisionbyzero.Grid.java
com.ggstudios.divisionbyzero.Hud.java
com.ggstudios.divisionbyzero.InGameMenu.java
com.ggstudios.divisionbyzero.InfoDialog.java
com.ggstudios.divisionbyzero.Label.java
com.ggstudios.divisionbyzero.LevelManager.java
com.ggstudios.divisionbyzero.LevelMap.java
com.ggstudios.divisionbyzero.LineGuide.java
com.ggstudios.divisionbyzero.MainActivity.java
com.ggstudios.divisionbyzero.Map.java
com.ggstudios.divisionbyzero.MessageDialog.java
com.ggstudios.divisionbyzero.ParticleEngine.java
com.ggstudios.divisionbyzero.PathFinder.java
com.ggstudios.divisionbyzero.PauseMenu.java
com.ggstudios.divisionbyzero.PictureBox.java
com.ggstudios.divisionbyzero.Player.java
com.ggstudios.divisionbyzero.PopupMenu.java
com.ggstudios.divisionbyzero.Rectangle.java
com.ggstudios.divisionbyzero.Shader.java
com.ggstudios.divisionbyzero.SpawnManager.java
com.ggstudios.divisionbyzero.SpriteAnimation.java
com.ggstudios.divisionbyzero.SpriteManager.java
com.ggstudios.divisionbyzero.Sprite.java
com.ggstudios.divisionbyzero.Statbar.java
com.ggstudios.divisionbyzero.StateManager.java
com.ggstudios.divisionbyzero.TargetRectangle.java
com.ggstudios.divisionbyzero.TextureManager.java
com.ggstudios.divisionbyzero.TowerInfoDialog.java
com.ggstudios.divisionbyzero.TowerLibrary.java
com.ggstudios.divisionbyzero.TowerManager.java
com.ggstudios.divisionbyzero.TowerMenu.java
com.ggstudios.divisionbyzero.Tower.java
com.ggstudios.divisionbyzero.UpdatableCollection.java
com.ggstudios.divisionbyzero.Updatable.java
com.ggstudios.divisionbyzero.UpgradeDialog.java
com.ggstudios.divisionbyzero.Utils.java
com.ggstudios.divisionbyzero.VBO.java
com.ggstudios.divisionbyzero.WaveControlDialog.java
com.ggstudios.divisionbyzero.WaveControlHud.java
com.ggstudios.divisionbyzero.ZoomControl.java
com.ggstudios.utils.BitmapUtils.java
com.ggstudios.utils.BufferUtils.java
com.ggstudios.utils.DebugLog.java
com.ggstudios.utils.ShaderUtils.java
com.ggstudios.widget.LevelMapBackground.java
fix.android.opengl.GLES20.java