Example usage for com.badlogic.gdx.graphics.g2d Batch enableBlending

List of usage examples for com.badlogic.gdx.graphics.g2d Batch enableBlending

Introduction

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

Prototype

public void enableBlending();

Source Link

Document

Enables blending for drawing sprites.

Usage

From source file:CB_Locator.Map.MapViewBase.java

License:Open Source License

private void renderMapTiles(Batch batch) {
    batch.disableBlending();/* w w  w  .  j av  a2s.  c o  m*/

    float faktor = camera.zoom;
    float dx = this.thisWorldRec.getCenterPosX() - MainViewBase.mainView.getCenterPosX();
    float dy = this.thisWorldRec.getCenterPosY() - MainViewBase.mainView.getCenterPosY();

    dy -= ySpeedVersatz;

    camera.position.set(0, 0, 0);
    float dxr = dx;
    float dyr = dy;

    if (!this.NorthOriented || CarMode) {
        camera.up.x = 0;
        camera.up.y = 1;
        camera.up.z = 0;
        camera.rotate(-mapHeading, 0, 0, 1);
        double angle = mapHeading * MathUtils.DEG_RAD;
        dxr = (float) (Math.cos(angle) * dx + Math.sin(angle) * dy);
        dyr = (float) (-Math.sin(angle) * dx + Math.cos(angle) * dy);
    } else {
        camera.up.x = 0;
        camera.up.y = 1;
        camera.up.z = 0;
    }
    camera.translate(-dxr * faktor, -dyr * faktor, 0);

    camera.update();

    Matrix4 mat = camera.combined;

    batch.setProjectionMatrix(mat);

    try {
        // das Alter aller Tiles um 1 erhhen
        mapTileLoader.increaseLoadedTilesAge();
    } catch (Exception e) {
        // LogCat announces a java.util.ConcurrentModificationException
    }
    // for (int tmpzoom = zoom; tmpzoom <= zoom; tmpzoom++)
    {
        int tmpzoom = aktZoom;

        int halfMapIntWidth = mapIntWidth / 2;
        int halfMapIntHeight = mapIntHeight / 2;

        int halfDrawingtWidth = drawingWidth / 2;
        int halfDrawingHeight = drawingHeight / 2;

        loVector.set(halfMapIntWidth - halfDrawingtWidth, halfMapIntHeight - halfDrawingHeight - ySpeedVersatz);
        ruVector.set(halfMapIntWidth + halfDrawingtWidth, halfMapIntHeight + halfDrawingHeight + ySpeedVersatz);
        lo.set(screenToDescriptor(loVector, aktZoom, lo));
        ru.set(screenToDescriptor(ruVector, aktZoom, ru));

        for (int i = lo.getX(); i <= ru.getX(); i++) {
            for (int j = lo.getY(); j <= ru.getY(); j++) {
                Descriptor desc = new Descriptor(i, j, tmpzoom, this.NightMode);
                boolean canDraw = mapTileLoader.markToDraw(desc);
                boolean canDrawOverlay = false;
                if (mapTileLoader.getCurrentOverlayLayer() != null) {
                    canDrawOverlay = mapTileLoader.markToDrawOverlay(desc);
                }

                if (!canDraw && tmpzoom == aktZoom) {

                    // create this Tile new
                    desc.Data = this;
                    mapTileLoader.reloadTile(this, desc, aktZoom);

                    // fr den aktuellen Zoom ist kein Tile vorhanden ->
                    // kleinere Zoomfaktoren durchsuchen
                    if (!renderBiggerTiles(batch, i, j, aktZoom)) {
                        // grere Zoomfaktoren noch durchsuchen, ob davon Tiles
                        // vorhanden sind...
                        // dafr mssen aber pro fehlendem Tile mehrere kleine
                        // Tiles gezeichnet werden (4 oder 16 oder 64...)
                        // dieser Aufruf kann auch rekursiv sein...
                        renderSmallerTiles(batch, i, j, aktZoom);
                    }
                }

                if (mapTileLoader.getCurrentOverlayLayer() != null) {
                    if (!canDrawOverlay && tmpzoom == aktZoom) {
                        if (!renderBiggerOverlayTiles(batch, i, j, aktZoom))
                            renderSmallerOverlayTiles(batch, i, j, aktZoom);
                    }
                }
            }
        }
    }

    // FIXME Change to Sorted List, close Texture changing!!
    /*
     * Sort First Symbols then Text!
     * 
     * Sort Symbols with Texture, close Texture changing!
     * 
     * Sort Text with TextType and Size, close Texture changing!
     */
    CB_List<TileGL_RotateDrawables> rotateList = new CB_List<TileGL_RotateDrawables>();

    mapTileLoader.sort();

    synchronized (screenCenterW) {
        for (int i = mapTileLoader.getDrawingSize() - 1; i > -1; i--) {
            TileGL tile = mapTileLoader.getDrawingTile(i);
            if (tile == null)
                continue;

            // Faktor, mit der dieses MapTile vergrert gezeichnet
            // werden mu
            long posFactor = getscaledMapTilePosFactor(tile);

            long xPos = tile.Descriptor.getX() * posFactor * tile.getWidth() - screenCenterW.x;
            long yPos = -(tile.Descriptor.getY() + 1) * posFactor * tile.getHeight() - screenCenterW.y;
            float xSize = tile.getWidth() * posFactor;
            float ySize = tile.getHeight() * posFactor;

            // Draw Names and Symbols only from Tile with right zoom factor
            boolean addToRotateList = tile.Descriptor.getZoom() == aktZoom;

            tile.draw(batch, xPos, yPos, xSize, ySize, addToRotateList ? rotateList : null);

        }
        batch.enableBlending();

        // FIXME sort rotate List first the Symbols then the Text! sort Text with same Font!
        // Don't change the Texture (improve the Performance)

        for (int i = 0, n = rotateList.size(); i < n; i++) {
            TileGL_RotateDrawables drw = rotateList.get(i);
            if (drw != null)
                drw.draw(batch, -mapHeading);
        }
        rotateList.truncate(0);
        rotateList = null;

    }
    mapTileLoader.clearDrawingTiles();

    if (mapTileLoader.getCurrentOverlayLayer() != null) {
        synchronized (screenCenterW) {
            for (int i = mapTileLoader.getDrawingSizeOverlay() - 1; i > -1; i--) {
                TileGL tile = mapTileLoader.getDrawingTileOverlay(i);
                if (tile == null)
                    continue;

                // Faktor, mit der dieses MapTile vergrert gezeichnet
                // werden mu
                long posFactor = getscaledMapTilePosFactor(tile);

                long xPos = tile.Descriptor.getX() * posFactor * tile.getWidth() - screenCenterW.x;
                long yPos = -(tile.Descriptor.getY() + 1) * posFactor * tile.getHeight() - screenCenterW.y;
                float xSize = tile.getWidth() * posFactor;
                float ySize = tile.getHeight() * posFactor;
                tile.draw(batch, xPos, yPos, xSize, ySize, rotateList);

            }
        }
        mapTileLoader.clearDrawingTilesOverlay();
    }

}

