JaypaK
Enthusiast
I was hoping someone would be able to help me. I have a problem where I need to render a 16x16 (1 tile) sprite, but for some reason it's rendering four. I have pictures of my code and the outcome of it.
The Screen class:
Code:
The SpriteSheet class:
Code:
Sprite class:
Code:
The outcome:
It looks like it renders 4 tiles instead of one, and I don't know why.
The Screen class:
Code:
Code:
package com.jaypak.top.graphics;
import java.util.Random;
public class Screen {
private int width, height;
public int[] pixels;
public final int MAP_SIZE = 64;
public final int MAP_MASK_SIZE = MAP_SIZE - 1;
public int[] tiles = new int[MAP_SIZE * MAP_SIZE];
private Random random = new Random();
public Screen(int width, int height){
this.width = width;
this.height = height;
pixels = new int[width * height];
for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++){
tiles[i] = random.nextInt(0xff);
}
}
public void clear(){
for(int i = 0; i < pixels.length; i++){
pixels[i] = 0;
}
}
public void render(int xOffset, int yOffset){
for(int y = 0; y < height; y++){
int yy = y + yOffset;
//if(yy < 0 || yy >= height) break;
for(int x = 0; x < width; x++){
int xx = x + xOffset;
//if(xx < 0 || xx >= width) break;
pixels[x + y * width] = Sprite.grass.pixels[(x & 15) + (y & 15) * Sprite.grass.SIZE];
}
}
}
}
The SpriteSheet class:
Code:
Code:
package com.jaypak.top.graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpriteSheet {
private String path;
public final int SIZE;
public int[] pixels;
public static SpriteSheet tiles = new SpriteSheet("/textures/SpriteSheet.png", 256);
public SpriteSheet(String path, int size){
this.path = path;
SIZE = size;
pixels = new int[SIZE * SIZE];
load();
}
private void load(){
try {
BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
int w = image.getWidth();
int h = image.getHeight();
image.getRGB(0, 0, w, h, pixels, 0, w);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sprite class:
Code:
Code:
package com.jaypak.top.graphics;
public class Sprite {
public final int SIZE;
private int x, y;
public int[] pixels;
private SpriteSheet sheet;
public static Sprite grass = new Sprite(16, 0, 0, SpriteSheet.tiles);
public Sprite(int size, int x, int y, SpriteSheet sheet){
SIZE = size;
pixels = new int[SIZE * SIZE];
this.x = x * size;
this.y = y * size;
this.sheet = sheet;
load();
}
private void load(){
for(y = 0; y < SIZE; y++){
for(x = 0; x < SIZE; x++){
pixels[x + y * SIZE] = sheet.pixels[(x + this.x) + (y + this.y) * sheet.SIZE];
}
}
}
}
The outcome:
It looks like it renders 4 tiles instead of one, and I don't know why.