j'ai écrit une carte renderer sur Slick2D qui se déplace avec le joueur:Java: Comment rendre la carte autour du lecteur en douceur?
//takes camera x and camera y as arguments
//values of those are based on player location
public static void render(float xrender, float yrender) {
xstart = (int) (xrender/Tile.SIZE);
ystart = (int) (yrender/Tile.SIZE);
int xend = (int) Math.ceil((float) Window.WIDTH/Tile.SIZE);
int yend = (int) Math.ceil((float) Window.HEIGHT/Tile.SIZE);
for (int x = xstart; x < xend; x++) {
for (int y = ystart; y < yend; y++) {
int tx = x-xstart;
int ty = y-ystart;
if (groundExists(tx, ty)) {//checks all tiles on current viewport and draws them
//ground[][] is defined as Image[][]
ground[tx][ty].draw(x * Tile.SIZE, y * Tile.SIZE, Tile.SIZE, Tile.SIZE);
}
}
}
}
Le problème est qu'il est basé sur des entiers et chaque carreau est représenté par un seul chiffre au lieu d'être représenté comme chaque pixel de la tuile (Tile.SIZE = 64 ici). Cela provoque le déplacement de la carte bloc par bloc au lieu d'un mouvement fluide.
J'ai essayé de résoudre le problème en ajoutant les décimales de XRender et yrender au tirage au sort des coordonnées comme ceci:
public static void render(float xrender, float yrender) {
xstart = (int) (xrender/Tile.SIZE);
ystart = (int) (yrender/Tile.SIZE);
int xend = (int) Math.ceil((float) Window.WIDTH/Tile.SIZE);
int yend = (int) Math.ceil((float) Window.HEIGHT/Tile.SIZE);
float xdecimal = xrender - (int) xrender; //hotfix for preserving decimals
float ydecimal = yrender - (int) yrender;
for (int x = xstart; x < xend; x++) {
for (int y = ystart; y < yend; y++) {
int tx = x-xstart;
int ty = y-ystart;
if (groundExists(tx, ty)) {
//decimals added to the starting coordinates
float dx = (float) x * Tile.SIZE + xdecimal;
float dy = (float) y * Tile.SIZE + ydecimal;
ground[tx][ty].draw(dx, dy, Tile.SIZE, Tile.SIZE);
}
}
}
}
Mais maintenant, la carte est incroyablement saccadé et pas lisse du tout. Je pense que j'ai besoin de réécrire à nouveau tout ce qui est rendu. Y a-t-il une sorte de truc pour le rendre lisse?
Pourriez-vous utiliser [double tampon] (https://en.wikipedia.org/wiki/Multiple_buffering) – Shark