Example usage for com.badlogic.gdx.graphics.g2d SpriteBatch end

List of usage examples for com.badlogic.gdx.graphics.g2d SpriteBatch end

Introduction

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

Prototype

@Override
    public void end() 

Source Link

Usage

From source file:at.juggle.games.memory.CreditsScreen.java

License:Apache License

@Override
public void render(SpriteBatch spriteBatch) {
    float h = MemoryGame.HEIGHT;
    float l = font.getLineHeight();

    minTimeShown -= Gdx.graphics.getDeltaTime();
    if (Gdx.input.justTouched() & minTimeShown < 0) {
        assets.options.gameState = GameOptions.STATE_MENU;
        minTimeShown = 2f;/*from w  w  w  .  j a v  a2s .c o m*/
    }
    // key listener ...
    if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE) || Gdx.input.isKeyPressed(Input.Keys.BACK)) { // get back to menu ...
        assets.options.gameState = GameOptions.STATE_MENU;
    }

    offset += Gdx.graphics.getDeltaTime() * 35f;
    if (offset > MemoryGame.HEIGHT)
        offset = -(creditsLines.length) * font.getLineHeight();
    spriteBatch.begin();
    // draw background ...
    spriteBatch.draw(assets.background, 0, 0, MemoryGame.WIDTH, MemoryGame.HEIGHT);

    spriteBatch.setColor(Color.WHITE);
    for (int i = 0; i < creditsLines.length; i++) {
        String line = creditsLines[i];
        font.draw(spriteBatch, line, 16, offset + (creditsLines.length - i) * font.getLineHeight());
    }
    // draw gradients for the fx
    spriteBatch.draw(assets.gradientBottom, 0, 0, MemoryGame.WIDTH, assets.gradientBottom.getHeight());
    spriteBatch.draw(assets.gradientTop, 0, MemoryGame.HEIGHT - assets.gradientTop.getHeight(),
            MemoryGame.WIDTH, assets.gradientTop.getHeight());
    spriteBatch.end();
}

From source file:at.juggle.games.memory.GameScreen.java

License:Apache License