From source file:com.bladecoder.engineeditor.scneditor.ScnWidget.java

License:Apache License

@Override
public void draw(Batch batch, float parentAlpha) {
    validate();/*from ww  w  . j ava  2s  . co  m*/

    Color tmp = batch.getColor();
    batch.setColor(Color.WHITE);

    if (scn != null && !loading && !loadingError) {
        // BACKGROUND
        batch.disableBlending();
        tile.draw(batch, getX(), getY(), getWidth(), getHeight());
        batch.enableBlending();

        Vector3 v = new Vector3(getX(), getY(), 0);
        v = v.prj(batch.getTransformMatrix());

        batch.end();

        HdpiUtils.glViewport((int) v.x, (int) v.y, (int) getWidth(), (int) (getHeight()));

        getStage().calculateScissors(bounds, scissors);

        if (ScissorStack.pushScissors(scissors)) {
            // WORLD CAMERA
            sceneBatch.setProjectionMatrix(camera.combined);
            sceneBatch.begin();

            Array<AtlasRegion> scnBackground = scn.getBackground();

            if (scnBackground != null) {
                sceneBatch.disableBlending();

                float x = 0;

                for (AtlasRegion tile : scnBackground) {
                    sceneBatch.draw(tile, x, 0f);
                    x += tile.getRegionWidth();
                }

                sceneBatch.enableBlending();
            }

            // draw layers from bottom to top
            List<SceneLayer> layers = scn.getLayers();
            for (int i = layers.size() - 1; i >= 0; i--) {
                SceneLayer layer = layers.get(i);

                if (!layer.isVisible())
                    continue;

                List<InteractiveActor> actors = layer.getActors();

                for (InteractiveActor a : actors) {
                    if (a instanceof SpriteActor) {
                        boolean visibility = a.isVisible();
                        a.setVisible(true);
                        ((SpriteActor) a).draw(sceneBatch);
                        a.setVisible(visibility);
                    }
                }
            }

            sceneBatch.end();
            ScissorStack.popScissors();
        }

        drawer.drawBGBounds();

        if (showWalkZone && scn.getPolygonalNavGraph() != null) {
            drawer.drawBBoxWalkZone(scn, false);

            drawer.drawPolygonVertices(scn.getPolygonalNavGraph().getWalkZone(), Color.GREEN);
        }

        drawer.drawBBoxActors(scn);

        if (selectedActor != null) {
            drawer.drawSelectedActor(selectedActor);
        }

        getStage().getViewport().apply();

        // SCREEN CAMERA
        batch.begin();

        drawFakeDepthMarkers((SpriteBatch) batch);

        if (!inScene) {
            faRenderer.draw((SpriteBatch) batch);
        }

        // DRAW COORDS
        Vector2 coords = new Vector2(Gdx.input.getX(), Gdx.input.getY());
        screenToWorldCoords(coords);
        String str = MessageFormat.format("({0}, {1})", (int) coords.x, (int) coords.y);

        textLayout.setText(defaultFont, str);

        RectangleRenderer.draw((SpriteBatch) batch, 0f, getY() + getHeight() - textLayout.height - 15,
                textLayout.width + 10, textLayout.height + 10, BLACK_TRANSPARENT);
        defaultFont.draw(batch, textLayout, 5, getHeight() + getY() - 10);

        batch.setColor(tmp);

    } else {
        background.draw(batch, getX(), getY(), getWidth(), getHeight());

        String s;

        if (loading) {
            s = "LOADING...";

            try {
                if (!EngineAssetManager.getInstance().isLoading()) {
                    loading = false;

                    scn.retrieveAssets();

                    drawer.setCamera(camera);

                    invalidate();
                }
            } catch (Exception e) {
                Message.showMsg(getStage(), "Could not load assets for scene", 4);
                e.printStackTrace();
                loadingError = true;
                loading = false;
            }

        } else if (loadingError) {
            s = "ERROR IN SCENE DATA. CANNOT DISPLAY SCENE";
        } else if (Ctx.project.getProjectDir() == null) {
            s = "CREATE OR LOAD A PROJECT";
        } else {
            s = "THERE ARE NO SCENES IN THIS CHAPTER YET";
        }

        textLayout.setText(bigFont, s);

        bigFont.draw(batch, textLayout, (getWidth() - textLayout.width) / 2,
                getHeight() / 2 + bigFont.getLineHeight() * 3);

    }

}

