I'had like to use a FrameBuffer and to set his size in world units instead of pixel.
When I set the FrameBuffer size in pixel, i have the expected result. But when I use another personal units. The result is messed up.
Here a snippet of the working code:
public class FBOTestLibGDX implements ApplicationListener {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.useGL30 = false;
cfg.width = 640;
cfg.height = 480;
cfg.resizable = false;
new LwjglApplication(new FBOTestLibGDX(), cfg);
}
Texture atlas;
FrameBuffer fbo;
TextureRegion fboRegion;
Matrix4 projection = new Matrix4();
SpriteBatch batch;
OrthographicCamera cam;
#Override
public void create() {
atlas = new Texture(Gdx.files.local("src/test/resources/fboData/testTexture.jpg"));
fbo = new FrameBuffer(Format.RGBA8888, atlas.getWidth(), atlas.getHeight(), false);
fboRegion = new TextureRegion(fbo.getColorBufferTexture(), atlas.getWidth(), atlas.getHeight());
fboRegion.flip(false, true); // FBO uses lower left, TextureRegion uses
projection.setToOrtho2D(0, 0, atlas.getWidth(), atlas.getHeight());
batch = new SpriteBatch();
batch.setProjectionMatrix(projection);
cam = new OrthographicCamera(5, 5);
cam.setToOrtho(false);
renderTextureInFBO();
}
protected void renderTextureInFBO() {
fbo.begin();
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.setProjectionMatrix(projection);
batch.draw(atlas, 0, 0);
batch.end();
fbo.end();
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 0f);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(fboRegion, 0, 0, 5, 5);
batch.end();
}
#Override
public void resize(int width, int height) {
cam.setToOrtho(false, 5, 5);
//batch.setProjectionMatrix(cam.combined);
}
#Override
public void pause() {}
#Override
public void resume() {}
#Override
public void dispose() {}
}
Using pixel I obtain this result:
But if I use world units for FrameBuffer like this:
public void create() {
...
fbo = new FrameBuffer(Format.RGBA8888, 5, 5, false);
fboRegion = new TextureRegion(fbo.getColorBufferTexture(), 5, 5);
fboRegion.flip(false, true);
projection.setToOrtho2D(0, 0, 5, 5);
...
}
protected void renderTextureInFBO() {
...
batch.draw(atlas, 0, 0,5,5);
...
}
I obtain a low scaled result:
The question is, it is possible to set the FrameBuffer size in world unit as we do for batch and viewport or it is mandatory to set it in pixel?
As specified in the documentation, a FrameBuffer size have to be defined in pixel. cf:
FrameBuffer specification
FrameBuffer
public FrameBuffer(Pixmap.Format format,
int width,
int height,
boolean hasDepth,
boolean hasStencil)
Creates a new FrameBuffer having the given dimensions and potentially a depth and a stencil buffer attached.
Parameters:
format - the format of the color buffer; according to the OpenGL ES 2.0 spec, only RGB565, RGBA4444 and RGB5_A1 are color-renderable
width - the width of the framebuffer in pixels
height - the height of the framebuffer in pixels
hasDepth - whether to attach a depth buffer
Throws
GdxRuntimeException - in case the FrameBuffer could not be created
Related
This is my first try in libGDX and I've not seen an issue like this before, googling didn't help either. What I'm trying to to display a background, Later on I'll make this move, but for me it was a great start to actually display the image. It displays, but it's streched out (See picture below)
And my code is:
private BombArrangement game;
private OrthographicCamera gameCamera;
private Viewport gamePort;
private Texture backGroundTexture;
public PlayScreen(BombArrangement game) {
this.game = game;
gameCamera = new OrthographicCamera();
gamePort = new FitViewport(BombArrangement.V_WIDTH, BombArrangement.V_HEIGHT, gameCamera);
backGroundTexture = new Texture("startbackground.png");
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();
game.batch.draw(new TextureRegion(backGroundTexture, 0, 0, BombArrangement.V_WIDTH, BombArrangement.V_HEIGHT), 0, 0);
game.batch.end();
}
#Override
public void resize(int width, int height) {
gamePort.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
I tried several things like textureregions, sprites and more but all of them give this result.
not exactly sure what your want to do but i use this to render my background in my main menu:
//Camera
camera = new OrthographicCamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
//Viewport
ScreenViewport viewport = new ScreenViewport(camera);
//Background
backgroundImage = new Texture(pathToImage);
//Stage
stage = new Stage();
stage.setViewport(viewport);
(this is located in my constructor and camera, backgroundImage and stage are fields in my class)
in render method
(ConfigData holds data of settings applied to the game; DEFAULT_WIDHT and -HEIGHT are just some values I use to initialize the window when not in fullscreen mode; Replace them with your values used in the DesktopLauncher for
config.widht
and
config.height
):
#Override
public void render(float delta) {
//Clear screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.getBatch().begin();
stage.getBatch().draw(backgroundImage, 0, 0, ConfigData.DEFAULT_WIDTH, ConfigData.DEFAULT_HEIGHT);
stage.getBatch().end();
stage.draw();
}
my resize method:
#Override
public void resize(int width, int height) {
camera.setToOrtho(false, width, height);
stage.getViewport().update(width, height);
}
hopes this helps somehow someone because i figured out this by myself and it costed some effort (:
As a response to your last comment:
You are now scaling down the height correctly but the width remains the same. Try to multiply the width by the same amount you scale the height down, so with some alterations to the code you linked in the comment (not tested):
private Texture texture;
private int x1, x2, speed, scaledHeight, scaledWidth;
public StartBackground() {
texture = new Texture("startbackground.png");
x1 = 0;
x2 = texture.getWidth();
speed = 5;
float imageRatio = Gdx.graphics.getHeight() / texture.getHeight();
scaledHeight = (int) (texture.getHeight() * imageRatio);
scaledWidth = (int) (texture.getWidth() * imageRatio);
}
public void updateAndRender(float deltaTime, SpriteBatch spriteBatch) {
x1 -= speed;
x2 -= speed;
// If image is off screen and not visible
if (x1 + texture.getWidth() <= 0) x1 = x2 + texture.getWidth();
if (x2 + texture.getWidth() <= 0) x2 = x1 + texture.getWidth();
// Render
spriteBatch.draw(texture, x1, 0, scaledWidth, scaledHeight);
spriteBatch.draw(texture, x2, 0, scaledWidth, scaledHeight);
}
I'm very new to programming so bear with me here...
I'm making a basic 2d android game. I am trying to simply make a sprite jump up in the air a bit and then land back to where it originally started. The sprite that I want to jump is "ball2". This is my code so far (Basically just added textures and the sprite in the correct position to start the jump but haven't done anything else).
Any help is appreciated.
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture background;
Texture ball;
Texture spike1;
Texture spike2;
#Override
public void create () {
batch = new SpriteBatch();
background = new Texture("gamebackground.png");
ball = new Texture("ball2.png");
ball.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
spike1 = new Texture("spike1.png");
spike1.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
spike2 = new Texture("spike2.png");
}
#Override
public void render () {
batch.begin();
float scaleFactor = 2.0f;
batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.draw(ball, 80, 145, ball.getWidth() * scaleFactor, ball.getHeight() * scaleFactor);
batch.end();
}
#Override
public void render () {
batch.begin();
float scaleFactor = 2.0f;
batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.draw(ball, 80, 145, ball.getWidth() * scaleFactor, ball.getHeight() * scaleFactor);
batch.end();
Gdx.input.setInputProcessor(new InputAdapter () {
#Override
public boolean keyDown (int keycode) {
if(keycode==Keys.UP)
{
ball.setHeight(ball.getHeight()+50);
}
return true;
}
}
}
Then you need to add the timer down down or you could add down button. Not sure if you have spikes on the top as well. I don't know what it's supposed to look like.
Can someone just give me a real quick answer on how i would implement a viewport so this sprite would fill the whole screen?
Thanks
Current code (not working):
public SplashScreen(Jump game)
{
this.game = game;
cam = new OrthographicCamera();
cam.setToOrtho(false, 1920, 1080);
sb = new SpriteBatch();
}
public void show()
{
}
public void render(float delta)
{
Gdx.gl20.glClearColor(0.2F, 0.6F, 1F, 1F);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(Assets.splash_spr_background, 0, 0);
sb.end();
}
On
#Override
public void resize(int width, int height) {
stage.getViewport().update(Gdx.graphics.getWidth(),
Gdx.graphics.getHeight(), false);
}
as spritebatch does not have a viewport. You have to control this using stage or cam.
you can do that.
I have this approach which works and scales as expected on Windows Desktop:
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch m_batch;
FitViewport m_viewport;
OrthographicCamera m_camera;
private Texture img;
private BitmapFont m_font;
#Override
public void create() {
Gdx.app.setLogLevel(Application.LOG_NONE);
m_batch = new SpriteBatch();
m_camera = new OrthographicCamera();
m_camera.position.set(640 / 2f, 400 / 2f, 0);
m_camera.update();
m_viewport = new FitViewport(640, 400, m_camera);
img = new Texture("badlogic.jpg");
m_font = new BitmapFont();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
m_batch.begin();
m_font.draw(m_batch, "X000000000000000000000000000000000000000000000000000000000000000000000000000000X", 0, m_camera.viewportHeight);
m_batch.draw(img, 0, 0);
m_batch.end();
}
#Override
public void resize(int width, int height) {
m_viewport.update(width, height);
}
}
Initial Window Size:
Bigger Window Size
But if I deploy to Android (in this case Nexus 7 2013) it seems not to scale properly:
Why does it not work? How to solve this?
You forgot to use the camera in your sprite batch. Call
spriteBatch.setProjectionMatrix(viewport.getCamera().combined);
before spriteBatch.begin();.
I have an image that I want to move to the right. To do this I increase its x position by 200f*Gdx.graphics.getDeltaTime(); every time render is called. I've read other posts where people had similar problems but were they were not multiplying by delta or Gdx.graphics.getDeltaTime(). Because I obviously do this why is my image flickering/vibrating when its moving???
My code:
public class gm extends Game{
OrthographicCamera cam;
SpriteBatch batch;
Texture img;
float x = 0;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
cam = new OrthographicCamera();
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
#Override
public void render () {
super.render();
//x += 200f*Gdx.graphics.getDeltaTime();
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(img, x += 100f*Gdx.graphics.getDeltaTime(), 0);
batch.end();
}
}