@Override
public void render(SpriteBatch spriteBatch) {
    if (model == null) {
        startGame();// w ww. j av  a2  s. c  om
    }
    // key listener ...
    if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE) || Gdx.input.isKeyPressed(Input.Keys.BACK)) { // get back to menu ...
        assets.options.buttonPressTimeout = 1f;
        assets.options.gameState = GameOptions.STATE_MENU;
    }

    h = MemoryGame.HEIGHT;

    cardHeight = (h - (numRows + 1) * offset) / numRows;

    // determine which card has been clicked and react in the model ...
    // but only if the intro animation is over ...
    if (model.introAnimation < 0) {
        compute(offset, cardHeight);
        // one time covering action after the animation.
        if (model.needsToBeCovered) {
            model.coverCards();
            model.needsToBeCovered = false;
        }
    } else
        model.introAnimation -= Gdx.graphics.getDeltaTime();
    // game time;
    if (!model.finished)
        model.time += Gdx.graphics.getDeltaTime();

    // the drawing

    spriteBatch.begin();
    // draw background ...
    spriteBatch.draw(assets.background, 0, 0, MemoryGame.WIDTH, MemoryGame.HEIGHT);

    for (int r = 0; r < numRows; r++) {
        for (int c = 0; c < numCols; c++) {
            cardX = c * (cardHeight - cardHeight / 5.5f);
            cardY = offset + r * (cardHeight + offset);
            float cardTime = model.introAnimationLength / ((float) model.numCards);
            if (cardTime * (c + numCols * r) < model.introAnimationLength - model.introAnimation) { // if is for the start animation ... also compensates the first click :)
                // draw the card ...
                if (model.getState((c + numCols * r)) == 0) {
                    spriteBatch.draw(assets.cardBackMark, cardX, cardY, cardHeight, cardHeight);
                } else if (model.getState((c + numCols * r)) == 1) {
                    spriteBatch.draw(assets.cardBack, cardX, cardY, cardHeight, cardHeight);
                } else if (model.getState((c + numCols * r)) >= 2) {
                    spriteBatch.draw(assets.card, cardX, cardY, cardHeight, cardHeight);

                    if (assets.icons == null) {
                        String str = "" + model.icon[(c + numCols * r)];
                        assets.font.draw(spriteBatch, str,
                                cardX + cardHeight / 2 - assets.font.getBounds(str).width / 2,
                                cardY + cardHeight / 2 + assets.font.getBounds(str).height / 2);
                    } else {
                        float iconSize = cardHeight / 2;
                        spriteBatch.draw(assets.icons[model.icon[(c + numCols * r)]],
                                cardX + cardHeight / 2 - iconSize / 2, cardY + cardHeight / 2 - iconSize / 2,
                                iconSize, iconSize);

                    }

                    if (model.getState((c + numCols * r)) == 3 && !model.finished) {
                        spriteBatch.draw(assets.check, cardX + cardHeight - 2f * cardHeight / 5.5f,
                                cardY + cardHeight / 16f, 32, 32);
                    }
                }
                // draw the icon ...
            }
        }
    }

    // draw time and moves ...
    if (model.introAnimation < 0 & !model.finished) {
        String moves = model.numberOfMoves + " tries";
        int secs = ((int) Math.floor(model.time)) % 60;
        int min = ((int) Math.floor(model.time)) / 60;
        String timeString = min + ":" + (secs < 10 ? "0" : "") + secs;
        assets.font.draw(spriteBatch, moves, MemoryGame.WIDTH - assets.font.getBounds(moves).width - 10,
                offset + assets.font.getLineHeight());
        assets.font.draw(spriteBatch, timeString,
                MemoryGame.WIDTH - assets.font.getBounds(timeString).width - 10,
                offset + assets.font.getLineHeight() * 2);
    }

    //        spriteBatch.draw(assets.undo, MemoryGame.WIDTH - assets.undo.getWidth() - 10, MemoryGame.HEIGHT - assets.undo.getHeight() - 10);

    if (model.finished) {
        spriteBatch.draw(assets.grey, 0, 0, MemoryGame.WIDTH, MemoryGame.HEIGHT);
        spriteBatch.draw(assets.positive, MemoryGame.WIDTH / 2 - assets.positive.getWidth() / 2,
                MemoryGame.HEIGHT / 2 - assets.positive.getHeight() / 2);
        assets.font.draw(spriteBatch, "Congratulations!",
                MemoryGame.WIDTH / 2 - assets.font.getBounds("Congratulations!").width / 2,
                MemoryGame.HEIGHT - assets.font.getBounds("Congratulations!").height);
        int secs = ((int) Math.floor(model.time)) % 60;
        int min = ((int) Math.floor(model.time)) / 60;
        String timeString = "Solved in " + min + ":" + (secs < 10 ? "0" : "") + secs + " and "
                + model.numberOfMoves + " tries";
        assets.smallFont.draw(spriteBatch, timeString,
                MemoryGame.WIDTH / 2 - assets.smallFont.getBounds(timeString).width / 2,
                assets.smallFont.getBounds(timeString).height * 2);

        assets.fireworks.findEmitter("fireworks").setPosition(MemoryGame.WIDTH - 100, 0);
        assets.fireworks.findEmitter("fireworks").draw(spriteBatch, Gdx.graphics.getDeltaTime());
    }

    spriteBatch.end();
}

From source file:at.juggle.games.memory.MenuScreen.java

License:Apache License

