Android Open Source - DivisionByZero Buffer Utils






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.utils;
//  w w  w. j a v a 2 s.  c om
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import android.opengl.GLES20;

public class BufferUtils {
  private static final int TYPE_SIZE = 4;
  private static final int SHORT_SIZE = Short.SIZE / 8;

  public static int createRectangleBuffer(float w, float h){
    float arr[] = {
        0, 0,
        w, 0,
        0, h,
        w, h
    };

    return copyToBuffer(arr);
  }

  public static int copyToBuffer(float[] arr){
    return copyToBuffer(arr, arr.length);
  }

  public static int copyToBuffer(float[] arr, int len) {
    // allocate a buffer of the right size...

    // size of float = 4 bytes so size of buffer = 4 * length of float array
    FloatBuffer floatBuf = ByteBuffer.allocateDirect(len * TYPE_SIZE)
        .order(ByteOrder.nativeOrder())    // use the device hardware's native byte order
        .asFloatBuffer();           // create a floating point buffer from the ByteBuffer

    floatBuf.put(arr, 0, len)        // copy the array into the buffer
    .position(0);              // set the buffer to read the first coordinate

    int err;
    if((err = GLES20.glGetError()) != 0) {
      DebugLog.e("buff", "err " + err);
    }
    
    int[] buffer = new int [1];
    GLES20.glGenBuffers(1, buffer, 0);
    int handle = buffer[0];
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, handle);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, len * TYPE_SIZE, floatBuf, GLES20.GL_STATIC_DRAW);

    if((err = GLES20.glGetError()) != 0) {
      DebugLog.e("buff", "err " + err);
    }
    
    return handle;
  }
  
  public static int copyToBuffer(int target, short[] arr, int len) {
    // allocate a buffer of the right size...

    // size of float = 4 bytes so size of buffer = 4 * length of float array
    ShortBuffer buf = ByteBuffer.allocateDirect(len * SHORT_SIZE)
        .order(ByteOrder.nativeOrder())    // use the device hardware's native byte order
        .asShortBuffer();           // create a floating point buffer from the ByteBuffer

    buf.put(arr, 0, len)                // copy the array into the buffer
    .position(0);              // set the buffer to read the first coordinate

    int[] buffer = new int [1];
    GLES20.glGenBuffers(1, buffer, 0);
    int handle = buffer[0];
    GLES20.glBindBuffer(target, handle);
    GLES20.glBufferData(target, len * SHORT_SIZE, buf, GLES20.GL_STATIC_DRAW);
    
    return handle;
  }
}




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