Example usage for com.badlogic.gdx.graphics.g2d TextureRegion getRegionWidth

List of usage examples for com.badlogic.gdx.graphics.g2d TextureRegion getRegionWidth

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g2d TextureRegion getRegionWidth.

Prototype

public int getRegionWidth() 

Source Link

Document

Returns the region's width.

Usage

From source file:br.com.animvs.koalory.entities.engine.graphics.tiles.TileRenderer.java

License:Apache License

@Override
public void renderTileLayer(TiledMapTileLayer layer) {
    final float color = Color.toFloatBits(1, 1, 1, layer.getOpacity());

    final int layerWidth = layer.getWidth();
    final int layerHeight = layer.getHeight();

    final float layerTileWidth = layer.getTileWidth() * unitScale;
    final float layerTileHeight = layer.getTileHeight() * unitScale;

    final int col1 = Math.max(0, (int) (cacheBounds.x / layerTileWidth));
    final int col2 = Math.min(layerWidth,
            (int) ((cacheBounds.x + cacheBounds.width + layerTileWidth) / layerTileWidth));

    final int row1 = Math.max(0, (int) (cacheBounds.y / layerTileHeight));
    final int row2 = Math.min(layerHeight,
            (int) ((cacheBounds.y + cacheBounds.height + layerTileHeight) / layerTileHeight));

    canCacheMoreN = row2 < layerHeight;
    canCacheMoreE = col2 < layerWidth;
    canCacheMoreW = col1 > 0;//from w ww  .  j ava  2  s  .  com
    canCacheMoreS = row1 > 0;

    float[] vertices = this.vertices;
    for (int row = row2; row >= row1; row--) {
        for (int col = col1; col < col2; col++) {
            final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
            if (cell == null)
                continue;

            final TiledMapTile tile = cell.getTile();
            if (tile == null)
                continue;

            count++;
            final boolean flipX = cell.getFlipHorizontally();
            final boolean flipY = cell.getFlipVertically();
            final int rotations = cell.getRotation();

            final TextureRegion region = tile.getTextureRegion();
            final Texture texture = region.getTexture();

            final float x1 = col * layerTileWidth + tile.getOffsetX() * unitScale;
            final float y1 = row * layerTileHeight + tile.getOffsetY() * unitScale;
            final float x2 = x1 + region.getRegionWidth() * unitScale;
            final float y2 = y1 + region.getRegionHeight() * unitScale;

            final float adjustX = 0.5f / texture.getWidth();
            final float adjustY = 0.5f / texture.getHeight();
            final float u1 = region.getU() + adjustX;
            final float v1 = region.getV2() - adjustY;
            final float u2 = region.getU2() - adjustX;
            final float v2 = region.getV() + adjustY;

            vertices[X1] = x1;
            vertices[Y1] = y1;
            vertices[C1] = color;
            vertices[U1] = u1;
            vertices[V1] = v1;

            vertices[X2] = x1;
            vertices[Y2] = y2;
            vertices[C2] = color;
            vertices[U2] = u1;
            vertices[V2] = v2;

            vertices[X3] = x2;
            vertices[Y3] = y2;
            vertices[C3] = color;
            vertices[U3] = u2;
            vertices[V3] = v2;

            vertices[X4] = x2;
            vertices[Y4] = y1;
            vertices[C4] = color;
            vertices[U4] = u2;
            vertices[V4] = v1;

            if (flipX) {
                float temp = vertices[U1];
                vertices[U1] = vertices[U3];
                vertices[U3] = temp;
                temp = vertices[U2];
                vertices[U2] = vertices[U4];
                vertices[U4] = temp;
            }
            if (flipY) {
                float temp = vertices[V1];
                vertices[V1] = vertices[V3];
                vertices[V3] = temp;
                temp = vertices[V2];
                vertices[V2] = vertices[V4];
                vertices[V4] = temp;
            }
            if (rotations != 0) {
                switch (rotations) {
                case Cell.ROTATE_90: {
                    float tempV = vertices[V1];
                    vertices[V1] = vertices[V2];
                    vertices[V2] = vertices[V3];
                    vertices[V3] = vertices[V4];
                    vertices[V4] = tempV;

                    float tempU = vertices[U1];
                    vertices[U1] = vertices[U2];
                    vertices[U2] = vertices[U3];
                    vertices[U3] = vertices[U4];
                    vertices[U4] = tempU;
                    break;
                }
                case Cell.ROTATE_180: {
                    float tempU = vertices[U1];
                    vertices[U1] = vertices[U3];
                    vertices[U3] = tempU;
                    tempU = vertices[U2];
                    vertices[U2] = vertices[U4];
                    vertices[U4] = tempU;
                    float tempV = vertices[V1];
                    vertices[V1] = vertices[V3];
                    vertices[V3] = tempV;
                    tempV = vertices[V2];
                    vertices[V2] = vertices[V4];
                    vertices[V4] = tempV;
                    break;
                }
                case Cell.ROTATE_270: {
                    float tempV = vertices[V1];
                    vertices[V1] = vertices[V4];
                    vertices[V4] = vertices[V3];
                    vertices[V3] = vertices[V2];
                    vertices[V2] = tempV;

                    float tempU = vertices[U1];
                    vertices[U1] = vertices[U4];
                    vertices[U4] = vertices[U3];
                    vertices[U3] = vertices[U2];
                    vertices[U2] = tempU;
                    break;
                }
                }
            }
            spriteCache.add(texture, vertices, 0, 20);
        }
    }
}

From source file:coder5560.gdxai.SteeringActor.java

License:Apache License