@Override
public void render(SpriteBatch spriteBatch) {
    assets.font.setScale(scaleFont, scaleFont);
    assets.font.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
    h = MemoryGame.HEIGHT;//from  w w  w  .  j av a  2 s. c  o  m
    l = assets.font.getLineHeight() * scaleFont + 18;

    if (Gdx.input.justTouched()) {
        // (x,y) from top left corner
        float x = Gdx.input.getX() / ((float) Gdx.graphics.getWidth()) * ((float) MemoryGame.WIDTH);
        float y = Gdx.input.getY() / ((float) Gdx.graphics.getHeight()) * ((float) MemoryGame.HEIGHT);
        if (x > l && x < 2 * MemoryGame.WIDTH / 3) {
            if (y > l + 5 && y < 2 * l - 10) { // new Game
                assets.options.gameState = GameOptions.STATE_GAME;
            } else if (y > 2 * l + 5 && y < 3 * l - 10) { // number of cards changed
                assets.options.nextNumberOfCards();
            } else if (y > 3 * l + 5 && y < 4 * l - 10) { // Mode
                assets.options.toggleGameMode();
            } else if (y > 4 * l + 5 && y < 5 * l - 10) { // Sound
                assets.options.toggleSoundOn();
            } else if (y > 5 * l + 5 && y < 6 * l - 10) { // credits
                assets.options.gameState = GameOptions.STATE_CREDITS;
            }
        }
    }

    // key listener ...
    if (assets.options.buttonPressTimeout < 0) {
        if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE) || Gdx.input.isKeyPressed(Input.Keys.BACK)) { // use the back button
            Gdx.app.exit();
        }
    } else
        assets.options.buttonPressTimeout -= Gdx.graphics.getDeltaTime();

    // do the drawing ...
    spriteBatch.begin();
    // draw background ...
    spriteBatch.draw(assets.background, 0, 0, MemoryGame.WIDTH, MemoryGame.HEIGHT);

    spriteBatch.setColor(Color.WHITE);
    if (MemoryGame.WIDTH / 3 - 20 < assets.logo.getWidth()) { // draw logo scaled
        float logoWidth = (int) (MemoryGame.WIDTH / 3f - 20f);
        spriteBatch.draw(assets.logo, MemoryGame.WIDTH - logoWidth - 10, 0, logoWidth, logoWidth);
    } else {
        spriteBatch.draw(assets.logo, MemoryGame.WIDTH - assets.logo.getWidth() - 10, 0);
    }

    assets.font.draw(spriteBatch, "> New Game", l, h - l);
    assets.font.draw(spriteBatch, "> Cards: ", l, h - 2 * l);
    assets.font.draw(spriteBatch, assets.options.getNumberOfCards() + "",
            2 * MemoryGame.WIDTH / 3 - assets.font.getBounds(assets.options.getNumberOfCards() + "").width,
            h - 2 * l);
    assets.font.draw(spriteBatch, "> Mode: ", l, h - 3 * l);
    assets.font.draw(spriteBatch, assets.options.getGameModeString(),
            2 * MemoryGame.WIDTH / 3 - assets.font.getBounds(assets.options.getGameModeString()).width,
            h - 3 * l);
    assets.font.draw(spriteBatch, "> Sound: ", l, h - 4 * l);
    assets.font.draw(spriteBatch, (assets.options.soundOn) ? "On" : "Off",
            2 * MemoryGame.WIDTH / 3 - assets.font.getBounds((assets.options.soundOn) ? "On" : "Off").width,
            h - 4 * l);
    assets.font.draw(spriteBatch, "> Credits", l, h - 5 * l);

    spriteBatch.end();
}

From source file:at.therefactory.jewelthief.Game.java

License:Open Source License

public void debug(SpriteBatch batch) {
    if (debug) {//w  w  w  . j  a  va 2s  .c  om
        String sb = "currentLevel=" + currentLevel + "\n" + "numCollectedJewels="
                + player.getNumCollectedJewels() + "\n";
        batch.begin();
        font.setColor(Color.BLACK);
        font.draw(batch, sb, 5, 20);
        font.setColor(Color.RED);
        font.draw(batch, "rectangleEnemyField", rectangleEnemyField.getX(),
                rectangleEnemyField.getY() + rectangleEnemyField.getHeight());
        font.setColor(Color.GREEN);
        font.draw(batch, "rectangleJewelField",
                rectangleJewelField.getX() + rectangleJewelField.getWidth() - 65,
                rectangleJewelField.getY() + rectangleJewelField.getHeight());
        font.setColor(Color.WHITE);
        font.draw(batch, "rectanglePlayerField",
                rectanglePlayerField.getX() + rectanglePlayerField.getWidth() - 70,
                rectanglePlayerField.getY() + rectanglePlayerField.getHeight());
        batch.end();
    }
}

