libgdx blurred 2d graphics - android

I am still getting to this problem, and i am feeling like my head is going to blow. Lets say, that i have this code for drawing a texture i created.
public MainMenuScreen(TheSpring game) {
this.game = game;
//camera for interface
guiCam = new OrthographicCamera(1020, 640);
guiCam.position.set(1020 / 2, 640/2, 0);
//buttons
play = new Button(10, 120, 400, 300, AssetLoader.play);
highscores = new Button(460, 10, 82, 102, AssetLoader.highscores);
sound = new Button(10, 10, 72, 72, AssetLoader.soundEnabled);
sound.AddTexture(AssetLoader.soundDisabled);
if (!Settings.soundEnabled)
sound.SwitchTexture();
help = new Button(120, 160, 80, 15, "Help", Color.WHITE);
//enable buttons
play.Enable(true);
highscores.Enable(true);
sound.Enable(true);
help.Enable(true);
//touchpoint for unproject
touchpoint = new Vector3();
}`
The texture i created for play button is also 400x300.
The problem is, that when i try to run this "app" my textures are blurred.
The Button is my class, and I am using its drawing method.
public void Draw(SpriteBatch batcher) {
if (enabled){
if (text != null) {
AssetLoader.ComicFont.setColor(clr);
AssetLoader.ComicFont.setScale(ScaleWidth, ScaleHeight);
AssetLoader.ComicFont.draw(batcher, text, x, y + height);
}
if (texture != null) {
batcher.draw(texture, x, y, width, height);
}
}
}
And the result is this (please don't judge me for my drawing skills, its only testing version)
For example, on the right side of button, there should be also black line, and lines should be straight. Can you please help me? :)

one possible solution can be setting your texture filters to Linear
By default they are not linear.
try this out
texture.setFilter(TextureFilter.Linear,TextureFilter.Linear);

Related

Libgdx Table Confusion

I'm trying to display in a vertical and centered way an Image, and a button for my Screen Menu.
I've almost accomplished that only using actors but I decided that maybe a layout would be the best practice here.
So I've done:
public MenuScreen(final MyGame myGame) {
this.myGame = myGame;
font = new BitmapFont();
camera = new OrthographicCamera();
camera.setToOrtho(false, 480, 800);
stage = new Stage();
batch = new SpriteBatch();
stage.getViewport() .setCamera(camera);
skin = new Skin(Gdx.files.internal("menu/glassy-ui.json"));
button = new TextButton("NEW GAME", skin);
button.setTransform(true);
button.setScale(0.5f);
button.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
myGame.setScreen(new GameScreen( myGame));
}
});
Image imageLogo = new Image( new Texture(Gdx.files.internal("titleLogo.png")));
// imageLogo.setScaling(Scaling.fit);
Table myTable = new Table();
myTable.setFillParent(true);
myTable .add(imageLogo);// .width(imageLogo.getWidth()).height(imageLogo.getHeight()).row() ;
myTable.row() ;
myTable.add(button);
stage.addActor(myTable);
Gdx.input.setInputProcessor(stage);
batch.setProjectionMatrix(camera.combined);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.begin();
stage.act(delta);
stage.draw();
batch.end();
}
But it seems that the viewport of the stage isn't affecting the table area which goes offscreen and I'm not even sure that the image is displayed...
This is what I see :
Tried to read wiki and approaches of other developers.
For what I've read everyone has different system to accomplish that goal, nobody used a camera, and I suspect that's my real problem.
Thanks!
stage.setDebugAll(true); //enable debug mode and check what's going on
camera.setToOrtho(false, 480, 800);
By this you're setting viewport of camera by a fix value. You are not resizing/scaling any Actor so make sure all your actor should have size in the context of 480, 800.
camera.position.set(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2,1);
This will move your camera in center.

Libgdx auto-scrolling camera

I'm developing a libgdx game. What I want to do is to create a screen where some squares "falls" from the top to the bottom. So:
1. I have to set a background image.
2. I have to generate some squares randomly.
3. I have to move the screen from the bottom to the top.
In order to get that I'm using a Sprite for the background, a lot of sprites for the squares. Then a camera to move the screen.
In the render method I have:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.input.setInputProcessor(stage);
batch.begin();
backgroundSprite.draw(batch);
for(Square square : squareList) {
//Gdx.app.log("[Match]", square.toString());
square.updatePosition();
square.setSize(20, 20);
square.draw(batch);
}
batch.end();
batch.setProjectionMatrix(fixedCamera.view);
fixedCamera.position.y +=1;
System.out.println("Camera position: x:" +fixedCamera.position.x+" y:"+fixedCamera.position.y);
fixedCamera.update();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
This is the code on the constructor. It's all I have in the class
final static int WIDTH = Gdx.app.getGraphics().getWidth();
final static int HEIGHT = Gdx.app.getGraphics().getHeight();
skin = new Skin(Gdx.files.internal("skin/flat-earth-ui.json"));
fixedCamera = new OrthographicCamera(WIDTH, HEIGHT );
stage = new Stage(new ScreenViewport());
batch = new SpriteBatch();
dynBatch = new SpriteBatch();
backgroundSprite = new Sprite(new Texture(Gdx.files.internal(scenario.getTexturePath())));
backgroundSprite.setPosition(0,0);
backgroundSprite.setSize(WIDTH, HEIGHT);
fixedCamera.position.set(0,0, 0);
fixedCamera.update();
The items are displayed correctly, but when the camera moves, all the sprites disappears...
How can I fix that? Is there any way to handle that more efficiently?
Your requirement can be achieved by these steps :
Create Sprite having screen Size and keep it on bottom.
Generate Square at Top of screen with random horizontal position and change y position of all Square(Sprite)
No need to move your camera. keep it in the middle of the screen.

What might be causing only half of textures to be drawn in an Android game?

I've had reports from a couple of users (from 85000 downloads) of the problem shown in the image below. No doubt it has occurred a few more times than this, but it's certainly rare.
I'm unable to reproduce the problem and don't believe it's specific to device as some users would appear to be playing the game perfectly happily on the same device models that have reported the problem.
The letters are drawn onto a frame buffer first to build them up from the circular background with the character drawn on top. They are then copied off and a new texture is created.
The background is also put together from multiple components on a frame buffer and copied off to create a texture, so I'm not too sure why that appears to work perfectly well when the letters don't. The background is drawn using the same FrameBuffer and the same SpriteBatch instance.
What it does look like
What it should look like
Method that create the images
private static Texture getTextureUsingGpu(String letter, Bubble.BubbleType bubbleType) {
if (!enabled)
return null;
StrokeFontHelper font = Assets.strokeFont;
font.setSettings(Fonts.BUBBLE_TEXT_SETTINGS);
TextureRegion tx = getBlockImage(letter, bubbleType);
FrameBuffer fb = TextureLoader.getFrameBuffer();
fb.begin();
Gdx.gl20.glClearColor(0.0f, 0.0f, 0.0f, 1);
// Make sure everything is really really clear! Trying to fix graphics glitches on some phones
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT | GL20.GL_STENCIL_BUFFER_BIT | GL20.GL_SUBPIXEL_BITS);
float width = tx.getRegionWidth();
float height = tx.getRegionHeight();
TextureLoader.sb.begin();
tx.flip(false, !tx.isFlipY());
TextureLoader.sb.disableBlending();
TextureLoader.sb.draw(tx, 0, 0);
TextureLoader.sb.enableBlending();
// Removed character drawing code to make it more readable
TextureLoader.sb.end();
Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, (int) width,
(int) height);
PixmapTextureData data = new PixmapTextureData(pm, Format.RGBA8888,
false, false, true);
Texture result = new Texture(data);
result.setFilter(TextureFilter.Linear, TextureFilter.Linear);
cacheTexture(letter, result, pm);
fb.end();
return result;
}
public static FrameBuffer getFrameBuffer() {
if (frameBuffer == null || frameBuffer.getWidth() != Gdx.graphics.getWidth() || frameBuffer.getHeight() != Gdx.graphics.getHeight()) {
if (frameBuffer != null)
frameBuffer.dispose();
// Create the highest quality frame buffer we can get away with
try {
frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
} catch (Exception e) {
try {
frameBuffer = new FrameBuffer(Pixmap.Format.RGB888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
} catch (Exception e2) {
frameBuffer = new FrameBuffer(Pixmap.Format.RGB565, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
}
}
// Set up the camera correctly for the frame buffer
camera = new OrthographicCamera(frameBuffer.getWidth(), frameBuffer.getHeight());
camera.position.set(frameBuffer.getWidth() * 0.5f, frameBuffer.getHeight() * 0.5f, 0);
camera.update();
sb.setProjectionMatrix(camera.combined);
}
return frameBuffer;
}
Edit
I've done some fiddling with this and have a very helpful user who has been testing versions for me. Here's what I've established.
If I use a ShapeRenderer rather than a SpriteBatch then I can draw over the whole area as expected.
It is almost certainly the point at which I draw textures to the FrameBuffer using the SpriteBatch that the problem occurs. It just doesn't draw to the bottom half of the textures. What's on the FreameBuffer is copied to the pixmap correctly.
Another Edit I've got a new visual glitch reported by a user. I don't know if this might shed some more light on a problem.

Load 3d .obj file and add background image to live wallpaper in android?

i want to load a 3d .obj file in live wallpaper and animate it. i use RAJAWALI library to load it. the problem is that the the 3d obj file is load and animate successfully but Loading background image are not loaded in some android devices. code is follow:
protected void initScene() {
DirectionalLight localDirectionalLight = new DirectionalLight(0.0F, 0.6F, 0.4F);
localDirectionalLight.setPower(3.0F);
PointLight mLight = new PointLight();
mLight.setPosition(0, 0, 0);
mLight.setPower(13);
Plane plane = new Plane(70, 70, 1, 1, 1);
plane.addLight(mLight);
plane.setMaterial(new SimpleMaterial());
plane.addTexture(mTextureManager.addTexture(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.pozadina)));
addChild(plane);
plane.setZ(10);
try
{
ObjParser localObjParser = new ObjParser(this.mContext.getResources(), this.mTextureManager, R.raw.allah);
localObjParser.parse();
this.strela = localObjParser.getParsedObject();
this.strela.addLight(localDirectionalLight);
if (this.tekstura == null)
{
BitmapFactory.Options localOptions2 = new BitmapFactory.Options();
localOptions2.inPurgeable = true;
this.strela.setColor(Color.WHITE);
}
this.strela.setScale(2.0F);
addChild(this.strela);
this.mCamera.setPosition(0.0F, 0.0F, -50.0F);
this.mCamera.setFarPlane(1000.0F);
Number3D localNumber3D = new Number3D(0.0F, 1.0F, 0.0F);
localNumber3D.normalize();
this.mAnim = new RotateAnimation3D(localNumber3D, 360.0F);
this.mAnim.setDuration(10000L);
this.mAnim.setRepeatCount(-1);
this.mAnim.setTransformable3D(this.strela);
return;
}
catch (Exception localException)
{
while (true)
localException.printStackTrace();
}
}
it give problem on SAMSUNG NEXUS android v4.1.
please help how can i do it?
This is tested and working on Galaxy S3. So it should work for you as well. I use it in my wallpapers. Try this:
SimpleMaterial localSimpleMaterial = new SimpleMaterial();
Bitmap localBitmap = BitmapFactory.decodeResource(this.mContext.getResources(), R.drawable.image);
localSimpleMaterial.addTexture(this.mTextureManager.addTexture(localBitmap));
Plane localPlane = new Plane(10.0F, 10.0F, 1, 1);
localPlane.setRotZ(-90.0F);
localPlane.setScale(3.7F);
localPlane.setPosition(0.0F, 0.0F, 10.0F);
localPlane.setMaterial(localSimpleMaterial);
addChild(localPlane);
Just make sure that your image is power of two, i.e., 256x256, 512x512, etc. Let me know how it works for you.
Edit: Also make sure you import rajawali.primitives not rajawali.math
Is the issue solved?
What are those devices on which the plane is not rendered? Are those Samsung?
Also please tell me is it rendered in grey color or not rendered at all?
I am asking because I ran into same problem where the solution was to adjust the mip-mapping. It took time as I was a beginner then, but there was a very simple solution:
Textures are not displayed on the sphere for Samsung Galaxy SL

TiledSprite not working as expected

I can't seem to get TiledSprite to work as I expect. There are no manuals, and the examples in AndEngineExamples is of no help.
Consider this code:
#Override
public void onLoadResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
mBlockAtlas = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
mBlockTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBlockAtlas, getApplicationContext(), "num_plasma.png", 0, 0, 3, 3);
getTextureManager().loadTexture(mBlockAtlas);
}
This loads a 512x512 texture that I've - manually - divided into 3x3 tiles just for testing purposes. Now, doing this:
public Scene onLoadScene() {
mScene = new Scene();
mScene.setBackground(new ColorBackground(0.5f, 0.1f, 0.6f));
TiledSprite foo, foo2;
foo = new TiledSprite(0, 0, mBlockTexture);
foo.setCurrentTileIndex(1);
foo2 = new TiledSprite(0, 200, mBlockTexture);
foo2.setCurrentTileIndex(5);
mScene.attachChild(foo);
mScene.attachChild(foo2);
return mScene;
}
This puts up two sprites on the screen (as expected), but they both shows the same tile (5)!
How are one supposed to do if you have a bunch of sprites using the same texture but showing different tiles?
I think you need to deepCopy() the Texture when you are creating the Sprites, like
foo2 = new TiledSprite(0, 200, mBlockTexture.deepCopy());

Categories

Resources