Search code examples
javabitmaprenderextendstile

Java 2D Game Tiles not rendering


This is my first time working with extends and stuff like that :)

In mt most recent program, I have BasicTile extending Tile. I construct the basic tile with a Bitmap. The bitmap is not an actual bitmap, its a class that I wrote that contains an array of integers (holding color values). When I render with the bitmap, I get a black screen. This goes away when I make the bitmap static (I do not want that because I want multiple basic tiles like grass, bushes, etc.) It also goes away if I set the texture right in the render method (I don't want to do that either bc it would load 60*256 bitmaps a second).

I tested some and in the constructor of the BasicTile the array in the bitmap contains the right values. In the render method it changed to only be the number -16777216 though.

The information seems to get lost somewhere in between. I am having problems finding where it gets lost because I do not do anything to the bitmap in between the constructor and the render method.

This is my Tile, BasicTile, and Bitmap classes:

public abstract class Tile {

public static final Tile[] tiles = new Tile[576];
public static final Tile VOID = new BasicTile(0, Art.spritesheet[0][0]);
public static final Tile STONE = new BasicTile(1, Art.spritesheet[1][0]);
public static final Tile GRASS = new BasicTile(2, Art.spritesheet[3][0]);

protected byte id;
protected boolean solid;
protected boolean emitter;

public Tile(int id, boolean isSolid, boolean isEmitter){
    this.solid = isSolid;
    this.emitter = isEmitter;
    tiles[id] = this;
}

public byte getId(){
    return id;
}

public boolean isSolid(){
    return solid;
}

public boolean isEmitter(){
    return emitter;
}

public abstract void render(Screen screen, int x, int y);

}

public class BasicTile extends Tile{

protected int tileId;
protected Bitmap texture;

public BasicTile(int id, Bitmap bitmap) {
    super(id, false, false);
    texture = bitmap;

}

public void render(Screen screen, int x, int y) {
    /*for(int i = 0; i < texture.h; i++){
        for(int j = 0; j < texture.w; j++){
            System.out.println(texture.pixels[j + i * texture.w]);
        }
    }*/ //the algorithm I used to debug (getting the values of the int array)
    screen.render(texture, x, y);
}

}

public class Bitmap {

public int w, h;
public int[] pixels;

public Bitmap(int w, int h){
    this.w = w;
    this.h = h;
    this.pixels = new int[w * h];
}

}

when I render to the screen, it renders it to another bigger array of ints :)

Added examples:

normal code: see above (sorry I can only post 2 links)

result: black screen

making bitmap static: in the BasicTile change "protected Bitmap texture;" to "protected static Bitmap texture;"

result

set it in the render method

result: same as the one with the bricks (so it works)

P.S.: If you need anything else to solve this issue please tell me :)


Solution

  • I figured out it was not displaying a black screen but instead the void tile... I don't know why (I'm working on a fix and it will be posted here) but if I change the public static final Tile VOID = new BasicTile(0, Art.spritesheet[0][0])

    to

    public static final Tile VOID = new BasicTile(0, Art.spritesheet[3][0])

    it renders the bricks :)