LibGDX: transform Texture to TextureRegion - libgdx

I am creating a TowerDefence Game in LibGDX and am now trying to replace the old Tile with a new StaticTiledMapTile.
But to create a StaticTiledMapTile I need a TextureRegion, not a Texture.
Now I'm trying to create a TextureRegion, which contains the whole Texture, but it doesn't seem to work. It always appears distorted.
I have tried the following:
TextureRegion region = new TextureRegion(new Texture("someImg.png"), 0, 0, 32, 32);
StaticTileMapTile tile = new StaticTiledMapTile(region);
getLayer().getCell(x,y).setTile(tile); //setting the new tile
In my opinion this should work (if the image, as it is, is 32px wide and 32px high).

Related

Libgdx get texture from Texture Atlas with findRegion

I have this code
textureAtlas = TextureAtlas("atlas.atlas")
val box = textureAtlas.findRegion("box")
I want to create a texture with "box". Is it possible? box.texture return the original texture, not the regioned. Oh and I don't want to use Sprite and SpriteBatch. I need this in 3D, not 2D.
Thanks
TextureAtlas actually not separating pieces. When you get region from atlas its just saying that this is the area you gonna use (u,v,u2,v2) and this is original reference to whole texture.
This is why batch.draw(Texture) and batch.draw(TextureRegion) are not same in use.
However taking part of picture as texture is possible.
You can use pixmap to do it.
First generate pixmap from atlas texture. Then create new empty pixmap in size of "box" area you want. Then assign pixel arrays and generate texture from your new pixmap.
It may be quite expensive due to your Textureatlas size.
You can use framebuffer.
Create FBbuilder and build new frame buffer.Draw texture region to this buffer and get texture from it.
Problem here is the sizes of texture will be same as viewport/screen sizes.I guess you can create new camera to change it to sizes you want.
GLFrameBuffer.FrameBufferBuilder frameBufferBuilder = new GLFrameBuffer.FrameBufferBuilder(widthofBox, heightofBox);
frameBufferBuilder.addColorTextureAttachment(GL30.GL_RGBA8, GL30.GL_RGBA, GL30.GL_UNSIGNED_BYTE);
frameBuffer = frameBufferBuilder.build();
OrthographicCamera c = new OrthographicCamera(widthofBox, heightofBox);
c.up.set(0, 1, 0);
c.direction.set(0, 0, -1);
c.position.set(widthofBox / 2, heightofBox / 2, 0f);
c.update();
batch.setProjectionMatrix(c.combined);
frameBuffer.begin();
batch.begin();
batch.draw(boxregion...)
batch.end();
frameBuffer.end();
Texture texturefbo = frameBuffer.getColorBufferTexture();
Texturefbo will be y flipped. You can fix this with texture draw method by setting scaleY to -1 or You can scale scaleY to -1 while drawing on framebuffer or can change camera like this
up.set(0, -1, 0);
direction.set(0, 0, 1);
to flip to camera on y axis.
Last thing came to my mind is mipmapping this texture.Its also not so hard.
texturefbo.bind();
Gdx.gl.glGenerateMipmap(GL20.GL_TEXTURE_2D);
texturefbo.setFilter(Texture.TextureFilter.MipMapLinearLinear,
Texture.TextureFilter.MipMapLinearLinear);
You can do this:
Texture boxTexture = new TextureRegion(textureAtlas.findRegion("box")).getTexture();

libgdx tiled map get player position in whole map to make a minimap

Actually I draw a part of my tiled map using camera zoom (with OrthogonalTiledMapRenderer on my main camera). Camera is centered on player position (who is always at center of the screen) in a box2d world.
// Set camera postion center on player (box2d body) on every update
camera.position.set(player.getBody().getWorldCenter(), 0);
I would like to create a minimap with a second camera. A simple filled rectangle with a point who represent player position on map.
So I created a new camera :
minimapCamera = new OrthographicCamera(1280, 720); // as my main camera
minimapCamera.zoom = 6; // larger zoom
minimapCamera.position.set(-1280 * 2, 720 * 3, 0); // align bottom right corner
And I try to draw shapes :
ShapeRenderer sr = new ShapeRenderer();
sr.setAutoShapeType(true);
sr.setProjectionMatrix(minimapCamera.combined);
sr.begin(ShapeType.Filled);
sr.setColor(0.5f, 0.5f, 0.5f, 0.3f);
// minimap rectangle background
sr.rect(0, 0, minimapCamera.viewportWidth, minimapCamera.viewportHeight);
sr.setColor(1f, 0f, 0f, 0.4f);
// player circle point
sr.circle(playerPos.x, playerPos.y, 20);
sr.end();
But how can I calculate the player position playerPos in the whole tiled map ? And how to draw only a part of zoomed map ? (I don't want to draw all map but a more larger section than my main camera).
EDIT :
If I want to reuse my tiled map in my minimap, how can I draw only player visible section in the minimap (at the moment, all the map is rendered even if I specify a small camera size) ?
Assuming your tile map makes use of rows and columns and each cell has a set width and height, you can get the player's position in the tile map array like this (hope you don't mind JavaScript):
var player={x:200,y:200};/* Or whatever position. */
var tile_width=tile_height=32;/* Or whatever dimension you're using. */
player.tile_x=Math.floor(player.x/tile_width);
player.tile_y=Math.floor(player.y/tile_height);
This gives you the exact x and y tile the player occupies in the world.

