Android Open Source - AmazingGame Game Renderer






From Project

Back to project page AmazingGame.

License

The source code is released under:

GNU General Public License

If you think the Android project AmazingGame 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 game.world;
/*w  w  w .  j a  v a2  s . c  o m*/
import game.helpers.AssetLoader;
import game.helpers.Constants;
import game.objects.TileSprite;
import game.objects.Unit;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class GameRenderer
{
  private GameMap map;
  private OrthographicCamera cam;
  private Stage stage;

  // private ShapeRenderer shapeRenderer;
  private SpriteBatch batcher;

  private TextureRegion tile;

  private Sprite sp;
  private TileSprite[][] currentMap;
  private Unit[][] currentUnits;
  boolean showFPS;

  public GameRenderer(GameMap map, int gameWidth, int gameHeight, Stage stage)
  {
    this.map = map;

    cam = new OrthographicCamera();
    cam.setToOrtho(true, gameWidth, gameHeight);

    batcher = new SpriteBatch();
    // Attach batcher to camera
    batcher.setProjectionMatrix(cam.combined);

    // shapeRenderer = new ShapeRenderer();
    // shapeRenderer.setProjectionMatrix(cam.combined);

    tile = AssetLoader.basicTile;

    sp = new Sprite(tile);

    currentMap = map.getTileMap();
    currentUnits = map.getUnits();

//    this.stage = stage;
//    stage.getViewport().setCamera(cam);
  }

  public void render(float delta)
  {
    Gdx.gl.glClearColor(0, 1, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batcher.setProjectionMatrix(cam.combined);
    batcher.begin();
    batcher.enableBlending();

    
    
    drawMap();
    drawUnits();

    if (showFPS)
    {
      System.err.println(1 / delta);
    }

    batcher.end();
  }

  public void drawMap()
  {
    for (int x = 0; x < currentMap.length; x++)
    {
      for (int y = 0; y < currentMap[x].length; y++)
      {
        if (cam.frustum.boundsInFrustum(currentMap[x][y].getCenter(),
            new Vector3(Constants.mapHeight*2, Constants.mapWidth*2, 0)))
        {
          currentMap[x][y].draw(batcher);
//          currentMap[x][y].setCenter(x * 64, y * 64);
        }
      }
    }
  }
  
  public void drawUnits()
  {
    for(Unit[] ua : currentUnits)
    {
      for(Unit u : ua)
      {
        u.draw(batcher);
      }
    }
  }

  public OrthographicCamera getCam()
  {
    return cam;
  }

  public void toggleFPS()
  {
    showFPS = !showFPS;
  }

  public void selectTile(int x, int y)
  {
    map.selectTile(x, y);
  }
}




Java Source Code List

game.IOSLauncher.java
game.MyGdxGame.java
game.android.AndroidLauncher.java
game.client.HtmlLauncher.java
game.desktop.DesktopLauncher.java
game.generator.MapGenerator.java
game.helpers.AssetLoader.java
game.helpers.Constants.java
game.helpers.MapHelper.java
game.helpers.TileClickHelper.java
game.input.MapInputHandler.java
game.objects.BlurpleBuilding.java
game.objects.BurnedMoose.java
game.objects.Grass.java
game.objects.PoopDesert.java
game.objects.River.java
game.objects.TileSprite.java
game.objects.Tree.java
game.objects.Unit.java
game.objects.Wall.java
game.objects.Water.java
game.screens.GameScreen.java
game.world.GameMap.java
game.world.GameRenderer.java
game.world.PlayerManager.java