Android Open Source - DivisionByZero Grid






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;
// ww w  . j  a v a2s .com
import java.util.List;

import com.ggstudios.utils.BitmapUtils;
import com.ggstudios.utils.BufferUtils;
import com.ggstudios.utils.DebugLog;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

public class Grid extends PictureBox {
  //the grid class is responsible for  drawing the game grid guide
  //and is capable of drawing extra guides for multipurpose uses...

  private static final String TAG = "Grid";
  
  //original is 18x13
  private static final int DEFAULT_TILES_ACROSS = 18;
  private static final int DEFAULT_TILES_DOWN = 13;

  private boolean perfectFit;
  public float extraWidth, extraHeight;

  private float tileW;
  private float tileH;

  private int potWidth, potHeight;

  private float gridW, gridH;
  private int tilesAcross = DEFAULT_TILES_ACROSS, tilesDown = DEFAULT_TILES_DOWN;

  private int bgResId = -1;
  private int lineColor = 0x80FFFFFF, lineThickness = 1;

  public Grid(){
    super(0, 0);
  }

  public Grid(float width, float height){
    super(0, 0);
    remeasure(width, height);
  }

  public Grid(float width, float height, int across, int down){
    super(0, 0);
    this.tilesDown = down;
    this.tilesAcross = across;
    remeasure(width, height);
  }
  
  public void sizeChanged(int newW, int newH) {
    remeasure(newW, newH);
  }

  public void remeasure(final float width, final float height){
    DebugLog.d(TAG, "remeasure(" + width + ", " + height + ")");
    DebugLog.d(TAG, "tilesAcross: " + tilesAcross + " down " + tilesDown);
    if(width == 0 || height == 0) return;

    w = width;
    h = height;

    if( height * ((float)tilesAcross/(float)tilesDown) > width){  //find the limiting factor
      //if it's width then
      h = width * ((float)tilesDown/(float)tilesAcross);
    }else{
      //it's height
      w = height * ((float)tilesAcross/(float)tilesDown);
    }

    perfectFit = tilesAcross/(float)tilesDown == width/height;

    tileW = w/tilesAcross;
    tileH = h/tilesDown;

    gridW = tileW*tilesAcross;
    gridH = tileH*tilesDown;

    if(!perfectFit){
      extraWidth = width - gridW;
      extraHeight = height - gridH;
    }
  }

  public void setBackgroundResource(int resId) {
    bgResId = resId;
  }

  public void setLineSpec(int lineColor, int lineThickness) {
    this.lineColor = lineColor;
    this.lineThickness = lineThickness;
  }

  public void useAsDrawable() {
    // caller wants to use this as a drawable...
    // generate textures and do initialization
    // needed to draw this grid.

    generateGrid(tilesAcross, tilesDown);
  }
  
  private int generateGrid(int tilesAcross, int tilesDown) {
    DebugLog.d(TAG, "generateGrid()");

    gridW = tilesAcross * tileW;
    gridH = tilesDown * tileH;

    potWidth = Utils.findSmallestPot((int)gridW);
    potHeight = Utils.findSmallestPot((int)gridH);

    Bitmap bitmap = Bitmap.createBitmap((int)(potWidth), (int) (potHeight), Bitmap.Config.RGB_565);

    bitmap.eraseColor(Color.argb((int)(GameRenderer.CLEAR_COLOR_A * 255), 
        (int)(GameRenderer.CLEAR_COLOR_R * 255), 
        (int)(GameRenderer.CLEAR_COLOR_G * 255), 
        (int)(GameRenderer.CLEAR_COLOR_B * 255)));

    Canvas c = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setColor(lineColor);
    paint.setStrokeWidth(lineThickness);
    
    if(bgResId > 0) {
      try {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inScaled = false;

        Bitmap bg = BitmapFactory.decodeResource(Core.context.getResources(), bgResId, opts);

        Paint p = new Paint(Paint.FILTER_BITMAP_FLAG);
        p.setDither(true);
        p.setAntiAlias(true);

        c.drawBitmap(bg, new Rect(0, 0, bg.getWidth(), bg.getHeight()), 
            new Rect(0, 0, (int) (gridW + lineThickness), (int)(gridH + lineThickness)), 
            p);
        bg.recycle();
      } catch (Exception e) {
        DebugLog.e(TAG, e);
      }
    }

    for(int i = 0; i < tilesAcross + 1; i++) {
      final float p = i * tileH;
      c.drawLine(p, 0, p, gridH, paint);
    }

    for(int i = 0; i < tilesDown + 1; i++) {
      final float p = i * tileW + 1;
      c.drawLine(0, p, gridW, p, paint);
    }
    
    // check if it's a custom map...
    if(Core.lm.isCustomMap()) {
      Paint blkPaint = new Paint();
      blkPaint.setColor(Color.BLACK);
      // if it is, we got some work to do...
      List<String> strs = Core.lm.getCustomMapArgs();
      int y = 0;
      for(String s : strs) {
        for(int i = 0; i < s.length(); i++) {
          char ch = s.charAt(i);
          if(ch == '@') {
            // '@' denotes a "dead" square
            // that is, no tower may be placed here nor can an enemy unit pass through here (except ghosts)
            c.drawRect(i*tileW, y*tileH, (i+1)*tileW, (y+1)*tileH, blkPaint);
          }
        }
        y++;
      }
    }

    textureHandle = BitmapUtils.loadBitmap(bitmap, textureHandle);

    handle = BufferUtils.createRectangleBuffer(potWidth, potHeight);

    this.x = 0;//this.extraWidth / 2;
    this.y = 0;//this.extraHeight / 2;

    w = gridW;
    h = gridH;
    
    drawingW = potWidth;
    drawingH = potHeight;
    
    return handle;
  }

  public int getTextureHandle() {
    return textureHandle;
  }

  public float getW() {
    return gridW;
  }

  public float getH() {
    return gridH;
  }

  public float getTileWidth() {
    return tileW;
  }

  public float getTileHeight() {
    return tileH;
  }

  public int getTilesAcross() {
    return tilesAcross;
  }

  public int getTilesDown() {
    return tilesDown;
  }

  @Override
  public void refresh() {
    generateGrid(tilesAcross, tilesDown);
  }
}




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