com.bombinggames.clonkrageremade.View.java Source code

Java tutorial

Introduction

Here is the source code for com.bombinggames.clonkrageremade.View.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bombinggames.clonkrageremade;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import java.util.ArrayList;

/**
 *
 * @author Benedikt Vogler
 */
public class View {

    private final SpriteBatch spriteBatch = new SpriteBatch(2000);
    private final ShapeRenderer shRenderer = new ShapeRenderer();
    private final BitmapFont font = new BitmapFont(false);
    private Texture bg;
    private Texture fg;
    private final ClonkRage game;

    public View(ClonkRage game) {
        this.game = game;
        Flint.loadSprite();
    }

    /**
     * Draw a string using the color white.
     *
     * @param msg
     * @param xPos screen space
     * @param yPos screen space
     * @param openbatch true if begin/end shoould be called
     */
    public void drawString(final String msg, final int xPos, final int yPos, boolean openbatch) {
        if (openbatch) {
            //spriteBatch.setProjectionMatrix(libGDXcamera.combined);
            spriteBatch.begin();
        }
        getFont().setColor(Color.WHITE.cpy());
        getFont().draw(spriteBatch, msg, xPos, yPos);
        if (openbatch) {
            spriteBatch.end();
        }
    }

    /**
     * Draw a string in a color. Using open batch.
     *
     * @param msg
     * @param xPos screen space
     * @param yPos screen space
     * @param color
     */
    public void drawString(final String msg, final int xPos, final int yPos, final Color color) {
        spriteBatch.setColor(Color.WHITE.cpy());
        getFont().setColor(color);
        getFont().draw(spriteBatch, msg, xPos, yPos);
    }

    SpriteBatch getSpriteBatch() {
        return spriteBatch;
    }

    ShapeRenderer getShapeRenderer() {
        return shRenderer;
    }

    private BitmapFont getFont() {
        return font;
    }

    public void render() {
        bg = new Texture(game.getBG());
        fg = new Texture(game.getFG());
        getSpriteBatch().begin();
        getSpriteBatch().draw(bg, 0, 1080, 1920, -1080);//bottom left corner
        getSpriteBatch().draw(fg, 0, 1080, 1920, -1080);//bottom left corner
        ArrayList<Flint> ents = ClonkRage.getEntities();
        for (Flint ent : ents) {
            ent.render(this);
        }
        getSpriteBatch().end();
        bg.dispose();//if you do not dispose, you will crash your computer because heap will grow unlimited
        fg.dispose();//if you do not dispose, you will crash your computer because heap will grow unlimited
    };

    void dispose() {
        bg.dispose();
        fg.dispose();
        font.dispose();
        spriteBatch.dispose();
    }

}