I am creating a game in which there is a 10x10 map and I need to calculate the size of these squares so that they all fit on the screen.
rect_x_end, rect_y_end
The size of the square by x and y
screen_x, screen_y
Screen Size
len_map_x, len_map_y
Map width and length = 10
I calculated the size of the square like this:
rect_x_end = screen_x / len_map_x
rect_y_end = screen_y / len_map_y
example:
64 = 640 / 10
48 = 480 / 10
In this example, everything works correctly, but as soon as I start resizing the screen, this method gives an incorrect result.
I was doing something similar to python (pygame) and everything worked there.
kucer0043.java
package com.kucer0043.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.ScreenUtils;
public class kucer0043 extends ApplicationAdapter {
private ShapeRenderer shapeRenderer;
private int map_size;
@Override
public void create () {
shapeRenderer = new ShapeRenderer();
map_size = 10; // square map size
}
@Override
public void render () {
ScreenUtils.clear(0.5f, 0.5f, 0.5f, 1);
int rect_x_end = Gdx.graphics.getWidth() / map_size;
int rect_y_end = Gdx.graphics.getHeight() / map_size;
int map_x = Gdx.input.getX() / rect_x_end;
int map_y = Gdx.input.getY() / rect_y_end;
int x = map_x * rect_x_end;
int y = Gdx.graphics.getHeight() - (map_y + 1) * rect_y_end;
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(0f,1f,0f,0f);
shapeRenderer.rect(x,y, rect_x_end, rect_y_end);
shapeRenderer.end();
}
@Override
public void dispose () {
}
}
The projection matrix of your ShapeRenderer
is calculated on initialization,you need to re-calculate it whenever the window changes size.
There are a few ways of achieving this, the simplest might be to just create a new ShapeRenderer
when the window resizes:
@Override
public void resize(int width, int height) {
shapeRenderer = new ShapeRenderer();
}