Algorithm for drawing tiles on screen

208 views Asked by At

I have struggled with this problem for several days now, and I just can't solve the problem myself.

I have a Minecraft 2D sort of game. The Blocks are divided into Chunks of 64 * 64 Blocks.

I have this draw function:

@Override
protected void gameDraw(Graphics2D g) {
    g.drawImage(Texture.getImage("background"), 0, 0, 800, 600, 0, 0, 800, 600, null);

    int pixelXMin = (int) (screenX);
    int pixelYMin = (int) (screenY);
    int pixelXMax = (int) (canvas.getWidth() + screenX);
    int pixelYMax = (int) (canvas.getHeight() + screenY);

    int blockXMin = pixelXMin / Block.BLOCK_PIXEL_WIDTH;
    int blockYMin = pixelYMin / Block.BLOCK_PIXEL_HEIGHT;
    int blockXMax = pixelXMax / Block.BLOCK_PIXEL_WIDTH;
    int blockYMax = pixelYMax / Block.BLOCK_PIXEL_HEIGHT;

    int diffX = pixelXMin % Block.BLOCK_PIXEL_WIDTH;
    int diffY = pixelYMin % Block.BLOCK_PIXEL_HEIGHT;

    for (int bx = blockXMin; bx < blockXMax; bx++) {
        for (int by = blockYMin; by < blockYMax; by++) {

            int chunkNum = bx / Chunk.COLUMNS_IN_CHUNK;
            int blockX = bx % Chunk.COLUMNS_IN_CHUNK;
            int blockY = by % Chunk.COLUMNS_IN_CHUNK;

            Chunk chunk = level.loadChunk(chunkNum);
            Block block = chunk.getBlock(blockX, blockY);

            if (block == null) { continue; }
            BlockData blockData = block.getBlockData();
            if (blockData.getType().getTextureName().equals("")) { continue; }

            int blockPosX = canvas.getWidth() - (bx * Block.BLOCK_PIXEL_WIDTH + diffX);
            int blockPosY = canvas.getHeight() - (by * Block.BLOCK_PIXEL_HEIGHT + diffY);

            BufferedImage image = Texture.getImage(blockData.getType().getName());

            g.drawImage(image, blockPosX, blockPosY, Block.BLOCK_PIXEL_WIDTH, Block.BLOCK_PIXEL_HEIGHT, null);
        }
    }
}

Here i tried to calculate the maximum and minimum number of pixels on the screen with the variables screenX and screenY who serve as "camera position". Then I loop through every block that is within these pixels.

The rest is sort of self explanatory.

Now for the problem: enter image description here As you can see does the game draw nicely, but when you change the values of screenX and screenY, the "box" of blocks move realtive to the screen, but the blocks stay at the same location.

How can I solve this? Thank you!

Link to dropbox download for .jar file and full sourcecode. gameDraw() is in CubeExplorer.java https://www.dropbox.com/sh/madienlwmhjt2wh/AADA83aKa00j5UFowhotfmmaa?dl=0

SOLUTION Here is my solution:

@Override
protected void gameDraw(Graphics2D g) {
    g.drawImage(Texture.getImage("background"), 0, 0, 800, 600, 0, 0, 800, 600, null);

    int x = (int) (screenX / Block.BLOCK_PIXEL_WIDTH);
    int y = (int) (screenY / Block.BLOCK_PIXEL_HEIGHT); 

    int blockXMin = x;
    int blockYMin = y;

    int blockXMax = x + canvas.getWidth() / Block.BLOCK_PIXEL_WIDTH + 2;
    int blockYMax = y + canvas.getWidth() / Block.BLOCK_PIXEL_WIDTH + 2;

    System.out.println(x + "," + y + "    " + blockXMax + "," + blockYMax);

    for(int blockX = blockXMin; blockX < blockXMax; blockX++) {
        for(int blockY = blockYMin; blockY < blockYMax; blockY++) {

            int chunkX = blockX / Chunk.COLUMNS_IN_CHUNK;
            Chunk chunk = level.loadChunk(chunkX);

            int blockChunkPosX = blockX % Chunk.COLUMNS_IN_CHUNK;
            int blockChunkPosY = blockY % Chunk.ROWS_IN_CHUNK;
            Block block = chunk.getBlock(blockChunkPosX, blockChunkPosY);

            if (block == null) { continue; }
            BlockData blockData = block.getBlockData();
            if (blockData.getType().getTextureName().equals("")) { continue; }

            int blockScreenLocX = (int) ((int) + blockX * Block.BLOCK_PIXEL_WIDTH - screenX);
            int blockScreenLocY = 600 - (int) ((int) + blockY * Block.BLOCK_PIXEL_HEIGHT - screenY);

            BufferedImage image = Texture.getImage(blockData.getType().getName());
            g.drawImage(image, blockScreenLocX, blockScreenLocY, Block.BLOCK_PIXEL_WIDTH, Block.BLOCK_PIXEL_HEIGHT, null);

        }    
    }
}
0

There are 0 answers