From source file:com.vlaaad.dice.states.GameMapState.java

License:Open Source License

private void addMap() {
    Image map = new Image(
            Config.assetManager.get("world-map.atlas", TextureAtlas.class).findRegion("world-map")) {
        @Override/*from ww  w  .  j a v a 2  s.co  m*/
        public void draw(Batch batch, float parentAlpha) {
            batch.disableBlending();
            super.draw(batch, parentAlpha);
            batch.enableBlending();
        }
    };

    Group group = new Group();
    group.addActor(map);

    ArrayList<Map> labels = MapHelper.get(Config.worldMapParams, "labels");
    for (Map labelInfo : labels) {
        Label label = new LocLabel(MapHelper.get(labelInfo, "key", "NOT SPECIFIED"));
        label.setPosition(MapHelper.get(labelInfo, "x", Numbers.ZERO).floatValue() - 50,
                MapHelper.get(labelInfo, "y", Numbers.ZERO).floatValue() - 50);
        label.setAlignment(Align.center);
        label.setSize(100, 100);
        label.setColor(LABEL);
        label.setTouchable(Touchable.disabled);

        group.addActor(label);
    }

    ArrayList<Map> images = MapHelper.get(Config.worldMapParams, "images");
    for (Map imageInfo : images) {
        Image image = new LocImage(MapHelper.get(imageInfo, "name", "NOT SPECIFIED"));
        image.setPosition(MapHelper.get(imageInfo, "x", Numbers.ZERO).floatValue() - 50,
                MapHelper.get(imageInfo, "y", Numbers.ZERO).floatValue() - 50);
        image.setSize(100, 100);
        image.setTouchable(Touchable.disabled);
        image.setScaling(Scaling.none);

        group.addActor(image);
    }

    for (BaseLevelDescription level : Config.levels) {
        if (level.hidden)
            continue;
        Table table = new Table();
        Image button = new Image(Config.skin.getDrawable("ui/level-icon/unknown"));
        button.setScaling(Scaling.none);
        button.setAlign(Align.bottom | Align.left);
        SoundHelper.init(button);
        button.setSize(button.getPrefWidth(), button.getPrefHeight());
        button.addListener(createStartLevelListener(level));
        buttonsByLevel.put(level, button);

        table.add(button).size(button.getWidth(), button.getHeight());
        Stack stack = new Stack();
        stack.setSize(100, 100);
        stack.setPosition(level.iconX /*- button.getWidth() / 2 */ - 50,
                level.iconY /*- button.getHeight() / 2 */ - 50);
        stack.add(table);

        if (level.useNumberInIcon) {
            int counter = 1;
            BaseLevelDescription check = level;
            while (check.parent != null) {
                counter++;
                check = Config.levels.get(check.parent);
            }
            Table labelTable = new Table(Config.skin);
            Label label = new Label(String.valueOf(counter), Config.skin);
            label.setTouchable(Touchable.disabled);
            labelTable.add(label).padLeft(1).padTop(-2);
            countersByLevel.put(level, label);
            stack.add(labelTable);
        } else if (level.levelIcon != null) {
            Table iconTable = new Table();
            iconTable.setTouchable(Touchable.disabled);
            Tile icon = new Tile("ui/level-icon/" + level.levelIcon);
            iconTable.add(icon).padTop(0);
            countersByLevel.put(level, icon);
            stack.add(iconTable);
        }
        group.addActor(stack);
    }
    refreshAvailableLevels();
    group.setSize(map.getPrefWidth(), map.getPrefHeight());

    pane = new ScrollPane(group, new ScrollPane.ScrollPaneStyle());
    pane.setOverscroll(false, false);
    pane.setFillParent(true);
    pane.setFlingTime(0.3f);
    //        pane.setSmoothScrolling(false);
    pane.setSize(stage.getWidth(), stage.getHeight());

    stage.addActor(pane);

    centerOnLevel(true);

    map.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            Logger.log(x + ", " + y);
        }
    });
}

