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

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

Introduction

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

Prototype

public void begin();

Source Link

Document

Sets up the Batch for drawing.

Usage

From source file:MyGdxGame.java

License:Apache License

private void renderKoala(float deltaTime) {
    // based on the koala state, get the animation frame
    TextureRegion frame = null;/*from w  w  w .  j a  v a  2  s  . c  om*/
    switch (koala.state) {
    case Standing:
        frame = stand.getKeyFrame(koala.stateTime);
        break;
    case Walking:
        frame = walk.getKeyFrame(koala.stateTime);
        break;
    case Jumping:
        frame = jump.getKeyFrame(koala.stateTime);
        break;
    }

    // draw the koala, depending on the current velocity
    // on the x-axis, draw the koala facing either right
    // or left
    Batch batch = renderer.getBatch();
    batch.begin();
    if (koala.facesRight) {
        batch.draw(frame, koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT);
    } else {
        batch.draw(frame, koala.position.x + Koala.WIDTH, koala.position.y, -Koala.WIDTH, Koala.HEIGHT);
    }
    batch.end();
}

From source file:CB_UI_Base.GL_UI.Controls.EditTextField.java

License:Open Source License

@Override
protected void render(Batch batch) {
    if (this.isDisposed())
        return;/*  w w  w .j a v a 2  s  . c  om*/

    displayTextLock.lock();
    try {
        boolean focused = GL.that.hasFocus(this);
        if (style.getBackground(focused) != null) {
            style.getBackground(focused).draw(batch, 0f, 0f, getWidth(), getHeight());
        }

        batch.end();
        batch.begin();

        // Background is drawn, now set scissor to inner rect
        Gdx.gl.glScissor((int) (intersectRec.getX() + bgLeftWidth),
                (int) (intersectRec.getY() + bgBottomHeight), (int) textWidth + 1, (int) textHeight + 1);

        float textY = (int) getHeight() - bgTopHeight + style.font.getDescent();

        if (selection != null) {
            // Selection zeilenweise durchgehen
            for (int lineNo = selection.cursorStart.y; lineNo <= selection.cursorEnd.y; lineNo++) {
                Line line = getNthLine(lineNo);
                if (line == null)
                    continue;
                int start = 0;
                if (lineNo == selection.cursorStart.y)
                    start = selection.cursorStart.x;
                int end = line.displayText.length();
                if (lineNo == selection.cursorEnd.y)
                    end = selection.cursorEnd.x;
                float selectionX = line.glyphPositions.get(start);
                float selectionY = line.glyphPositions.get(end);
                float selectionWidth = selectionY - selectionX;
                style.selection
                        .draw(batch, selectionX + bgLeftWidth - leftPos,
                                textY + this.style.font.getLineHeight() * (topLine - 1 - lineNo)
                                        - style.font.getDescent() / 2,
                                selectionWidth, this.style.font.getLineHeight());
            }
        }

        if ((lines.size() == 1) && (getNthLine(0).displayText.length() == 0)) {
            if (!focused && messageText != null) {
                if (style.messageFontColor != null) {
                    style.font.setColor(style.messageFontColor.r, style.messageFontColor.g,
                            style.messageFontColor.b, style.messageFontColor.a);
                } else {
                    style.font.setColor(0.7f, 0.7f, 0.7f, 1f);
                }
                style.font.draw(batch, messageText, bgLeftWidth, textY);
            }
        } else {
            style.font.setColor(style.fontColor.r, style.fontColor.g, style.fontColor.b, style.fontColor.a); //?
            textY += this.style.font.getLineHeight() * topLine;
            for (Line line : lines) {
                style.font.draw(batch, line.displayText, bgLeftWidth - leftPos, textY);
                textY -= this.style.font.getLineHeight();
            }
        }

        if (focused) {
            if (cursorOn && style.cursor != null) {
                textY = (int) getHeight() - bgTopHeight + style.font.getDescent();
                cursorHeight = this.style.font.getLineHeight() + style.font.getDescent() / 2;
                float xpos = getCursorX();
                style.cursor.draw(batch, xpos, getCursorY() + cursorHeight + style.font.getDescent(),
                        style.cursor.getMinWidth(), cursorHeight);
            }
        }

        if (focused && (selection == null)) {
            if (blinkTimer == null)
                blinkStart();
        } else {
            if (blinkTimer != null)
                blinkStop();
        }
    } catch (Exception e) {
    } finally {
        displayTextLock.unlock();
    }
}

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  ava2s .c  o 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.bladecoder.engineeditor.ui.SceneList.java

