Skip to content

Instantly share code, notes, and snippets.

@Tom-Ski
Created April 27, 2016 21:49
Show Gist options
  • Save Tom-Ski/33d7f8819aca782ad55626bad7e1ef45 to your computer and use it in GitHub Desktop.
Save Tom-Ski/33d7f8819aca782ad55626bad7e1ef45 to your computer and use it in GitHub Desktop.
package com.orangepixel.framebuffertest;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.Controllers;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
public class MyFrameBufferTest extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
FrameBuffer frameBuffer;
TextureRegion frameBufferRegion = null;
OrthographicCamera camera;
boolean created = false;
@Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
camera = new OrthographicCamera();
created = true;
generateFrameBuffers(240, 160);
}
@Override
public void render () {
frameBuffer.begin();
Gdx.gl.glClearColor(0.1f, 0, 0.6f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.setToOrtho(true, frameBuffer.getWidth(), frameBuffer.getHeight());
camera.update();
Gdx.gl.glClearColor(1.0f, 1.0f, 1.0f, 255 / 255.0f);
batch.begin();
batch.draw(img, 0, 0);
batch.end();
frameBuffer.end();
camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.update();
// render pixel-art buffer to screen
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
batch.setColor(1, 1, 1, 1);
batch.draw(frameBufferRegion, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.end();
}
@Override
public void dispose () {
img.dispose();
batch.dispose();
if (frameBuffer != null) {
frameBuffer.dispose();
}
}
@Override
public void resize (int width, int height) {
// the problem, moving this to outside resize will also fix the problem
generateFrameBuffers(width, height);
}
public final void generateFrameBuffers (int width, int height) {
if (!created) return;
if (frameBuffer != null)
frameBuffer.dispose();
frameBuffer = new FrameBuffer(Format.RGB888, width, height, false);
frameBuffer.getColorBufferTexture().setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
frameBufferRegion = new TextureRegion(frameBuffer.getColorBufferTexture(), 0, 0, width, height);
frameBufferRegion.flip(false, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment