virtual coordinates for a real menu renderer

This commit is contained in:
Mike Schwörer 2017-11-25 15:38:45 +01:00
parent 33ec805a5f
commit 2271bc35d6
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
4 changed files with 81 additions and 15 deletions

View File

@ -6,6 +6,8 @@ import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
public class CRAssets {
public static Texture TEX_BLANK;
public static Texture TEX_PLAYER_UP;
public static Texture TEX_PLAYER_RIGHT;
public static Texture TEX_PLAYER_LEFT;
@ -20,6 +22,13 @@ public class CRAssets {
public static Texture TEX_COLORWHEEL;
static {
{
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(1, 1, 1, 1);
pixmap.fill();
TEX_BLANK = new Texture(pixmap);
}
{
Pixmap pixmap = new Pixmap(32, 32, Format.RGBA8888);
pixmap.setColor(1, 0, 0, 1);

View File

@ -5,31 +5,29 @@ import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import java.util.ArrayList;
import java.util.List;
import de.samdev.colorrunner.CRGame;
public abstract class BaseMenu implements Screen {
private final int VIEWPORT_WIDTH = 100;
private final int VIEWPORT_HEIGHT = 100;
private final int VIRTUAL_VIEWPORT_WIDTH = 1024;
private final int VIRTUAL_VIEWPORT_HEIGHT = 640;
private List<BaseMenuElement> elements = new ArrayList<BaseMenuElement>();
private OrthographicCamera cam;
private SpriteBatch sbatch = new SpriteBatch();
private ShapeRenderer linebatch = new ShapeRenderer();
public BaseMenu() {
super();
cam = new OrthographicCamera();
cam.position.x += VIEWPORT_WIDTH/2;
cam.position.y += VIEWPORT_HEIGHT/2;
cam.viewportWidth += VIEWPORT_WIDTH;
cam.viewportHeight += VIEWPORT_HEIGHT;
cam.update();
sbatch.setProjectionMatrix(cam.combined);
updateCam();
init();
}
@ -53,21 +51,71 @@ public abstract class BaseMenu implements Screen {
e.update(delta);
}
// ####### RENDER #########
// ####### DEBUG RENDER #########
Gdx.gl.glClearColor(0.22f, 0.22f, 0.22f, 1f);
Gdx.gl.glClearColor(1, 0, 1, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (CRGame.DEBUG) {
linebatch.begin(ShapeRenderer.ShapeType.Line);
linebatch.setColor(0, 0, 1, 1);
linebatch.rect(0, 0, VIRTUAL_VIEWPORT_WIDTH, VIRTUAL_VIEWPORT_HEIGHT);
linebatch.end();
}
// ####### REAL RENDER #########
sbatch.begin();
for (BaseMenuElement e : elements) {
e.render(sbatch);
}
sbatch.end();
}
private void updateCam() {
float deviceW = Gdx.graphics.getWidth();
float deviceH = Gdx.graphics.getHeight();
float real_ratio = deviceW / deviceH;
float virt_ratio = VIRTUAL_VIEWPORT_WIDTH * 1f / VIRTUAL_VIEWPORT_HEIGHT;
if (real_ratio > virt_ratio) {
// fix to height
cam.viewportHeight = VIRTUAL_VIEWPORT_HEIGHT;
cam.viewportWidth = real_ratio * VIRTUAL_VIEWPORT_HEIGHT;
} else {
// fix to width
cam.viewportHeight = VIRTUAL_VIEWPORT_WIDTH / real_ratio;
cam.viewportWidth = VIRTUAL_VIEWPORT_WIDTH;
}
cam.position.x = VIRTUAL_VIEWPORT_WIDTH / 2f;
cam.position.y = VIRTUAL_VIEWPORT_HEIGHT / 2f;
cam.update();
sbatch.setProjectionMatrix(cam.combined);
linebatch.setProjectionMatrix(cam.combined);
}
@Override
public void resize(int width, int height) {
// NOP
updateCam();
}
@Override

View File

@ -9,7 +9,14 @@ public class LevelSelectMenu extends BaseMenu {
@Override
protected void init() {
add(new CRMenuRectangle(1, 1, 30, 30, Color.RED));
for (int x = 0; x < 6; x++) {
for (int y = 0; y < 4; y++) {
add(new CRMenuRectangle(48 + (32+128)*x, 48 + (32+112)*y, 128, 128, Color.YELLOW));
}
}
}
}

View File

@ -21,13 +21,15 @@ public class CRMenuRectangle extends BaseMenuElement {
@Override
public void render(SpriteBatch batch) {
batch.draw(CRAssets.TEX_PLAYER_UP, Bounds.x, Bounds.y, Bounds.width, Bounds.height);
batch.setColor(Color);
batch.draw(CRAssets.TEX_BLANK, Bounds.x, Bounds.y, Bounds.width, Bounds.height);
batch.setColor(0);
}
@Override
public void update(float delta) {
Gdx.app.log("crmenurect", "> " + delta);
//Gdx.app.log("crmenurect", "> " + delta);
}
}