License:Apache License

public TextureRegion getBgIcon(String atlas, String region) {

    // check here for dispose instead in project loading because the opengl
    // context lost in new project thread
    if (disposeBgCache) {
        dispose();//from  w  w w  . j av a  2 s.  c  o m
        disposeBgCache = false;
    }

    String s = atlas + "#" + region;
    TextureRegion icon = bgIconCache.get(s);

    if (icon == null) {
        Batch batch = getStage().getBatch();
        batch.end();

        icon = createBgIcon(atlas, region);

        if (icon != null) {
            bgIconCache.put(s, icon);
        } else {
            EngineLogger.error("Error creating Background icon");
        }

        batch.begin();

    }

    return icon;
}

From source file:com.github.mkjensen.breakall.actor.Box2DActor.java

License:Apache License

@Override
public void draw(Batch batch, float parentAlpha) {
    batch.end();//from   ww  w . j ava2 s. com
    renderer.setProjectionMatrix(batch.getProjectionMatrix());
    renderer.setTransformMatrix(batch.getTransformMatrix());
    draw(renderer);
    batch.begin();
}

From source file:com.ixeption.libgdx.transitions.impl.AlphaFadingTransition.java

License:Apache License

@Override
public void render(Batch batch, Texture currentScreenTexture, Texture nextScreenTexture, float alpha) {
    alpha = Interpolation.fade.apply(alpha);
    batch.begin();
    batch.setColor(1, 1, 1, 1);//from ww  w  .j  a  v a2  s.c o m
    batch.draw(currentScreenTexture, 0, 0, 0, 0, currentScreenTexture.getWidth(),
            currentScreenTexture.getHeight(), 1, 1, 0, 0, 0, currentScreenTexture.getWidth(),
            currentScreenTexture.getHeight(), false, true);
    batch.setColor(1, 1, 1, alpha);
    batch.draw(nextScreenTexture, 0, 0, 0, 0, nextScreenTexture.getWidth(), nextScreenTexture.getHeight(), 1, 1,
            0, 0, 0, nextScreenTexture.getWidth(), nextScreenTexture.getHeight(), false, true);
    batch.end();

}

From source file:com.ixeption.libgdx.transitions.impl.ColorFadeTransition.java

License:Apache License

@Override
public void render(Batch batch, Texture currentScreenTexture, Texture nextScreenTexture, float percent) {
    float width = currentScreenTexture.getWidth();
    float height = currentScreenTexture.getHeight();
    float x = 0;//from  www .j  a  v  a2  s  .co m
    float y = 0;

    if (interpolation != null)
        percent = interpolation.apply(percent);

    batch.begin();

    float fade = percent * 2;

    if (fade > 1.0f) {
        fade = 1.0f - (percent * 2 - 1.0f);
        color.a = 1.0f - fade;
        batch.setColor(color);

        batch.draw(nextScreenTexture, 0, 0, width / 2, height / 2, nextScreenTexture.getWidth(),
                nextScreenTexture.getHeight(), 1, 1, 0, 0, 0, nextScreenTexture.getWidth(),
                nextScreenTexture.getHeight(), false, true);

    } else {

        color.a = 1.0f - fade;
        batch.setColor(color);

        batch.draw(currentScreenTexture, 0, 0, width / 2, height / 2, width, height, 1, 1, 0, 0, 0, (int) width,
                (int) height, false, true);

    }

    color.a = fade;

    batch.setColor(color);
    batch.draw(texture, 0, 0, width, height);
    batch.end();
    batch.setColor(Color.WHITE);

}

From source file:com.ixeption.libgdx.transitions.impl.RotatingTransition.java

License:Apache License

