Reduce time delay on Resume - android

I have a simple libgdx code for game which contains 1 texture and a particle effect with 4 emitters.
Whenever I resume to the game screen or lock-unlock the phone, I get a delay of about 3 seconds.
How do I reduce this delay?
One thing I have tried is that by reducing the texture image size.
Before i had a texture image of 300kb and used to get a delay of 5 seconds, now i hve reduced it to 60kb
and now I get a delay of 3 seconds.
Is there any way programaticaly that I can reduce the delay. I dont want to show any spalsh screen
Code:
#Override
public void show() {
SpriteBatch batch = new SpriteBatch();
Texture tex = new Texture(Gdx.files.internal("data/bg1.jpg"));
Sprite sprite = new Sprite(tex);
ParticleEffect pe = new ParticleEffect();
pe.load(Gdx.files.internal("data/pe1.p"), Gdx.files.internal("data"));
pe.start();
}
#Override
public void render(float delta) {
gl.glClearColor(0f, 0f, 0f, 1f);
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gl.glViewport(0, 0, (int) Width, (int) Height);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
pe.draw(batch, delta);
batch.end();
}

Why don't you just try making the local variables in show() field variables and initializing them in the constructor rather than in the show method. I would still recommend measuring which part is taking the most time as the one comment states.

Related

How to have multiple sprite in one atlas for libgdx?

I am using libGdx(Android) to create a simple animation
I used TexturePacker to create a sprite of multiple images ,I was using about 50 images , texture packer put them in more than one sprite sheet( 3 sprites) and exports a .atlas file with them , which contains all information about images:
TextureAtlas texture = new TextureAtlas(Gdx.files.internal("image.atlas");
Animation anime = new Animation(1/30f, texture.getRegions());
and this is render method
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
elapsedTime += Gdx.graphics.getDeltaTime();
batch.draw((TextureRegion)anime.getKeyFrame(elapsedTime, true), 0, 0,w,h);
batch.end();
}
when I tested my code it works, but only the first sprite is appeared and loop, the other 2 sprites aren't !

Process image with android opengl es but output image is weird in a certain device

I wrote a demo about camera which is similar to ContinuousCaptureActivity of grafika (Source code of ContinuousCaptureActivity.java). For every frame, I added some operations besides drawing the frame to screen.
I create a fbo, then bind it and draw the frame to the fbo. In order to check the image rendered to the fbo, I read back the image data back to RAM using pbo and save the image as jpg every 50 frames.
In this way, I got the jpg file but it was scaled, picture is as follows:
I supposed the matrix from SurfaceTexture caused the scaling because every drawFrame() applied that matrix.So I created anothter fbo and draw the frame of the first fbo to this fbo, then I save the image as jpg and the image turn OK as expected. Look at this one:
So the conclusion is that 2 draws will restore the image correctly.
But the image is weird in a huawei phone of mine(model:huawei MT7-CL00). Look at it:
I have checked the image of the first fbo with the huawei phone, it is scaled as expected but not weird. So I think the error ocurred in the scecond fbo. But I can not figure out the reason. Who can give me some advices?
Some source codes:
fbo definitions:
private GlTexture mGlTexture;
private GlFrameBuffer mGlFrameBuffer;
private GlFrameBuffer mGlFrameBuffer1;
GlTexture and GlFrameBuffer class source codes:
http://www.paste.org/80680
bindFbo method:
private void bindFBO(int fbo)
{
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fbo);
GLES20.glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
SurfaceView sv = (SurfaceView) findViewById(R.id.continuousCapture_surfaceView);
GLES20.glViewport(0, 0, 640, 480);
}
the key codes in drawFrame():
mDisplaySurface.makeCurrent();
mCameraTexture.updateTexImage();
mCameraTexture.getTransformMatrix(mTmpMatrix);
// Fill the SurfaceView with it.
SurfaceView sv = (SurfaceView) findViewById(R.id.continuousCapture_surfaceView);
int viewWidth = 640;
int viewHeight = 480;
GLES20.glViewport(0, 0, viewWidth, viewHeight);
mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);
//drawExtra(mFrameNum, viewWidth, viewHeight);
mDisplaySurface.swapBuffers();
bindFBO(mGlFrameBuffer.getID());
mFullFrameBlit.drawFrame(mTextureId, mTmpMatrix);
bindFBO(mGlFrameBuffer1.getID());
mFullFrameBlit2d.drawFrame(mGlTexture.getID(), mTmpMatrix);
String filename = "/sdcard/test/" + System.currentTimeMillis() + ".jpg";
new File(filename).getParentFile().mkdirs();
if (mFrameNum % 50 == 0)
{
try {
getPixelFromPBO(viewWidth, viewHeight, filename);
}
catch (Exception e) {
}
}

Dynamic texture creation with font using libgdx

