I'm trying to get a tiledmap to render over the whole screen. The problem is that the top or right 100px, everything is just black there. My tiledmap is around 100 tiles, so it should be able to render more than what it renders.
This is how it looks right now:
1: This bar is expected as I do, currently, not have anything to render there.
2 & 3: These bars are unexpected as there are more parts of my tiledmap to render there.
I've already tried changing my Viewport from StretchViewport to FitViewport as well as FillViewport, but that doesn't change anything.
Here's my constructor in the PlayScreen class:
public Play(GameStateManager gsm) {
super(gsm);
//set up box2d
world = new World(new Vector2(0, -20.81f), true);
cl = new MyContactListener();
world.setContactListener(cl);
b2dr = new Box2DDebugRenderer();
//set up viewport
vw = new StretchViewport(1280, 720, cam);
cam.update();
//create player
createPlayer();
//create tiles
createTiles();
//setup box2D cam
b2dCam = new OrthographicCamera();
b2dCam.setToOrtho(false, Game.V_WIDTH / PPM, Game.V_HEIGHT / PPM);
}
Here's my createTiles() method:
private void createTiles(){
loader = new TmxMapLoader();
tileMap = loader.load("data/lvl1.tmx");
tmr = new OrthogonalTiledMapRenderer(tileMap);
cam.update();
//get the size of the tiles
tileSize = tileMap.getProperties().get("tilewidth", Integer.class);
//create box2d bodies for all tiledmap layers
TiledMapTileLayer layer;
layer = (TiledMapTileLayer) tileMap.getLayers().get("red");
createLayer(layer, B2DVars.BIT_RED);
layer = (TiledMapTileLayer) tileMap.getLayers().get("orange");
createLayer(layer, B2DVars.BIT_ORANGE);
layer = (TiledMapTileLayer) tileMap.getLayers().get("yellow");
createLayer(layer, B2DVars.BIT_YELLOW);
layer = (TiledMapTileLayer) tileMap.getLayers().get("green");
createLayer(layer, B2DVars.BIT_GREEN);
layer = (TiledMapTileLayer) tileMap.getLayers().get("blue");
createLayer(layer, B2DVars.BIT_BLUE);
layer = (TiledMapTileLayer) tileMap.getLayers().get("pink");
createLayer(layer, B2DVars.BIT_PINK);
}
The render() method:
public void render(float delta) {
//clear screen
Gdx.gl.glClearColor(0.619607843f, 0.79607843137f, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//set cam to follow player
cam.position.set(
player.getPosition().x * PPM + Game.V_WIDTH / 4,
Game.V_HEIGHT / 2,
0
);
//draw tile map
tmr.setView(cam);
tmr.render();
//draw player
sb.setProjectionMatrix(cam.combined);
player.render(sb);
//draw box2d world
b2dr.render(world, b2dCam.combined);
//responsive resizing
resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
And here's the create() method inside my main game class:
public void create () {
Gdx.input.setInputProcessor(new MyInputProcessor());
//asset managing
res = new Content();
res.loadTexture("data/player/player.png", "player");
sb = new SpriteBatch();
cam = new OrthographicCamera();
cam.setToOrtho(false, V_WIDTH, V_HEIGHT);
hudCam = new OrthographicCamera();
hudCam.setToOrtho(false, V_WIDTH, V_HEIGHT);
gsm = new GameStateManager(this);
}
I did not expect this game to get those black bars, I've created other games before and they have never gotten those. How can I remove them (2 & 3 on the image)?
Related
So I have 2 screens: a level select screen which uses a FitViewport with a resolution of 720 x 405 and then a game screen which uses a FillViewport with a resolution of 480 x 270 scaled by the amount of pixels per meter (/100).
After completing a certain level I want to return to the level select screen. However, upon return it seems to be fully zoomed in.
Here is how it should look, and how it looks when first launched
Here is how it looks after coming out of the game
Here is the level screen code:
public LevelScreen(CrossplatformApp game) {
this.game = game;
this.camera = new OrthographicCamera();
this.levelstage = new Stage(new FitViewport(Constants.WIDTH, Constants.HEIGHT, camera));
this.background = new Texture("Screens/LevelScreen/LevelSelection.png");
this.backbutton = new Texture("Screens/BackButton.png");
this.level1texture = new Texture("Screens/LevelScreen/Button1.png");
this.level2texture = new Texture("Screens/LevelScreen/Button2.png");
this.level3texture = new Texture("Screens/LevelScreen/Button3.png");
}
#Override
public void resize(int width, int height) {
levelstage.getViewport().update(width, height, true);
}
And the game screen:
this.game = gameFile;
this.camera = new OrthographicCamera();
this.viewport = new FillViewport(Constants.V_WIDTH / Constants.PPM, Constants.V_HEIGHT / Constants.PPM, camera);
this.levelHUD = new HUD(gameFile.batch, WorldPicker.getWorldName(playerMemory.player.worldAndLevelData.getCurrentWorld(), playerMemory.player.worldAndLevelData.getCurrentLevel()));
When changing viewports you need to apply the viewport before drawing with:
this.viewport.apply();
I'm trying to render a sprite/texture on top of a tiled map in libgdx/android studio. Note that my map is 20x25 with 8 pixel tiles and splitTiles is a Texture Region.
public void show() {
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.setToOrtho(false, 20, 25);
batch = new SpriteBatch();
map = new TmxMapLoader().load("centipedeMap.tmx");
renderer = new OrthogonalTiledMapRenderer(map,1/8f)
}
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setView(cam);
renderer.render();
player.update();
ex.update();
batch.setProjectionMatrix(cam.combined);
cam.update();
batch.begin();
Texture tilesImage = new Texture(Gdx.files.internal("tile.png"));
TextureRegion[][] splitTiles = TextureRegion.split(tilesImage, 8, 8);
batch.draw(splitTiles[0][0],50,50);
batch.end();
}
Your camera viewport dimension is 20, 25 and you're drawing your TextureRegion at 50,50 so your image is rendered out of screen so not visible.
Recommendation : Texture is heavy object so try to avoid creating multiple instance of same Texture.
you've camera viewport is in dimension of 20,25 that is same as tileMap(20,25) dimension so camera 1 unit denotes one cell of TileMap.
you can detect collision with Tiles,it is not necessary to create Rectangle object.
I'm trying to draw simple text with libGDX on the screen. The thing that I want to consider is the size of the text. I would like to draw score of the player on the screen which i would like it to be big. But even i use the freetype fonts, it's not looking smooth. Here is the code:
SpriteBatch batch;
String string;
FreeTypeFontGenerator generator;
FreeTypeFontParameter parameter;
BitmapFont font;
#Override
public void create () {
batch = new SpriteBatch();
generator = new FreeTypeFontGenerator(Gdx.files.internal("arial.ttf"));
parameter = new FreeTypeFontParameter();
parameter.size = 100;
font = generator.generateFont(parameter);
string = "0123456";
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
batch.begin();
font.draw(batch, string, 50, 400);
batch.end();
}
Am i on the right path to draw the score or some HUD screen to the viewport? Is there a nicer way or is there a way to do this with the fonts easily? (For starting to learn from scratch?)
EDIT: Here is how it looks like
Try setting the minFilter and magFilter of FreeTypeFontGeneratorParameter to Linear, like this:
#Override
public void create () {
batch = new SpriteBatch();
generator = new FreeTypeFontGenerator(Gdx.files.internal("arial.ttf"));
parameter = new FreeTypeFontParameter();
parameter.size = 100;
parameter.minFilter = Texture.TextureFilter.Linear;
parameter.magFilter = Texture.TextureFilter.Linear;
font = generator.generateFont(parameter);
string = "0123456";
}
This is from day 6 of the flappy bird recreation tutorial
-http://www.kilobolt.com/day-6-adding-graphics---welcome-to-the-necropolis.html
Here is the image file i am using for texture in my game. It is a 256px x 64px .png file.
Here is the class that I used for loading the texture and the specific TextureRegion(part of the texure) that I want the SpriteBatch to draw.
public class AssetLoader {
public static Texture texture;
public static TextureRegion bg;
public static void load() {
texture = new Texture(Gdx.files.internal("data/texture.png"));
bg = new TextureRegion(texture, 0, 0, 136, 43);
}
}
And I call AssertLoader.load(), along with setting up game screen from
public class MyGdxGame extends Game{
#Override
public void create() {
AssetLoader.load();
setScreen(new GameScreen());
}
}
And inside GameScreen.java
public class GameScreen implements Screen {
//delegate render task
private GameRenderer renderer;
public GameScreen() {
float screenHeight = Gdx.graphics.getHeight();
float screenWidth = Gdx.graphics.getWidth();
float gameWidth = 136;
float gameHeight = screenHeight / (screenWidth / gameWidth);
renderer = new GameRenderer((int)gameHeight);
}
}
And inside GameRederer, the class I delegate to render the game
public class GameRenderer {
private int gameHeight;
private SpriteBatch batch;
private OrthographicCamera cam;
public GameRenderer(int gameHeight) {
this.gameHeight = gameHeight;
cam = new OrthographicCamera();
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
cam.setToOrtho(true, 136, gameHeight);
}
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.disableBlending();
batch.draw(AssetLoader.bg, 0, (gameHeight/2) + 23, 136, 43);
batch.end()
}
}
What I get when I run the desktop version of the game is the black screen shown above(black because i set the background to black with these lines of code
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Does anyone know why the SpritchBatch drawing isn't showing up? I extracted the texture portion of the texture I wanted with this line of code(starts from 0,0, width of 136, height of 43) - used GIMP - to find out portion to cut.
bg = new TextureRegion(texture, 0, 0, 136, 43);
I also put a few log statements(removed them from view) to ensure that before drawing, bg's width and height were set correctly, which they were. And the issue can't be game height because I used a print statement and found that to be 204 which means that this expression, (gameHeight/2) + 23 will evaluate to 125 which is in bounds between 0 and game height.
I checked out other threads as well.
My issue can't be libgdx spritebatch not rendering textures because the SpriteBatch should overwrite the background.
And it can't be LibGDX: Android SpriteBatch not drawing because i am running mine on desktop, not andorid.
could be that you have to first put cam.setToOrtho(true, 136, gameHeight);before the batch, so I can not confirm hopefully help
public GameRenderer(int gameHeight) {
this.gameHeight = gameHeight;
cam = new OrthographicCamera();
batch = new SpriteBatch();
cam.setToOrtho(true, 136, gameHeight);
batch.setProjectionMatrix(cam.combined);
}
If anyone's having a similar issue, the way I solved the problem was to call
batch.setProjectionMatrix(cam.combined);
after
cam.setToOrtho(true, 136, gameHeight);
Which didn't really make sense to me because it's still the same Matrix4 in Camera.java, that is
public final Matrix4 combined = new Matrix4();
Was hoping someone else could clarify that.
I make simply aplication in LibGDX. I have got two textures (background and sprite). The background is diplayed ok but I don't see the sprite. I have tried many solutions but nothing works. I don't know what is going on. Thanks for help.
Here is my code:
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
Texture.setEnforcePotImages(false);
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("graphic/bg1.png"));
bad = new Texture(Gdx.files.internal("graphic/b.png"));
layerOne = new TextureRegion(texture);
spriteb = new Sprite(bad);
rbg = new ParallaxBackground(new ParallaxLayer[]{
new ParallaxLayer(layerOne, new Vector2(),new Vector2()),
}, w, h,new Vector2());
}
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
rbg.render();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
spriteb.setPosition(400, 200);
spriteb.draw(batch);
batch.end();
}
Your camera viewport is too small for the Sprite position
camera = new OrthographicCamera(1, h/w);
...
spriteb.setPosition(400, 200);
The sprite is being rendered far outside of the viewport.
Try setting your camera like this:
camera = new OrthographicCamera(w, (h/w)*h);