Sprite not displaying unless android app pauses - LibGDX - java

Here is the code:
http://hastebin.com/ugufakeyap.java
When I run this, the sprite "aspectRatios" is not displayed on the screen. But when I pause the program by pressing my home button and then resuming the program, it displays the sprite onto the screen. Why is this? It's really annoying.

you need to call camera.update(); after changing position. My best guess is that libGDX automatically updates the camera on resume.
public void create () {
batch = new SpriteBatch();
txt = new Texture(Gdx.files.internal("test2.png"));
aspectRatios = new Sprite(txt);
aspectRatios.setPosition(0,0);
//aspectRatios.setSize(100,100);
camera = new OrthographicCamera();
viewport = new StretchViewport(1080,1920,camera);
camera.position.set(camera.viewportWidth/2,camera.viewportHeight/2,0);
camera.update();
viewport.apply();
//batch.setProjectionMatrix(camera.combined);
Gdx.input.setInputProcessor(this);
}
You would normally update the batch projection matrix there as well but you do it every frame in the render method anyway.

Some people helped me on the libGDX irc channel.
It turns out that I wasn't calling
viewport.update(width, height, centerCamera);
(Thanks to Xoppa)
And (according to ASneakyFox):
"you have to assume that the android device isnt going to be conveniently the size you want, itll call resize when the app opens up or changes orientation so you can modify your layout to be the dimensions of the screen (ie change the viewport)"
Here is the updated version of the code:
http://hastebin.com/olivopocix.java

Related

LibGDX reset or remove Orthographic camera

I'm quite new to LibGDX but not a beginner in java programming. I was making a Flappy bird like game. I used Orthographic camera in my play state so it would follow the bird, but now i want to make a game over screen. I created a class with all the code i need but when i try it still uses the Orthographic camera at the birds position before it died, so the textures on that game over screen are heavily out of place, although isTouched() method is at the right position, so if i press that region i set earlier it restarts the game as it should. The only problem are the textures. Could someone help me solve the problem as i can't figure it out.
I think you want something similar to HUD, which is always displayed on the screen, right?
If so, you can use another orthographic camera, name it hudCam or something you remember.
Not sure if necessary but I changed Y-down of the camera to false like this:
hudCamera.setToOrtho(false, WIDTH / 2, HEIGHT / 2); // width and height being the screen size
Then you can just switch between the cameras when drawing.
batch.setProjectionMatrix(camera.combined);
draw...
batch.setProjectionMatrix(hudCamera.combined);
draw HUD stuff...
To get the correct mouse coordinates, just do like you did with the other camera, unproject it.

How to create a proper ortographic camera in libgdx

So I am having a little hard time understanding how ortographic cameras work in libgdx.
what I want is to have a camera that will only render things within a square while having another camera set the bounds for my whole screen.
So here, I was able to do what I wanted on the whole screen for the game pad. But, the thing you see on the top right is the background map of the game and i want to render the parts only fall within the red square you see here. How do I achieve that?
Are cameras supposed to do that or do I need to figure out a way to do it manually? I am really confused as to how cameras and projection matrices work.
Here on this screen, The red square and the green pad on the left are being drawn using the projection matrix of my screen camera. The map(top right) is drawn using my map cam.
Map cam is a view port of 400x400 but as you can see , the tiles are rectangular and that isnt the aspect ration i want. If someone can briefly explain how cameras work , I'd greatly appreciate it.
The reason I am not posting my code here is because I feel like I need to understand how camera mechanics work to even code it properly so I want to address that issue first.
Following #Tenfour04's advice worked perfectly. In case anyone wonders what I wanted to achieve. Here's a picture.
A camera alone cannot crop off part of the screen. For that you need to use glViewport. There is already a Viewport class in Libgdx that can do that for you. You will need two orthographic cameras (one for the map and one for the GUI), but the viewport can create its own.
private Viewport viewport;
//in create:
viewport = new FitViewport(400, 400);
//in resize:
viewport.update(width, height);
//in render:
viewport.getCamera().position.set(/*...move your map camera as needed*/);
viewport.apply(); //viewport cropped to your red square
batch.setProjectionMatrix(viewport.getCamera().combined);
batch.begin();
//draw map
batch.end();
//return to full screen viewport
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.setProjectionMatrix(yourGUICamera.combined);
batch.begin();
//draw gui
batch.end();
What happens, is the camera will fit itself to the size of the screen. In order to change this, you would want to use a FrameBuffer. The frame buffer will constrain the camera into the desired size, then can be drawn as a texture.
Create the frame buffer with the dimensions being in pixels.
//Initialize the buffer
FrameBuffer fbo = new FrameBuffer(Format.RGB565, width, helght, false);
Render the world within the buffer.
fbo.begin();
//Draw the world here
fbo.end();
Draw the buffer to the screen with a batch.
batch.begin();
batch.draw(fbo.getColorBufferTexture(), x, y);
batch.end();