LibGDX FrameBuffer

I'm trying to make a game where you build a spaceship from parts, and fly it around and such.
I would like to create the ship from a series of components (from a TextureAtlas for instance). I'd like to draw the ship from it's component textures, and then save the drawn ship as one large Texture. (So i don't have to draw 50 component textures, just one ship texture. What would be the best way to go about this?
I've been trying to do so using a FrameBuffer. I've been able to draw the components to a Texture, and draw the texture to the screen, but no matter what I try the Texture ends up with a solid background the size of the frame buffer. It's like the clear command can't clear with transparency. Here's the drawing code I have at the moment. The ship is just a Sprite which i save the FrameBuffer texture to.
public void render(){
if (ship == null){
int screenwidth = Gdx.graphics.getWidth();
int screenheight = Gdx.graphics.getHeight();
SpriteBatch fb = new SpriteBatch();
FrameBuffer fbo = new FrameBuffer(Format.RGB888, screenwidth, screenheight, false);
fbo.begin();
fb.enableBlending();
Gdx.gl.glBlendFuncSeparate(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClearColor(1, 0, 1, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
fb.begin();
atlas.createSprite("0").draw(fb);
fb.end();
fbo.end();
ship = new Sprite(fbo.getColorBufferTexture());
ship.setPosition(0, -screenheight);
}
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.enableBlending();
batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ZERO);
ship.draw(batch);
batch.end();
}
The problem here lies in this line:
FrameBuffer fbo = new FrameBuffer(Format.RGB888, screenwidth, screenheight, false);
specifically with Format.RGB888. This line is saying that your FrameBuffer should be Red (8 bits) followed by Green (8 bits) followed by Blue (8 bits). Notice however, that this format doesn't have any bits for Alpha (transparency). In order to get transparency out of your frame buffer, you probably instead want to use the Format.RGBA8888, which includes an additional 8 bits for Alpha.
Hope this helps.

Create a pixmap which is the difference of two shapes

I'd like to create an overlay in my libGDX game to be used during onboarding. Basically I'd like to overlay the entire screen with a 40% alpha rectangle (that's easy) but I want a cutout of a circle on top of the area of interest where the user should focus attention. Is there a way to create a texture which is the difference of two shapes or pixmaps?
Pixmap overlay = new Pixmap(screenWidth, screenHeight, Pixmap.Format.RGBA8888);
overlay.setColor(0, 0, 0, 0.4f);
overlay.fillRectangle(0, 0, screenWidth, screenHeight);
Pixmap overlayCutout = new Pixmap(screenWidth, screenHeight, Pixmap.Format.RGBA8888);
overlayCutout.setColor(Color.WHITE);
overlayCutout.fillCircle(focusAreaX, focusAreaY, radius);
Texture t = new Texture(overlay);
//subtract overlayCutout??
overlay.dispose();
overlayCutout.dispose();
batch.draw(t);
I'm not sure I completely got what you want to do, but I think the trick is to use only one Pixmap...
Pixmap overlay = new Pixmap(screenWidth, screenHeight, Pixmap.Format.RGBA8888);
overlay.setColor(0, 0, 0, 0.4f);
overlay.fillRectangle(0, 0, screenWidth, screenHeight);
// the trick is to "redraw" the inner circle with an "invisible" colour alpha=0
overlay.setBlending(Blending.None);
overlay.setColor(1, 1, 1, 0);
overlay.fillCircle(focusAreaX, focusAreaY, radius);
overlay.setBlending(Blending.SourceOver);
Texture t = new Texture(overlay);
overlay.dispose();
batch.draw(t);

circular colorTransform

Is there a way to apply a colorTransform to a BitmapData in a circle rather than in a rectangle?
Instead of erasing rectangular parts of an image by reducing the alpha channel as in the code below, I'd like to do it in circles.
_bitmap.colorTransform(new Rectangle(mouseX-d/2, mouseY-d/2, d, d),
new ColorTransform(1, 1, 1, .5, 0, 0, 0, 1));
I do have some code which loops through the pixels, extracts the alpha value and uses setPixel but it seams significantly slower than the colorTransform function.
Try creating a circle using the drawing API (flash.display.Graphics) and then drawing that onto the bitmap data with BlendMode.ERASE. That might solve your problem, if I understand it correctly.
var circle : Shape = new Shape;
circle.graphics.beginFill(0xffcc00, 1);
circle.graphics.drawEllipse(-50, -50, 100, 100);
// Create a transformation matrix for the draw() operation, with
// a translation matching the mouse position.
var mtx : Matrix = new Matrix();
mtx.translate(mouseX, mouseY);
// Draw circle at mouse position with the ERASE blend mode, to
// set affected pixels to alpha=0.
myBitmap.draw(circle, mtx, null, BlendMode.ERASE);
I'm not 100% sure that the ERASE blend mode works satisfyingly with the draw() command, but I can't see why it shouldn't. Please let me know how it works out!