From source file:com.anhld.game.WorldRenderer.java

License:Apache License

private void renderWorld(SpriteBatch batch) {
    worldController.cameraHelper.applyTo(camera);
    camera.update();//from   w ww .j  av  a2 s.c  o  m
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    if (Stores.getInstance().getObjectStores().get(Constants.BACKGROUND) != null) {
        batch.draw(Stores.getInstance().getObjectStores().get(Constants.BACKGROUND),
                -Stores.getInstance().getObjectStores().get(Constants.BACKGROUND).getRegionWidth() * 0.5f,
                -Stores.getInstance().getObjectStores().get(Constants.BACKGROUND).getRegionHeight() * 0.5f);
    }

    if (Stores.getInstance().getObjectStores().get(Constants.SHADOWN) != null) {
        Stores.getInstance().getObjectStores().get(Constants.SHADOWN)
                .setRegionWidth((int) virtualViewport.getVirtualWidth());
        batch.draw(Stores.getInstance().getObjectStores().get(Constants.SHADOWN),
                -virtualViewport.getVirtualWidth() / 2, virtualViewport.getVirtualHeight() / 2
                        - Stores.getInstance().getObjectStores().get(Constants.SHADOWN).getRegionHeight());
    }

    if (Stores.getInstance().getObjectStores().get(Constants.SCORE) != null)
        batch.draw(Stores.getInstance().getObjectStores().get(Constants.SCORE), -50,
                virtualViewport.getVirtualHeight() / 2
                        - Stores.getInstance().getObjectStores().get(Constants.SCORE).getRegionHeight() - 10);
    worldController.gameLogic.render(batch);
    batch.end();

}

From source file:com.badlogic.gdx.tests.PremultiplyAlpha.java

private void gpuPremultiplyAlpha(String in, String out) {
    Texture texture = new Texture(Gdx.files.absolute(in));
    texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
    FrameBuffer buffer = new FrameBuffer(Format.RGBA8888, texture.getWidth(), texture.getHeight(), false);
    buffer.getColorBufferTexture().setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
    ShaderProgram shader = new ShaderProgram(VERTEX_SHADER, FRAG_SHADER);
    Gdx.app.log("Log", shader.getLog());
    SpriteBatch batch = new SpriteBatch(10);
    batch.getProjectionMatrix().setToOrtho2D(0, 0, texture.getWidth(), texture.getHeight());
    batch.disableBlending();/*from   www.  jav a  2  s.c  o  m*/
    batch.setShader(shader);

    //Premultiply
    buffer.begin();
    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(texture, 0, 0);
    batch.end();
    Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, texture.getWidth(), texture.getHeight());
    buffer.end();

    //Save
    PixmapIO.writePNG(Gdx.files.absolute(out), pixmap);
    buffer.dispose();
    texture.dispose();
    pixmap.dispose();
    Gdx.app.exit();
}

From source file:com.binarytenshi.nopassing.state.MainGameScreen.java

@Override
public void render(float delta) {
    Camera camera = CameraHandler.getCamera();
    SpriteBatch batch = NoPassing.getSpriteBatch();
    Rectangle viewport = CameraHandler.getViewport();

    camera.update();//from  w  w w.ja  va 2 s .  c om
    camera.apply(Gdx.gl10);

    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glViewport((int) viewport.x, (int) viewport.y, (int) viewport.width, (int) viewport.height);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    MapHandler.draw(batch);
    batch.end();

    SplineHandler.drawSplines();
}

From source file:com.bladecoder.engine.model.Scene.java

License:Apache License

