Hi guys, I’m new to this forum so firstly would like to say Hello. I’ve been trying to learn programming alone for several years (on-and-off when life hasnt taken over!), started way back in mid 90’s with C++, got nowhere LOL!, tried Visual Basic; GameMaker and didnt enjoy the creations much and gave up. Much later in life, I found SFML + SDL and tbh struggled again with a few aspects of the language and gave up!. Much more later, I found the wonderful LWJGL and also M$ XNA Studio and this time I was cooking with gas!! I’ve managed to create a couple of finished 2D games in XNA which were highly playable (only released to my friends as copyright law was not a concern of mine whilst making these :D) and also some nice projects in LWJGL.
More years passed and no I have much time to myself and I have found the wonderful LibGDX framework. I will never turn back and I feel like I am close to be in a position to release some decent games.
Right thats enough about me! The subject I am stuck on now is one that I always struggle with, every single time I think I understand it, and try something new, it manages to smash my dreams back into the ground… Yes you guessed it… I’m talking viewports!
The actual issue is: Ive done several hours researching this over the last couple days and contacting people on the Badlogic forum. The game runs perfectly now in Desktop (even when I resize the window etc) but in Android the background is not positioned correctly. (When i say background, its actually my ‘viewport1’ and holds an image of a handheld console controller, inside of which the game-screen (ie. viewport2) is housed.
Image of this here: http://imgur.com/a/sThgt - this works on desktop, but when i run on Android here is the result :[ http://imgur.com/a/2f54e
I think my Resize() method is actually working fine, I believe my problem is in the AndroidLauncher. (Becoz I havent released of my previous game efforts, I’ve never actually bothered to port many to an Android device. But the ones I have , which worked fine, there was only one viewport and a much more simplistic overlay D-Pad which id create or use Scene2d. This time I have a few projects good enough for market but I need final polish first such as this controller gimmick. Just a preference of mine, a challenge I set myself which I must understand before trying to become an actual indie developer.
Sorry for the mountain of text, I do rabbit on a bit lol soz!
Anyways, here’s the class code I’m talking about. If anyone can help me figure this out I’d be hugely grateful.
(PLEASE NOTE: ALSO IF I ADD “config.fullscreen = true” into my desktopLauncher then I get the same incorrect result as per photo above.)
GameScreen class code:
package com.megabro.wasteland.screens;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.megabro.wasteland.tools.Constants;
import com.megabro.wasteland.tools.InputController;
public class GameScreen extends ScreenAdapter {
Game game;
SpriteBatch batch;
OrthographicCamera cam2, cam1;
Viewport v1, v2;
int innerScreenX = 164;
int innerScreenY = 45;
Sprite backgroundSprite;
InputController inputController;
TmxMapLoader mapLoader;
TiledMap map;
TiledMapRenderer mapRenderer;
int viewportWidthPercentage, viewportHeightPercentage;
public GameScreen(Game game) {
this.game = game;
batch = new SpriteBatch();
backgroundSprite = new Sprite(new Texture("handheld-landscape.png"));
cam2 = new OrthographicCamera();
cam1 = new OrthographicCamera();
v1 = new FitViewport(Constants.BG_WIDTH, Constants.BG_HEIGHT, cam1);
backgroundSprite.setSize(v1.getWorldWidth(), v1.getWorldHeight());
cam1.setToOrtho(false);
v2 = new FitViewport(Constants.GRID_WIDTH, Constants.GRID_HEIGHT, cam2);
v2.setScreenBounds((int)(v1.getScreenX() + (v1.getWorldWidth() * 0.205f)), (int)(v1.getScreenY() + (v1.getWorldHeight() * 0.1f)), Constants.GRID_WIDTH, Constants.GRID_HEIGHT);
viewportWidthPercentage = Constants.GRID_WIDTH / Constants.BG_WIDTH;
viewportHeightPercentage = Constants.GRID_HEIGHT / Constants.BG_HEIGHT;
// cam2.setToOrtho(false);
inputController = new InputController(new Texture("debugRect.png"), v1);
mapLoader = new TmxMapLoader();
map = mapLoader.load("level1.tmx");
mapRenderer = new OrthogonalTiledMapRenderer(map);
v2.apply();
cam2.position.set(v2.getWorldWidth() /2, v2.getWorldHeight() / 2,0);
cam2.update();
}
@Override
public void render(float delta) {
update(delta);
clearScreen();
draw();
}
private void update(float delta) {
}
private void clearScreen() {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
private void draw() {
v2.apply();
cam2.position.set(cam2.position.x + 1, cam2.position.y - 0,0);
cam2.update();
mapRenderer.setView(cam2);
mapRenderer.render();
batch.setProjectionMatrix(cam1.combined);
v1.apply();
batch.begin();
backgroundSprite.draw(batch);
inputController.drawDebug(batch);
batch.end();
}
@Override
public void resize(int width, int height) {
v1.apply();
v1.update(width, height);
v2.apply();
v2.update(width * Constants.GRID_WIDTH / Constants.BG_WIDTH, height * Constants.GRID_HEIGHT / Constants.BG_HEIGHT);
v2.setScreenX((int)(v1.getScreenX() + (v1.getScreenWidth() * 0.205f)));
v2.setScreenY((int)(v1.getScreenY() + (v1.getScreenHeight() * 0.1f)));
}
@Override
public void dispose() {
}
}