public SteeringActor(TextureRegion region, boolean independentFacing) {
    this.independentFacing = independentFacing;
    this.region = region;
    this.position = new Vector2();
    this.linearVelocity = new Vector2();
    this.setBounds(0, 0, region.getRegionWidth(), region.getRegionHeight());
    this.boundingRadius = (region.getRegionWidth() + region.getRegionHeight()) / 4f;
    this.setOrigin(region.getRegionWidth() * .5f, region.getRegionHeight() * .5f);
}

From source file:com.agateau.pixelwheels.Assets.java

License:Open Source License

private static void removeBorders(TextureRegion region) {
    region.setRegionX(region.getRegionX() + 2);
    region.setRegionY(region.getRegionY() + 2);
    region.setRegionWidth(region.getRegionWidth() - 4);
    region.setRegionHeight(region.getRegionHeight() - 4);
}

From source file:com.agateau.pixelwheels.bonus.Missile.java

License:Open Source License

private void drawTarget(Batch batch) {
    if (mTarget == null) {
        return;/* ww  w . j  av a  2s  .  c  o m*/
    }
    batch.setColor(mStatus == Status.LOCKED ? LOCKED_COLOR : TARGETED_COLOR);
    TextureRegion region = mAssets.target;
    float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth();
    float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight();
    batch.draw(region, mTarget.getX() - w / 2, mTarget.getY() - h / 2, w, h);
    batch.setColor(Color.WHITE);
}

From source file:com.agateau.pixelwheels.bonus.Missile.java

License:Open Source License

private void drawReactorFire(Batch batch) {
    TextureRegion region = mAssets.turboFlame.getKeyFrame(mTime, true);
    Vector2 center = mBody.getPosition();
    float angle = mBody.getAngle();
    float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth();
    float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight();
    float refH = Constants.UNIT_FOR_PIXEL * -WIDTH / 2;
    float x = center.x + refH * MathUtils.cos(angle);
    float y = center.y + refH * MathUtils.sin(angle);
    batch.draw(region, x - w / 2, y - h, // pos
            w / 2, h, // origin
            w, h, // size
            1, 1, // scale
            angle * MathUtils.radDeg - 90);
}

From source file:com.agateau.pixelwheels.gameobjet.AnimationObject.java

License:Open Source License

@Override
public void draw(Batch batch, ZLevel zLevel) {
    if (mTime < 0) {
        return;/*from w w w .  jav  a 2  s  .  co  m*/
    }
    if (zLevel == ZLevel.OBSTACLES) {
        TextureRegion region = mAnimation.getKeyFrame(mTime);
        float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth();
        float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight();
        batch.draw(region, mPosX - w / 2, mPosY - h / 2, w, h);
    }
}

From source file:com.agateau.pixelwheels.racer.VehicleRenderer.java

License:Open Source License

private void drawTurbo(Batch batch) {
    TextureRegion region = mAssets.turboFlame.getKeyFrame(mVehicle.getTurboTime(), true);
    Body body = mVehicle.getBody();//w  ww  . j a v a2 s  .c o m
    Vector2 center = body.getPosition();
    float angle = body.getAngle() * MathUtils.radiansToDegrees;
    float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth();
    float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight();
    float refH = -mVehicle.getWidth() / 2;
    float x = center.x + refH * MathUtils.cosDeg(angle);
    float y = center.y + refH * MathUtils.sinDeg(angle);
    batch.draw(region, x - w / 2, y - h, // pos
            w / 2, h, // origin
            w, h, // size
            1, 1, // scale
            angle - 90);
}

From source file:com.agateau.pixelwheels.racer.Wheel.java

License:Open Source License

public Wheel(GameWorld gameWorld, Vehicle vehicle, TextureRegion region, float posX, float posY, float angle) {
    mGameWorld = gameWorld;// ww  w  . j  a  va 2s  . com
    mVehicle = vehicle;
    mRegion = region;

    float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth();
    float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight();

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(posX, posY);
    bodyDef.angle = angle * MathUtils.degreesToRadians;
    mBody = mGameWorld.getBox2DWorld().createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.set(Box2DUtils.createOctogon(w, h, w / 4, w / 4));
    mBody.createFixture(shape, 2f);
    shape.dispose();
}

From source file:com.agateau.pixelwheels.utils.BodyRegionDrawer.java

License:Open Source License

public void draw(Body body, TextureRegion region) {
    Vector2 center = body.getPosition();
    float angle = body.getAngle();
    float x = center.x + mOffsetX * MathUtils.cos(angle) - mOffsetY * MathUtils.sin(angle);
    float y = center.y + mOffsetX * MathUtils.sin(angle) + mOffsetY * MathUtils.cos(angle);
    float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth();
    float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight();
    mBatch.draw(region, x - w / 2, y - h / 2, // pos
            w / 2, h / 2, // origin
            w, h, // size
            mScale, mScale, angle * MathUtils.radDeg);
}

From source file:com.agateau.pixelwheels.utils.BodyRegionDrawer.java

License:Open Source License

public void drawShadow(Body body, TextureRegion region) {
    Vector2 center = body.getPosition();
    float angle = body.getAngle() * MathUtils.radiansToDegrees;
    float offset = (SHADOW_OFFSET_PX + mZ * Z_MAX_SHADOW_OFFSET_PX + (mScale - 1) * SCALE_MAX_SHADOW_OFFSET_PX)
            * Constants.UNIT_FOR_PIXEL;//w w  w  . j  a v  a 2s. c  o  m
    float x = center.x + offset;
    float y = center.y - offset;
    float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth();
    float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight();
    Color old = mBatch.getColor();
    mBatch.setColor(0, 0, 0, SHADOW_ALPHA);
    mBatch.draw(region, x - w / 2, y - h / 2, // pos
            w / 2, h / 2, // origin
            w, h, // size
            1, 1, // scale
            angle);
    mBatch.setColor(old);
}