public void draw(SpriteBatch batch) {

    if (background != null) {
        batch.disableBlending();/*from   w w w  .j  a va  2s  .c o m*/
        batch.setProjectionMatrix(camera.calculateParallaxMatrix(1, 1));
        batch.begin();

        float x = 0;

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

        batch.end();
        batch.enableBlending();
    }

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

        batch.setProjectionMatrix(camera.calculateParallaxMatrix(layer.getParallaxMultiplier(), 1));
        batch.begin();
        layer.draw(batch);
        batch.end();
    }
}

From source file:com.bladecoder.engine.model.Sprite3DRenderer.java

License:Apache License

@Override
public void draw(SpriteBatch batch, float x, float y, float scale) {

    x = x - getWidth() / 2 * scale;//  ww  w.j a v  a  2  s.c o m

    if (USE_FBO) {
        batch.draw(tex, x, y, 0, 0, width, height, scale, scale, 0);
    } else {
        float p0x, p0y, pfx, pfy;

        Vector3 tmp = new Vector3(); // TODO Make static for performance?
        updateViewport();

        // get screen coords for x and y
        tmp.set(x, y, 0);

        tmp.mul(batch.getTransformMatrix());
        tmp.prj(batch.getProjectionMatrix());
        p0x = VIEWPORT.width * (tmp.x + 1) / 2;
        p0y = VIEWPORT.height * (tmp.y + 1) / 2;

        tmp.set(x + width * scale, y + height * scale, 0);
        tmp.mul(batch.getTransformMatrix());
        tmp.prj(batch.getProjectionMatrix());
        pfx = VIEWPORT.width * (tmp.x + 1) / 2;
        pfy = VIEWPORT.height * (tmp.y + 1) / 2;

        batch.end();

        Gdx.gl20.glViewport((int) (p0x + VIEWPORT.x), (int) (p0y + VIEWPORT.y), (int) (pfx - p0x),
                (int) (pfy - p0y));

        Gdx.gl.glClear(GL20.GL_DEPTH_BUFFER_BIT
                | (Gdx.graphics.getBufferFormat().coverageSampling ? GL20.GL_COVERAGE_BUFFER_BIT_NV : 0));

        drawModel();

        Gdx.gl20.glViewport((int) VIEWPORT.x, (int) VIEWPORT.y, (int) VIEWPORT.width, (int) VIEWPORT.height);
        batch.begin();
    }
}

From source file:com.bladecoder.engine.ui.CreditsScreen.java

License:Apache License

@Override
public void render(float delta) {
    final SpriteBatch batch = ui.getBatch();
    final int width = (int) viewport.getWorldWidth();
    final int height = (int) viewport.getWorldHeight();

    Gdx.gl.glClearColor(0, 0, 0, 1);//ww  w. j  a v  a  2  s . co m
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(viewport.getCamera().combined);
    batch.begin();

    if (style.background != null) {
        style.background.draw(batch, 0, 0, width, height);
    }

    scrollY += delta * SPEED * EngineAssetManager.getInstance().getScale();

    float y = scrollY;

    if (stringHead >= credits.size())
        ui.setCurrentScreen(Screens.MENU_SCREEN);

    for (int i = stringHead; i < credits.size(); i++) {
        String s = credits.get(i);

        char type = 'c'; // types are 'c' -> credit, 't' -> title, 'i' -> image, 's' -> space, 'm' -> music

        if (s.indexOf('#') != -1) {
            type = s.charAt(0);
            s = s.substring(2);
        }

        switch (type) {
        case 't':
            y = processCreditTitle(batch, width, height, y, i, s);
            break;
        case 'i':
            y = processCreditImage(batch, width, height, y, i, s);
            break;
        case 's':
            y = processCreditSpace(height, y, i, s);
            break;
        case 'm':
            processCreditMusic(s);
            break;
        default:
            y = processCreditDefault(batch, width, height, y, i, s);
            break;
        }

        if (y < 0) {
            break;
        }
    }

    batch.end();
}