@Override
public void render(Batch batch, Texture currentScreenTexture, Texture nextScreenTexture, float percent) {
    float width = currentScreenTexture.getWidth();
    float height = currentScreenTexture.getHeight();
    float x = 0;/*w ww . j  a  v a  2  s .co  m*/
    float y = 0;

    float scalefactor;

    switch (scaling) {
    case IN:
        scalefactor = percent;
        break;
    case OUT:
        scalefactor = 1.0f - percent;
        break;
    case NONE:
    default:
        scalefactor = 1.0f;
        break;
    }

    float rotation = 1;
    if (interpolation != null)
        rotation = interpolation.apply(percent);

    batch.begin();
    batch.draw(currentScreenTexture, 0, 0, width / 2, height / 2, width, height, 1, 1, 0, 0, 0, (int) width,
            (int) height, false, true);
    batch.draw(nextScreenTexture, 0, 0, width / 2, height / 2, nextScreenTexture.getWidth(),
            nextScreenTexture.getHeight(), scalefactor, scalefactor, rotation * angle, 0, 0,
            nextScreenTexture.getWidth(), nextScreenTexture.getHeight(), false, true);
    batch.end();

}

From source file:com.ixeption.libgdx.transitions.impl.SlicingTransition.java

License:Apache License

@Override
public void render(Batch batch, Texture currentScreenTexture, Texture nextScreenTexture, float percent) {
    float width = currentScreenTexture.getWidth();
    float height = currentScreenTexture.getHeight();
    float x = 0;//  ww  w.  ja v  a 2  s. co  m
    float y = 0;
    int sliceWidth = (int) (width / slices.size);

    batch.begin();
    batch.draw(currentScreenTexture, 0, 0, 0, 0, width, height, 1, 1, 0, 0, 0, (int) width, (int) height, false,
            true);
    if (interpolation != null)
        percent = interpolation.apply(percent);
    for (int i = 0; i < slices.size; i++) {

        x = i * sliceWidth;

        float offsetY = height * (1 + slices.get(i) / (float) slices.size);
        switch (direction) {
        case UP:
            y = -offsetY + offsetY * percent;
            break;
        case DOWN:
            y = offsetY - offsetY * percent;
            break;
        case UPDOWN:
            if (i % 2 == 0) {
                y = -offsetY + offsetY * percent;
            } else {
                y = offsetY - offsetY * percent;
            }
            break;
        }
        batch.draw(nextScreenTexture, x, y, 0, 0, sliceWidth, nextScreenTexture.getHeight(), 1, 1, 0,
                i * sliceWidth, 0, sliceWidth, nextScreenTexture.getHeight(), false, true);
    }
    batch.end();
}

From source file:com.ixeption.libgdx.transitions.impl.SlidingTransition.java

License:Apache License

@Override
public void render(Batch batch, Texture currentScreenTexture, Texture nextScreenTexture, float percent) {
    float width = currentScreenTexture.getWidth();
    float height = currentScreenTexture.getHeight();
    float x = 0;/*from  ww w  . j a  v a  2s . c o m*/
    float y = 0;
    if (interpolation != null)
        percent = interpolation.apply(percent);

    switch (direction) {
    case LEFT:
        x = -width * percent;
        if (!slideOut)
            x += width;
        break;
    case RIGHT:
        x = width * percent;
        if (!slideOut)
            x -= width;
        break;
    case UP:
        y = height * percent;
        if (!slideOut)
            y -= height;
        break;
    case DOWN:
        y = -height * percent;
        if (!slideOut)
            y += height;
        break;
    }
    Texture texBottom = slideOut ? nextScreenTexture : currentScreenTexture;
    Texture texTop = slideOut ? currentScreenTexture : nextScreenTexture;

    batch.begin();
    batch.draw(texBottom, 0, 0, 0, 0, width, height, 1, 1, 0, 0, 0, (int) width, (int) height, false, true);
    batch.draw(texTop, x, y, 0, 0, nextScreenTexture.getWidth(), nextScreenTexture.getHeight(), 1, 1, 0, 0, 0,
            nextScreenTexture.getWidth(), nextScreenTexture.getHeight(), false, true);
    batch.end();

}