From 9da00fac4a8ba7bf06130e7746c6be1daf8b88ff Mon Sep 17 00:00:00 2001 From: Recrown Date: Mon, 4 Sep 2017 00:01:09 -0500 Subject: [PATCH] preparing structure for an animated background --- .../rhythmbullet/screens/GameScreen.java | 157 +++++++++++++++++- .../rhythmbullet/stages/GamePlayArea.java | 135 +-------------- 2 files changed, 159 insertions(+), 133 deletions(-) diff --git a/core/src/zero1hd/rhythmbullet/screens/GameScreen.java b/core/src/zero1hd/rhythmbullet/screens/GameScreen.java index 622df4c..5ebae04 100755 --- a/core/src/zero1hd/rhythmbullet/screens/GameScreen.java +++ b/core/src/zero1hd/rhythmbullet/screens/GameScreen.java @@ -5,6 +5,10 @@ import com.badlogic.gdx.InputMultiplexer; import com.badlogic.gdx.Screen; import com.badlogic.gdx.ScreenAdapter; import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.Pixmap.Format; +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.graphics.glutils.FrameBuffer; +import com.badlogic.gdx.graphics.glutils.ShaderProgram; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.ui.ImageButton; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; @@ -25,6 +29,15 @@ public class GameScreen extends ScreenAdapter { private CoreMusicInfo music; + private ShaderProgram gaussianBlurShader; + private ShaderProgram brightFilterShader; + private ShaderProgram combineShader; + private FrameBuffer lightFilterBuffer; + private FrameBuffer normalBuffer; + private FrameBuffer hBlur, vBlur; + private TextureRegion fboRegion; + private int fboSize; + /** * The game screen where the game play area, and hud are placed. * @param core The game context object @@ -62,10 +75,72 @@ public class GameScreen extends ScreenAdapter { @Override public void show() { Gdx.input.setInputProcessor(inputs); - gameArea.loadShaders(core.getPrefs()); + loadShaders(); super.show(); } - + + /** + * needs to be called right after set as screen (should be called in show method). + * This is due to the saving preference being done once the screen is hidden. + * @param prefs + */ + public void loadShaders() { + if (core.getPrefs().getBoolean("glow shader")) { + Gdx.app.debug("Shader", "using glow shader"); + brightFilterShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/bright_filter.fsh")); + if (!brightFilterShader.isCompiled()) { + System.err.println(brightFilterShader.getLog()); + System.exit(0); + } + if (brightFilterShader.getLog().length() != 0) { + System.out.println(brightFilterShader.getLog()); + } + + gaussianBlurShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/gaussian_blur.fsh")); + if (!gaussianBlurShader.isCompiled()) { + System.err.println(gaussianBlurShader.getLog()); + System.exit(0); + } + if (gaussianBlurShader.getLog().length() != 0) { + System.out.println(gaussianBlurShader.getLog()); + } + + combineShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/combine.fsh")); + if (!combineShader.isCompiled()) { + System.err.println(combineShader.getLog()); + System.exit(0); + } + if (combineShader.getLog().length() != 0) { + System.out.println(combineShader.getLog()); + } + + + if (Gdx.graphics.getWidth() < 1024) { + fboSize = 2048; + } else if (Gdx.graphics.getWidth() < 2048) { + fboSize = 4096; + } else { + fboSize = 4096; + } + + lightFilterBuffer = new FrameBuffer(Format.RGBA8888, fboSize/4, fboSize/4, false); + normalBuffer = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false); + hBlur = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false); + vBlur = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false); + + fboRegion = new TextureRegion(); + + combineShader.begin(); + combineShader.setUniformi("u_texture1", 1); + combineShader.end(); + + vBlur.getColorBufferTexture().bind(1); + + Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0); + } + + ShaderProgram.pedantic = false; + } @Override public void render(float delta) { Gdx.gl.glClearColor(0f, 0f, 0f, 1f); @@ -80,7 +155,71 @@ public class GameScreen extends ScreenAdapter { } gameArea.getViewport().apply(); - gameArea.draw(); + if (gaussianBlurShader != null) { + //Begin drawing a normal version of screen + normalBuffer.begin(); + Gdx.gl.glClearColor(0f, 0f, 0f, 0f); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + gameArea.draw(); + gameArea.getCollisionDetector().renderParticles(gameArea.getBatch(), Gdx.graphics.getDeltaTime(), true); + normalBuffer.end( + gameArea.getViewport().getScreenX(), + gameArea.getViewport().getScreenY(), + gameArea.getViewport().getScreenWidth(), + gameArea.getViewport().getScreenHeight()); + //END + + //Begin light filtering + lightFilterBuffer.begin(); + Gdx.gl.glClearColor(0f, 0f, 0f, 0f); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + gameArea.getBatch().setShader(brightFilterShader); + gameArea.getBatch().begin(); //BATCH STARTS HERE + gameArea.getRoot().draw(gameArea.getBatch(), 1f); + gameArea.getBatch().flush(); + lightFilterBuffer.end( + gameArea.getViewport().getScreenX(), + gameArea.getViewport().getScreenY(), + gameArea.getViewport().getScreenWidth(), + gameArea.getViewport().getScreenHeight()); + + //Horizontal gaussian blur + hBlur.begin(); + fboRegion.setTexture(lightFilterBuffer.getColorBufferTexture()); + gameArea.getBatch().setShader(gaussianBlurShader); + gaussianBlurShader.setUniformf("horizontal", 1); + gameArea.getBatch().draw(fboRegion, 0f, 0f, RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT); + gameArea.getBatch().flush(); + hBlur.end( + gameArea.getViewport().getScreenX(), + gameArea.getViewport().getScreenY(), + gameArea.getViewport().getScreenWidth(), + gameArea.getViewport().getScreenHeight()); + + //Vertical gaussian blur + vBlur.begin(); + fboRegion.setTexture(hBlur.getColorBufferTexture()); + gameArea.getBatch().setShader(gaussianBlurShader); + gaussianBlurShader.setUniformf("horizontal", 0); + gameArea.getBatch().draw(fboRegion, 0f, 0f, RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT); + gameArea.getBatch().flush(); + vBlur.end( + gameArea.getViewport().getScreenX(), + gameArea.getViewport().getScreenY(), + gameArea.getViewport().getScreenWidth(), + gameArea.getViewport().getScreenHeight()); + + //Draw everything to screen + gameArea.getBatch().setShader(combineShader); + fboRegion.setRegion(normalBuffer.getColorBufferTexture()); + fboRegion.flip(false, true); + gameArea.getBatch().draw(fboRegion, 0f, 0f, RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT); + gameArea.getBatch().setShader(null); + gameArea.getBatch().end(); //BATCH ENDS HERE + } else { + gameArea.draw(); + } + gameHUD.getViewport().apply(); gameHUD.draw(); if (music != null && !music.getPlaybackMusic().isPlaying()) { @@ -102,6 +241,18 @@ public class GameScreen extends ScreenAdapter { public void dispose() { gameHUD.dispose(); gameArea.dispose(); + + if (gaussianBlurShader != null) { + normalBuffer.dispose(); + lightFilterBuffer.dispose(); + hBlur.dispose(); + vBlur.dispose(); + + brightFilterShader.dispose(); + gaussianBlurShader.dispose(); + combineShader.dispose(); + gaussianBlurShader.dispose(); + } super.dispose(); } diff --git a/core/src/zero1hd/rhythmbullet/stages/GamePlayArea.java b/core/src/zero1hd/rhythmbullet/stages/GamePlayArea.java index 0a8994e..0715e8a 100755 --- a/core/src/zero1hd/rhythmbullet/stages/GamePlayArea.java +++ b/core/src/zero1hd/rhythmbullet/stages/GamePlayArea.java @@ -3,11 +3,6 @@ package zero1hd.rhythmbullet.stages; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Preferences; import com.badlogic.gdx.assets.AssetManager; -import com.badlogic.gdx.graphics.GL20; -import com.badlogic.gdx.graphics.Pixmap.Format; -import com.badlogic.gdx.graphics.g2d.TextureRegion; -import com.badlogic.gdx.graphics.glutils.FrameBuffer; -import com.badlogic.gdx.graphics.glutils.ShaderProgram; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.utils.viewport.FitViewport; @@ -35,15 +30,6 @@ public class GamePlayArea extends Stage { public ScoreManager score = new ScoreManager(); - private ShaderProgram gaussianBlurShader; - private ShaderProgram brightFilterShader; - private ShaderProgram combineShader; - private FrameBuffer lightFilterBuffer; - private FrameBuffer normalBuffer; - private FrameBuffer hBlur, vBlur; - private TextureRegion fboRegion; - private int fboSize; - public GamePlayArea(AssetManager assetManager, Preferences prefs) { super(new FitViewport(RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT)); Gdx.app.debug("Game Area", "new area created"); @@ -65,120 +51,9 @@ public class GamePlayArea extends Stage { return audioMap; } - /** - * needs to be called right after set as screen (should be called in show method). - * This is due to the saving preference being done once the screen is hidden. - * @param prefs - */ - public void loadShaders(Preferences prefs) { - if (prefs.getBoolean("glow shader")) { - Gdx.app.debug("Shader", "using glow shader"); - brightFilterShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/bright_filter.fsh")); - if (!brightFilterShader.isCompiled()) { - System.err.println(brightFilterShader.getLog()); - System.exit(0); - } - if (brightFilterShader.getLog().length() != 0) { - System.out.println(brightFilterShader.getLog()); - } - - gaussianBlurShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/gaussian_blur.fsh")); - if (!gaussianBlurShader.isCompiled()) { - System.err.println(gaussianBlurShader.getLog()); - System.exit(0); - } - if (gaussianBlurShader.getLog().length() != 0) { - System.out.println(gaussianBlurShader.getLog()); - } - - combineShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/combine.fsh")); - if (!combineShader.isCompiled()) { - System.err.println(combineShader.getLog()); - System.exit(0); - } - if (combineShader.getLog().length() != 0) { - System.out.println(combineShader.getLog()); - } - - - if (Gdx.graphics.getWidth() < 1024) { - fboSize = 2048; - } else if (Gdx.graphics.getWidth() < 2048) { - fboSize = 4096; - } else { - fboSize = 4096; - } - - lightFilterBuffer = new FrameBuffer(Format.RGBA8888, fboSize/4, fboSize/4, false); - normalBuffer = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false); - hBlur = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false); - vBlur = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false); - - fboRegion = new TextureRegion(); - - combineShader.begin(); - combineShader.setUniformi("u_texture1", 1); - combineShader.end(); - - vBlur.getColorBufferTexture().bind(1); - - Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0); - } - - ShaderProgram.pedantic = false; - } - @Override public void draw() { - if (gaussianBlurShader != null) { - //Begin drawing a normal version of screen - normalBuffer.begin(); - Gdx.gl.glClearColor(0f, 0f, 0f, 0f); - Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - super.draw(); - collisionDetector.renderParticles(getBatch(), Gdx.graphics.getDeltaTime(), true); - normalBuffer.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight()); - //END - - //Begin light filtering - lightFilterBuffer.begin(); - Gdx.gl.glClearColor(0f, 0f, 0f, 0f); - Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - getBatch().setShader(brightFilterShader); - getBatch().begin(); //BATCH STARTS HERE - getRoot().draw(getBatch(), 1f); - getBatch().flush(); - lightFilterBuffer.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight()); - - //Horizontal gaussian blur - hBlur.begin(); - fboRegion.setTexture(lightFilterBuffer.getColorBufferTexture()); - getBatch().setShader(gaussianBlurShader); - gaussianBlurShader.setUniformf("horizontal", 1); - getBatch().draw(fboRegion, 0f, 0f, RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT); - getBatch().flush(); - hBlur.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight()); - - //Vertical gaussian blur - vBlur.begin(); - fboRegion.setTexture(hBlur.getColorBufferTexture()); - getBatch().setShader(gaussianBlurShader); - gaussianBlurShader.setUniformf("horizontal", 0); - getBatch().draw(fboRegion, 0f, 0f, RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT); - getBatch().flush(); - vBlur.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight()); - - //Draw everything to screen - getBatch().setShader(combineShader); - fboRegion.setRegion(normalBuffer.getColorBufferTexture()); - fboRegion.flip(false, true); - getBatch().draw(fboRegion, 0f, 0f, RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT); - getBatch().setShader(null); - getBatch().end(); //BATCH ENDS HERE - - } else { - super.draw(); - } + super.draw(); } @Override @@ -264,10 +139,10 @@ public class GamePlayArea extends Stage { @Override public void dispose() { - if (gaussianBlurShader != null) { - normalBuffer.dispose(); - gaussianBlurShader.dispose(); - } super.dispose(); } + + public CollisionDetector getCollisionDetector() { + return collisionDetector; + } }