Fitviewport Wrong Camera Positioning

I have developed a game on my phone which has 1080x1920(portrait) resolution. And I set my fitviewport according to it. It looks like this on my phone:
But on any other phone or on desktop camera has wrong positioning. It looks like this on other devices:
I have used fitviewport in other projects before but never encountered that problem. May it be because of portrait mode?
EDIT
Related code is here:
//in create
camera = new Orthographiccamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeigth());
viewport = new FitViewport(1080,1920,camera);
//in resize
viewport.update(width,height);
//before starting batch
batch.setProjectionMatrix(camera.combined);
You have two contradicting statements in your code: camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeigth()). This will set the camera's viewport size to the display size and then center the camera.
On the other hand, you want to use a FitViewport with a fixed size of 1080x1920.
If your screen size is exactly 1080x1920, there's no problem, because the center will be the same. If the screen size differs from the one you use in FitViewport, then setToOrtho() will set the camera's center to a position that's not the center of 1080x1920 and that's why you notice the offset.
Using viewport.update(width, height, true) will correct this. The last parameter will center the camera correctly and override what happened in setToOrtho().
Let the viewport manage your camera and remove the call of camera.setToOrth().

Android - Canvas.drawBitmap doesn't work correctly on certain devices

I have a problem with my android app which draws graphs of functions on a plane. To do this I use a custom View. The user can pan the coordinate system with touch. In the onDraw method, the coordinate system and the labels are drawn directly on the view's canvas, but the graphs themselves are buffered onto an additional bitmap(for performace reasons) - during the movement of the finger this bitmap is drawn onto the view's canvas translated so as to match the coordinate system, and the functions are redrawn only when the user releases the touch.
public void onDraw(Canvas canvas){
if (start) {
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_4444);
hCanvas = new Canvas(bitmap);
start = false;
}
drawCoordinateSystem(canvas);
drawFunctions(canvas,bitmap,hCanvas);
}
In onTouch event listener I call invalidate(). There is a boolean "moving", which is set to true for ACTION_UP and to false for ACTION_DOWN.
public void drawFunctions(Canvas canvas,Bitmap bitmap,Canvas hCanvas){
if (moving) canvas.drawBitmap(bitmap,left+translateX,top+translateY,paint);
else {
hCanvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR);
//code for drawing the functions on hCanvas
canvas.drawBitmap(bitmap,0,0,paint);
}
}
In ACTION_MOVE I calculate the translateX and translateY values appropriately.
Now where is the problem:
If I compile the app for sdk 10, it works fine on all devices I tested it on(android 2.2-4.2 smartphones and tablets, virtual and physical). However, when I compile it for sdk 14, it works well for many devices, except for some tablets, where the coordinate system is drawn correctly, but somehow the bitmap containing the functions is not drawn on the view's canvas and the functions are not visible. The strange thing is, if I try to make a bitmap out of the view's canvas:
Bitmap bmp = this.getDrawingCache();
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
In the saved bitmap the functions are visible.
Another thing is - the issue occurs for i.a. Nexus 7, meanwhile on an AVD created with the preset Nexus 7 everything works fine.
What can be the problem?
http://developer.android.com/reference/android/graphics/Bitmap.Config.html
I'll copy and paste the relevant part:
Bitmap.Config ARGB_4444 This field was deprecated in API level . Because of the poor
quality of this configuration, it is advised to use ARGB_8888 instead.
Bitmap.Config ARGB_8888 Each pixel is stored on 4 bytes.
Change your 4444 to 8888 and it should work.

How do i get the dimensions of the screen when orientation changes? [android]

I know how to get the dimensions of the screen onCreate. I was making a runnable which will allow me to get the dimensions inside of that.
The thing is that when i rotate my device, it doesnt seem to properly return the dimensions.
I was thinking to do it onConfigurationChanged() as that seems to firre when i rotate my device. I was trying to do the following but i dont think it was giving valid data.
Rect r = new Rect();
Window win = getWindow();
win,getDecorView().getWindowVisibleDisplayFrame(r);
int height = r.height();
int width = r.width();
but it seems that they arent correct. I was trying to move a created item around the screen, and it doesnt go to the edges of the screen. I dont think it has anything to do with my drag function.
I feel the dimensions are incorrect.
How is this done? Am i on the right path?
The workaround i had created was that onConfigurationChange() I created a bundle with attributes and then recalled onCreate(). Essentially this would recreate in the new orientation.
This was what i did to get around this issue at hand, and it made it so the layout is rerendered.
Bundle b = new Bundle();
b.putString("test","data here");
onCreate(b);

Categories