package com.blarg.gdx.graphics;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.decals.DecalBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.Disposable;

public class RenderContext implements Disposable {
	public final ExtendedSpriteBatch spriteBatch;
	public final DecalBatch decalBatch;
	public final BillboardSpriteBatch billboardSpriteBatch;
	public final ShapeRenderer debugGeometryRenderer;
	public final ModelBatch modelBatch;
	public final ScreenPixelScaler pixelScaler;
	public final SolidColorTextureCache solidColorTextures;

	AlphaTestCameraGroupStrategy cameraGroupStrategy;
	Camera perspectiveCamera;
	OrthographicCamera orthographicCamera;

	public RenderContext(boolean use2dPixelScaling) {
		Gdx.app.debug("RenderContext", "ctor");
		spriteBatch = new ExtendedSpriteBatch();
		debugGeometryRenderer = new ShapeRenderer();
		modelBatch = new ModelBatch();
		solidColorTextures = new SolidColorTextureCache();

		if (use2dPixelScaling)
			pixelScaler = new DefaultScreenPixelScaler();
		else
			pixelScaler = new NoScaleScreenPixelScaler();
		pixelScaler.calculateScale(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		orthographicCamera = new OrthographicCamera(pixelScaler.getScaledWidth(), pixelScaler.getScaledHeight());

		setDefaultPerspectiveCamera();

		cameraGroupStrategy = new AlphaTestCameraGroupStrategy(perspectiveCamera);
		decalBatch = new DecalBatch(cameraGroupStrategy);
		billboardSpriteBatch = new BillboardSpriteBatch();
	}

	public Camera getPerspectiveCamera() {
		return perspectiveCamera;
	}

	public OrthographicCamera getOrthographicCamera() {
		return orthographicCamera;
	}

	public void setPerspectiveCamera(Camera camera) {
		perspectiveCamera = camera;
		if (cameraGroupStrategy != null)
			cameraGroupStrategy.setCamera(camera);
	}

	public void setDefaultPerspectiveCamera() {
		PerspectiveCamera camera = new PerspectiveCamera(60.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.position.set(0.0f, 0.0f, 0.0f);
		camera.lookAt(0.0f, 0.0f, 1.0f);
		camera.near = 0.1f;
		camera.far = 100.0f;
		camera.update();
		setPerspectiveCamera(camera);
	}

	public void clear() {
		clear(0.0f, 0.0f, 0.0f, 1.0f);
	}

	public void clear(float red, float green, float blue, float alpha) {
		Gdx.graphics.getGL20().glClearColor(red, green, blue, alpha);
		Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
	}

	public void onPreRender() {
		spriteBatch.setProjectionMatrix(orthographicCamera.combined);
		spriteBatch.setPixelScale(pixelScaler.getScale());
		debugGeometryRenderer.setProjectionMatrix(perspectiveCamera.combined);
		billboardSpriteBatch.begin(decalBatch, perspectiveCamera);
	}

	public void onPostRender() {
		billboardSpriteBatch.end();
	}

	public void onUpdate(float delta) {
		perspectiveCamera.update();
		orthographicCamera.update();
	}

	public void onResize(int width, int height) {
		Gdx.app.debug("RenderContext", String.format("onResize(%d, %d)", width, height));
		pixelScaler.calculateScale(width, height);
		orthographicCamera.setToOrtho(false, pixelScaler.getScaledWidth(), pixelScaler.getScaledHeight());
		perspectiveCamera.viewportWidth = width;
		perspectiveCamera.viewportHeight = height;
	}

	public void onPause() {
		Gdx.app.debug("RenderContext", String.format("onPause"));
		solidColorTextures.onPause();
	}

	public void onResume() {
		Gdx.app.debug("RenderContext", String.format("onResume"));
		solidColorTextures.onResume();
	}

	@Override
	public void dispose() {
		Gdx.app.debug("RenderContext", String.format("dispose"));
		solidColorTextures.dispose();
		spriteBatch.dispose();
		decalBatch.dispose();
		cameraGroupStrategy.dispose();
	}
}

@@ -596,45 +596,8 @@
 ch;%0A
-%09public final DecalBatch decalBatch;%0A
 %09pub
@@ -641,32 +641,32 @@
 ardSpriteBatch;%0A
+
 %09public final Sh
@@ -844,59 +844,8 @@
 s;%0A%0A
-%09AlphaTestCameraGroupStrategy cameraGroupStrategy;%0A
 %09Cam
@@ -1040,24 +1040,77 @@
 iteBatch();%0A
+%09%09billboardSpriteBatch = new BillboardSpriteBatch();%0A
 %09%09debugGeome
@@ -1587,191 +1587,8 @@
 a();
-%0A%0A%09%09cameraGroupStrategy = new AlphaTestCameraGroupStrategy(perspectiveCamera);%0A%09%09decalBatch = new DecalBatch(cameraGroupStrategy);%0A%09%09billboardSpriteBatch = new BillboardSpriteBatch();
 %0A%09%7D%0A
@@ -1831,85 +1831,8 @@
 ra;%0A
-%09%09if (cameraGroupStrategy != null)%0A%09%09%09cameraGroupStrategy.setCamera(camera);%0A
 %09%7D%0A%0A
@@ -2670,69 +2670,8 @@
 d);%0A
-%09%09billboardSpriteBatch.begin(decalBatch, perspectiveCamera);%0A
 %09%7D%0A%0A
@@ -2704,38 +2704,8 @@
 ) %7B%0A
-%09%09billboardSpriteBatch.end();%0A
 %09%7D%0A%0A
@@ -3546,25 +3546,34 @@
 ispose();%0A%09%09
-s
+billboardS
 priteBatch.d
@@ -3588,51 +3588,19 @@
 ;%0A%09%09
-decalBatch.dispose();%0A%09%09cameraGroupStrategy
+spriteBatch
 .dis

update RenderContext