I'm working on a word game and I was dynamically creating the textures for the letter tiles when the game loads, comprising of a background image and a font.
To do this I was drawing pixmaps onto pixmaps, this was all fine until I started working on scaling. The font scaling on the pixmaps was terrible, even with bilinear filtering turned on (left image below) even though my scaled fonts were looking pretty good elsewhere.
So I decided to get round this I'd use a frame buffer, render everything to that and then copy that out to a pixmap and create a texture from that. That way I could use the gpu filtering and it should look exactly the same as my other fonts, (middle image below) but it still didn't look quite as nice as the other fonts. A slight dark line round the outside, it looks like the alpha blending isn't working properly.
I then tried drawing straight over the tiles with the font at runtime to make sure it wasn't my imagination, and this definitely looks better with smooth blending into the image below (right image below), but this impacts my frame rate quite a lot.
So my question is, why is drawing to the frame buffer not producing the same result as when I draw to the screen? Code below.
Texture tx = Assets.loadTexture("bubbles/BubbleBlue.png");
tx.setFilter(TextureFilter.Linear, TextureFilter.Linear);
SpriteBatch sb = new SpriteBatch();
FrameBuffer fb = new FrameBuffer(Format.RGBA8888,
LayoutManager.getWidth(), LayoutManager.getHeight(), false);
fb.begin();
sb.begin();
sb.draw(tx, 0, 0, LetterGrid.blockWidth, LetterGrid.blockHeight);
Assets.candara80.font.getRegion().getTexture()
.setFilter(TextureFilter.Linear, TextureFilter.Linear);
Assets.candara80.setSize(0.15f);
TextBounds textBounds = Assets.candara80.getBounds(letter);
Assets.candara80.drawText(sb, letter,
(LetterGrid.blockWidth - textBounds.width) / 2,
(LetterGrid.blockHeight + textBounds.height) / 2);
sb.end();
Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0,
(int) LetterGrid.blockWidth, (int) LetterGrid.blockHeight);
Pixmap flipped = flipPixmap(pm);
result = new Texture(flipped);
fb.end();
pm.dispose();
flipped.dispose();
tx.dispose();
fb.dispose();
sb.dispose();
set PROJECTION is the problem.
EXAMPLE
public Texture texture(Color fg_color, Color bg_color)
{
Pixmap pm = render( fg_color, bg_color );
texture = new Texture(pm);//***here's your new dynamic texture***
disposables.add(texture);//store the texture
}
//---------------------------
public Pixmap render(Color fg_color, Color bg_color)
{
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
SpriteBatch spriteBatch = new SpriteBatch();
m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false);
m_fbo.begin();
Gdx.gl.glClearColor(bg_color.r, bg_color.g, bg_color.b, bg_color.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
/**set PROJECTION**/
Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
spriteBatch.setProjectionMatrix(normalProjection);
spriteBatch.begin();
spriteBatch.setColor(fg_color);
//do some drawing ***here's where you draw your dynamic texture***
...
spriteBatch.end();//finish write to buffer
pm = ScreenUtils.getFrameBufferPixmap(0, 0, (int) width, (int) height);//write frame buffer to Pixmap
m_fbo.end();
// pm.dispose();
// flipped.dispose();
// tx.dispose();
m_fbo.dispose();
m_fbo = null;
spriteBatch.dispose();
// return texture;
return pm;
}

LibGdx don't draw properly a circle in float space

My camera is set to a normalized space (1 unit in height and 1.5 unit in width). But it seems the circle algorithm of the ShapeRenderer works only in integer space. Is there a workaround ?
public void create() {
camera = new OrthographicCamera();
camera.setToOrtho(false, 1.5f, 1f);
shapes = new ShapeRenderer();
}
public void drawScene() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
shapes.setProjectionMatrix(camera.combined);
shapes.begin(ShapeType.Circle);
shapes.setColor(1, 0, 0, 1);
shapes.circle(0.75f, 0.5f,0.5f);
shapes.end();
}
ShapeRenderer uses 6 times the cube root of the radius to estimate how many segments it needs to draw a circle.
In your case that you end up with 4 segments (6 * 0.793 is roughly 4.76). Which is what you see.
ShapeRenderer assumes units are screen pixels according to the documentation, so these are reasonable estimates.

How do I create an orthographic camera that correctly scales my sprites/textures with libgdx?

I'm creating a game with libgdx that I want to run at a higher resolution on the desktop, but I want it to scale everything down correctly when I run it on android at smaller resolutions. I've read that the best way to do this is to not use a pixel perfect camera, and instead to use world coordinates, but I'm not sure how to correctly do that.
This is the code I have right now:
#Override
public void create() {
characterTexture = new Texture(Gdx.files.internal("character.png"));
characterTextureRegion = new TextureRegion(characterTexture, 0, 0, 100, 150);
batch = new SpriteBatch();
Gdx.gl10.glClearColor(0.4f, 0.6f, 0.9f, 1);
float aspectRatio = (float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight();
camera= new OrthographicCamera(aspectRatio, 1.0f);
}
#Override
public void render() {
GL10 gl = Gdx.graphics.getGL10();
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
camera.apply(gl);
batch.setProjectionMatrix(camera.combined);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
draw();
}
private void draw() {
//batch.getProjectionMatrix().set(camera.combined);
batch.begin();
batch.draw(characterTextureRegion, 0, 0, // the bottom left corner of the box, unrotated
1f, 1f, // the rotation center relative to the bottom left corner of the box
0.390625f, 0.5859375f, // the width and height of the box
1, 1, // the scale on the x- and y-axis
0); // the rotation angle
batch.end();
}
The texture I'm use is 256x256 with the actual image in it being 100x150.
This is the result I get when I run the game: http://i.imgur.com/HV9Bi.png
The sprite that gets rendered is massive, considering this is the original image: http://i.imgur.com/q1cZT.png
What's the best way to go about making it so that the sprites get rendered at their original size while still keeping the ability to have the game scale correctly when played in different resolutions?
I've only found two solutions, both of which I don't like.
The image showed up how it was supposed to if I used pixel coordinates for the camera, but then that didn't scale at all when I put it on my phone with a different resolution.
I can scale the texture region down when I draw it, but it seems like there is a better way because it is extremely tedious trying to figure out the correct number to scale it by.
Have you ever used the Libgdx setup tool? When you create a project with it, it has a sample image that is displayed. It seems to keep it's ratio correct no matter what size you change the screen to.
public class RotationTest implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
Stage stage;
public boolean leonAiming = true;
#Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2); }....
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
First of all you need to fix boundaries to the world (I mean to your game ). In that world only you actors(game characters) should play. If you are crossing boundaries, manage it with camera like showing up, down, left and right.

Categories

Resources