com.github.skittishSloth.openSkies.maps.PlanetScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.github.skittishSloth.openSkies.maps.PlanetScreen.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.github.skittishSloth.openSkies.maps;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.math.Matrix4;
import com.github.skittishSloth.openSkies.maps.terrains.ElevationParameters;
import com.github.skittishSloth.openSkies.maps.terrains.LatitudeParameters;
import com.github.skittishSloth.openSkies.maps.terrains.TerrainParameters;
import com.github.skittishSloth.openSkies.maps.terrains.TerrainTile;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author mcory01
 */
public class PlanetScreen implements Screen {

    public PlanetScreen() {
        //        final ElevationParameters elevParams = ElevationParameters.buildEvenRanges();
        //        final ElevationParameters elevParams = ElevationParameters.buildByPercent(75, 5, 15, 5, 5);
        final ElevationParameters elevParams = ElevationParameters.buildByEnumValues();
        final LatitudeParameters latParams = LatitudeParameters.getEvenPercentages();
        final TerrainParameters terrainParams = new TerrainParameters(elevParams, latParams, 10, 9, 9);

        final MapGenerator mg = new MapGenerator(terrainParams);
        tileSize = 1;
        width = Gdx.graphics.getWidth() / tileSize;
        height = Gdx.graphics.getHeight() / tileSize;
        tiles = mg.generateTiles(width, height);
        elevationTexture = mg.generateElevationTexture(tiles, width, height, tileSize);
        rawElevationTexture = mg.generateRawElevationTexture(tiles, width, height, tileSize);
        temperatureTexture = mg.generateTemperatureTexture(tiles, width, height, tileSize);
        rainfallTexture = mg.generateRainfallTexture(tiles, width, height, tileSize);
        mineralTexture = mg.generateMineralsTexture(tiles, width, height, tileSize);
        elevationGrayScaleTexture = mg.generateElevationGrayscaleTexture(tiles, width, height, tileSize);

        batch = new SpriteBatch();

        final Texture latColorTexture = mg.generateLatitudeTexture(tiles, width, height, tileSize);
        final FrameBuffer frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
        final OrthographicCamera camera = new OrthographicCamera();
        camera.setToOrtho(true);
        final Matrix4 oldBatchMatrix = batch.getProjectionMatrix();
        batch.setProjectionMatrix(camera.combined);
        frameBuffer.begin();
        batch.begin();
        batch.draw(elevationTexture, 0, 0);
        final Sprite latColorSprite = new Sprite(latColorTexture);
        latColorSprite.setAlpha(0.5f);
        latColorSprite.draw(batch);
        batch.end();
        frameBuffer.end();

        batch.setProjectionMatrix(oldBatchMatrix);
        latitudeTexture = frameBuffer.getColorBufferTexture();
        currentTextureIdx = 0;

        textures.add(elevationTexture);
        textures.add(rawElevationTexture);
        textures.add(elevationGrayScaleTexture);
        textures.add(temperatureTexture);
        textures.add(rainfallTexture);
        textures.add(mineralTexture);
        textures.add(latitudeTexture);
        //        textures.add(zoomedElevationTexture);

        numTextures = textures.size();
    }

    @Override
    public void render(final float delta) {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        if (Gdx.input.isKeyJustPressed(Input.Keys.LEFT)) {
            currentTextureIdx--;
        } else if (Gdx.input.isKeyJustPressed(Input.Keys.RIGHT)) {
            currentTextureIdx++;
        }

        if (currentTextureIdx < 0) {
            currentTextureIdx += numTextures;
        } else if (currentTextureIdx >= numTextures) {
            currentTextureIdx -= numTextures;
        }

        final Texture currentTexture = textures.get(currentTextureIdx);
        batch.begin();
        batch.draw(currentTexture, 0, 0);
        batch.end();
    }

    @Override
    public void resize(final int width, final int height) {

    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        for (final Texture t : textures) {
            t.dispose();
        }
        textures.clear();
        batch.dispose();
    }

    private int currentTextureIdx = 0;

    private final Texture elevationTexture;
    private final Texture temperatureTexture;
    private final Texture rainfallTexture;
    private final Texture elevationGrayScaleTexture;
    private final Texture latitudeTexture;
    private final Texture rawElevationTexture;
    private final Texture mineralTexture;
    private final List<Texture> textures = new ArrayList<Texture>();
    private final int numTextures;
    private final SpriteBatch batch;

    private final int tileSize;
    private final int width;
    private final int height;
    private final TerrainTile[][] tiles;
}