From source file:io.piotrjastrzebski.sfg.game.objects.Background.java

License:Open Source License

public void draw(Batch batch) {
    batch.setProjectionMatrix(camera.calculateParallaxMatrix(0, 0));
    batch.disableBlending();//from   w w w  .j a  va2 s. co m
    batch.begin();
    batch.draw(bg_back, -width * 0.5f, -height * 0.5f + offset, width, height - offset);
    batch.enableBlending();
    for (int i = 0; i < NUM_STARS; i++) {
        stars.get(i).draw(batch);
    }
    moon.draw(batch);

    batch.setProjectionMatrix(camera.calculateParallaxMatrix(MID_SCALE, 0));
    for (int i = 0; i < NUM_MID_BG; i++) {
        float xPos = i * mid_width + mid_offset;
        // dont draw if outside of bounds
        if (xPos <= (camera.position.x * MID_SCALE + width * 0.5f))
            batch.draw(bg_mid, xPos, -height * 0.5f + offset, mid_width, mid_height);
    }
    batch.setProjectionMatrix(camera.calculateParallaxMatrix(FRONT_SCALE, 0));
    for (int i = 0; i < NUM_FRONT_BG; i++) {
        float xPos = i * front_width + front_offset;
        // dont draw if outside of bounds
        if (xPos <= (camera.position.x * FRONT_SCALE + width * 0.5f))
            batch.draw(bg_front, xPos, -height * 0.5f + offset - 0.9f, front_width, front_height);
    }

    batch.end();
}

From source file:mobi.shad.s3lib.main.S3Gfx.java

License:Apache License

public static void drawBackground(Batch batch, BaseShader shader, float startX, float startY, float endX,
        float endY) {

    final Matrix4 projectionMatrix = batch.getProjectionMatrix();
    batch.setProjectionMatrix(fixedCamera.combined);
    shader.begin();//ww  w  .  j  ava  2  s .  co m
    shader.setStartPosition(startX * 2, startY * 2);
    shader.setResolutionShader(endX - startX, endY - startY);
    shader.setProjectionMatrix(batch.getProjectionMatrix());
    shader.setModelViewMatrix(batch.getTransformMatrix());
    batch.setShader(shader.getShader());
    batch.setColor(1f, 1f, 1f, 1f);
    batch.enableBlending();
    batch.draw(defaultTexture, startX, startY, endX - startX, endY - startY);
    batch.setShader(null);
    batch.setProjectionMatrix(projectionMatrix);
}